From c824eae5663394f454c6422000e7704f2bce571f Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Thu, 14 May 2015 11:35:41 +0300 Subject: [PATCH] Renaming NewInstance provider to Factory provider --- objects/__init__.py | 4 ++-- objects/providers.py | 29 ++++++++++++++++-------- tests/test_decorators.py | 13 ++++++----- tests/test_injections.py | 4 ++-- tests/test_providers.py | 48 ++++++++++++++++++++-------------------- 5 files changed, 55 insertions(+), 43 deletions(-) diff --git a/objects/__init__.py b/objects/__init__.py index f5921f20..4b9e34d3 100644 --- a/objects/__init__.py +++ b/objects/__init__.py @@ -7,7 +7,7 @@ from .catalog import AbstractCatalog from .providers import Provider from .providers import Delegate -from .providers import NewInstance +from .providers import Factory from .providers import Singleton from .providers import ExternalDependency from .providers import Class @@ -32,7 +32,7 @@ __all__ = ('AbstractCatalog', # Providers 'Provider', 'Delegate', - 'NewInstance', + 'Factory', 'Singleton', 'ExternalDependency', 'Class', diff --git a/objects/providers.py b/objects/providers.py index a40471a8..a69d1f05 100644 --- a/objects/providers.py +++ b/objects/providers.py @@ -69,11 +69,11 @@ class Delegate(Provider): return self.delegated -class NewInstance(Provider): +class Factory(Provider): - """New instance provider. + """Factory provider. - New instance providers will create and return new instance on every call. + Factory providers will create and return new instance on every call. """ __slots__ = ('provides', 'kwargs', 'attributes', 'methods') @@ -81,7 +81,7 @@ class NewInstance(Provider): def __init__(self, provides, *injections): """Initializer.""" if not isinstance(provides, class_types): - raise Error('NewInstance provider expects to get class, ' + + raise Error('Factory provider expects to get class, ' + 'got {0} instead'.format(str(provides))) self.provides = provides self.kwargs = tuple((injection @@ -93,7 +93,7 @@ class NewInstance(Provider): self.methods = tuple((injection for injection in injections if is_method_injection(injection))) - super(NewInstance, self).__init__() + super(Factory, self).__init__() def __call__(self, *args, **kwargs): """Return provided instance.""" @@ -114,24 +114,35 @@ class NewInstance(Provider): return instance -class Singleton(NewInstance): +class NewInstance(Factory): + + """NewInstance provider. + + It is synonym of Factory provider. NewInstance provider is considered to + be deprecated, but will be able to use for further backward + compatibility. + """ + + +class Singleton(Provider): """Singleton provider. Singleton provider will create instance once and return it on every call. """ - __slots__ = ('instance',) + __slots__ = ('instance', 'factory') def __init__(self, *args, **kwargs): """Initializer.""" self.instance = None - super(Singleton, self).__init__(*args, **kwargs) + self.factory = Factory(*args, **kwargs) + super(Singleton, self).__init__() def __call__(self, *args, **kwargs): """Return provided instance.""" if not self.instance: - self.instance = super(Singleton, self).__call__(*args, **kwargs) + self.instance = self.factory(*args, **kwargs) return self.instance def reset(self): diff --git a/tests/test_decorators.py b/tests/test_decorators.py index c6ce5f42..1f3dbbb8 100644 --- a/tests/test_decorators.py +++ b/tests/test_decorators.py @@ -7,7 +7,7 @@ from objects.decorators import inject from objects.catalog import AbstractCatalog -from objects.providers import NewInstance +from objects.providers import Factory from objects.providers import Object from objects.providers import Value @@ -40,14 +40,15 @@ class OverrideTests(unittest.TestCase): self.assertEqual(self.Catalog.obj(), 1) self.assertEqual(self.Catalog.another_obj(), 2) + class InjectTests(unittest.TestCase): """Inject decorator test cases.""" def test_decorated(self): """Test `inject()` decorated callback.""" - provider1 = NewInstance(object) - provider2 = NewInstance(list) + provider1 = Factory(object) + provider2 = Factory(list) @inject(KwArg('a', provider1)) @inject(KwArg('b', provider2)) @@ -67,8 +68,8 @@ class InjectTests(unittest.TestCase): def test_decorated_kwargs_priority(self): """Test `inject()` decorated callback kwargs priority.""" - provider1 = NewInstance(object) - provider2 = NewInstance(list) + provider1 = Factory(object) + provider2 = Factory(list) object_a = object() @inject(KwArg('a', provider1)) @@ -90,7 +91,7 @@ class InjectTests(unittest.TestCase): def test_decorated_with_args(self): """Test `inject()` decorated callback with args.""" - provider = NewInstance(list) + provider = Factory(list) object_a = object() @inject(KwArg('b', provider)) diff --git a/tests/test_injections.py b/tests/test_injections.py index b9612e1a..aa682ecd 100644 --- a/tests/test_injections.py +++ b/tests/test_injections.py @@ -7,7 +7,7 @@ from objects.injections import KwArg from objects.injections import Attribute from objects.injections import Method -from objects.providers import NewInstance +from objects.providers import Factory class InjectionTests(unittest.TestCase): @@ -27,7 +27,7 @@ class InjectionTests(unittest.TestCase): def test_value_with_provider_injectable(self): """Test Injection value property with provider.""" - injection = Injection('some_arg_name', NewInstance(object)) + injection = Injection('some_arg_name', Factory(object)) self.assertIsInstance(injection.value, object) diff --git a/tests/test_providers.py b/tests/test_providers.py index 85ac1cba..0dc37ee7 100644 --- a/tests/test_providers.py +++ b/tests/test_providers.py @@ -4,7 +4,7 @@ import unittest2 as unittest from objects.providers import Provider from objects.providers import Delegate -from objects.providers import NewInstance +from objects.providers import Factory from objects.providers import Singleton from objects.providers import ExternalDependency from objects.providers import Class @@ -130,13 +130,13 @@ class DelegateTests(unittest.TestCase): self.assertIs(delegated2, self.delegated) -class NewInstanceTests(unittest.TestCase): +class FactoryTests(unittest.TestCase): - """NewInstance test cases.""" + """Factory test cases.""" class Example(object): - """Example class for NewInstance provider tests.""" + """Example class for Factory provider tests.""" def __init__(self, init_arg1=None, init_arg2=None): """Initializer. @@ -164,15 +164,15 @@ class NewInstanceTests(unittest.TestCase): def test_is_provider(self): """Test `is_provider` check.""" - self.assertTrue(is_provider(NewInstance(self.Example))) + self.assertTrue(is_provider(Factory(self.Example))) def test_init_with_not_class(self): """Test creation of provider with not a class.""" - self.assertRaises(Error, NewInstance, 123) + self.assertRaises(Error, Factory, 123) def test_call(self): """Test creation of new instances.""" - provider = NewInstance(self.Example) + provider = Factory(self.Example) instance1 = provider() instance2 = provider() @@ -182,9 +182,9 @@ class NewInstanceTests(unittest.TestCase): def test_call_with_init_args(self): """Test creation of new instances with init args injections.""" - provider = NewInstance(self.Example, - KwArg('init_arg1', 'i1'), - KwArg('init_arg2', 'i2')) + provider = Factory(self.Example, + KwArg('init_arg1', 'i1'), + KwArg('init_arg2', 'i2')) instance1 = provider() instance2 = provider() @@ -201,9 +201,9 @@ class NewInstanceTests(unittest.TestCase): def test_call_with_attributes(self): """Test creation of new instances with attribute injections.""" - provider = NewInstance(self.Example, - Attribute('attribute1', 'a1'), - Attribute('attribute2', 'a2')) + provider = Factory(self.Example, + Attribute('attribute1', 'a1'), + Attribute('attribute2', 'a2')) instance1 = provider() instance2 = provider() @@ -220,9 +220,9 @@ class NewInstanceTests(unittest.TestCase): def test_call_with_methods(self): """Test creation of new instances with method injections.""" - provider = NewInstance(self.Example, - Method('method1', 'm1'), - Method('method2', 'm2')) + provider = Factory(self.Example, + Method('method1', 'm1'), + Method('method2', 'm2')) instance1 = provider() instance2 = provider() @@ -239,7 +239,7 @@ class NewInstanceTests(unittest.TestCase): def test_call_with_context_args(self): """Test creation of new instances with context args.""" - provider = NewInstance(self.Example) + provider = Factory(self.Example) instance = provider(11, 22) self.assertEqual(instance.init_arg1, 11) @@ -247,8 +247,8 @@ class NewInstanceTests(unittest.TestCase): def test_call_with_context_kwargs(self): """Test creation of new instances with context kwargs.""" - provider = NewInstance(self.Example, - KwArg('init_arg1', 1)) + provider = Factory(self.Example, + KwArg('init_arg1', 1)) instance1 = provider(init_arg2=22) self.assertEqual(instance1.init_arg1, 1) @@ -260,9 +260,9 @@ class NewInstanceTests(unittest.TestCase): def test_call_overridden(self): """Test creation of new instances on overridden provider.""" - provider = NewInstance(self.Example) - overriding_provider1 = NewInstance(dict) - overriding_provider2 = NewInstance(list) + provider = Factory(self.Example) + overriding_provider1 = Factory(dict) + overriding_provider2 = Factory(list) provider.override(overriding_provider1) provider.override(overriding_provider2) @@ -323,12 +323,12 @@ class ExternalDependencyTests(unittest.TestCase): def test_call_overridden(self): """Test call of overridden external dependency.""" - self.provider.override(NewInstance(list)) + self.provider.override(Factory(list)) self.assertIsInstance(self.provider(), list) def test_call_overridden_but_not_instance_of(self): """Test call of overridden external dependency, but not instance of.""" - self.provider.override(NewInstance(dict)) + self.provider.override(Factory(dict)) self.assertRaises(Error, self.provider) def test_call_not_overridden(self):