mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-27 12:04:00 +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
158 lines
4.1 KiB
Python
158 lines
4.1 KiB
Python
"""Provider tests."""
|
|
|
|
import warnings
|
|
|
|
from dependency_injector import providers, errors
|
|
from pytest import fixture, raises
|
|
|
|
|
|
@fixture
|
|
def provider():
|
|
return providers.Provider()
|
|
|
|
|
|
def test_is_provider(provider):
|
|
assert providers.is_provider(provider) is True
|
|
|
|
|
|
def test_call(provider):
|
|
with raises(NotImplementedError):
|
|
provider()
|
|
|
|
|
|
def test_delegate(provider):
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
delegate1 = provider.delegate()
|
|
delegate2 = provider.delegate()
|
|
|
|
assert isinstance(delegate1, providers.Delegate)
|
|
assert delegate1() is provider
|
|
|
|
assert isinstance(delegate2, providers.Delegate)
|
|
assert delegate2() is provider
|
|
|
|
assert delegate1 is not delegate2
|
|
|
|
|
|
def test_provider(provider):
|
|
delegate1 = provider.provider
|
|
|
|
assert isinstance(delegate1, providers.Delegate)
|
|
assert delegate1() is provider
|
|
|
|
delegate2 = provider.provider
|
|
|
|
assert isinstance(delegate2, providers.Delegate)
|
|
assert delegate2() is provider
|
|
|
|
assert delegate1 is not delegate2
|
|
|
|
|
|
def test_override(provider):
|
|
overriding_provider = providers.Provider()
|
|
provider.override(overriding_provider)
|
|
assert provider.overridden == (overriding_provider,)
|
|
assert provider.last_overriding is overriding_provider
|
|
|
|
|
|
def test_double_override(provider):
|
|
overriding_provider1 = providers.Object(1)
|
|
overriding_provider2 = providers.Object(2)
|
|
|
|
provider.override(overriding_provider1)
|
|
overriding_provider1.override(overriding_provider2)
|
|
|
|
assert provider() == overriding_provider2()
|
|
|
|
|
|
def test_overriding_context(provider):
|
|
overriding_provider = providers.Provider()
|
|
with provider.override(overriding_provider):
|
|
assert provider.overridden == (overriding_provider,)
|
|
assert provider.overridden == tuple()
|
|
assert not provider.overridden
|
|
|
|
|
|
def test_override_with_itself(provider):
|
|
with raises(errors.Error):
|
|
provider.override(provider)
|
|
|
|
|
|
def test_override_with_not_provider(provider):
|
|
obj = object()
|
|
provider.override(obj)
|
|
assert provider() is obj
|
|
|
|
|
|
def test_reset_last_overriding(provider):
|
|
overriding_provider1 = providers.Provider()
|
|
overriding_provider2 = providers.Provider()
|
|
|
|
provider.override(overriding_provider1)
|
|
provider.override(overriding_provider2)
|
|
|
|
assert provider.overridden[-1] is overriding_provider2
|
|
assert provider.last_overriding is overriding_provider2
|
|
|
|
provider.reset_last_overriding()
|
|
assert provider.overridden[-1] is overriding_provider1
|
|
assert provider.last_overriding is overriding_provider1
|
|
|
|
provider.reset_last_overriding()
|
|
assert provider.overridden == tuple()
|
|
assert not provider.overridden
|
|
assert provider.last_overriding is None
|
|
|
|
|
|
def test_reset_last_overriding_of_not_overridden_provider(provider):
|
|
with raises(errors.Error):
|
|
provider.reset_last_overriding()
|
|
|
|
|
|
def test_reset_override(provider):
|
|
overriding_provider = providers.Provider()
|
|
provider.override(overriding_provider)
|
|
|
|
assert provider.overridden
|
|
assert provider.overridden == (overriding_provider,)
|
|
|
|
provider.reset_override()
|
|
|
|
assert provider.overridden == tuple()
|
|
|
|
|
|
def test_deepcopy(provider):
|
|
provider_copy = providers.deepcopy(provider)
|
|
|
|
assert provider is not provider_copy
|
|
assert isinstance(provider, providers.Provider)
|
|
|
|
|
|
def test_deepcopy_from_memo(provider):
|
|
provider_copy_memo = providers.Provider()
|
|
provider_copy = providers.deepcopy(provider, memo={id(provider): provider_copy_memo})
|
|
assert provider_copy is provider_copy_memo
|
|
|
|
|
|
def test_deepcopy_overridden(provider):
|
|
overriding_provider = providers.Provider()
|
|
|
|
provider.override(overriding_provider)
|
|
|
|
provider_copy = providers.deepcopy(provider)
|
|
overriding_provider_copy = provider_copy.overridden[0]
|
|
|
|
assert provider is not provider_copy
|
|
assert isinstance(provider, providers.Provider)
|
|
|
|
assert overriding_provider is not overriding_provider_copy
|
|
assert isinstance(overriding_provider_copy, providers.Provider)
|
|
|
|
|
|
def test_repr(provider):
|
|
assert repr(provider) == (
|
|
"<dependency_injector.providers."
|
|
"Provider() at {0}>".format(hex(id(provider)))
|
|
)
|