2021-02-01 17:42:21 +03:00
|
|
|
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"
|
2021-02-01 17:42:21 +03:00
|
|
|
),
|
|
|
|
bar=providers.Resource(
|
|
|
|
dict,
|
2021-10-01 03:09:42 +03:00
|
|
|
foo="bar"
|
2021-02-01 17:42:21 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2021-02-01 17:42:21 +03:00
|
|
|
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"
|
2021-02-01 17:42:21 +03:00
|
|
|
),
|
|
|
|
bar=providers.Resource(
|
|
|
|
dict,
|
2021-10-01 03:09:42 +03:00
|
|
|
foo="bar"
|
2021-02-01 17:42:21 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2021-02-01 17:42:21 +03:00
|
|
|
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"
|
2021-02-01 17:42:21 +03:00
|
|
|
),
|
|
|
|
bar=providers.Resource(
|
|
|
|
dict,
|
2021-10-01 03:09:42 +03:00
|
|
|
foo="bar"
|
2021-02-01 17:42:21 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2021-02-01 17:42:21 +03:00
|
|
|
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"
|
2021-02-01 17:42:21 +03:00
|
|
|
),
|
|
|
|
bar=providers.Resource(
|
|
|
|
dict,
|
2021-10-01 03:09:42 +03:00
|
|
|
foo="bar"
|
2021-02-01 17:42:21 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2021-02-01 17:42:21 +03:00
|
|
|
self.assertEqual(len(all_providers), 2)
|