mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-23 18:13:56 +03:00
94aca21fb8
* Add pytest and pytest-asyncio to the requirements * Update aiohttp ext test * Update setup.cfg * Update tox.ini * Add pytest to the tox requirements * Update tox.ini * Move configuration to tox.ini * Add pytest configs * Rename pytest-py34-py35.ini -> pytest-py35.ini * Update config file paths * Update makefile * Migrate common tests to pytest * Migrate FastAPI and Flask wiring tests * Rename flask and fastapi wiring test files * Move wiring autoloader tests * Add pytest-asyncio to the tox.ini * Migrate wiring async injection tests * Migrate main wiring tests * Migrate wiring string module and package names tests * Migrate wiring config tests * Migrate misc wiring tests * Update tests structure * Migrate misc wiring tests * Refactor container.from_schema() API tests * Migrate container.from_schema() integration tests * Rename schema samples * Update sample imports * Migrate container tests * Refactor container tests * Migrate container self tests * Migrate container instance tests * Migrate container custom string attribute name tests * Migrate container async resource tests * Fix py2 container tests * Migrate container cls tests * Migrate container class custom string cls as atrribute name tests * Migrate ext.aiohttp tests * Migrate ext.flasks tests * Update ext package tests doc block * Migrate provider utils tests * Migrate Factory async mode tests * Migrate async tests * Rename common test module * Refactor asserts in provider tests * Migrate factory tests * Migrate selector provider tests * Migrate object provider tests * Migrate self provider tests * Migrate delegate provider tests * Migrate provider tests * Migrate dependency provider tests * Migrate dependencies container provider tests * Fix warnings * Migrate list provider tests * Migrate dict provider tests * Migrate callable tests * Migrate injection tests * Migrate container provider tests * Migrate coroutine providers * Migrate traversal tests * Migrate resource tests * Migrate configuration tests * Migrate provided instance provider tests * Update doc blocks and imports * Migrate singleton tests * Update changelog and cosmetic fixes
246 lines
6.7 KiB
Python
246 lines
6.7 KiB
Python
"""Dict provider tests."""
|
|
|
|
import sys
|
|
|
|
from dependency_injector import providers
|
|
|
|
|
|
def test_is_provider():
|
|
assert providers.is_provider(providers.Dict()) is True
|
|
|
|
|
|
def test_provided_instance_provider():
|
|
provider = providers.Dict()
|
|
assert isinstance(provider.provided, providers.ProvidedInstance)
|
|
|
|
|
|
def test_init_with_non_string_keys():
|
|
a1 = object()
|
|
a2 = object()
|
|
provider = providers.Dict({a1: "i1", a2: "i2"})
|
|
|
|
dict1 = provider()
|
|
dict2 = provider()
|
|
|
|
assert dict1 == {a1: "i1", a2: "i2"}
|
|
assert dict2 == {a1: "i1", a2: "i2"}
|
|
|
|
assert dict1 is not dict2
|
|
|
|
|
|
def test_init_with_string_and_non_string_keys():
|
|
a1 = object()
|
|
provider = providers.Dict({a1: "i1"}, a2="i2")
|
|
|
|
dict1 = provider()
|
|
dict2 = provider()
|
|
|
|
assert dict1 == {a1: "i1", "a2": "i2"}
|
|
assert dict2 == {a1: "i1", "a2": "i2"}
|
|
|
|
assert dict1 is not dict2
|
|
|
|
|
|
def test_call_with_init_keyword_args():
|
|
provider = providers.Dict(a1="i1", a2="i2")
|
|
|
|
dict1 = provider()
|
|
dict2 = provider()
|
|
|
|
assert dict1 == {"a1": "i1", "a2": "i2"}
|
|
assert dict2 == {"a1": "i1", "a2": "i2"}
|
|
|
|
assert dict1 is not dict2
|
|
|
|
|
|
def test_call_with_context_keyword_args():
|
|
provider = providers.Dict(a1="i1", a2="i2")
|
|
assert provider(a3="i3", a4="i4") == {"a1": "i1", "a2": "i2", "a3": "i3", "a4": "i4"}
|
|
|
|
|
|
def test_call_with_provider():
|
|
provider = providers.Dict(
|
|
a1=providers.Factory(str, "i1"),
|
|
a2=providers.Factory(str, "i2"),
|
|
)
|
|
assert provider() == {"a1": "i1", "a2": "i2"}
|
|
|
|
|
|
def test_fluent_interface():
|
|
provider = providers.Dict() \
|
|
.add_kwargs(a1="i1", a2="i2")
|
|
assert provider() == {"a1": "i1", "a2": "i2"}
|
|
|
|
|
|
def test_add_kwargs():
|
|
provider = providers.Dict() \
|
|
.add_kwargs(a1="i1") \
|
|
.add_kwargs(a2="i2")
|
|
assert provider.kwargs == {"a1": "i1", "a2": "i2"}
|
|
|
|
|
|
def test_add_kwargs_non_string_keys():
|
|
a1 = object()
|
|
a2 = object()
|
|
provider = providers.Dict() \
|
|
.add_kwargs({a1: "i1"}) \
|
|
.add_kwargs({a2: "i2"})
|
|
assert provider.kwargs == {a1: "i1", a2: "i2"}
|
|
|
|
|
|
def test_add_kwargs_string_and_non_string_keys():
|
|
a2 = object()
|
|
provider = providers.Dict() \
|
|
.add_kwargs(a1="i1") \
|
|
.add_kwargs({a2: "i2"})
|
|
assert provider.kwargs == {"a1": "i1", a2: "i2"}
|
|
|
|
|
|
def test_set_kwargs():
|
|
provider = providers.Dict() \
|
|
.add_kwargs(a1="i1", a2="i2") \
|
|
.set_kwargs(a3="i3", a4="i4")
|
|
assert provider.kwargs == {"a3": "i3", "a4": "i4"}
|
|
|
|
|
|
def test_set_kwargs_non_string_keys():
|
|
a3 = object()
|
|
a4 = object()
|
|
provider = providers.Dict() \
|
|
.add_kwargs(a1="i1", a2="i2") \
|
|
.set_kwargs({a3: "i3", a4: "i4"})
|
|
assert provider.kwargs == {a3: "i3", a4: "i4"}
|
|
|
|
|
|
def test_set_kwargs_string_and_non_string_keys():
|
|
a3 = object()
|
|
provider = providers.Dict() \
|
|
.add_kwargs(a1="i1", a2="i2") \
|
|
.set_kwargs({a3: "i3"}, a4="i4")
|
|
assert provider.kwargs == {a3: "i3", "a4": "i4"}
|
|
|
|
|
|
def test_clear_kwargs():
|
|
provider = providers.Dict() \
|
|
.add_kwargs(a1="i1", a2="i2") \
|
|
.clear_kwargs()
|
|
assert provider.kwargs == {}
|
|
|
|
|
|
def test_call_overridden():
|
|
provider = providers.Dict(a1="i1", a2="i2")
|
|
overriding_provider1 = providers.Dict(a2="i2", a3="i3")
|
|
overriding_provider2 = providers.Dict(a3="i3", a4="i4")
|
|
|
|
provider.override(overriding_provider1)
|
|
provider.override(overriding_provider2)
|
|
|
|
instance1 = provider()
|
|
instance2 = provider()
|
|
|
|
assert instance1 is not instance2
|
|
assert instance1 == {"a3": "i3", "a4": "i4"}
|
|
assert instance2 == {"a3": "i3", "a4": "i4"}
|
|
|
|
|
|
def test_deepcopy():
|
|
provider = providers.Dict(a1="i1", a2="i2")
|
|
|
|
provider_copy = providers.deepcopy(provider)
|
|
|
|
assert provider is not provider_copy
|
|
assert provider.kwargs == provider_copy.kwargs
|
|
assert isinstance(provider, providers.Dict)
|
|
|
|
|
|
def test_deepcopy_from_memo():
|
|
provider = providers.Dict(a1="i1", a2="i2")
|
|
provider_copy_memo = providers.Dict(a1="i1", a2="i2")
|
|
|
|
provider_copy = providers.deepcopy(
|
|
provider,
|
|
memo={id(provider): provider_copy_memo},
|
|
)
|
|
|
|
assert provider_copy is provider_copy_memo
|
|
|
|
|
|
def test_deepcopy_kwargs():
|
|
provider = providers.Dict()
|
|
dependent_provider1 = providers.Factory(list)
|
|
dependent_provider2 = providers.Factory(dict)
|
|
|
|
provider.add_kwargs(d1=dependent_provider1, d2=dependent_provider2)
|
|
|
|
provider_copy = providers.deepcopy(provider)
|
|
dependent_provider_copy1 = provider_copy.kwargs["d1"]
|
|
dependent_provider_copy2 = provider_copy.kwargs["d2"]
|
|
|
|
assert provider.kwargs != provider_copy.kwargs
|
|
|
|
assert dependent_provider1.cls is dependent_provider_copy1.cls
|
|
assert dependent_provider1 is not dependent_provider_copy1
|
|
|
|
assert dependent_provider2.cls is dependent_provider_copy2.cls
|
|
assert dependent_provider2 is not dependent_provider_copy2
|
|
|
|
|
|
def test_deepcopy_kwargs_non_string_keys():
|
|
a1 = object()
|
|
a2 = object()
|
|
|
|
dependent_provider1 = providers.Factory(list)
|
|
dependent_provider2 = providers.Factory(dict)
|
|
|
|
provider = providers.Dict({a1: dependent_provider1, a2: dependent_provider2})
|
|
|
|
provider_copy = providers.deepcopy(provider)
|
|
dependent_provider_copy1 = provider_copy.kwargs[a1]
|
|
dependent_provider_copy2 = provider_copy.kwargs[a2]
|
|
|
|
assert provider.kwargs != provider_copy.kwargs
|
|
|
|
assert dependent_provider1.cls is dependent_provider_copy1.cls
|
|
assert dependent_provider1 is not dependent_provider_copy1
|
|
|
|
assert dependent_provider2.cls is dependent_provider_copy2.cls
|
|
assert dependent_provider2 is not dependent_provider_copy2
|
|
|
|
|
|
def test_deepcopy_overridden():
|
|
provider = providers.Dict()
|
|
object_provider = providers.Object(object())
|
|
|
|
provider.override(object_provider)
|
|
|
|
provider_copy = providers.deepcopy(provider)
|
|
object_provider_copy = provider_copy.overridden[0]
|
|
|
|
assert provider is not provider_copy
|
|
assert provider.kwargs == provider_copy.kwargs
|
|
assert isinstance(provider, providers.Dict)
|
|
|
|
assert object_provider is not object_provider_copy
|
|
assert isinstance(object_provider_copy, providers.Object)
|
|
|
|
|
|
def test_deepcopy_with_sys_streams():
|
|
provider = providers.Dict()
|
|
provider.add_kwargs(stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
|
|
|
|
provider_copy = providers.deepcopy(provider)
|
|
|
|
assert provider is not provider_copy
|
|
assert isinstance(provider_copy, providers.Dict)
|
|
assert provider.kwargs["stdin"] is sys.stdin
|
|
assert provider.kwargs["stdout"] is sys.stdout
|
|
assert provider.kwargs["stderr"] is sys.stderr
|
|
|
|
|
|
def test_repr():
|
|
provider = providers.Dict(a1=1, a2=2)
|
|
assert repr(provider) == (
|
|
"<dependency_injector.providers."
|
|
"Dict({0}) at {1}>".format(repr(provider.kwargs), hex(id(provider)))
|
|
)
|