2020-10-09 22:16:27 +03:00
|
|
|
from decimal import Decimal
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from dependency_injector.wiring import wire, Provide
|
|
|
|
|
2020-10-21 00:48:54 +03:00
|
|
|
# Runtime import to avoid syntax errors in samples on Python < 3.5
|
|
|
|
import os
|
|
|
|
_SAMPLES_DIR = os.path.abspath(
|
|
|
|
os.path.sep.join((
|
|
|
|
os.path.dirname(__file__),
|
|
|
|
'../samples/',
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
import sys
|
|
|
|
sys.path.append(_SAMPLES_DIR)
|
|
|
|
|
|
|
|
from wiringsamples import module, package
|
|
|
|
from wiringsamples.service import Service
|
2020-10-28 20:44:11 +03:00
|
|
|
from wiringsamples.container import Container, SubContainer
|
2020-10-09 22:16:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2020-10-21 00:48:54 +03:00
|
|
|
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
|
2020-10-09 22:16:27 +03:00
|
|
|
service = test_function()
|
|
|
|
self.assertIsInstance(service, 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)
|
|
|
|
|
2020-10-28 20:11:07 +03:00
|
|
|
def test_class_classmethod_wiring(self):
|
|
|
|
service = module.TestClass.class_method()
|
|
|
|
self.assertIsInstance(service, Service)
|
|
|
|
|
|
|
|
def test_class_staticmethod_wiring(self):
|
|
|
|
service = module.TestClass.static_method()
|
|
|
|
self.assertIsInstance(service, Service)
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
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):
|
|
|
|
int_value, str_value, decimal_value = module.test_config_value()
|
|
|
|
self.assertEqual(int_value, 10)
|
|
|
|
self.assertEqual(str_value, '10')
|
|
|
|
self.assertEqual(decimal_value, Decimal(10))
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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()
|
2020-10-21 00:48:54 +03:00
|
|
|
from wiringsamples.package.subpackage.submodule import test_function
|
2020-10-09 22:16:27 +03:00
|
|
|
self.assertIsInstance(test_function(), Provide)
|
|
|
|
|
|
|
|
def test_unwire_package_function_by_reference(self):
|
2020-10-21 00:48:54 +03:00
|
|
|
from wiringsamples.package.subpackage import submodule
|
2020-10-09 22:16:27 +03:00
|
|
|
self.container.unwire()
|
|
|
|
self.assertIsInstance(submodule.test_function(), Provide)
|
2020-10-28 20:44:11 +03:00
|
|
|
|
|
|
|
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)
|