From b3bd8e888b3a88f990824b058c8bfd09cc3c7be1 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Fri, 5 Feb 2021 08:30:27 -0500 Subject: [PATCH] Add tests for selector provider --- docs/main/changelog.rst | 4 ++ tests/unit/providers/test_selector_py2_py3.py | 49 ++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 6e439e8d..7c85cf41 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -7,6 +7,10 @@ that were made in every particular version. From version 0.7.6 *Dependency Injector* framework strictly follows `Semantic versioning`_ +Development version +------------------- +- Add tests for ``Selector`` provider. + 4.17.0 ------ - Add ``FastAPI`` + ``SQLAlchemy`` example. diff --git a/tests/unit/providers/test_selector_py2_py3.py b/tests/unit/providers/test_selector_py2_py3.py index 783fe43a..ee86bcc2 100644 --- a/tests/unit/providers/test_selector_py2_py3.py +++ b/tests/unit/providers/test_selector_py2_py3.py @@ -6,7 +6,7 @@ import sys import unittest2 as unittest -from dependency_injector import providers +from dependency_injector import providers, errors class SelectorTests(unittest.TestCase): @@ -33,6 +33,28 @@ class SelectorTests(unittest.TestCase): with self.selector.override('two'): self.assertEqual(provider(), 2) + def test_call_undefined_provider(self): + provider = providers.Selector( + self.selector, + one=providers.Object(1), + two=providers.Object(2), + ) + + with self.selector.override('three'): + with self.assertRaises(errors.Error): + provider() + + def test_call_selector_is_none(self): + provider = providers.Selector( + self.selector, + one=providers.Object(1), + two=providers.Object(2), + ) + + with self.selector.override(None): + with self.assertRaises(errors.Error): + provider() + def test_call_any_callable(self): provider = providers.Selector( functools.partial(next, itertools.cycle(['one', 'two'])), @@ -70,6 +92,19 @@ class SelectorTests(unittest.TestCase): self.assertIs(provider.one, provider_one) self.assertIs(provider.two, provider_two) + def test_getattr_attribute_error(self): + provider_one = providers.Object(1) + provider_two = providers.Object(2) + + provider = providers.Selector( + self.selector, + one=provider_one, + two=provider_two, + ) + + with self.assertRaises(AttributeError): + _ = provider.provider_three + def test_call_overridden(self): provider = providers.Selector(self.selector, sample=providers.Object(1)) overriding_provider1 = providers.Selector(self.selector, sample=providers.Object(2)) @@ -81,6 +116,18 @@ class SelectorTests(unittest.TestCase): with self.selector.override('sample'): self.assertEqual(provider(), 3) + def test_providers_attribute(self): + provider_one = providers.Object(1) + provider_two = providers.Object(2) + + provider = providers.Selector( + self.selector, + one=provider_one, + two=provider_two, + ) + + self.assertEqual(provider.providers, {'one': provider_one, 'two': provider_two}) + def test_deepcopy(self): provider = providers.Selector(self.selector)