python-dependency-injector/tests/unit/containers/test_traversal_py3.py

94 lines
3.0 KiB
Python
Raw Normal View History

import unittest
from dependency_injector import containers, providers
class TraverseProviderTests(unittest.TestCase):
def test_nested_providers(self):
class Container(containers.DeclarativeContainer):
obj_factory = providers.DelegatedFactory(
dict,
foo=providers.Resource(
dict,
2021-10-01 03:09:42 +03:00
foo="bar"
),
bar=providers.Resource(
dict,
2021-10-01 03:09:42 +03:00
foo="bar"
)
)
container = Container()
all_providers = list(container.traverse())
self.assertIn(container.obj_factory, all_providers)
2021-10-01 03:09:42 +03:00
self.assertIn(container.obj_factory.kwargs["foo"], all_providers)
self.assertIn(container.obj_factory.kwargs["bar"], all_providers)
self.assertEqual(len(all_providers), 3)
def test_nested_providers_with_filtering(self):
class Container(containers.DeclarativeContainer):
obj_factory = providers.DelegatedFactory(
dict,
foo=providers.Resource(
dict,
2021-10-01 03:09:42 +03:00
foo="bar"
),
bar=providers.Resource(
dict,
2021-10-01 03:09:42 +03:00
foo="bar"
)
)
container = Container()
all_providers = list(container.traverse(types=[providers.Resource]))
2021-10-01 03:09:42 +03:00
self.assertIn(container.obj_factory.kwargs["foo"], all_providers)
self.assertIn(container.obj_factory.kwargs["bar"], all_providers)
self.assertEqual(len(all_providers), 2)
class TraverseProviderDeclarativeTests(unittest.TestCase):
def test_nested_providers(self):
class Container(containers.DeclarativeContainer):
obj_factory = providers.DelegatedFactory(
dict,
foo=providers.Resource(
dict,
2021-10-01 03:09:42 +03:00
foo="bar"
),
bar=providers.Resource(
dict,
2021-10-01 03:09:42 +03:00
foo="bar"
)
)
all_providers = list(Container.traverse())
self.assertIn(Container.obj_factory, all_providers)
2021-10-01 03:09:42 +03:00
self.assertIn(Container.obj_factory.kwargs["foo"], all_providers)
self.assertIn(Container.obj_factory.kwargs["bar"], all_providers)
self.assertEqual(len(all_providers), 3)
def test_nested_providers_with_filtering(self):
class Container(containers.DeclarativeContainer):
obj_factory = providers.DelegatedFactory(
dict,
foo=providers.Resource(
dict,
2021-10-01 03:09:42 +03:00
foo="bar"
),
bar=providers.Resource(
dict,
2021-10-01 03:09:42 +03:00
foo="bar"
)
)
all_providers = list(Container.traverse(types=[providers.Resource]))
2021-10-01 03:09:42 +03:00
self.assertIn(Container.obj_factory.kwargs["foo"], all_providers)
self.assertIn(Container.obj_factory.kwargs["bar"], all_providers)
self.assertEqual(len(all_providers), 2)