mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-04-14 22:24:25 +03:00
Migrate main wiring tests
This commit is contained in:
parent
5e59e8d729
commit
3f682a9ede
|
@ -98,6 +98,12 @@ def test_provide_provider(service_provider: Callable[..., Service] = Provide["se
|
|||
return service
|
||||
|
||||
|
||||
@inject
|
||||
def test_provider_provider(service_provider: Callable[..., Service] = Provider["service.provider"]):
|
||||
service = service_provider()
|
||||
return service
|
||||
|
||||
|
||||
@inject
|
||||
def test_provided_instance(some_value: int = Provide["service", provided().foo["bar"].call()]):
|
||||
return some_value
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
"""Test module for wiring with invalid type of marker for attribute injection."""
|
||||
|
||||
from dependency_injector.wiring import Closing
|
||||
|
||||
|
||||
service = Closing["service"]
|
|
@ -3,8 +3,8 @@
|
|||
import contextlib
|
||||
import importlib
|
||||
|
||||
from pytest import fixture
|
||||
from dependency_injector.wiring import register_loader_containers, unregister_loader_containers
|
||||
from pytest import fixture
|
||||
|
||||
from wiringsamples import module
|
||||
from wiringsamples.service import Service
|
||||
|
|
315
tests/unit/wiring/provider_ids/test_main_py36.py
Normal file
315
tests/unit/wiring/provider_ids/test_main_py36.py
Normal file
|
@ -0,0 +1,315 @@
|
|||
"""Main wiring tests."""
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from dependency_injector import errors
|
||||
from dependency_injector.wiring import Provide, Provider, wire
|
||||
from pytest import fixture, mark, raises
|
||||
|
||||
from wiringsamples import module, package, resourceclosing
|
||||
from wiringsamples.service import Service
|
||||
from wiringsamples.container import Container, SubContainer
|
||||
|
||||
|
||||
@fixture(autouse=True)
|
||||
def container():
|
||||
container = Container(config={"a": {"b": {"c": 10}}})
|
||||
container.wire(
|
||||
modules=[module],
|
||||
packages=[package],
|
||||
)
|
||||
yield container
|
||||
container.unwire()
|
||||
|
||||
|
||||
@fixture
|
||||
def subcontainer():
|
||||
container = SubContainer()
|
||||
container.wire(
|
||||
modules=[module],
|
||||
packages=[package],
|
||||
)
|
||||
yield container
|
||||
container.unwire()
|
||||
|
||||
|
||||
@fixture
|
||||
def resourceclosing_container():
|
||||
container = resourceclosing.Container()
|
||||
container.wire(modules=[resourceclosing])
|
||||
yield container
|
||||
container.unwire()
|
||||
|
||||
|
||||
def test_package_lookup():
|
||||
from wiringsamples.package import test_package_function
|
||||
service = test_package_function()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_package_subpackage_lookup():
|
||||
from wiringsamples.package.subpackage import test_package_function
|
||||
service = test_package_function()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_package_submodule_lookup():
|
||||
from wiringsamples.package.subpackage.submodule import test_function
|
||||
service = test_function()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_module_attributes_wiring():
|
||||
assert isinstance(module.service, Service)
|
||||
assert isinstance(module.service_provider(), Service)
|
||||
assert isinstance(module.undefined, Provide)
|
||||
|
||||
|
||||
def test_module_attribute_wiring_with_invalid_marker(container: Container):
|
||||
from wiringsamples import module_invalid_attr_injection
|
||||
with raises(Exception, match="Unknown type of marker {0}".format(module_invalid_attr_injection.service)):
|
||||
container.wire(modules=[module_invalid_attr_injection])
|
||||
|
||||
|
||||
def test_class_wiring():
|
||||
test_class_object = module.TestClass()
|
||||
assert isinstance(test_class_object.service, Service)
|
||||
|
||||
|
||||
def test_class_wiring_context_arg(container: Container):
|
||||
test_service = container.service()
|
||||
test_class_object = module.TestClass(service=test_service)
|
||||
assert test_class_object.service is test_service
|
||||
|
||||
|
||||
def test_class_method_wiring():
|
||||
test_class_object = module.TestClass()
|
||||
service = test_class_object.method()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_class_classmethod_wiring():
|
||||
service = module.TestClass.class_method()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_instance_classmethod_wiring():
|
||||
instance = module.TestClass()
|
||||
service = instance.class_method()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_class_staticmethod_wiring():
|
||||
service = module.TestClass.static_method()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_instance_staticmethod_wiring():
|
||||
instance = module.TestClass()
|
||||
service = instance.static_method()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_class_attribute_wiring():
|
||||
assert isinstance(module.TestClass.service, Service)
|
||||
assert isinstance(module.TestClass.service_provider(), Service)
|
||||
assert isinstance(module.TestClass.undefined, Provide)
|
||||
|
||||
|
||||
def test_function_wiring():
|
||||
service = module.test_function()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_function_wiring_context_arg(container: Container):
|
||||
test_service = container.service()
|
||||
service = module.test_function(service=test_service)
|
||||
assert service is test_service
|
||||
|
||||
|
||||
def test_function_wiring_provider():
|
||||
service = module.test_function_provider()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_function_wiring_provider_context_arg(container: Container):
|
||||
test_service = container.service()
|
||||
service = module.test_function_provider(service_provider=lambda: test_service)
|
||||
assert service is test_service
|
||||
|
||||
|
||||
def test_configuration_option():
|
||||
(
|
||||
value_int,
|
||||
value_float,
|
||||
value_str,
|
||||
value_decimal,
|
||||
value_required,
|
||||
value_required_int,
|
||||
value_required_float,
|
||||
value_required_str,
|
||||
value_required_decimal,
|
||||
) = module.test_config_value()
|
||||
|
||||
assert value_int == 10
|
||||
assert value_float == 10.0
|
||||
assert value_str == "10"
|
||||
assert value_decimal == Decimal(10)
|
||||
assert value_required == 10
|
||||
assert value_required_int == 10
|
||||
assert value_required_float == 10.0
|
||||
assert value_required_str == "10"
|
||||
assert value_required_decimal == Decimal(10)
|
||||
|
||||
|
||||
def test_configuration_option_required_undefined(container: Container):
|
||||
container.config.reset_override()
|
||||
with raises(errors.Error, match="Undefined configuration option \"config.a.b.c\""):
|
||||
module.test_config_value_required_undefined()
|
||||
|
||||
|
||||
def test_provide_provider():
|
||||
service = module.test_provide_provider()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_provider_provider():
|
||||
service = module.test_provider_provider()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_provided_instance(container: Container):
|
||||
class TestService:
|
||||
foo = {"bar": lambda: 10}
|
||||
|
||||
with container.service.override(TestService()):
|
||||
some_value = module.test_provided_instance()
|
||||
assert some_value == 10
|
||||
|
||||
|
||||
def test_subcontainer():
|
||||
some_value = module.test_subcontainer_provider()
|
||||
assert some_value == 1
|
||||
|
||||
|
||||
def test_config_invariant(container: Container):
|
||||
config = {
|
||||
"option": {
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
},
|
||||
"switch": "a",
|
||||
}
|
||||
container.config.from_dict(config)
|
||||
|
||||
value_default = module.test_config_invariant()
|
||||
assert value_default == 1
|
||||
|
||||
with container.config.switch.override("a"):
|
||||
value_a = module.test_config_invariant()
|
||||
assert value_a == 1
|
||||
|
||||
with container.config.switch.override("b"):
|
||||
value_b = module.test_config_invariant()
|
||||
assert value_b == 2
|
||||
|
||||
|
||||
def test_wire_with_class_error():
|
||||
with raises(Exception):
|
||||
wire(
|
||||
container=Container,
|
||||
modules=[module],
|
||||
)
|
||||
|
||||
|
||||
def test_unwire_function(container: Container):
|
||||
container.unwire()
|
||||
assert isinstance(module.test_function(), Provide)
|
||||
|
||||
|
||||
def test_unwire_class(container: Container):
|
||||
container.unwire()
|
||||
test_class_object = module.TestClass()
|
||||
assert isinstance(test_class_object.service, Provide)
|
||||
|
||||
|
||||
def test_unwire_class_method(container: Container):
|
||||
container.unwire()
|
||||
test_class_object = module.TestClass()
|
||||
assert isinstance(test_class_object.method(), Provide)
|
||||
|
||||
|
||||
def test_unwire_package_function(container: Container):
|
||||
container.unwire()
|
||||
from wiringsamples.package.subpackage.submodule import test_function
|
||||
assert isinstance(test_function(), Provide)
|
||||
|
||||
|
||||
def test_unwire_package_function_by_reference(container: Container):
|
||||
from wiringsamples.package.subpackage import submodule
|
||||
container.unwire()
|
||||
assert isinstance(submodule.test_function(), Provide)
|
||||
|
||||
|
||||
def test_unwire_module_attributes(container: Container):
|
||||
container.unwire()
|
||||
assert isinstance(module.service, Provide)
|
||||
assert isinstance(module.service_provider, Provider)
|
||||
assert isinstance(module.undefined, Provide)
|
||||
|
||||
|
||||
def test_unwire_class_attributes(container: Container):
|
||||
container.unwire()
|
||||
assert isinstance(module.TestClass.service, Provide)
|
||||
assert isinstance(module.TestClass.service_provider, Provider)
|
||||
assert isinstance(module.TestClass.undefined, Provide)
|
||||
|
||||
|
||||
@mark.usefixtures("subcontainer")
|
||||
def test_wire_multiple_containers():
|
||||
service, some_value = module.test_provide_from_different_containers()
|
||||
assert isinstance(service, Service)
|
||||
assert some_value == 1
|
||||
|
||||
|
||||
@mark.usefixtures("resourceclosing_container")
|
||||
def test_closing_resource():
|
||||
resourceclosing.Service.reset_counter()
|
||||
|
||||
result_1 = resourceclosing.test_function()
|
||||
assert isinstance(result_1, resourceclosing.Service)
|
||||
assert result_1.init_counter == 1
|
||||
assert result_1.shutdown_counter == 1
|
||||
|
||||
result_2 = resourceclosing.test_function()
|
||||
assert isinstance(result_2, resourceclosing.Service)
|
||||
assert result_2.init_counter == 2
|
||||
assert result_2.shutdown_counter == 2
|
||||
|
||||
assert result_1 is not result_2
|
||||
|
||||
|
||||
@mark.usefixtures("resourceclosing_container")
|
||||
def test_closing_resource_context():
|
||||
resourceclosing.Service.reset_counter()
|
||||
service = resourceclosing.Service()
|
||||
|
||||
result_1 = resourceclosing.test_function(service=service)
|
||||
assert result_1 is service
|
||||
assert result_1.init_counter == 0
|
||||
assert result_1.shutdown_counter == 0
|
||||
|
||||
result_2 = resourceclosing.test_function(service=service)
|
||||
assert result_2 is service
|
||||
assert result_2.init_counter == 0
|
||||
assert result_2.shutdown_counter == 0
|
||||
|
||||
|
||||
def test_class_decorator():
|
||||
service = module.test_class_decorator()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_container():
|
||||
service = module.test_container()
|
||||
assert isinstance(service, Service)
|
|
@ -3,8 +3,8 @@
|
|||
import contextlib
|
||||
import importlib
|
||||
|
||||
from pytest import fixture
|
||||
from dependency_injector.wiring import register_loader_containers, unregister_loader_containers
|
||||
from pytest import fixture
|
||||
|
||||
from wiringstringidssamples import module
|
||||
from wiringstringidssamples.service import Service
|
||||
|
|
315
tests/unit/wiring/string_ids/test_main_py36.py
Normal file
315
tests/unit/wiring/string_ids/test_main_py36.py
Normal file
|
@ -0,0 +1,315 @@
|
|||
"""Main wiring tests."""
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from dependency_injector import errors
|
||||
from dependency_injector.wiring import Provide, Provider, wire
|
||||
from pytest import fixture, mark, raises
|
||||
|
||||
from wiringstringidssamples import module, package, resourceclosing
|
||||
from wiringstringidssamples.service import Service
|
||||
from wiringstringidssamples.container import Container, SubContainer
|
||||
|
||||
|
||||
@fixture(autouse=True)
|
||||
def container():
|
||||
container = Container(config={"a": {"b": {"c": 10}}})
|
||||
container.wire(
|
||||
modules=[module],
|
||||
packages=[package],
|
||||
)
|
||||
yield container
|
||||
container.unwire()
|
||||
|
||||
|
||||
@fixture
|
||||
def subcontainer():
|
||||
container = SubContainer()
|
||||
container.wire(
|
||||
modules=[module],
|
||||
packages=[package],
|
||||
)
|
||||
yield container
|
||||
container.unwire()
|
||||
|
||||
|
||||
@fixture
|
||||
def resourceclosing_container():
|
||||
container = resourceclosing.Container()
|
||||
container.wire(modules=[resourceclosing])
|
||||
yield container
|
||||
container.unwire()
|
||||
|
||||
|
||||
def test_package_lookup():
|
||||
from wiringstringidssamples.package import test_package_function
|
||||
service = test_package_function()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_package_subpackage_lookup():
|
||||
from wiringstringidssamples.package.subpackage import test_package_function
|
||||
service = test_package_function()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_package_submodule_lookup():
|
||||
from wiringstringidssamples.package.subpackage.submodule import test_function
|
||||
service = test_function()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_module_attributes_wiring():
|
||||
assert isinstance(module.service, Service)
|
||||
assert isinstance(module.service_provider(), Service)
|
||||
assert isinstance(module.undefined, Provide)
|
||||
|
||||
|
||||
def test_module_attribute_wiring_with_invalid_marker(container: Container):
|
||||
from wiringstringidssamples import module_invalid_attr_injection
|
||||
with raises(Exception, match="Unknown type of marker {0}".format(module_invalid_attr_injection.service)):
|
||||
container.wire(modules=[module_invalid_attr_injection])
|
||||
|
||||
|
||||
def test_class_wiring():
|
||||
test_class_object = module.TestClass()
|
||||
assert isinstance(test_class_object.service, Service)
|
||||
|
||||
|
||||
def test_class_wiring_context_arg(container: Container):
|
||||
test_service = container.service()
|
||||
test_class_object = module.TestClass(service=test_service)
|
||||
assert test_class_object.service is test_service
|
||||
|
||||
|
||||
def test_class_method_wiring():
|
||||
test_class_object = module.TestClass()
|
||||
service = test_class_object.method()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_class_classmethod_wiring():
|
||||
service = module.TestClass.class_method()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_instance_classmethod_wiring():
|
||||
instance = module.TestClass()
|
||||
service = instance.class_method()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_class_staticmethod_wiring():
|
||||
service = module.TestClass.static_method()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_instance_staticmethod_wiring():
|
||||
instance = module.TestClass()
|
||||
service = instance.static_method()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_class_attribute_wiring():
|
||||
assert isinstance(module.TestClass.service, Service)
|
||||
assert isinstance(module.TestClass.service_provider(), Service)
|
||||
assert isinstance(module.TestClass.undefined, Provide)
|
||||
|
||||
|
||||
def test_function_wiring():
|
||||
service = module.test_function()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_function_wiring_context_arg(container: Container):
|
||||
test_service = container.service()
|
||||
service = module.test_function(service=test_service)
|
||||
assert service is test_service
|
||||
|
||||
|
||||
def test_function_wiring_provider():
|
||||
service = module.test_function_provider()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_function_wiring_provider_context_arg(container: Container):
|
||||
test_service = container.service()
|
||||
service = module.test_function_provider(service_provider=lambda: test_service)
|
||||
assert service is test_service
|
||||
|
||||
|
||||
def test_configuration_option():
|
||||
(
|
||||
value_int,
|
||||
value_float,
|
||||
value_str,
|
||||
value_decimal,
|
||||
value_required,
|
||||
value_required_int,
|
||||
value_required_float,
|
||||
value_required_str,
|
||||
value_required_decimal,
|
||||
) = module.test_config_value()
|
||||
|
||||
assert value_int == 10
|
||||
assert value_float == 10.0
|
||||
assert value_str == "10"
|
||||
assert value_decimal == Decimal(10)
|
||||
assert value_required == 10
|
||||
assert value_required_int == 10
|
||||
assert value_required_float == 10.0
|
||||
assert value_required_str == "10"
|
||||
assert value_required_decimal == Decimal(10)
|
||||
|
||||
|
||||
def test_configuration_option_required_undefined(container: Container):
|
||||
container.config.reset_override()
|
||||
with raises(errors.Error, match="Undefined configuration option \"config.a.b.c\""):
|
||||
module.test_config_value_required_undefined()
|
||||
|
||||
|
||||
def test_provide_provider():
|
||||
service = module.test_provide_provider()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_provider_provider():
|
||||
service = module.test_provider_provider()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_provided_instance(container: Container):
|
||||
class TestService:
|
||||
foo = {"bar": lambda: 10}
|
||||
|
||||
with container.service.override(TestService()):
|
||||
some_value = module.test_provided_instance()
|
||||
assert some_value == 10
|
||||
|
||||
|
||||
def test_subcontainer():
|
||||
some_value = module.test_subcontainer_provider()
|
||||
assert some_value == 1
|
||||
|
||||
|
||||
def test_config_invariant(container: Container):
|
||||
config = {
|
||||
"option": {
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
},
|
||||
"switch": "a",
|
||||
}
|
||||
container.config.from_dict(config)
|
||||
|
||||
value_default = module.test_config_invariant()
|
||||
assert value_default == 1
|
||||
|
||||
with container.config.switch.override("a"):
|
||||
value_a = module.test_config_invariant()
|
||||
assert value_a == 1
|
||||
|
||||
with container.config.switch.override("b"):
|
||||
value_b = module.test_config_invariant()
|
||||
assert value_b == 2
|
||||
|
||||
|
||||
def test_wire_with_class_error():
|
||||
with raises(Exception):
|
||||
wire(
|
||||
container=Container,
|
||||
modules=[module],
|
||||
)
|
||||
|
||||
|
||||
def test_unwire_function(container: Container):
|
||||
container.unwire()
|
||||
assert isinstance(module.test_function(), Provide)
|
||||
|
||||
|
||||
def test_unwire_class(container: Container):
|
||||
container.unwire()
|
||||
test_class_object = module.TestClass()
|
||||
assert isinstance(test_class_object.service, Provide)
|
||||
|
||||
|
||||
def test_unwire_class_method(container: Container):
|
||||
container.unwire()
|
||||
test_class_object = module.TestClass()
|
||||
assert isinstance(test_class_object.method(), Provide)
|
||||
|
||||
|
||||
def test_unwire_package_function(container: Container):
|
||||
container.unwire()
|
||||
from wiringstringidssamples.package.subpackage.submodule import test_function
|
||||
assert isinstance(test_function(), Provide)
|
||||
|
||||
|
||||
def test_unwire_package_function_by_reference(container: Container):
|
||||
from wiringstringidssamples.package.subpackage import submodule
|
||||
container.unwire()
|
||||
assert isinstance(submodule.test_function(), Provide)
|
||||
|
||||
|
||||
def test_unwire_module_attributes(container: Container):
|
||||
container.unwire()
|
||||
assert isinstance(module.service, Provide)
|
||||
assert isinstance(module.service_provider, Provider)
|
||||
assert isinstance(module.undefined, Provide)
|
||||
|
||||
|
||||
def test_unwire_class_attributes(container: Container):
|
||||
container.unwire()
|
||||
assert isinstance(module.TestClass.service, Provide)
|
||||
assert isinstance(module.TestClass.service_provider, Provider)
|
||||
assert isinstance(module.TestClass.undefined, Provide)
|
||||
|
||||
|
||||
@mark.usefixtures("subcontainer")
|
||||
def test_wire_multiple_containers():
|
||||
service, some_value = module.test_provide_from_different_containers()
|
||||
assert isinstance(service, Service)
|
||||
assert some_value == 1
|
||||
|
||||
|
||||
@mark.usefixtures("resourceclosing_container")
|
||||
def test_closing_resource():
|
||||
resourceclosing.Service.reset_counter()
|
||||
|
||||
result_1 = resourceclosing.test_function()
|
||||
assert isinstance(result_1, resourceclosing.Service)
|
||||
assert result_1.init_counter == 1
|
||||
assert result_1.shutdown_counter == 1
|
||||
|
||||
result_2 = resourceclosing.test_function()
|
||||
assert isinstance(result_2, resourceclosing.Service)
|
||||
assert result_2.init_counter == 2
|
||||
assert result_2.shutdown_counter == 2
|
||||
|
||||
assert result_1 is not result_2
|
||||
|
||||
|
||||
@mark.usefixtures("resourceclosing_container")
|
||||
def test_closing_resource_context():
|
||||
resourceclosing.Service.reset_counter()
|
||||
service = resourceclosing.Service()
|
||||
|
||||
result_1 = resourceclosing.test_function(service=service)
|
||||
assert result_1 is service
|
||||
assert result_1.init_counter == 0
|
||||
assert result_1.shutdown_counter == 0
|
||||
|
||||
result_2 = resourceclosing.test_function(service=service)
|
||||
assert result_2 is service
|
||||
assert result_2.init_counter == 0
|
||||
assert result_2.shutdown_counter == 0
|
||||
|
||||
|
||||
def test_class_decorator():
|
||||
service = module.test_class_decorator()
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_container():
|
||||
service = module.test_container()
|
||||
assert isinstance(service, Service)
|
|
@ -1,10 +1,7 @@
|
|||
from decimal import Decimal
|
||||
import unittest
|
||||
|
||||
from dependency_injector.wiring import (
|
||||
wire,
|
||||
Provide,
|
||||
Provider,
|
||||
Closing,
|
||||
)
|
||||
from dependency_injector import containers, errors
|
||||
|
@ -27,288 +24,12 @@ import sys
|
|||
sys.path.append(_TOP_DIR)
|
||||
sys.path.append(_SAMPLES_DIR)
|
||||
|
||||
from wiringsamples import module, package
|
||||
from wiringsamples import module
|
||||
from wiringsamples.service import Service
|
||||
from wiringsamples.container import Container, SubContainer
|
||||
from wiringsamples.container import Container
|
||||
from wiringsamples.wire_relative_string_names import wire_with_relative_string_names
|
||||
|
||||
|
||||
class WiringTest(unittest.TestCase):
|
||||
|
||||
container: Container
|
||||
|
||||
def setUp(self) -> None:
|
||||
self.container = Container(config={"a": {"b": {"c": 10}}})
|
||||
self.container.wire(
|
||||
modules=[module],
|
||||
packages=[package],
|
||||
)
|
||||
self.addCleanup(self.container.unwire)
|
||||
|
||||
def test_package_lookup(self):
|
||||
from wiringsamples.package import test_package_function
|
||||
service = test_package_function()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_package_subpackage_lookup(self):
|
||||
from wiringsamples.package.subpackage import test_package_function
|
||||
service = test_package_function()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_package_submodule_lookup(self):
|
||||
from wiringsamples.package.subpackage.submodule import test_function
|
||||
service = test_function()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_module_attributes_wiring(self):
|
||||
self.assertIsInstance(module.service, Service)
|
||||
self.assertIsInstance(module.service_provider(), Service)
|
||||
self.assertIsInstance(module.undefined, Provide)
|
||||
|
||||
def test_module_attribute_wiring_with_invalid_marker(self):
|
||||
from wiringsamples import module_invalid_attr_injection
|
||||
with self.assertRaises(Exception) as context:
|
||||
self.container.wire(modules=[module_invalid_attr_injection])
|
||||
self.assertEqual(
|
||||
str(context.exception),
|
||||
"Unknown type of marker {0}".format(module_invalid_attr_injection.service),
|
||||
)
|
||||
|
||||
def test_class_wiring(self):
|
||||
test_class_object = module.TestClass()
|
||||
self.assertIsInstance(test_class_object.service, Service)
|
||||
|
||||
def test_class_wiring_context_arg(self):
|
||||
test_service = self.container.service()
|
||||
|
||||
test_class_object = module.TestClass(service=test_service)
|
||||
self.assertIs(test_class_object.service, test_service)
|
||||
|
||||
def test_class_method_wiring(self):
|
||||
test_class_object = module.TestClass()
|
||||
service = test_class_object.method()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_class_classmethod_wiring(self):
|
||||
service = module.TestClass.class_method()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_instance_classmethod_wiring(self):
|
||||
instance = module.TestClass()
|
||||
service = instance.class_method()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_class_staticmethod_wiring(self):
|
||||
service = module.TestClass.static_method()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_instance_staticmethod_wiring(self):
|
||||
instance = module.TestClass()
|
||||
service = instance.static_method()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_class_attribute_wiring(self):
|
||||
self.assertIsInstance(module.TestClass.service, Service)
|
||||
self.assertIsInstance(module.TestClass.service_provider(), Service)
|
||||
self.assertIsInstance(module.TestClass.undefined, Provide)
|
||||
|
||||
def test_function_wiring(self):
|
||||
service = module.test_function()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_function_wiring_context_arg(self):
|
||||
test_service = self.container.service()
|
||||
|
||||
service = module.test_function(service=test_service)
|
||||
self.assertIs(service, test_service)
|
||||
|
||||
def test_function_wiring_provider(self):
|
||||
service = module.test_function_provider()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_function_wiring_provider_context_arg(self):
|
||||
test_service = self.container.service()
|
||||
|
||||
service = module.test_function_provider(service_provider=lambda: test_service)
|
||||
self.assertIs(service, test_service)
|
||||
|
||||
def test_configuration_option(self):
|
||||
(
|
||||
value_int,
|
||||
value_float,
|
||||
value_str,
|
||||
value_decimal,
|
||||
value_required,
|
||||
value_required_int,
|
||||
value_required_float,
|
||||
value_required_str,
|
||||
value_required_decimal,
|
||||
) = module.test_config_value()
|
||||
|
||||
self.assertEqual(value_int, 10)
|
||||
self.assertEqual(value_float, 10.0)
|
||||
self.assertEqual(value_str, "10")
|
||||
self.assertEqual(value_decimal, Decimal(10))
|
||||
self.assertEqual(value_required, 10)
|
||||
self.assertEqual(value_required_int, 10)
|
||||
self.assertEqual(value_required_float, 10.0)
|
||||
self.assertEqual(value_required_str, "10")
|
||||
self.assertEqual(value_required_decimal, Decimal(10))
|
||||
|
||||
def test_configuration_option_required_undefined(self):
|
||||
self.container.config.reset_override()
|
||||
with self.assertRaisesRegex(errors.Error, "Undefined configuration option \"config.a.b.c\""):
|
||||
module.test_config_value_required_undefined()
|
||||
|
||||
def test_provide_provider(self):
|
||||
service = module.test_provide_provider()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_provider_provider(self):
|
||||
service = module.test_provider_provider()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_provided_instance(self):
|
||||
class TestService:
|
||||
foo = {
|
||||
"bar": lambda: 10,
|
||||
}
|
||||
|
||||
with self.container.service.override(TestService()):
|
||||
some_value = module.test_provided_instance()
|
||||
self.assertEqual(some_value, 10)
|
||||
|
||||
def test_subcontainer(self):
|
||||
some_value = module.test_subcontainer_provider()
|
||||
self.assertEqual(some_value, 1)
|
||||
|
||||
def test_config_invariant(self):
|
||||
config = {
|
||||
"option": {
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
},
|
||||
"switch": "a",
|
||||
}
|
||||
self.container.config.from_dict(config)
|
||||
|
||||
value_default = module.test_config_invariant()
|
||||
self.assertEqual(value_default, 1)
|
||||
|
||||
with self.container.config.switch.override("a"):
|
||||
value_a = module.test_config_invariant()
|
||||
self.assertEqual(value_a, 1)
|
||||
|
||||
with self.container.config.switch.override("b"):
|
||||
value_b = module.test_config_invariant()
|
||||
self.assertEqual(value_b, 2)
|
||||
|
||||
def test_wire_with_class_error(self):
|
||||
with self.assertRaises(Exception):
|
||||
wire(
|
||||
container=Container,
|
||||
modules=[module],
|
||||
)
|
||||
|
||||
def test_unwire_function(self):
|
||||
self.container.unwire()
|
||||
self.assertIsInstance(module.test_function(), Provide)
|
||||
|
||||
def test_unwire_class(self):
|
||||
self.container.unwire()
|
||||
test_class_object = module.TestClass()
|
||||
self.assertIsInstance(test_class_object.service, Provide)
|
||||
|
||||
def test_unwire_class_method(self):
|
||||
self.container.unwire()
|
||||
test_class_object = module.TestClass()
|
||||
self.assertIsInstance(test_class_object.method(), Provide)
|
||||
|
||||
def test_unwire_package_function(self):
|
||||
self.container.unwire()
|
||||
from wiringsamples.package.subpackage.submodule import test_function
|
||||
self.assertIsInstance(test_function(), Provide)
|
||||
|
||||
def test_unwire_package_function_by_reference(self):
|
||||
from wiringsamples.package.subpackage import submodule
|
||||
self.container.unwire()
|
||||
self.assertIsInstance(submodule.test_function(), Provide)
|
||||
|
||||
def test_unwire_module_attributes(self):
|
||||
self.container.unwire()
|
||||
self.assertIsInstance(module.service, Provide)
|
||||
self.assertIsInstance(module.service_provider, Provider)
|
||||
self.assertIsInstance(module.undefined, Provide)
|
||||
|
||||
def test_unwire_class_attributes(self):
|
||||
self.container.unwire()
|
||||
self.assertIsInstance(module.TestClass.service, Provide)
|
||||
self.assertIsInstance(module.TestClass.service_provider, Provider)
|
||||
self.assertIsInstance(module.TestClass.undefined, Provide)
|
||||
|
||||
def test_wire_multiple_containers(self):
|
||||
sub_container = SubContainer()
|
||||
sub_container.wire(
|
||||
modules=[module],
|
||||
packages=[package],
|
||||
)
|
||||
self.addCleanup(sub_container.unwire)
|
||||
|
||||
service, some_value = module.test_provide_from_different_containers()
|
||||
|
||||
self.assertIsInstance(service, Service)
|
||||
self.assertEqual(some_value, 1)
|
||||
|
||||
def test_closing_resource(self):
|
||||
from wiringsamples import resourceclosing
|
||||
|
||||
resourceclosing.Service.reset_counter()
|
||||
|
||||
container = resourceclosing.Container()
|
||||
container.wire(modules=[resourceclosing])
|
||||
self.addCleanup(container.unwire)
|
||||
|
||||
result_1 = resourceclosing.test_function()
|
||||
self.assertIsInstance(result_1, resourceclosing.Service)
|
||||
self.assertEqual(result_1.init_counter, 1)
|
||||
self.assertEqual(result_1.shutdown_counter, 1)
|
||||
|
||||
result_2 = resourceclosing.test_function()
|
||||
self.assertIsInstance(result_2, resourceclosing.Service)
|
||||
self.assertEqual(result_2.init_counter, 2)
|
||||
self.assertEqual(result_2.shutdown_counter, 2)
|
||||
|
||||
self.assertIsNot(result_1, result_2)
|
||||
|
||||
def test_closing_resource_context(self):
|
||||
from wiringsamples import resourceclosing
|
||||
|
||||
resourceclosing.Service.reset_counter()
|
||||
service = resourceclosing.Service()
|
||||
|
||||
container = resourceclosing.Container()
|
||||
container.wire(modules=[resourceclosing])
|
||||
self.addCleanup(container.unwire)
|
||||
|
||||
result_1 = resourceclosing.test_function(service=service)
|
||||
self.assertIs(result_1, service)
|
||||
self.assertEqual(result_1.init_counter, 0)
|
||||
self.assertEqual(result_1.shutdown_counter, 0)
|
||||
|
||||
result_2 = resourceclosing.test_function(service=service)
|
||||
self.assertIs(result_2, service)
|
||||
self.assertEqual(result_2.init_counter, 0)
|
||||
self.assertEqual(result_2.shutdown_counter, 0)
|
||||
|
||||
def test_class_decorator(self):
|
||||
service = module.test_class_decorator()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_container(self):
|
||||
service = module.test_container()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
|
||||
class WiringWithStringModuleAndPackageNamesTest(unittest.TestCase):
|
||||
|
||||
container: Container
|
||||
|
|
|
@ -1,15 +1,8 @@
|
|||
import contextlib
|
||||
from decimal import Decimal
|
||||
import importlib
|
||||
import unittest
|
||||
|
||||
from dependency_injector.wiring import (
|
||||
wire,
|
||||
Provide,
|
||||
Provider,
|
||||
Closing,
|
||||
register_loader_containers,
|
||||
unregister_loader_containers,
|
||||
)
|
||||
from dependency_injector import containers, providers, errors
|
||||
|
||||
|
@ -33,270 +26,7 @@ sys.path.append(_SAMPLES_DIR)
|
|||
|
||||
from wiringstringidssamples import module, package
|
||||
from wiringstringidssamples.service import Service
|
||||
from wiringstringidssamples.container import Container, SubContainer
|
||||
|
||||
|
||||
class WiringTest(unittest.TestCase):
|
||||
|
||||
container: Container
|
||||
|
||||
def setUp(self) -> None:
|
||||
self.container = Container(config={"a": {"b": {"c": 10}}})
|
||||
self.container.wire(
|
||||
modules=[module],
|
||||
packages=[package],
|
||||
)
|
||||
self.addCleanup(self.container.unwire)
|
||||
|
||||
def test_package_lookup(self):
|
||||
from wiringstringidssamples.package import test_package_function
|
||||
service = test_package_function()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_package_subpackage_lookup(self):
|
||||
from wiringstringidssamples.package.subpackage import test_package_function
|
||||
service = test_package_function()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_package_submodule_lookup(self):
|
||||
from wiringstringidssamples.package.subpackage.submodule import test_function
|
||||
service = test_function()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_module_attributes_wiring(self):
|
||||
self.assertIsInstance(module.service, Service)
|
||||
self.assertIsInstance(module.service_provider(), Service)
|
||||
self.assertIsInstance(module.undefined, Provide)
|
||||
|
||||
def test_class_wiring(self):
|
||||
test_class_object = module.TestClass()
|
||||
self.assertIsInstance(test_class_object.service, Service)
|
||||
|
||||
def test_class_wiring_context_arg(self):
|
||||
test_service = self.container.service()
|
||||
|
||||
test_class_object = module.TestClass(service=test_service)
|
||||
self.assertIs(test_class_object.service, test_service)
|
||||
|
||||
def test_class_method_wiring(self):
|
||||
test_class_object = module.TestClass()
|
||||
service = test_class_object.method()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_class_classmethod_wiring(self):
|
||||
service = module.TestClass.class_method()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_instance_classmethod_wiring(self):
|
||||
instance = module.TestClass()
|
||||
service = instance.class_method()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_class_staticmethod_wiring(self):
|
||||
service = module.TestClass.static_method()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_instance_staticmethod_wiring(self):
|
||||
instance = module.TestClass()
|
||||
service = instance.static_method()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_class_attribute_wiring(self):
|
||||
self.assertIsInstance(module.TestClass.service, Service)
|
||||
self.assertIsInstance(module.TestClass.service_provider(), Service)
|
||||
self.assertIsInstance(module.TestClass.undefined, Provide)
|
||||
|
||||
def test_function_wiring(self):
|
||||
service = module.test_function()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_function_wiring_context_arg(self):
|
||||
test_service = self.container.service()
|
||||
|
||||
service = module.test_function(service=test_service)
|
||||
self.assertIs(service, test_service)
|
||||
|
||||
def test_function_wiring_provider(self):
|
||||
service = module.test_function_provider()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_function_wiring_provider_context_arg(self):
|
||||
test_service = self.container.service()
|
||||
|
||||
service = module.test_function_provider(service_provider=lambda: test_service)
|
||||
self.assertIs(service, test_service)
|
||||
|
||||
def test_configuration_option(self):
|
||||
(
|
||||
value_int,
|
||||
value_float,
|
||||
value_str,
|
||||
value_decimal,
|
||||
value_required,
|
||||
value_required_int,
|
||||
value_required_float,
|
||||
value_required_str,
|
||||
value_required_decimal,
|
||||
) = module.test_config_value()
|
||||
|
||||
self.assertEqual(value_int, 10)
|
||||
self.assertEqual(value_float, 10.0)
|
||||
self.assertEqual(value_str, "10")
|
||||
self.assertEqual(value_decimal, Decimal(10))
|
||||
self.assertEqual(value_required, 10)
|
||||
self.assertEqual(value_required_int, 10)
|
||||
self.assertEqual(value_required_float, 10.0)
|
||||
self.assertEqual(value_required_str, "10")
|
||||
self.assertEqual(value_required_decimal, Decimal(10))
|
||||
|
||||
def test_configuration_option_required_undefined(self):
|
||||
self.container.config.reset_override()
|
||||
with self.assertRaisesRegex(errors.Error, "Undefined configuration option \"config.a.b.c\""):
|
||||
module.test_config_value_required_undefined()
|
||||
|
||||
def test_provide_provider(self):
|
||||
service = module.test_provide_provider()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_provided_instance(self):
|
||||
class TestService:
|
||||
foo = {
|
||||
"bar": lambda: 10,
|
||||
}
|
||||
|
||||
with self.container.service.override(TestService()):
|
||||
some_value = module.test_provided_instance()
|
||||
self.assertEqual(some_value, 10)
|
||||
|
||||
def test_subcontainer(self):
|
||||
some_value = module.test_subcontainer_provider()
|
||||
self.assertEqual(some_value, 1)
|
||||
|
||||
def test_config_invariant(self):
|
||||
config = {
|
||||
"option": {
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
},
|
||||
"switch": "a",
|
||||
}
|
||||
self.container.config.from_dict(config)
|
||||
|
||||
value_default = module.test_config_invariant()
|
||||
self.assertEqual(value_default, 1)
|
||||
|
||||
with self.container.config.switch.override("a"):
|
||||
value_a = module.test_config_invariant()
|
||||
self.assertEqual(value_a, 1)
|
||||
|
||||
with self.container.config.switch.override("b"):
|
||||
value_b = module.test_config_invariant()
|
||||
self.assertEqual(value_b, 2)
|
||||
|
||||
def test_wire_with_class_error(self):
|
||||
with self.assertRaises(Exception):
|
||||
wire(
|
||||
container=Container,
|
||||
modules=[module],
|
||||
)
|
||||
|
||||
def test_unwire_function(self):
|
||||
self.container.unwire()
|
||||
self.assertIsInstance(module.test_function(), Provide)
|
||||
|
||||
def test_unwire_class(self):
|
||||
self.container.unwire()
|
||||
test_class_object = module.TestClass()
|
||||
self.assertIsInstance(test_class_object.service, Provide)
|
||||
|
||||
def test_unwire_class_method(self):
|
||||
self.container.unwire()
|
||||
test_class_object = module.TestClass()
|
||||
self.assertIsInstance(test_class_object.method(), Provide)
|
||||
|
||||
def test_unwire_package_function(self):
|
||||
self.container.unwire()
|
||||
from wiringstringidssamples.package.subpackage.submodule import test_function
|
||||
self.assertIsInstance(test_function(), Provide)
|
||||
|
||||
def test_unwire_package_function_by_reference(self):
|
||||
from wiringstringidssamples.package.subpackage import submodule
|
||||
self.container.unwire()
|
||||
self.assertIsInstance(submodule.test_function(), Provide)
|
||||
|
||||
def test_unwire_module_attributes(self):
|
||||
self.container.unwire()
|
||||
self.assertIsInstance(module.service, Provide)
|
||||
self.assertIsInstance(module.service_provider, Provider)
|
||||
self.assertIsInstance(module.undefined, Provide)
|
||||
|
||||
def test_unwire_class_attributes(self):
|
||||
self.container.unwire()
|
||||
self.assertIsInstance(module.TestClass.service, Provide)
|
||||
self.assertIsInstance(module.TestClass.service_provider, Provider)
|
||||
self.assertIsInstance(module.TestClass.undefined, Provide)
|
||||
|
||||
def test_wire_multiple_containers(self):
|
||||
sub_container = SubContainer()
|
||||
sub_container.wire(
|
||||
modules=[module],
|
||||
packages=[package],
|
||||
)
|
||||
self.addCleanup(sub_container.unwire)
|
||||
|
||||
service, some_value = module.test_provide_from_different_containers()
|
||||
|
||||
self.assertIsInstance(service, Service)
|
||||
self.assertEqual(some_value, 1)
|
||||
|
||||
def test_closing_resource(self):
|
||||
from wiringstringidssamples import resourceclosing
|
||||
|
||||
resourceclosing.Service.reset_counter()
|
||||
|
||||
container = resourceclosing.Container()
|
||||
container.wire(modules=[resourceclosing])
|
||||
self.addCleanup(container.unwire)
|
||||
|
||||
result_1 = resourceclosing.test_function()
|
||||
self.assertIsInstance(result_1, resourceclosing.Service)
|
||||
self.assertEqual(result_1.init_counter, 1)
|
||||
self.assertEqual(result_1.shutdown_counter, 1)
|
||||
|
||||
result_2 = resourceclosing.test_function()
|
||||
self.assertIsInstance(result_2, resourceclosing.Service)
|
||||
self.assertEqual(result_2.init_counter, 2)
|
||||
self.assertEqual(result_2.shutdown_counter, 2)
|
||||
|
||||
self.assertIsNot(result_1, result_2)
|
||||
|
||||
def test_closing_resource_context(self):
|
||||
from wiringstringidssamples import resourceclosing
|
||||
|
||||
resourceclosing.Service.reset_counter()
|
||||
service = resourceclosing.Service()
|
||||
|
||||
container = resourceclosing.Container()
|
||||
container.wire(modules=[resourceclosing])
|
||||
self.addCleanup(container.unwire)
|
||||
|
||||
result_1 = resourceclosing.test_function(service=service)
|
||||
self.assertIs(result_1, service)
|
||||
self.assertEqual(result_1.init_counter, 0)
|
||||
self.assertEqual(result_1.shutdown_counter, 0)
|
||||
|
||||
result_2 = resourceclosing.test_function(service=service)
|
||||
self.assertIs(result_2, service)
|
||||
self.assertEqual(result_2.init_counter, 0)
|
||||
self.assertEqual(result_2.shutdown_counter, 0)
|
||||
|
||||
def test_class_decorator(self):
|
||||
service = module.test_class_decorator()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
||||
def test_container(self):
|
||||
service = module.test_container()
|
||||
self.assertIsInstance(service, Service)
|
||||
from wiringstringidssamples.container import Container
|
||||
|
||||
|
||||
class WiringAndFastAPITest(unittest.TestCase):
|
||||
|
|
Loading…
Reference in New Issue
Block a user