From 3bb9146496649d2123bf8fc605cdf127c5c2f9e4 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Wed, 9 Nov 2016 23:45:25 +0200 Subject: [PATCH] Add tests for Callable provider --- .../providers/callables.pyx | 2 +- tests/unit/providers/test_callables.py | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/dependency_injector/providers/callables.pyx b/src/dependency_injector/providers/callables.pyx index fc895363..c3eef390 100644 --- a/src/dependency_injector/providers/callables.pyx +++ b/src/dependency_injector/providers/callables.pyx @@ -77,7 +77,7 @@ cdef class Callable(Provider): if copied is not None: return copied - copied = self.__class__(self.__provies, + copied = self.__class__(self.provides, *deepcopy(self.args, memo), **deepcopy(self.kwargs, memo)) diff --git a/tests/unit/providers/test_callables.py b/tests/unit/providers/test_callables.py index f17c0e4d..717b8397 100644 --- a/tests/unit/providers/test_callables.py +++ b/tests/unit/providers/test_callables.py @@ -58,6 +58,30 @@ class CallableTests(unittest.TestCase): self.assertTupleEqual(provider(), (1, 2, 3, 4)) + def test_set_args(self): + provider = providers.Callable(self.example) \ + .add_args(1, 2) \ + .set_args(3, 4) + self.assertEqual(provider.args, tuple([3, 4])) + + def test_set_kwargs(self): + provider = providers.Callable(self.example) \ + .add_kwargs(init_arg3=3, init_arg4=4) \ + .set_kwargs(init_arg3=4, init_arg4=5) + self.assertEqual(provider.kwargs, dict(init_arg3=4, init_arg4=5)) + + def test_clear_args(self): + provider = providers.Callable(self.example) \ + .add_args(1, 2) \ + .clear_args() + self.assertEqual(provider.args, tuple()) + + def test_clear_kwargs(self): + provider = providers.Callable(self.example) \ + .add_kwargs(init_arg3=3, init_arg4=4) \ + .clear_kwargs() + self.assertEqual(provider.kwargs, dict()) + def test_call_overridden(self): provider = providers.Callable(self.example) @@ -66,6 +90,82 @@ class CallableTests(unittest.TestCase): self.assertTupleEqual(provider(), (1, 2, 3, 4)) + def test_deepcopy(self): + provider = providers.Callable(self.example) + + provider_copy = providers.deepcopy(provider) + + self.assertIsNot(provider, provider_copy) + self.assertIs(provider.provides, provider_copy.provides) + self.assertIsInstance(provider, providers.Callable) + + def test_deepcopy_from_memo(self): + provider = providers.Callable(self.example) + provider_copy_memo = providers.Callable(self.example) + + provider_copy = providers.deepcopy( + provider, memo={id(provider): provider_copy_memo}) + + self.assertIs(provider_copy, provider_copy_memo) + + def test_deepcopy_args(self): + provider = providers.Callable(self.example) + dependent_provider1 = providers.Callable(list) + dependent_provider2 = providers.Callable(dict) + + provider.add_args(dependent_provider1, dependent_provider2) + + provider_copy = providers.deepcopy(provider) + dependent_provider_copy1 = provider_copy.args[0] + dependent_provider_copy2 = provider_copy.args[1] + + self.assertNotEqual(provider.args, provider_copy.args) + + self.assertIs(dependent_provider1.provides, + dependent_provider_copy1.provides) + self.assertIsNot(dependent_provider1, dependent_provider_copy1) + + self.assertIs(dependent_provider2.provides, + dependent_provider_copy2.provides) + self.assertIsNot(dependent_provider2, dependent_provider_copy2) + + def test_deepcopy_kwargs(self): + provider = providers.Callable(self.example) + dependent_provider1 = providers.Callable(list) + dependent_provider2 = providers.Callable(dict) + + provider.add_kwargs(a1=dependent_provider1, a2=dependent_provider2) + + provider_copy = providers.deepcopy(provider) + dependent_provider_copy1 = provider_copy.kwargs['a1'] + dependent_provider_copy2 = provider_copy.kwargs['a2'] + + self.assertNotEqual(provider.kwargs, provider_copy.kwargs) + + self.assertIs(dependent_provider1.provides, + dependent_provider_copy1.provides) + self.assertIsNot(dependent_provider1, dependent_provider_copy1) + + self.assertIs(dependent_provider2.provides, + dependent_provider_copy2.provides) + self.assertIsNot(dependent_provider2, dependent_provider_copy2) + + def test_deepcopy_overridden(self): + provider = providers.Callable(self.example) + object_provider = providers.Object(object()) + + provider.override(object_provider) + + provider_copy = providers.deepcopy(provider) + object_provider_copy = provider_copy.overridden[0] + + self.assertIsNot(provider, provider_copy) + self.assertIs(provider.provides, provider_copy.provides) + self.assertIsInstance(provider, providers.Callable) + + self.assertIsNot(object_provider, object_provider_copy) + self.assertIsInstance(object_provider_copy, providers.Object) + def test_repr(self): provider = providers.Callable(self.example)