def test_deepcopy_from_memo(self): provider = providers.Dependency(int) provider_copy_memo = providers.Provider() provider_copy = providers.deepcopy( provider, memo={id(provider): provider_copy_memo}) self.assertIs(provider_copy, provider_copy_memo)
def test_deepcopy_from_memo(): provider = providers.Self() provider_copy_memo = providers.Provider() provider_copy = providers.deepcopy(provider, memo={id(provider): provider_copy_memo}) assert provider_copy is provider_copy_memo
def test_set_get_del_providers(): a_p13 = providers.Provider() b_p23 = providers.Provider() ContainerA.p13 = a_p13 ContainerB.p23 = b_p23 assert ContainerA.providers == dict( p11=ContainerA.p11, p12=ContainerA.p12, p13=a_p13, ) assert ContainerB.providers == dict( p11=ContainerA.p11, p12=ContainerA.p12, p21=ContainerB.p21, p22=ContainerB.p22, p23=b_p23, ) assert ContainerA.cls_providers == dict( p11=ContainerA.p11, p12=ContainerA.p12, p13=a_p13, ) assert ContainerB.cls_providers == dict( p21=ContainerB.p21, p22=ContainerB.p22, p23=b_p23, ) del ContainerA.p13 del ContainerB.p23 assert ContainerA.providers == dict(p11=ContainerA.p11, p12=ContainerA.p12) assert ContainerB.providers == dict( p11=ContainerA.p11, p12=ContainerA.p12, p21=ContainerB.p21, p22=ContainerB.p22, ) assert ContainerA.cls_providers == dict(p11=ContainerA.p11, p12=ContainerA.p12) assert ContainerB.cls_providers == dict(p21=ContainerB.p21, p22=ContainerB.p22)
def test_reset_last_overriding(self): overriding_provider1 = providers.Provider() overriding_provider2 = providers.Provider() self.provider.override(overriding_provider1) self.provider.override(overriding_provider2) self.assertIs(self.provider.overridden[-1], overriding_provider2) self.assertIs(self.provider.last_overriding, overriding_provider2) self.provider.reset_last_overriding() self.assertIs(self.provider.overridden[-1], overriding_provider1) self.assertIs(self.provider.last_overriding, overriding_provider1) self.provider.reset_last_overriding() self.assertFalse(self.provider.overridden) self.assertIsNone(self.provider.last_overriding)
def test_traversal_provider_and_overriding(): provider1 = providers.Provider() provider2 = providers.Provider() provider3 = providers.Provider() provider3.override(provider2) provider = providers.Delegate(provider1) provider.override(provider3) all_providers = list(provider.traverse()) assert len(all_providers) == 3 assert provider1 in all_providers assert provider2 in all_providers assert provider3 in all_providers
def test_traverse_types_filtering(): provider1 = providers.Resource(dict) provider2 = providers.Resource(dict) provider3 = providers.Provider() provider = providers.Provider() provider.override(provider1) provider.override(provider2) provider.override(provider3) all_providers = list( providers.traverse(provider, types=[providers.Resource])) assert len(all_providers) == 2 assert provider1 in all_providers assert provider2 in all_providers
def test_traversal_default(): another_provider = providers.Provider() provider = providers.Dependency(default=another_provider) all_providers = list(provider.traverse()) assert len(all_providers) == 1 assert another_provider in all_providers
def test_deepcopy_from_memo(self): provider = providers.Object(1) provider_copy_memo = providers.Provider() provider_copy = providers.deepcopy( provider, memo={id(provider): provider_copy_memo}) self.assertIs(provider_copy, provider_copy_memo)
def test_traversal_provider(): another_provider = providers.Provider() provider = providers.Delegate(another_provider) all_providers = list(provider.traverse()) assert len(all_providers) == 1 assert another_provider in all_providers
def test_reset_last_overriding(provider): overriding_provider1 = providers.Provider() overriding_provider2 = providers.Provider() provider.override(overriding_provider1) provider.override(overriding_provider2) assert provider.overridden[-1] is overriding_provider2 assert provider.last_overriding is overriding_provider2 provider.reset_last_overriding() assert provider.overridden[-1] is overriding_provider1 assert provider.last_overriding is overriding_provider1 provider.reset_last_overriding() assert provider.overridden == tuple() assert not provider.overridden assert provider.last_overriding is None
def test_traverse_cycled_graph(): provider1 = providers.Provider() provider2 = providers.Provider() provider2.override(provider1) provider3 = providers.Provider() provider3.override(provider2) provider1.override( provider3) # Cycle: provider3 -> provider2 -> provider1 -> provider3 all_providers = list(providers.traverse(provider1)) assert len(all_providers) == 3 assert provider1 in all_providers assert provider2 in all_providers assert provider3 in all_providers
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_traversal_overriding_nested(self): provider1 = providers.Provider() provider2 = providers.Provider() provider2.override(provider1) provider3 = providers.Provider() provider3.override(provider2) provider = providers.Provider() provider.override(provider3) all_providers = list(provider.traverse()) self.assertEqual(len(all_providers), 3) self.assertIn(provider1, all_providers) self.assertIn(provider2, all_providers) self.assertIn(provider3, all_providers)
def test_item_getter(self): provides = providers.Object({'foo': 'bar'}) provider = providers.ItemGetter() provider.set_provides(provides) provider.set_name('foo') self.assertIs(provider.provides, provides) self.assertEqual(provider.name, 'foo') self.assertIs(provider.set_provides(providers.Provider()), provider) self.assertIs(provider.set_name('foo'), provider)
def test_item_getter(): provides = providers.Object({"foo": "bar"}) provider = providers.ItemGetter() provider.set_provides(provides) provider.set_name("foo") assert provider.provides is provides assert provider.name == "foo" assert provider.set_provides(providers.Provider()) is provider assert provider.set_name("foo") is provider
def test_attribute_getter(self): provides = providers.Object(object()) provider = providers.AttributeGetter() provider.set_provides(provides) provider.set_name('__dict__') self.assertIs(provider.provides, provides) self.assertEqual(provider.name, '__dict__') self.assertIs(provider.set_provides(providers.Provider()), provider) self.assertIs(provider.set_name('__dict__'), provider)
def test_attribute_getter(): provides = providers.Object(object()) provider = providers.AttributeGetter() provider.set_provides(provides) provider.set_name("__dict__") assert provider.provides is provides assert provider.name == "__dict__" assert provider.set_provides(providers.Provider()) is provider assert provider.set_name("__dict__") is provider
def test_reset_override(self): overriding_provider = providers.Provider() self.provider.override(overriding_provider) self.assertTrue(self.provider.overridden) self.assertEqual(self.provider.overridden, (overriding_provider, )) self.provider.reset_override() self.assertEqual(self.provider.overridden, tuple())
def test_traverse(): provider1 = providers.Provider() provided = provider1.provided provider = provided["item"] all_providers = list(provider.traverse()) assert len(all_providers) == 2 assert provider1 in all_providers assert provided in all_providers
def test_traverse(self): provider1 = providers.Provider() provided = provider1.provided provider = provided['item'] all_providers = list(provider.traverse()) self.assertEqual(len(all_providers), 2) self.assertIn(provider1, all_providers) self.assertIn(provided, all_providers)
def test_traversal_overriding(self): provider1 = providers.Provider() provider2 = providers.Provider() provider3 = providers.DependenciesContainer( provider1=provider1, provider2=provider2, ) provider = providers.DependenciesContainer() provider.override(provider3) all_providers = list(provider.traverse()) self.assertEqual(len(all_providers), 5) self.assertIn(provider1, all_providers) self.assertIn(provider2, all_providers) self.assertIn(provider3, all_providers) self.assertIn(provider.provider1, all_providers) self.assertIn(provider.provider2, all_providers)
def test_traversal_overriding(): provider1 = providers.Provider() provider2 = providers.Provider() provider3 = providers.DependenciesContainer( provider1=provider1, provider2=provider2, ) provider = providers.DependenciesContainer() provider.override(provider3) all_providers = list(provider.traverse()) assert len(all_providers) == 5 assert provider1 in all_providers assert provider2 in all_providers assert provider3 in all_providers assert provider.provider1 in all_providers assert provider.provider2 in all_providers
def test_reset_override(provider): overriding_provider = providers.Provider() provider.override(overriding_provider) assert provider.overridden assert provider.overridden == (overriding_provider,) provider.reset_override() assert provider.overridden == tuple()
def test_set_get_del_providers(self): a_p13 = providers.Provider() b_p23 = providers.Provider() ContainerA.p13 = a_p13 ContainerB.p23 = b_p23 self.assertEqual( ContainerA.providers, dict(p11=ContainerA.p11, p12=ContainerA.p12, p13=a_p13)) self.assertEqual( ContainerB.providers, dict(p11=ContainerA.p11, p12=ContainerA.p12, p21=ContainerB.p21, p22=ContainerB.p22, p23=b_p23)) self.assertEqual( ContainerA.cls_providers, dict(p11=ContainerA.p11, p12=ContainerA.p12, p13=a_p13)) self.assertEqual( ContainerB.cls_providers, dict(p21=ContainerB.p21, p22=ContainerB.p22, p23=b_p23)) del ContainerA.p13 del ContainerB.p23 self.assertEqual(ContainerA.providers, dict(p11=ContainerA.p11, p12=ContainerA.p12)) self.assertEqual( ContainerB.providers, dict(p11=ContainerA.p11, p12=ContainerA.p12, p21=ContainerB.p21, p22=ContainerB.p22)) self.assertEqual(ContainerA.cls_providers, dict(p11=ContainerA.p11, p12=ContainerA.p12)) self.assertEqual(ContainerB.cls_providers, dict(p21=ContainerB.p21, p22=ContainerB.p22))
def test_traverse(): provider1 = providers.Provider() provided = provider1.provided method = provided.method provider = method.call() all_providers = list(provider.traverse()) assert len(all_providers) == 3 assert provider1 in all_providers assert provided in all_providers assert method in all_providers
def test_init_with_grand_child_provider(): # Bug: https://github.com/ets-labs/python-dependency-injector/issues/350 provider = providers.Provider() container = ContainerC(p11=provider) assert isinstance(container.p11, providers.Provider) assert isinstance(container.p12, providers.Provider) assert isinstance(container.p21, providers.Provider) assert isinstance(container.p22, providers.Provider) assert isinstance(container.p31, providers.Provider) assert isinstance(container.p32, providers.Provider) assert container.p11.last_overriding is provider
def test_traverse(self): provider1 = providers.Provider() provided = provider1.provided method = provided.method provider = method.call() all_providers = list(provider.traverse()) self.assertEqual(len(all_providers), 3) self.assertIn(provider1, all_providers) self.assertIn(provided, all_providers) self.assertIn(method, all_providers)
def test_async_mode_enabling_on_overriding(self): dependency = object() async def _get_dependency_async(): return dependency provider = providers.Provider() provider.override(providers.Callable(_get_dependency_async)) self.assertTrue(provider.is_async_mode_undefined()) self._run(provider()) self.assertTrue(provider.is_async_mode_enabled())
def test_deepcopy_overridden(provider): overriding_provider = providers.Provider() provider.override(overriding_provider) provider_copy = providers.deepcopy(provider) overriding_provider_copy = provider_copy.overridden[0] assert provider is not provider_copy assert isinstance(provider, providers.Dependency) assert overriding_provider is not overriding_provider_copy assert isinstance(overriding_provider_copy, providers.Provider)
def test_providers_attribute_with_redefinition(self): p1 = providers.Provider() p2 = providers.Provider() class ContainerA2(ContainerA): p11 = p1 p12 = p2 self.assertEqual( ContainerA.providers, { 'p11': ContainerA.p11, 'p12': ContainerA.p12, }, ) self.assertEqual( ContainerA2.providers, { 'p11': p1, 'p12': p2, }, )