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,213 +42,227 @@ 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():
|
||||||
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
|
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
||||||
|
|
||||||
def test(self):
|
|
||||||
application = Application(config=_copied(TEST_CONFIG_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):
|
|
||||||
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
|
||||||
class D(containers.DeclarativeContainer):
|
|
||||||
foo = providers.Object("foo")
|
|
||||||
|
|
||||||
class A(containers.DeclarativeContainer):
|
def test_override():
|
||||||
d = providers.DependenciesContainer()
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
||||||
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
|
class D(containers.DeclarativeContainer):
|
||||||
|
foo = providers.Object("foo")
|
||||||
|
|
||||||
class B(containers.DeclarativeContainer):
|
class A(containers.DeclarativeContainer):
|
||||||
d = providers.Container(D)
|
d = providers.DependenciesContainer()
|
||||||
|
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
|
||||||
|
|
||||||
a = providers.Container(A, d=d)
|
class B(containers.DeclarativeContainer):
|
||||||
|
d = providers.Container(D)
|
||||||
|
|
||||||
b = B(d=D())
|
a = providers.Container(A, d=d)
|
||||||
result = b.a().bar()
|
|
||||||
assert result == "foo++"
|
|
||||||
|
|
||||||
def test_override_not_root_provider(self):
|
b = B(d=D())
|
||||||
# See: https://github.com/ets-labs/python-dependency-injector/issues/379
|
result = b.a().bar()
|
||||||
class NestedContainer(containers.DeclarativeContainer):
|
assert result == "foo++"
|
||||||
settings = providers.Configuration()
|
|
||||||
|
|
||||||
print_settings = providers.Callable(
|
|
||||||
lambda s: s,
|
|
||||||
settings,
|
|
||||||
)
|
|
||||||
|
|
||||||
class TestContainer(containers.DeclarativeContainer):
|
def test_override_not_root_provider():
|
||||||
settings = providers.Configuration()
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/379
|
||||||
|
class NestedContainer(containers.DeclarativeContainer):
|
||||||
|
settings = providers.Configuration()
|
||||||
|
|
||||||
root_container = providers.Container(
|
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,
|
NestedContainer,
|
||||||
settings=settings,
|
settings=settings,
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
not_root_container = providers.Selector(
|
container_using_factory = TestContainer(settings=dict(
|
||||||
settings.container,
|
container="using_factory",
|
||||||
using_factory=providers.Factory(
|
foo="bar"
|
||||||
NestedContainer,
|
))
|
||||||
settings=settings,
|
assert container_using_factory.root_container().print_settings() == {"container": "using_factory", "foo": "bar"}
|
||||||
),
|
assert container_using_factory.not_root_container().print_settings() == {"container": "using_factory", "foo": "bar"}
|
||||||
using_container=providers.Container(
|
|
||||||
NestedContainer,
|
|
||||||
settings=settings,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
container_using_factory = TestContainer(settings=dict(
|
container_using_container = TestContainer(settings=dict(
|
||||||
container="using_factory",
|
container="using_container",
|
||||||
foo="bar"
|
foo="bar"
|
||||||
))
|
))
|
||||||
assert container_using_factory.root_container().print_settings() == {"container": "using_factory", "foo": "bar"}
|
assert container_using_container.root_container().print_settings() == {"container": "using_container", "foo": "bar"}
|
||||||
assert container_using_factory.not_root_container().print_settings() == {"container": "using_factory", "foo": "bar"}
|
assert container_using_container.not_root_container().print_settings() == {"container": "using_container", "foo": "bar"}
|
||||||
|
|
||||||
container_using_container = TestContainer(settings=dict(
|
|
||||||
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"}
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
class D(containers.DeclarativeContainer):
|
def test_lazy_overriding():
|
||||||
foo = providers.Object("foo")
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
||||||
|
class D(containers.DeclarativeContainer):
|
||||||
|
foo = providers.Object("foo")
|
||||||
|
|
||||||
class A(containers.DeclarativeContainer):
|
class A(containers.DeclarativeContainer):
|
||||||
d = providers.DependenciesContainer()
|
d = providers.DependenciesContainer()
|
||||||
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
|
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
|
||||||
|
|
||||||
class B(containers.DeclarativeContainer):
|
class B(containers.DeclarativeContainer):
|
||||||
d = providers.DependenciesContainer()
|
d = providers.DependenciesContainer()
|
||||||
|
|
||||||
a = providers.Container(A, d=d)
|
a = providers.Container(A, d=d)
|
||||||
|
|
||||||
b = B(d=D())
|
b = B(d=D())
|
||||||
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()
|
|
||||||
|
|
||||||
class D(containers.DeclarativeContainer):
|
def test_lazy_overriding_deep():
|
||||||
foo = providers.Object("foo")
|
# Extended version of test_lazy_overriding()
|
||||||
|
class D(containers.DeclarativeContainer):
|
||||||
|
foo = providers.Object("foo")
|
||||||
|
|
||||||
class C(containers.DeclarativeContainer):
|
class C(containers.DeclarativeContainer):
|
||||||
d = providers.DependenciesContainer()
|
d = providers.DependenciesContainer()
|
||||||
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
|
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
|
||||||
|
|
||||||
class A(containers.DeclarativeContainer):
|
class A(containers.DeclarativeContainer):
|
||||||
d = providers.DependenciesContainer()
|
d = providers.DependenciesContainer()
|
||||||
c = providers.Container(C, d=d)
|
c = providers.Container(C, d=d)
|
||||||
|
|
||||||
class B(containers.DeclarativeContainer):
|
class B(containers.DeclarativeContainer):
|
||||||
d = providers.DependenciesContainer()
|
d = providers.DependenciesContainer()
|
||||||
|
|
||||||
a = providers.Container(A, d=d)
|
a = providers.Container(A, d=d)
|
||||||
|
|
||||||
b = B(d=D())
|
b = B(d=D())
|
||||||
result = b.a().c().bar()
|
result = b.a().c().bar()
|
||||||
assert result == "foo++"
|
assert result == "foo++"
|
||||||
|
|
||||||
def test_reset_last_overriding(self):
|
|
||||||
application = Application(config=_copied(TEST_CONFIG_1))
|
|
||||||
application.core.override(Core(config=_copied(TEST_CONFIG_2["core"])))
|
|
||||||
|
|
||||||
application.core.reset_last_overriding()
|
def test_reset_last_overriding():
|
||||||
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
|
application.core.override(Core(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
application.core.reset_last_overriding()
|
||||||
|
|
||||||
def test_reset_last_overriding_only_overridden(self):
|
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
||||||
application = Application(config=_copied(TEST_CONFIG_1))
|
|
||||||
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
|
||||||
|
|
||||||
application.core.reset_last_overriding()
|
|
||||||
|
|
||||||
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
def test_reset_last_overriding_only_overridden():
|
||||||
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
|
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
def test_override_context_manager(self):
|
application.core.reset_last_overriding()
|
||||||
application = Application(config=_copied(TEST_CONFIG_1))
|
|
||||||
overriding_core = Core(config=_copied(TEST_CONFIG_2["core"]))
|
|
||||||
|
|
||||||
with application.core.override(overriding_core) as context_core:
|
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
||||||
assert application.dict_factory() == {"value": TEST_VALUE_2}
|
|
||||||
assert context_core() is overriding_core
|
|
||||||
|
|
||||||
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
|
||||||
|
|
||||||
def test_reset_override(self):
|
def test_override_context_manager():
|
||||||
application = Application(config=_copied(TEST_CONFIG_1))
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
application.core.override(Core(config=_copied(TEST_CONFIG_2["core"])))
|
overriding_core = Core(config=_copied(TEST_CONFIG_2["core"]))
|
||||||
|
|
||||||
application.core.reset_override()
|
with application.core.override(overriding_core) as context_core:
|
||||||
|
assert application.dict_factory() == {"value": TEST_VALUE_2}
|
||||||
|
assert context_core() is overriding_core
|
||||||
|
|
||||||
assert application.dict_factory() == {"value": None}
|
assert application.dict_factory() == {"value": TEST_VALUE_1}
|
||||||
|
|
||||||
def test_reset_override_only_overridden(self):
|
|
||||||
application = Application(config=_copied(TEST_CONFIG_1))
|
|
||||||
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
|
||||||
|
|
||||||
application.core.reset_override()
|
def test_reset_override():
|
||||||
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
|
application.core.override(Core(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
assert application.dict_factory() == {"value": None}
|
application.core.reset_override()
|
||||||
|
|
||||||
def test_assign_parent(self):
|
assert application.dict_factory() == {"value": None}
|
||||||
parent = providers.DependenciesContainer()
|
|
||||||
provider = providers.Container(Core)
|
|
||||||
|
|
||||||
provider.assign_parent(parent)
|
|
||||||
|
|
||||||
assert provider.parent is parent
|
def test_reset_override_only_overridden():
|
||||||
|
application = Application(config=_copied(TEST_CONFIG_1))
|
||||||
|
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
def test_parent_name(self):
|
application.core.reset_override()
|
||||||
container = containers.DynamicContainer()
|
|
||||||
provider = providers.Container(Core)
|
|
||||||
container.name = provider
|
|
||||||
assert provider.parent_name == "name"
|
|
||||||
|
|
||||||
def test_parent_name_with_deep_parenting(self):
|
assert application.dict_factory() == {"value": None}
|
||||||
provider = providers.Container(Core)
|
|
||||||
container = providers.DependenciesContainer(name=provider)
|
|
||||||
_ = providers.DependenciesContainer(container=container)
|
|
||||||
assert provider.parent_name == "container.name"
|
|
||||||
|
|
||||||
def test_parent_name_is_none(self):
|
|
||||||
provider = providers.Container(Core)
|
|
||||||
assert provider.parent_name is None
|
|
||||||
|
|
||||||
def test_parent_deepcopy(self):
|
def test_assign_parent():
|
||||||
container = containers.DynamicContainer()
|
parent = providers.DependenciesContainer()
|
||||||
provider = providers.Container(Core)
|
provider = providers.Container(Core)
|
||||||
container.name = provider
|
|
||||||
|
|
||||||
copied = providers.deepcopy(container)
|
provider.assign_parent(parent)
|
||||||
|
|
||||||
assert container.name.parent is container
|
assert provider.parent is parent
|
||||||
assert copied.name.parent is copied
|
|
||||||
|
|
||||||
assert container is not copied
|
|
||||||
assert container.name is not copied.name
|
|
||||||
assert container.name.parent is not copied.name.parent
|
|
||||||
|
|
||||||
def test_resolve_provider_name(self):
|
def test_parent_name():
|
||||||
container = providers.Container(Core)
|
container = containers.DynamicContainer()
|
||||||
assert container.resolve_provider_name(container.value_getter) == "value_getter"
|
provider = providers.Container(Core)
|
||||||
|
container.name = provider
|
||||||
|
assert provider.parent_name == "name"
|
||||||
|
|
||||||
def test_resolve_provider_name_no_provider(self):
|
|
||||||
container = providers.Container(Core)
|
def test_parent_name_with_deep_parenting():
|
||||||
with raises(errors.Error):
|
provider = providers.Container(Core)
|
||||||
container.resolve_provider_name(providers.Provider())
|
container = providers.DependenciesContainer(name=provider)
|
||||||
|
_ = providers.DependenciesContainer(container=container)
|
||||||
|
assert provider.parent_name == "container.name"
|
||||||
|
|
||||||
|
|
||||||
|
def test_parent_name_is_none():
|
||||||
|
provider = providers.Container(Core)
|
||||||
|
assert provider.parent_name is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_parent_deepcopy():
|
||||||
|
container = containers.DynamicContainer()
|
||||||
|
provider = providers.Container(Core)
|
||||||
|
container.name = provider
|
||||||
|
|
||||||
|
copied = providers.deepcopy(container)
|
||||||
|
|
||||||
|
assert container.name.parent is container
|
||||||
|
assert copied.name.parent is copied
|
||||||
|
|
||||||
|
assert container is not copied
|
||||||
|
assert container.name is not copied.name
|
||||||
|
assert container.name.parent is not copied.name.parent
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_provider_name():
|
||||||
|
container = providers.Container(Core)
|
||||||
|
assert container.resolve_provider_name(container.value_getter) == "value_getter"
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_provider_name_no_provider():
|
||||||
|
container = providers.Container(Core)
|
||||||
|
with raises(errors.Error):
|
||||||
|
container.resolve_provider_name(providers.Provider())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user