From 91ea284723f016168c3c96fbe131a5d9606d1c04 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Sat, 14 Mar 2015 15:57:30 +0200 Subject: [PATCH] Some tests for NewInstance provider, NewInstance provider refactoring --- objects/providers.py | 23 ++++----- tests/test_providers.py | 101 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 107 insertions(+), 17 deletions(-) diff --git a/objects/providers.py b/objects/providers.py index 2f2d9c6d..bef02325 100644 --- a/objects/providers.py +++ b/objects/providers.py @@ -1,5 +1,6 @@ """Providers module.""" +from inspect import isclass from collections import Iterable from .utils import ensure_is_provider @@ -74,6 +75,9 @@ class NewInstance(Provider): def __init__(self, provides, *injections): """Initializer.""" + if not isclass(provides): + raise Error('NewInstance provider expects to get class, ' + + 'got {} instead'.format(str(provides))) self.provides = provides self.init_args = tuple((injection for injection in injections @@ -91,19 +95,16 @@ class NewInstance(Provider): if self.overridden: return self.last_overriding(*args, **kwargs) - init_injections = dict(((injection.name, injection.value) - for injection in self.init_args)) - init_injections.update(kwargs) + init_kwargs = dict(((injection.name, injection.value) + for injection in self.init_args)) + init_kwargs.update(kwargs) - instance = self.provides(*args, **init_injections) + instance = self.provides(*args, **init_kwargs) - if not self.attributes: - for injection in self.attributes: - setattr(instance, injection.name, injection.value) - - if not self.methods: - for injection in self.methods: - getattr(instance, injection.name)(injection.value) + for attribute in self.attributes: + setattr(instance, attribute.name, attribute.value) + for method in self.methods: + getattr(instance, method.name)(method.value) return instance diff --git a/tests/test_providers.py b/tests/test_providers.py index 7fb177ff..c3f83b9d 100644 --- a/tests/test_providers.py +++ b/tests/test_providers.py @@ -117,23 +117,112 @@ class NewInstanceTest(unittest.TestCase): """NewInstance test cases.""" + class Example(object): + + """Example class for NewInstance provider tests.""" + + def __init__(self, init_arg1=None, init_arg2=None): + """Initializer. + + :param init_arg1: + :param init_arg2: + :return: + """ + self.init_arg1 = init_arg1 + self.init_arg2 = init_arg2 + + self.attribute1 = None + self.attribute2 = None + + self.method1_value = None + self.method2_value = None + + def method1(self, value): + """Setter method 1.""" + self.method1_value = value + + def method2(self, value): + """Setter method 2.""" + self.method2_value = value + def test_is_provider(self): """Test `is_provider` check.""" - self.assertTrue(is_provider(NewInstance(object))) + self.assertTrue(is_provider(NewInstance(self.Example))) + + def test_init_with_not_class(self): + """Test creation of provider with not a class.""" + self.assertRaises(Error, NewInstance, 123) def test_call(self): """Test creation of new instances.""" - provider = NewInstance(object) - instance1 = provider + provider = NewInstance(self.Example) + instance1 = provider() instance2 = provider() self.assertIsNot(instance1, instance2) - self.assertIsInstance(instance1, object) - self.assertIsInstance(instance2, object) + self.assertIsInstance(instance1, self.Example) + self.assertIsInstance(instance2, self.Example) + + def test_call_with_init_args(self): + """Test creation of new instances with init args injections.""" + provider = NewInstance(self.Example, + InitArg('init_arg1', 'i1'), + InitArg('init_arg2', 'i2')) + + instance1 = provider() + instance2 = provider() + + self.assertEqual(instance1.init_arg1, 'i1') + self.assertEqual(instance1.init_arg2, 'i2') + + self.assertEqual(instance2.init_arg1, 'i1') + self.assertEqual(instance2.init_arg2, 'i2') + + self.assertIsNot(instance1, instance2) + self.assertIsInstance(instance1, self.Example) + self.assertIsInstance(instance2, self.Example) + + def test_call_with_attributes(self): + """Test creation of new instances with attribute injections.""" + provider = NewInstance(self.Example, + Attribute('attribute1', 'a1'), + Attribute('attribute2', 'a2')) + + instance1 = provider() + instance2 = provider() + + self.assertEqual(instance1.attribute1, 'a1') + self.assertEqual(instance1.attribute2, 'a2') + + self.assertEqual(instance2.attribute1, 'a1') + self.assertEqual(instance2.attribute2, 'a2') + + self.assertIsNot(instance1, instance2) + self.assertIsInstance(instance1, self.Example) + self.assertIsInstance(instance2, self.Example) + + def test_call_with_methods(self): + """Test creation of new instances with method injections.""" + provider = NewInstance(self.Example, + Method('method1', 'm1'), + Method('method2', 'm2')) + + instance1 = provider() + instance2 = provider() + + self.assertEqual(instance1.method1_value, 'm1') + self.assertEqual(instance1.method2_value, 'm2') + + self.assertEqual(instance2.method1_value, 'm1') + self.assertEqual(instance2.method2_value, 'm2') + + self.assertIsNot(instance1, instance2) + self.assertIsInstance(instance1, self.Example) + self.assertIsInstance(instance2, self.Example) def test_call_overridden(self): """Test creation of new instances on overridden provider.""" - provider = NewInstance(object) + provider = NewInstance(self.Example) overriding_provider1 = NewInstance(dict) overriding_provider2 = NewInstance(list)