2020-06-23 05:45:16 +03:00
|
|
|
"""Dependency injector container provider unit tests."""
|
|
|
|
|
|
|
|
import copy
|
|
|
|
|
2021-03-06 04:17:28 +03:00
|
|
|
import unittest
|
2020-06-23 05:45:16 +03:00
|
|
|
|
2021-01-12 16:41:59 +03:00
|
|
|
from dependency_injector import containers, providers, errors
|
2020-06-23 05:45:16 +03:00
|
|
|
|
|
|
|
|
2021-10-01 03:09:42 +03:00
|
|
|
TEST_VALUE_1 = "core_section_value1"
|
2020-06-23 05:45:16 +03:00
|
|
|
TEST_CONFIG_1 = {
|
2021-10-01 03:09:42 +03:00
|
|
|
"core": {
|
|
|
|
"section": {
|
|
|
|
"value": TEST_VALUE_1,
|
2020-06-23 05:45:16 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-01 03:09:42 +03:00
|
|
|
TEST_VALUE_2 = "core_section_value2"
|
2020-06-23 05:45:16 +03:00
|
|
|
TEST_CONFIG_2 = {
|
2021-10-01 03:09:42 +03:00
|
|
|
"core": {
|
|
|
|
"section": {
|
|
|
|
"value": TEST_VALUE_2,
|
2020-06-23 05:45:16 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _copied(value):
|
|
|
|
return copy.deepcopy(value)
|
|
|
|
|
|
|
|
|
|
|
|
class TestCore(containers.DeclarativeContainer):
|
2021-10-01 03:09:42 +03:00
|
|
|
config = providers.Configuration("core")
|
2020-06-23 05:45:16 +03:00
|
|
|
value_getter = providers.Callable(lambda _: _, config.section.value)
|
|
|
|
|
|
|
|
|
|
|
|
class TestApplication(containers.DeclarativeContainer):
|
2021-10-01 03:09:42 +03:00
|
|
|
config = providers.Configuration("config")
|
2020-06-23 05:45:16 +03:00
|
|
|
core = providers.Container(TestCore, config=config.core)
|
|
|
|
dict_factory = providers.Factory(dict, value=core.value_getter)
|
|
|
|
|
|
|
|
|
|
|
|
class ContainerTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test(self):
|
|
|
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_1})
|
2020-06-23 05:45:16 +03:00
|
|
|
|
|
|
|
def test_double_override(self):
|
|
|
|
application = TestApplication()
|
|
|
|
application.config.override(_copied(TEST_CONFIG_1))
|
|
|
|
application.config.override(_copied(TEST_CONFIG_2))
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_2})
|
2021-01-12 16:41:59 +03:00
|
|
|
|
|
|
|
def test_override(self):
|
2021-01-27 22:01:33 +03:00
|
|
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
2021-01-12 16:41:59 +03:00
|
|
|
class D(containers.DeclarativeContainer):
|
2021-10-01 03:09:42 +03:00
|
|
|
foo = providers.Object("foo")
|
2021-01-12 16:41:59 +03:00
|
|
|
|
|
|
|
class A(containers.DeclarativeContainer):
|
|
|
|
d = providers.DependenciesContainer()
|
2021-10-01 03:09:42 +03:00
|
|
|
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
|
2021-01-12 16:41:59 +03:00
|
|
|
|
|
|
|
class B(containers.DeclarativeContainer):
|
|
|
|
d = providers.Container(D)
|
|
|
|
|
|
|
|
a = providers.Container(A, d=d)
|
|
|
|
|
|
|
|
b = B(d=D())
|
|
|
|
result = b.a().bar()
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(result, "foo++")
|
2021-01-27 22:01:33 +03:00
|
|
|
|
|
|
|
def test_override_not_root_provider(self):
|
|
|
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/379
|
|
|
|
class NestedContainer(containers.DeclarativeContainer):
|
|
|
|
settings = providers.Configuration()
|
|
|
|
|
|
|
|
print_settings = providers.Callable(
|
|
|
|
lambda s: s,
|
|
|
|
settings,
|
|
|
|
)
|
|
|
|
|
|
|
|
class TestContainer(containers.DeclarativeContainer):
|
|
|
|
settings = providers.Configuration()
|
|
|
|
|
|
|
|
root_container = providers.Container(
|
|
|
|
NestedContainer,
|
|
|
|
settings=settings,
|
|
|
|
)
|
|
|
|
|
|
|
|
not_root_container = providers.Selector(
|
|
|
|
settings.container,
|
|
|
|
using_factory=providers.Factory(
|
|
|
|
NestedContainer,
|
|
|
|
settings=settings,
|
|
|
|
),
|
|
|
|
using_container=providers.Container(
|
|
|
|
NestedContainer,
|
|
|
|
settings=settings,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
container_using_factory = TestContainer(settings=dict(
|
2021-10-01 03:09:42 +03:00
|
|
|
container="using_factory",
|
|
|
|
foo="bar"
|
2021-01-27 22:01:33 +03:00
|
|
|
))
|
|
|
|
self.assertEqual(
|
|
|
|
container_using_factory.root_container().print_settings(),
|
2021-10-01 03:09:42 +03:00
|
|
|
{"container": "using_factory", "foo": "bar"},
|
2021-01-27 22:01:33 +03:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
container_using_factory.not_root_container().print_settings(),
|
2021-10-01 03:09:42 +03:00
|
|
|
{"container": "using_factory", "foo": "bar"},
|
2021-01-27 22:01:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
container_using_container = TestContainer(settings=dict(
|
2021-10-01 03:09:42 +03:00
|
|
|
container="using_container",
|
|
|
|
foo="bar"
|
2021-01-27 22:01:33 +03:00
|
|
|
))
|
|
|
|
self.assertEqual(
|
|
|
|
container_using_container.root_container().print_settings(),
|
2021-10-01 03:09:42 +03:00
|
|
|
{"container": "using_container", "foo": "bar"},
|
2021-01-27 22:01:33 +03:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
container_using_container.not_root_container().print_settings(),
|
2021-10-01 03:09:42 +03:00
|
|
|
{"container": "using_container", "foo": "bar"},
|
2021-01-27 22:01:33 +03:00
|
|
|
)
|
2021-01-12 16:41:59 +03:00
|
|
|
|
|
|
|
def test_override_by_not_a_container(self):
|
|
|
|
provider = providers.Container(TestCore)
|
|
|
|
|
|
|
|
with self.assertRaises(errors.Error):
|
2021-10-01 03:09:42 +03:00
|
|
|
provider.override(providers.Object("foo"))
|
2021-01-14 01:07:41 +03:00
|
|
|
|
|
|
|
def test_lazy_overriding(self):
|
2021-02-05 16:59:16 +03:00
|
|
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
|
|
|
|
2021-01-14 01:07:41 +03:00
|
|
|
class D(containers.DeclarativeContainer):
|
|
|
|
foo = providers.Object("foo")
|
|
|
|
|
|
|
|
class A(containers.DeclarativeContainer):
|
|
|
|
d = providers.DependenciesContainer()
|
|
|
|
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
|
|
|
|
|
|
|
|
class B(containers.DeclarativeContainer):
|
|
|
|
d = providers.DependenciesContainer()
|
|
|
|
|
|
|
|
a = providers.Container(A, d=d)
|
|
|
|
|
|
|
|
b = B(d=D())
|
|
|
|
result = b.a().bar()
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(result, "foo++")
|
2021-02-05 16:59:16 +03:00
|
|
|
|
|
|
|
def test_lazy_overriding_deep(self):
|
|
|
|
# Extended version of test_lazy_overriding()
|
|
|
|
|
|
|
|
class D(containers.DeclarativeContainer):
|
|
|
|
foo = providers.Object("foo")
|
|
|
|
|
|
|
|
class C(containers.DeclarativeContainer):
|
|
|
|
d = providers.DependenciesContainer()
|
|
|
|
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
|
|
|
|
|
|
|
|
class A(containers.DeclarativeContainer):
|
|
|
|
d = providers.DependenciesContainer()
|
|
|
|
c = providers.Container(C, d=d)
|
|
|
|
|
|
|
|
class B(containers.DeclarativeContainer):
|
|
|
|
d = providers.DependenciesContainer()
|
|
|
|
|
|
|
|
a = providers.Container(A, d=d)
|
|
|
|
|
|
|
|
b = B(d=D())
|
|
|
|
result = b.a().c().bar()
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(result, "foo++")
|
2021-02-13 00:58:23 +03:00
|
|
|
|
2021-03-03 17:05:15 +03:00
|
|
|
def test_reset_last_overriding(self):
|
|
|
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
2021-10-01 03:09:42 +03:00
|
|
|
application.core.override(TestCore(config=_copied(TEST_CONFIG_2["core"])))
|
2021-03-03 17:05:15 +03:00
|
|
|
|
|
|
|
application.core.reset_last_overriding()
|
|
|
|
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_1})
|
2021-03-03 17:05:15 +03:00
|
|
|
|
|
|
|
def test_reset_last_overriding_only_overridden(self):
|
|
|
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
2021-10-01 03:09:42 +03:00
|
|
|
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
2021-03-03 17:05:15 +03:00
|
|
|
|
|
|
|
application.core.reset_last_overriding()
|
|
|
|
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_1})
|
2021-03-03 17:05:15 +03:00
|
|
|
|
|
|
|
def test_override_context_manager(self):
|
|
|
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
2021-10-01 03:09:42 +03:00
|
|
|
overriding_core = TestCore(config=_copied(TEST_CONFIG_2["core"]))
|
2021-03-03 17:05:15 +03:00
|
|
|
|
|
|
|
with application.core.override(overriding_core) as context_core:
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_2})
|
2021-03-03 17:05:15 +03:00
|
|
|
self.assertIs(context_core(), overriding_core)
|
|
|
|
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_1})
|
2021-03-03 17:05:15 +03:00
|
|
|
|
|
|
|
def test_reset_override(self):
|
|
|
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
2021-10-01 03:09:42 +03:00
|
|
|
application.core.override(TestCore(config=_copied(TEST_CONFIG_2["core"])))
|
2021-03-03 17:05:15 +03:00
|
|
|
|
|
|
|
application.core.reset_override()
|
|
|
|
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(application.dict_factory(), {"value": None})
|
2021-03-03 17:05:15 +03:00
|
|
|
|
|
|
|
def test_reset_override_only_overridden(self):
|
|
|
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
2021-10-01 03:09:42 +03:00
|
|
|
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
2021-03-03 17:05:15 +03:00
|
|
|
|
|
|
|
application.core.reset_override()
|
|
|
|
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(application.dict_factory(), {"value": None})
|
2021-03-03 17:05:15 +03:00
|
|
|
|
2021-02-13 00:58:23 +03:00
|
|
|
def test_assign_parent(self):
|
|
|
|
parent = providers.DependenciesContainer()
|
|
|
|
provider = providers.Container(TestCore)
|
|
|
|
|
|
|
|
provider.assign_parent(parent)
|
|
|
|
|
|
|
|
self.assertIs(provider.parent, parent)
|
|
|
|
|
|
|
|
def test_parent_name(self):
|
|
|
|
container = containers.DynamicContainer()
|
|
|
|
provider = providers.Container(TestCore)
|
|
|
|
container.name = provider
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(provider.parent_name, "name")
|
2021-02-13 00:58:23 +03:00
|
|
|
|
|
|
|
def test_parent_name_with_deep_parenting(self):
|
|
|
|
provider = providers.Container(TestCore)
|
|
|
|
container = providers.DependenciesContainer(name=provider)
|
|
|
|
_ = providers.DependenciesContainer(container=container)
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(provider.parent_name, "container.name")
|
2021-02-13 00:58:23 +03:00
|
|
|
|
|
|
|
def test_parent_name_is_none(self):
|
|
|
|
provider = providers.Container(TestCore)
|
|
|
|
self.assertIsNone(provider.parent_name)
|
|
|
|
|
|
|
|
def test_parent_deepcopy(self):
|
|
|
|
container = containers.DynamicContainer()
|
|
|
|
provider = providers.Container(TestCore)
|
|
|
|
container.name = provider
|
|
|
|
|
|
|
|
copied = providers.deepcopy(container)
|
|
|
|
|
|
|
|
self.assertIs(container.name.parent, container)
|
|
|
|
self.assertIs(copied.name.parent, copied)
|
|
|
|
|
|
|
|
self.assertIsNot(container, copied)
|
|
|
|
self.assertIsNot(container.name, copied.name)
|
|
|
|
self.assertIsNot(container.name.parent, copied.name.parent)
|
|
|
|
|
|
|
|
def test_resolve_provider_name(self):
|
|
|
|
container = providers.Container(TestCore)
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(container.resolve_provider_name(container.value_getter), "value_getter")
|
2021-02-13 00:58:23 +03:00
|
|
|
|
|
|
|
def test_resolve_provider_name_no_provider(self):
|
|
|
|
container = providers.Container(TestCore)
|
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
container.resolve_provider_name(providers.Provider())
|