mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
f961ff536a
* Add single container prototype * Add multiple containers prototype * Add integration tests * Implement from_*() methods and add tests * Prototype inline injections * Add integration test for inline providers * Refactor integration tests * Add integration test for reordered schema * Remove unused imports from tests * Refactor schema module * Update tests to match latest schemas * Add mypy_boto3_s3 to the test requirements * Add boto3 to the test requirements * Add set_provides for Callable, Factory, and Singleton providers * Fix warnings in tests * Add typing stubs for Callable, Factory, and Singleton .set_provides() attributes * Fix singleton children to have optional provides * Implement provider to provider resolving * Fix pypy3 tests * Implement boto3 session use case and add tests * Implement lazy initialization and improve copying for Callable, Factory, Singleton, and Coroutine providers * Fix Python 2 tests * Add region name for boto3 integration example * Remove f-strings from set_provides() * Fix schema flake8 errors * Implement lazy initialization and improve copying for Delegate provider * Implement lazy initialization and improve copying for Object provider * Speed up wiring tests * Implement lazy initialization and improve copying for FactoryAggregate provider * Implement lazy initialization and improve copying for Selector provider * Implement lazy initialization and improve copying for Dependency provider * Implement lazy initialization and improve copying for Resource provider * Implement lazy initialization and improve copying for Configuration provider * Implement lazy initialization and improve copying for ProvidedInstance provider * Implement lazy initialization and improve copying for AttributeGetter provider * Implement lazy initialization and improve copying for ItemGetter provider * Implement lazy initialization and improve copying for MethodCaller provder * Update changelog * Fix typing in wiring module * Fix wiring module loader uninstallation issue * Fix provided instance providers error handing in asynchronous mode Co-authored-by: Roman Mogylatov <rmk@Romans-MacBook-Pro.local>
224 lines
7.0 KiB
Python
224 lines
7.0 KiB
Python
"""Dependency injector selector provider unit tests."""
|
|
|
|
import functools
|
|
import itertools
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
from dependency_injector import providers, errors
|
|
|
|
|
|
class SelectorTests(unittest.TestCase):
|
|
|
|
selector = providers.Configuration()
|
|
|
|
def test_is_provider(self):
|
|
self.assertTrue(providers.is_provider(providers.Selector(self.selector)))
|
|
|
|
def test_init_optional(self):
|
|
one = providers.Object(1)
|
|
two = providers.Object(2)
|
|
|
|
provider = providers.Selector()
|
|
provider.set_selector(self.selector)
|
|
provider.set_providers(one=one, two=two)
|
|
|
|
self.assertEqual(provider.providers, {'one': one, 'two': two})
|
|
with self.selector.override('one'):
|
|
self.assertEqual(provider(), one())
|
|
with self.selector.override('two'):
|
|
self.assertEqual(provider(), two())
|
|
|
|
def test_set_selector_returns_self(self):
|
|
provider = providers.Selector()
|
|
self.assertIs(provider.set_selector(self.selector), provider)
|
|
|
|
def test_set_providers_returns_self(self):
|
|
provider = providers.Selector()
|
|
self.assertIs(provider.set_providers(one=providers.Provider()), provider)
|
|
|
|
def test_provided_instance_provider(self):
|
|
provider = providers.Selector(self.selector)
|
|
self.assertIsInstance(provider.provided, providers.ProvidedInstance)
|
|
|
|
def test_call(self):
|
|
provider = providers.Selector(
|
|
self.selector,
|
|
one=providers.Object(1),
|
|
two=providers.Object(2),
|
|
)
|
|
|
|
with self.selector.override('one'):
|
|
self.assertEqual(provider(), 1)
|
|
|
|
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'])),
|
|
one=providers.Object(1),
|
|
two=providers.Object(2),
|
|
)
|
|
|
|
self.assertEqual(provider(), 1)
|
|
self.assertEqual(provider(), 2)
|
|
self.assertEqual(provider(), 1)
|
|
self.assertEqual(provider(), 2)
|
|
|
|
def test_call_with_context_args(self):
|
|
provider = providers.Selector(
|
|
self.selector,
|
|
one=providers.Callable(lambda *args, **kwargs: (args, kwargs)),
|
|
)
|
|
|
|
with self.selector.override('one'):
|
|
args, kwargs = provider(1, 2, three=3, four=4)
|
|
|
|
self.assertEqual(args, (1, 2))
|
|
self.assertEqual(kwargs, {'three': 3, 'four': 4})
|
|
|
|
def test_getattr(self):
|
|
provider_one = providers.Object(1)
|
|
provider_two = providers.Object(2)
|
|
|
|
provider = providers.Selector(
|
|
self.selector,
|
|
one=provider_one,
|
|
two=provider_two,
|
|
)
|
|
|
|
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))
|
|
overriding_provider2 = providers.Selector(self.selector, sample=providers.Object(3))
|
|
|
|
provider.override(overriding_provider1)
|
|
provider.override(overriding_provider2)
|
|
|
|
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)
|
|
|
|
provider_copy = providers.deepcopy(provider)
|
|
|
|
self.assertIsNot(provider, provider_copy)
|
|
self.assertIsInstance(provider, providers.Selector)
|
|
|
|
def test_deepcopy_from_memo(self):
|
|
provider = providers.Selector(self.selector)
|
|
provider_copy_memo = providers.Selector(self.selector)
|
|
|
|
provider_copy = providers.deepcopy(
|
|
provider,
|
|
memo={id(provider): provider_copy_memo},
|
|
)
|
|
|
|
self.assertIs(provider_copy, provider_copy_memo)
|
|
|
|
def test_deepcopy_overridden(self):
|
|
provider = providers.Selector(self.selector)
|
|
object_provider = providers.Object(object())
|
|
|
|
provider.override(object_provider)
|
|
|
|
provider_copy = providers.deepcopy(provider)
|
|
object_provider_copy = provider_copy.overridden[0]
|
|
|
|
self.assertIsNot(provider, provider_copy)
|
|
self.assertIsInstance(provider, providers.Selector)
|
|
|
|
self.assertIsNot(object_provider, object_provider_copy)
|
|
self.assertIsInstance(object_provider_copy, providers.Object)
|
|
|
|
def test_deepcopy_with_sys_streams(self):
|
|
provider = providers.Selector(
|
|
self.selector,
|
|
stdin=providers.Object(sys.stdin),
|
|
stdout=providers.Object(sys.stdout),
|
|
stderr=providers.Object(sys.stderr),
|
|
|
|
)
|
|
|
|
provider_copy = providers.deepcopy(provider)
|
|
|
|
self.assertIsNot(provider, provider_copy)
|
|
self.assertIsInstance(provider_copy, providers.Selector)
|
|
|
|
with self.selector.override('stdin'):
|
|
self.assertIs(provider(), sys.stdin)
|
|
|
|
with self.selector.override('stdout'):
|
|
self.assertIs(provider(), sys.stdout)
|
|
|
|
with self.selector.override('stderr'):
|
|
self.assertIs(provider(), sys.stderr)
|
|
|
|
def test_repr(self):
|
|
provider = providers.Selector(
|
|
self.selector,
|
|
one=providers.Object(1),
|
|
two=providers.Object(2),
|
|
)
|
|
|
|
self.assertIn(
|
|
'<dependency_injector.providers.Selector({0}'.format(repr(self.selector)),
|
|
repr(provider),
|
|
)
|
|
self.assertIn('one={0}'.format(repr(provider.one)), repr(provider))
|
|
self.assertIn('two={0}'.format(repr(provider.two)), repr(provider))
|
|
self.assertIn('at {0}'.format(hex(id(provider))), repr(provider))
|