From 41abbc2e879dc609755dbe0b285e6ea507c26e8d Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Mon, 22 Jun 2020 22:35:36 -0400 Subject: [PATCH] Add unit tests --- .../unit/providers/test_container_py2_py3.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/unit/providers/test_container_py2_py3.py diff --git a/tests/unit/providers/test_container_py2_py3.py b/tests/unit/providers/test_container_py2_py3.py new file mode 100644 index 00000000..49445131 --- /dev/null +++ b/tests/unit/providers/test_container_py2_py3.py @@ -0,0 +1,54 @@ +"""Dependency injector container provider unit tests.""" + +import copy + +import unittest2 as unittest + +from dependency_injector import containers, providers + + +TEST_VALUE_1 = 'core_section_value1' +TEST_CONFIG_1 = { + 'core': { + 'section': { + 'value': TEST_VALUE_1, + }, + }, +} + +TEST_VALUE_2 = 'core_section_value2' +TEST_CONFIG_2 = { + 'core': { + 'section': { + 'value': TEST_VALUE_2, + }, + }, +} + + +def _copied(value): + return copy.deepcopy(value) + + +class TestCore(containers.DeclarativeContainer): + config = providers.Configuration('core') + value_getter = providers.Callable(lambda _: _, config.section.value) + + +class TestApplication(containers.DeclarativeContainer): + config = providers.Configuration('config') + core = providers.Container(TestCore, config=config.core) + dict_factory = providers.Factory(dict, value=core.value_getter) + + +class ContainerTests(unittest.TestCase): + + def test(self): + application = TestApplication(config=_copied(TEST_CONFIG_1)) + self.assertEqual(application.dict_factory(), {'value': TEST_VALUE_1}) + + def test_double_override(self): + application = TestApplication() + application.config.override(_copied(TEST_CONFIG_1)) + application.config.override(_copied(TEST_CONFIG_2)) + self.assertEqual(application.dict_factory(), {'value': TEST_VALUE_2})