mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
1c6160e827
* Add overriding functionality on declarative container initialization * Update changelog * Update bundles example
192 lines
6.6 KiB
Python
192 lines
6.6 KiB
Python
"""Dependency injector dynamic container unit tests."""
|
|
|
|
import unittest2 as unittest
|
|
|
|
from dependency_injector import (
|
|
containers,
|
|
providers,
|
|
errors,
|
|
)
|
|
|
|
|
|
class ContainerA(containers.DeclarativeContainer):
|
|
p11 = providers.Provider()
|
|
p12 = providers.Provider()
|
|
|
|
|
|
class DeclarativeContainerInstanceTests(unittest.TestCase):
|
|
|
|
def test_providers_attribute(self):
|
|
container_a1 = ContainerA()
|
|
container_a2 = ContainerA()
|
|
|
|
self.assertIsNot(container_a1.p11, container_a2.p11)
|
|
self.assertIsNot(container_a1.p12, container_a2.p12)
|
|
self.assertNotEqual(container_a1.providers, container_a2.providers)
|
|
|
|
def test_set_get_del_providers(self):
|
|
p13 = providers.Provider()
|
|
|
|
container_a1 = ContainerA()
|
|
container_a2 = ContainerA()
|
|
|
|
container_a1.p13 = p13
|
|
container_a2.p13 = p13
|
|
|
|
self.assertEqual(ContainerA.providers, dict(p11=ContainerA.p11,
|
|
p12=ContainerA.p12))
|
|
self.assertEqual(ContainerA.cls_providers, dict(p11=ContainerA.p11,
|
|
p12=ContainerA.p12))
|
|
|
|
self.assertEqual(container_a1.providers, dict(p11=container_a1.p11,
|
|
p12=container_a1.p12,
|
|
p13=p13))
|
|
self.assertEqual(container_a2.providers, dict(p11=container_a2.p11,
|
|
p12=container_a2.p12,
|
|
p13=p13))
|
|
|
|
del container_a1.p13
|
|
self.assertEqual(container_a1.providers, dict(p11=container_a1.p11,
|
|
p12=container_a1.p12))
|
|
|
|
del container_a2.p13
|
|
self.assertEqual(container_a2.providers, dict(p11=container_a2.p11,
|
|
p12=container_a2.p12))
|
|
|
|
del container_a1.p11
|
|
del container_a1.p12
|
|
self.assertEqual(container_a1.providers, dict())
|
|
self.assertEqual(ContainerA.providers, dict(p11=ContainerA.p11,
|
|
p12=ContainerA.p12))
|
|
|
|
del container_a2.p11
|
|
del container_a2.p12
|
|
self.assertEqual(container_a2.providers, dict())
|
|
self.assertEqual(ContainerA.providers, dict(p11=ContainerA.p11,
|
|
p12=ContainerA.p12))
|
|
|
|
def test_set_invalid_provider_type(self):
|
|
container_a = ContainerA()
|
|
container_a.provider_type = providers.Object
|
|
|
|
with self.assertRaises(errors.Error):
|
|
container_a.px = providers.Provider()
|
|
|
|
self.assertIs(ContainerA.provider_type,
|
|
containers.DeclarativeContainer.provider_type)
|
|
|
|
def test_set_providers(self):
|
|
p13 = providers.Provider()
|
|
p14 = providers.Provider()
|
|
container_a = ContainerA()
|
|
|
|
container_a.set_providers(p13=p13, p14=p14)
|
|
|
|
self.assertIs(container_a.p13, p13)
|
|
self.assertIs(container_a.p14, p14)
|
|
|
|
def test_override(self):
|
|
class _Container(containers.DeclarativeContainer):
|
|
p11 = providers.Provider()
|
|
|
|
class _OverridingContainer1(containers.DeclarativeContainer):
|
|
p11 = providers.Provider()
|
|
|
|
class _OverridingContainer2(containers.DeclarativeContainer):
|
|
p11 = providers.Provider()
|
|
p12 = providers.Provider()
|
|
|
|
container = _Container()
|
|
overriding_container1 = _OverridingContainer1()
|
|
overriding_container2 = _OverridingContainer2()
|
|
|
|
container.override(overriding_container1)
|
|
container.override(overriding_container2)
|
|
|
|
self.assertEqual(container.overridden,
|
|
(overriding_container1,
|
|
overriding_container2))
|
|
self.assertEqual(container.p11.overridden,
|
|
(overriding_container1.p11,
|
|
overriding_container2.p11))
|
|
|
|
self.assertEqual(_Container.overridden, tuple())
|
|
self.assertEqual(_Container.p11.overridden, tuple())
|
|
|
|
def test_override_with_itself(self):
|
|
container = ContainerA()
|
|
with self.assertRaises(errors.Error):
|
|
container.override(container)
|
|
|
|
def test_override_providers(self):
|
|
p1 = providers.Provider()
|
|
p2 = providers.Provider()
|
|
container_a = ContainerA()
|
|
|
|
container_a.override_providers(p11=p1, p12=p2)
|
|
|
|
self.assertIs(container_a.p11.last_overriding, p1)
|
|
self.assertIs(container_a.p12.last_overriding, p2)
|
|
|
|
def test_override_providers_with_unknown_provider(self):
|
|
container_a = ContainerA()
|
|
|
|
with self.assertRaises(AttributeError):
|
|
container_a.override_providers(unknown=providers.Provider())
|
|
|
|
def test_reset_last_overridding(self):
|
|
class _Container(containers.DeclarativeContainer):
|
|
p11 = providers.Provider()
|
|
|
|
class _OverridingContainer1(containers.DeclarativeContainer):
|
|
p11 = providers.Provider()
|
|
|
|
class _OverridingContainer2(containers.DeclarativeContainer):
|
|
p11 = providers.Provider()
|
|
p12 = providers.Provider()
|
|
|
|
container = _Container()
|
|
overriding_container1 = _OverridingContainer1()
|
|
overriding_container2 = _OverridingContainer2()
|
|
|
|
container.override(overriding_container1)
|
|
container.override(overriding_container2)
|
|
container.reset_last_overriding()
|
|
|
|
self.assertEqual(container.overridden,
|
|
(overriding_container1,))
|
|
self.assertEqual(container.p11.overridden,
|
|
(overriding_container1.p11,))
|
|
|
|
def test_reset_last_overridding_when_not_overridden(self):
|
|
container = ContainerA()
|
|
|
|
with self.assertRaises(errors.Error):
|
|
container.reset_last_overriding()
|
|
|
|
def test_reset_override(self):
|
|
class _Container(containers.DeclarativeContainer):
|
|
p11 = providers.Provider()
|
|
|
|
class _OverridingContainer1(containers.DeclarativeContainer):
|
|
p11 = providers.Provider()
|
|
|
|
class _OverridingContainer2(containers.DeclarativeContainer):
|
|
p11 = providers.Provider()
|
|
p12 = providers.Provider()
|
|
|
|
container = _Container()
|
|
overriding_container1 = _OverridingContainer1()
|
|
overriding_container2 = _OverridingContainer2()
|
|
|
|
container.override(overriding_container1)
|
|
container.override(overriding_container2)
|
|
container.reset_override()
|
|
|
|
self.assertEqual(container.overridden, tuple())
|
|
self.assertEqual(container.p11.overridden, tuple())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|