Add tests for selector provider

This commit is contained in:
Roman Mogylatov 2021-02-05 08:30:27 -05:00
parent 35f280ac8a
commit b3bd8e888b
2 changed files with 52 additions and 1 deletions

View File

@ -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.

View File

@ -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)