mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-05 12:53:30 +03:00
Migrate container provider tests
This commit is contained in:
parent
a11c976213
commit
8a07bf0e39
|
@ -1,4 +1,4 @@
|
||||||
"""Dependency injector container provider unit tests."""
|
"""Container provider tests."""
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
|
@ -42,19 +42,19 @@ class Application(containers.DeclarativeContainer):
|
||||||
dict_factory = providers.Factory(dict, value=core.value_getter)
|
dict_factory = providers.Factory(dict, value=core.value_getter)
|
||||||
|
|
||||||
|
|
||||||
class ContainerTests(unittest.TestCase):
|
def test():
|
||||||
|
|
||||||
def test(self):
|
|
||||||
application = Application(config=_copied(TEST_CONFIG_1))
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
||||||
|
|
||||||
def test_double_override(self):
|
|
||||||
|
def test_double_override():
|
||||||
application = Application()
|
application = Application()
|
||||||
application.config.override(_copied(TEST_CONFIG_1))
|
application.config.override(_copied(TEST_CONFIG_1))
|
||||||
application.config.override(_copied(TEST_CONFIG_2))
|
application.config.override(_copied(TEST_CONFIG_2))
|
||||||
assert application.dict_factory() == {"value": TEST_VALUE_2}
|
assert application.dict_factory() == {"value": TEST_VALUE_2}
|
||||||
|
|
||||||
def test_override(self):
|
|
||||||
|
def test_override():
|
||||||
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
||||||
class D(containers.DeclarativeContainer):
|
class D(containers.DeclarativeContainer):
|
||||||
foo = providers.Object("foo")
|
foo = providers.Object("foo")
|
||||||
|
@ -72,7 +72,8 @@ class ContainerTests(unittest.TestCase):
|
||||||
result = b.a().bar()
|
result = b.a().bar()
|
||||||
assert result == "foo++"
|
assert result == "foo++"
|
||||||
|
|
||||||
def test_override_not_root_provider(self):
|
|
||||||
|
def test_override_not_root_provider():
|
||||||
# See: https://github.com/ets-labs/python-dependency-injector/issues/379
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/379
|
||||||
class NestedContainer(containers.DeclarativeContainer):
|
class NestedContainer(containers.DeclarativeContainer):
|
||||||
settings = providers.Configuration()
|
settings = providers.Configuration()
|
||||||
|
@ -116,15 +117,16 @@ class ContainerTests(unittest.TestCase):
|
||||||
assert container_using_container.root_container().print_settings() == {"container": "using_container", "foo": "bar"}
|
assert container_using_container.root_container().print_settings() == {"container": "using_container", "foo": "bar"}
|
||||||
assert container_using_container.not_root_container().print_settings() == {"container": "using_container", "foo": "bar"}
|
assert container_using_container.not_root_container().print_settings() == {"container": "using_container", "foo": "bar"}
|
||||||
|
|
||||||
def test_override_by_not_a_container(self):
|
|
||||||
|
def test_override_by_not_a_container():
|
||||||
provider = providers.Container(Core)
|
provider = providers.Container(Core)
|
||||||
|
|
||||||
with raises(errors.Error):
|
with raises(errors.Error):
|
||||||
provider.override(providers.Object("foo"))
|
provider.override(providers.Object("foo"))
|
||||||
|
|
||||||
def test_lazy_overriding(self):
|
|
||||||
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
|
||||||
|
|
||||||
|
def test_lazy_overriding():
|
||||||
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
||||||
class D(containers.DeclarativeContainer):
|
class D(containers.DeclarativeContainer):
|
||||||
foo = providers.Object("foo")
|
foo = providers.Object("foo")
|
||||||
|
|
||||||
|
@ -141,9 +143,9 @@ class ContainerTests(unittest.TestCase):
|
||||||
result = b.a().bar()
|
result = b.a().bar()
|
||||||
assert result == "foo++"
|
assert result == "foo++"
|
||||||
|
|
||||||
def test_lazy_overriding_deep(self):
|
|
||||||
# Extended version of test_lazy_overriding()
|
|
||||||
|
|
||||||
|
def test_lazy_overriding_deep():
|
||||||
|
# Extended version of test_lazy_overriding()
|
||||||
class D(containers.DeclarativeContainer):
|
class D(containers.DeclarativeContainer):
|
||||||
foo = providers.Object("foo")
|
foo = providers.Object("foo")
|
||||||
|
|
||||||
|
@ -164,7 +166,8 @@ class ContainerTests(unittest.TestCase):
|
||||||
result = b.a().c().bar()
|
result = b.a().c().bar()
|
||||||
assert result == "foo++"
|
assert result == "foo++"
|
||||||
|
|
||||||
def test_reset_last_overriding(self):
|
|
||||||
|
def test_reset_last_overriding():
|
||||||
application = Application(config=_copied(TEST_CONFIG_1))
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
application.core.override(Core(config=_copied(TEST_CONFIG_2["core"])))
|
application.core.override(Core(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
|
@ -172,7 +175,8 @@ class ContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
||||||
|
|
||||||
def test_reset_last_overriding_only_overridden(self):
|
|
||||||
|
def test_reset_last_overriding_only_overridden():
|
||||||
application = Application(config=_copied(TEST_CONFIG_1))
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
|
@ -180,7 +184,8 @@ class ContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
||||||
|
|
||||||
def test_override_context_manager(self):
|
|
||||||
|
def test_override_context_manager():
|
||||||
application = Application(config=_copied(TEST_CONFIG_1))
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
overriding_core = Core(config=_copied(TEST_CONFIG_2["core"]))
|
overriding_core = Core(config=_copied(TEST_CONFIG_2["core"]))
|
||||||
|
|
||||||
|
@ -190,7 +195,8 @@ class ContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
||||||
|
|
||||||
def test_reset_override(self):
|
|
||||||
|
def test_reset_override():
|
||||||
application = Application(config=_copied(TEST_CONFIG_1))
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
application.core.override(Core(config=_copied(TEST_CONFIG_2["core"])))
|
application.core.override(Core(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
|
@ -198,7 +204,8 @@ class ContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
assert application.dict_factory() == {"value": None}
|
assert application.dict_factory() == {"value": None}
|
||||||
|
|
||||||
def test_reset_override_only_overridden(self):
|
|
||||||
|
def test_reset_override_only_overridden():
|
||||||
application = Application(config=_copied(TEST_CONFIG_1))
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
|
@ -206,7 +213,8 @@ class ContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
assert application.dict_factory() == {"value": None}
|
assert application.dict_factory() == {"value": None}
|
||||||
|
|
||||||
def test_assign_parent(self):
|
|
||||||
|
def test_assign_parent():
|
||||||
parent = providers.DependenciesContainer()
|
parent = providers.DependenciesContainer()
|
||||||
provider = providers.Container(Core)
|
provider = providers.Container(Core)
|
||||||
|
|
||||||
|
@ -214,23 +222,27 @@ class ContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
assert provider.parent is parent
|
assert provider.parent is parent
|
||||||
|
|
||||||
def test_parent_name(self):
|
|
||||||
|
def test_parent_name():
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
provider = providers.Container(Core)
|
provider = providers.Container(Core)
|
||||||
container.name = provider
|
container.name = provider
|
||||||
assert provider.parent_name == "name"
|
assert provider.parent_name == "name"
|
||||||
|
|
||||||
def test_parent_name_with_deep_parenting(self):
|
|
||||||
|
def test_parent_name_with_deep_parenting():
|
||||||
provider = providers.Container(Core)
|
provider = providers.Container(Core)
|
||||||
container = providers.DependenciesContainer(name=provider)
|
container = providers.DependenciesContainer(name=provider)
|
||||||
_ = providers.DependenciesContainer(container=container)
|
_ = providers.DependenciesContainer(container=container)
|
||||||
assert provider.parent_name == "container.name"
|
assert provider.parent_name == "container.name"
|
||||||
|
|
||||||
def test_parent_name_is_none(self):
|
|
||||||
|
def test_parent_name_is_none():
|
||||||
provider = providers.Container(Core)
|
provider = providers.Container(Core)
|
||||||
assert provider.parent_name is None
|
assert provider.parent_name is None
|
||||||
|
|
||||||
def test_parent_deepcopy(self):
|
|
||||||
|
def test_parent_deepcopy():
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
provider = providers.Container(Core)
|
provider = providers.Container(Core)
|
||||||
container.name = provider
|
container.name = provider
|
||||||
|
@ -244,11 +256,13 @@ class ContainerTests(unittest.TestCase):
|
||||||
assert container.name is not copied.name
|
assert container.name is not copied.name
|
||||||
assert container.name.parent is not copied.name.parent
|
assert container.name.parent is not copied.name.parent
|
||||||
|
|
||||||
def test_resolve_provider_name(self):
|
|
||||||
|
def test_resolve_provider_name():
|
||||||
container = providers.Container(Core)
|
container = providers.Container(Core)
|
||||||
assert container.resolve_provider_name(container.value_getter) == "value_getter"
|
assert container.resolve_provider_name(container.value_getter) == "value_getter"
|
||||||
|
|
||||||
def test_resolve_provider_name_no_provider(self):
|
|
||||||
|
def test_resolve_provider_name_no_provider():
|
||||||
container = providers.Container(Core)
|
container = providers.Container(Core)
|
||||||
with raises(errors.Error):
|
with raises(errors.Error):
|
||||||
container.resolve_provider_name(providers.Provider())
|
container.resolve_provider_name(providers.Provider())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user