python-dependency-injector/tests/unit/providers/test_provided_instance_py2_py3.py

210 lines
6.1 KiB
Python
Raw Normal View History

"""Dependency injector provided instance provider unit tests."""
import unittest
from dependency_injector import containers, providers
class Service:
def __init__(self, value):
self.value = value
self.values = [self.value]
2021-02-05 16:48:25 +03:00
def __call__(self):
return self.value
def __getitem__(self, item):
return self.values[item]
2021-02-05 16:48:25 +03:00
def get_value(self):
return self.value
def get_closure(self):
def closure():
return self.value
return closure
class Client:
def __init__(self, value):
self.value = value
class Container(containers.DeclarativeContainer):
2021-10-01 03:09:42 +03:00
service = providers.Singleton(Service, value="foo")
client_attribute = providers.Factory(
Client,
value=service.provided.value,
)
client_item = providers.Factory(
Client,
value=service.provided[0],
)
client_attribute_item = providers.Factory(
Client,
value=service.provided.values[0],
)
client_method_call = providers.Factory(
Client,
value=service.provided.get_value.call(),
)
2021-02-05 16:48:25 +03:00
client_method_closure_call = providers.Factory(
Client,
value=service.provided.get_closure.call().call(),
)
client_provided_call = providers.Factory(
Client,
value=service.provided.call(),
)
class ProvidedInstanceTests(unittest.TestCase):
def setUp(self):
self.container = Container()
def test_is_provider(self):
self.assertTrue(providers.is_provider(self.container.service.provided))
def test_attribute(self):
client = self.container.client_attribute()
2021-10-01 03:09:42 +03:00
self.assertEqual(client.value, "foo")
def test_item(self):
client = self.container.client_item()
2021-10-01 03:09:42 +03:00
self.assertEqual(client.value, "foo")
def test_attribute_item(self):
client = self.container.client_attribute_item()
2021-10-01 03:09:42 +03:00
self.assertEqual(client.value, "foo")
def test_method_call(self):
client = self.container.client_method_call()
2021-10-01 03:09:42 +03:00
self.assertEqual(client.value, "foo")
2021-02-05 16:48:25 +03:00
def test_method_closure_call(self):
client = self.container.client_method_closure_call()
2021-10-01 03:09:42 +03:00
self.assertEqual(client.value, "foo")
2021-02-05 16:48:25 +03:00
def test_provided_call(self):
client = self.container.client_provided_call()
2021-10-01 03:09:42 +03:00
self.assertEqual(client.value, "foo")
2021-02-05 16:48:25 +03:00
def test_call_overridden(self):
2021-10-01 03:09:42 +03:00
value = "bar"
with self.container.service.override(Service(value)):
self.assertEqual(self.container.client_attribute().value, value)
self.assertEqual(self.container.client_item().value, value)
self.assertEqual(self.container.client_attribute_item().value, value)
self.assertEqual(self.container.client_method_call().value, value)
def test_repr_provided_instance(self):
provider = self.container.service.provided
self.assertEqual(
2021-10-01 03:09:42 +03:00
"ProvidedInstance(\"{0}\")".format(repr(self.container.service)),
repr(provider),
)
def test_repr_attribute_getter(self):
provider = self.container.service.provided.value
self.assertEqual(
2021-10-01 03:09:42 +03:00
"AttributeGetter(\"value\")",
repr(provider),
)
def test_repr_item_getter(self):
2021-10-01 03:09:42 +03:00
provider = self.container.service.provided["test-test"]
self.assertEqual(
2021-10-01 03:09:42 +03:00
"ItemGetter(\"test-test\")",
repr(provider),
)
Schemas (#429) * 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>
2021-03-20 20:16:51 +03:00
class LazyInitTests(unittest.TestCase):
def test_provided_instance(self):
provides = providers.Object(object())
provider = providers.ProvidedInstance()
provider.set_provides(provides)
self.assertIs(provider.provides, provides)
self.assertIs(provider.set_provides(providers.Provider()), provider)
def test_attribute_getter(self):
provides = providers.Object(object())
provider = providers.AttributeGetter()
provider.set_provides(provides)
2021-10-01 03:09:42 +03:00
provider.set_name("__dict__")
Schemas (#429) * 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>
2021-03-20 20:16:51 +03:00
self.assertIs(provider.provides, provides)
2021-10-01 03:09:42 +03:00
self.assertEqual(provider.name, "__dict__")
Schemas (#429) * 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>
2021-03-20 20:16:51 +03:00
self.assertIs(provider.set_provides(providers.Provider()), provider)
2021-10-01 03:09:42 +03:00
self.assertIs(provider.set_name("__dict__"), provider)
Schemas (#429) * 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>
2021-03-20 20:16:51 +03:00
def test_item_getter(self):
2021-10-01 03:09:42 +03:00
provides = providers.Object({"foo": "bar"})
Schemas (#429) * 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>
2021-03-20 20:16:51 +03:00
provider = providers.ItemGetter()
provider.set_provides(provides)
2021-10-01 03:09:42 +03:00
provider.set_name("foo")
Schemas (#429) * 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>
2021-03-20 20:16:51 +03:00
self.assertIs(provider.provides, provides)
2021-10-01 03:09:42 +03:00
self.assertEqual(provider.name, "foo")
Schemas (#429) * 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>
2021-03-20 20:16:51 +03:00
self.assertIs(provider.set_provides(providers.Provider()), provider)
2021-10-01 03:09:42 +03:00
self.assertIs(provider.set_name("foo"), provider)
Schemas (#429) * 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>
2021-03-20 20:16:51 +03:00
def test_method_caller(self):
provides = providers.Object(lambda: 42)
provider = providers.MethodCaller()
provider.set_provides(provides)
self.assertIs(provider.provides, provides)
self.assertEqual(provider(), 42)
self.assertIs(provider.set_provides(providers.Provider()), provider)
class ProvidedInstancePuzzleTests(unittest.TestCase):
def test_puzzled(self):
2021-10-01 03:09:42 +03:00
service = providers.Singleton(Service, value="foo-bar")
dependency = providers.Object(
{
2021-10-01 03:09:42 +03:00
"a": {
"b": {
"c1": 10,
"c2": lambda arg: {"arg": arg}
},
},
},
)
test_list = providers.List(
2021-10-01 03:09:42 +03:00
dependency.provided["a"]["b"]["c1"],
dependency.provided["a"]["b"]["c2"].call(22)["arg"],
dependency.provided["a"]["b"]["c2"].call(service)["arg"],
dependency.provided["a"]["b"]["c2"].call(service)["arg"].value,
dependency.provided["a"]["b"]["c2"].call(service)["arg"].get_value.call(),
)
result = test_list()
self.assertEqual(
result,
[
10,
22,
service(),
2021-10-01 03:09:42 +03:00
"foo-bar",
"foo-bar",
],
)
class ProvidedInstanceInBaseClassTests(unittest.TestCase):
def test_provided_attribute(self):
provider = providers.Provider()
assert isinstance(provider.provided, providers.ProvidedInstance)