python-dependency-injector/tests/unit/samples/wiringsamples/module.py
Roman Mogylatov f961ff536a
Schemas (#429)
* Add single container prototype

* Add multiple containers prototype

* Add integration tests

* Implement from_*() methods and add tests

* Prototype inline injections

* Add integration test for inline providers

* Refactor integration tests

* Add integration test for reordered schema

* Remove unused imports from tests

* Refactor schema module

* Update tests to match latest schemas

* Add mypy_boto3_s3 to the test requirements

* Add boto3 to the test requirements

* Add set_provides for Callable, Factory, and Singleton providers

* Fix warnings in tests

* Add typing stubs for Callable, Factory, and Singleton .set_provides() attributes

* Fix singleton children to have optional provides

* Implement provider to provider resolving

* Fix pypy3 tests

* Implement boto3 session use case and add tests

* Implement lazy initialization and improve copying for Callable, Factory, Singleton, and Coroutine providers

* Fix Python 2 tests

* Add region name for boto3 integration example

* Remove f-strings from set_provides()

* Fix schema flake8 errors

* Implement lazy initialization and improve copying for Delegate provider

* Implement lazy initialization and improve copying for Object provider

* Speed up wiring tests

* Implement lazy initialization and improve copying for FactoryAggregate provider

* Implement lazy initialization and improve copying for Selector provider

* Implement lazy initialization and improve copying for Dependency provider

* Implement lazy initialization and improve copying for Resource provider

* Implement lazy initialization and improve copying for Configuration provider

* Implement lazy initialization and improve copying for ProvidedInstance provider

* Implement lazy initialization and improve copying for AttributeGetter provider

* Implement lazy initialization and improve copying for ItemGetter provider

* Implement lazy initialization and improve copying for MethodCaller provder

* Update changelog

* Fix typing in wiring module

* Fix wiring module loader uninstallation issue

* Fix provided instance providers error handing in asynchronous mode

Co-authored-by: Roman Mogylatov <rmk@Romans-MacBook-Pro.local>
2021-03-20 13:16:51 -04:00

131 lines
3.6 KiB
Python

"""Test module for wiring."""
from decimal import Decimal
from typing import Callable
from dependency_injector import providers
from dependency_injector.wiring import inject, Provide, Provider
from .container import Container, SubContainer
from .service import Service
service: Service = Provide[Container.service]
service_provider: Callable[..., Service] = Provider[Container.service]
undefined: Callable = Provide[providers.Provider()]
class TestClass:
service: Service = Provide[Container.service]
service_provider: Callable[..., Service] = Provider[Container.service]
undefined: Callable = Provide[providers.Provider()]
@inject
def __init__(self, service: Service = Provide[Container.service]):
self.service = service
@inject
def method(self, service: Service = Provide[Container.service]):
return service
@classmethod
@inject
def class_method(cls, service: Service = Provide[Container.service]):
return service
@staticmethod
@inject
def static_method(service: Service = Provide[Container.service]):
return service
@inject
def test_function(service: Service = Provide[Container.service]):
return service
@inject
def test_function_provider(service_provider: Callable[..., Service] = Provider[Container.service]):
service = service_provider()
return service
@inject
def test_config_value(
value_int: int = Provide[Container.config.a.b.c.as_int()],
value_float: float = Provide[Container.config.a.b.c.as_float()],
value_str: str = Provide[Container.config.a.b.c.as_(str)],
value_decimal: Decimal = Provide[Container.config.a.b.c.as_(Decimal)],
value_required: str = Provide[Container.config.a.b.c.required()],
value_required_int: int = Provide[Container.config.a.b.c.required().as_int()],
value_required_float: float = Provide[Container.config.a.b.c.required().as_float()],
value_required_str: str = Provide[Container.config.a.b.c.required().as_(str)],
value_required_decimal: str = Provide[Container.config.a.b.c.required().as_(Decimal)],
):
return (
value_int,
value_float,
value_str,
value_decimal,
value_required,
value_required_int,
value_required_float,
value_required_str,
value_required_decimal,
)
@inject
def test_config_value_required_undefined(
value_required: int = Provide[Container.config.a.b.c.required()],
):
return value_required
@inject
def test_provide_provider(service_provider: Callable[..., Service] = Provider[Container.service.provider]):
service = service_provider()
return service
@inject
def test_provided_instance(some_value: int = Provide[Container.service.provided.foo['bar'].call()]):
return some_value
@inject
def test_subcontainer_provider(some_value: int = Provide[Container.sub.int_object]):
return some_value
@inject
def test_config_invariant(some_value: int = Provide[Container.config.option[Container.config.switch]]):
return some_value
@inject
def test_provide_from_different_containers(
service: Service = Provide[Container.service],
some_value: int = Provide[SubContainer.int_object],
):
return service, some_value
class ClassDecorator:
def __init__(self, fn):
self._fn = fn
def __call__(self, *args, **kwargs):
return self._fn(*args, **kwargs)
@ClassDecorator
@inject
def test_class_decorator(service: Service = Provide[Container.service]):
return service
def test_container(container: Container = Provide[Container]):
return container.service()