mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-12-01 05:53:59 +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
203 lines
5.1 KiB
Python
203 lines
5.1 KiB
Python
"""ProvidedInstance provider tests."""
|
|
|
|
from dependency_injector import containers, providers
|
|
from pytest import fixture
|
|
|
|
|
|
class Service:
|
|
def __init__(self, value):
|
|
self.value = value
|
|
self.values = [self.value]
|
|
|
|
def __call__(self):
|
|
return self.value
|
|
|
|
def __getitem__(self, item):
|
|
return self.values[item]
|
|
|
|
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):
|
|
|
|
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(),
|
|
)
|
|
client_method_closure_call = providers.Factory(
|
|
Client,
|
|
value=service.provided.get_closure.call().call(),
|
|
)
|
|
|
|
client_provided_call = providers.Factory(
|
|
Client,
|
|
value=service.provided.call(),
|
|
)
|
|
|
|
|
|
@fixture
|
|
def container():
|
|
return Container()
|
|
|
|
|
|
def test_is_provider(container):
|
|
assert providers.is_provider(container.service.provided) is True
|
|
|
|
|
|
def test_attribute(container):
|
|
client = container.client_attribute()
|
|
assert client.value == "foo"
|
|
|
|
|
|
def test_item(container):
|
|
client = container.client_item()
|
|
assert client.value == "foo"
|
|
|
|
|
|
def test_attribute_item(container):
|
|
client = container.client_attribute_item()
|
|
assert client.value == "foo"
|
|
|
|
|
|
def test_method_call(container):
|
|
client = container.client_method_call()
|
|
assert client.value == "foo"
|
|
|
|
|
|
def test_method_closure_call(container):
|
|
client = container.client_method_closure_call()
|
|
assert client.value == "foo"
|
|
|
|
|
|
def test_provided_call(container):
|
|
client = container.client_provided_call()
|
|
assert client.value == "foo"
|
|
|
|
|
|
def test_call_overridden(container):
|
|
value = "bar"
|
|
with container.service.override(Service(value)):
|
|
assert container.client_attribute().value == value
|
|
assert container.client_item().value == value
|
|
assert container.client_attribute_item().value == value
|
|
assert container.client_method_call().value == value
|
|
|
|
|
|
def test_repr_provided_instance(container):
|
|
provider = container.service.provided
|
|
assert repr(provider) == "ProvidedInstance(\"{0}\")".format(repr(container.service))
|
|
|
|
|
|
def test_repr_attribute_getter(container):
|
|
provider = container.service.provided.value
|
|
assert repr(provider) == "AttributeGetter(\"value\")"
|
|
|
|
|
|
def test_repr_item_getter(container):
|
|
provider = container.service.provided["test-test"]
|
|
assert repr(provider) == "ItemGetter(\"test-test\")"
|
|
|
|
|
|
def test_provided_instance():
|
|
provides = providers.Object(object())
|
|
provider = providers.ProvidedInstance()
|
|
provider.set_provides(provides)
|
|
assert provider.provides is provides
|
|
assert provider.set_provides(providers.Provider()) is provider
|
|
|
|
|
|
def test_attribute_getter():
|
|
provides = providers.Object(object())
|
|
provider = providers.AttributeGetter()
|
|
provider.set_provides(provides)
|
|
provider.set_name("__dict__")
|
|
assert provider.provides is provides
|
|
assert provider.name == "__dict__"
|
|
assert provider.set_provides(providers.Provider()) is provider
|
|
assert provider.set_name("__dict__") is provider
|
|
|
|
|
|
def test_item_getter():
|
|
provides = providers.Object({"foo": "bar"})
|
|
provider = providers.ItemGetter()
|
|
provider.set_provides(provides)
|
|
provider.set_name("foo")
|
|
assert provider.provides is provides
|
|
assert provider.name == "foo"
|
|
assert provider.set_provides(providers.Provider()) is provider
|
|
assert provider.set_name("foo") is provider
|
|
|
|
|
|
def test_method_caller():
|
|
provides = providers.Object(lambda: 42)
|
|
provider = providers.MethodCaller()
|
|
provider.set_provides(provides)
|
|
assert provider.provides is provides
|
|
assert provider() == 42
|
|
assert provider.set_provides(providers.Provider()) is provider
|
|
|
|
|
|
def test_puzzled():
|
|
service = providers.Singleton(Service, value="foo-bar")
|
|
|
|
dependency = providers.Object(
|
|
{
|
|
"a": {
|
|
"b": {
|
|
"c1": 10,
|
|
"c2": lambda arg: {"arg": arg}
|
|
},
|
|
},
|
|
},
|
|
)
|
|
|
|
test_list = providers.List(
|
|
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()
|
|
assert result == [
|
|
10,
|
|
22,
|
|
service(),
|
|
"foo-bar",
|
|
"foo-bar",
|
|
]
|
|
|
|
|
|
def test_provided_attribute_in_base_class():
|
|
provider = providers.Provider()
|
|
assert isinstance(provider.provided, providers.ProvidedInstance)
|