Sanitize callable and creational provider tests

This commit is contained in:
Roman Mogilatov 2016-05-18 01:17:39 +03:00
parent 65512366d2
commit a0b8b11a91
3 changed files with 23 additions and 119 deletions

View File

@ -2,11 +2,7 @@
import unittest2 as unittest import unittest2 as unittest
from dependency_injector import ( from dependency_injector import providers, utils, errors
providers,
utils,
errors,
)
class CallableTests(unittest.TestCase): class CallableTests(unittest.TestCase):
@ -40,20 +36,14 @@ class CallableTests(unittest.TestCase):
self.assertTupleEqual(provider(), (1, 2, 3, 4)) self.assertTupleEqual(provider(), (1, 2, 3, 4))
def test_call_with_keyword_args(self): def test_call_with_keyword_args(self):
"""Test call with keyword args. """Test call with keyword args."""
New simplified syntax.
"""
provider = providers.Callable(self.example) \ provider = providers.Callable(self.example) \
.kwargs(arg1=1, arg2=2, arg3=3, arg4=4) .kwargs(arg1=1, arg2=2, arg3=3, arg4=4)
self.assertTupleEqual(provider(), (1, 2, 3, 4)) self.assertTupleEqual(provider(), (1, 2, 3, 4))
def test_call_with_positional_and_keyword_args(self): def test_call_with_positional_and_keyword_args(self):
"""Test call with positional and keyword args. """Test call with positional and keyword args."""
Simplified syntax of positional and keyword arg injections.
"""
provider = providers.Callable(self.example) \ provider = providers.Callable(self.example) \
.args(1, 2) \ .args(1, 2) \
.kwargs(arg3=3, arg4=4) .kwargs(arg3=3, arg4=4)

View File

@ -2,12 +2,7 @@
import unittest2 as unittest import unittest2 as unittest
from dependency_injector import ( from dependency_injector import providers, injections, utils, errors
providers,
injections,
utils,
errors,
)
class Example(object): class Example(object):
@ -78,8 +73,9 @@ class FactoryTests(unittest.TestCase):
ExampleProvider(list) ExampleProvider(list)
def test_call(self): def test_call(self):
"""Test creation of new instances.""" """Test call."""
provider = providers.Factory(Example) provider = providers.Factory(Example)
instance1 = provider() instance1 = provider()
instance2 = provider() instance2 = provider()
@ -88,10 +84,7 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance2, Example) self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_args(self): def test_call_with_init_positional_args(self):
"""Test creation of new instances with init positional args. """Test call with init positional args."""
New simplified syntax.
"""
provider = providers.Factory(Example) \ provider = providers.Factory(Example) \
.args('i1', 'i2') .args('i1', 'i2')
@ -109,10 +102,7 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance2, Example) self.assertIsInstance(instance2, Example)
def test_call_with_init_keyword_args(self): def test_call_with_init_keyword_args(self):
"""Test creation of new instances with init keyword args. """Test call with init keyword args."""
New simplified syntax.
"""
provider = providers.Factory(Example) \ provider = providers.Factory(Example) \
.kwargs(init_arg1='i1', init_arg2='i2') .kwargs(init_arg1='i1', init_arg2='i2')
@ -130,32 +120,7 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance2, Example) self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_and_keyword_args(self): def test_call_with_init_positional_and_keyword_args(self):
"""Test creation of new instances with init positional and keyword args. """Test call with init positional and keyword args."""
Simplified syntax of positional and keyword arg injections.
"""
provider = providers.Factory(Example) \
.args('i1') \
.kwargs(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, Example)
self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_and_keyword_args_extended_syntax(self):
"""Test creation of new instances with init positional and keyword args.
Extended syntax of positional and keyword arg injections.
"""
provider = providers.Factory(Example) \ provider = providers.Factory(Example) \
.args('i1') \ .args('i1') \
.kwargs(init_arg2='i2') .kwargs(init_arg2='i2')
@ -174,7 +139,7 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance2, Example) self.assertIsInstance(instance2, Example)
def test_call_with_attributes(self): def test_call_with_attributes(self):
"""Test creation of new instances with attribute injections.""" """Test call with attribute injections."""
provider = providers.Factory(Example) \ provider = providers.Factory(Example) \
.attributes(attribute1='a1', attribute2='a2') .attributes(attribute1='a1', attribute2='a2')
@ -192,9 +157,10 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance2, Example) self.assertIsInstance(instance2, Example)
def test_call_with_context_args(self): def test_call_with_context_args(self):
"""Test creation of new instances with context args.""" """Test call with context args."""
provider = providers.Factory(Example) \ provider = providers.Factory(Example) \
.args(11, 22) .args(11, 22)
instance = provider(33, 44) instance = provider(33, 44)
self.assertEqual(instance.init_arg1, 11) self.assertEqual(instance.init_arg1, 11)
@ -203,7 +169,7 @@ class FactoryTests(unittest.TestCase):
self.assertEqual(instance.init_arg4, 44) self.assertEqual(instance.init_arg4, 44)
def test_call_with_context_kwargs(self): def test_call_with_context_kwargs(self):
"""Test creation of new instances with context kwargs.""" """Test call with context kwargs."""
provider = providers.Factory(Example) \ provider = providers.Factory(Example) \
.kwargs(init_arg1=1) .kwargs(init_arg1=1)
@ -216,9 +182,10 @@ class FactoryTests(unittest.TestCase):
self.assertEqual(instance2.init_arg2, 22) self.assertEqual(instance2.init_arg2, 22)
def test_call_with_context_args_and_kwargs(self): def test_call_with_context_args_and_kwargs(self):
"""Test creation of new instances with context args and kwargs.""" """Test call with context args and kwargs."""
provider = providers.Factory(Example) \ provider = providers.Factory(Example) \
.args(11) .args(11)
instance = provider(22, init_arg3=33, init_arg4=44) instance = provider(22, init_arg3=33, init_arg4=44)
self.assertEqual(instance.init_arg1, 11) self.assertEqual(instance.init_arg1, 11)
@ -227,7 +194,7 @@ class FactoryTests(unittest.TestCase):
self.assertEqual(instance.init_arg4, 44) self.assertEqual(instance.init_arg4, 44)
def test_call_overridden(self): def test_call_overridden(self):
"""Test creation of new instances on overridden provider.""" """Test call on overridden provider."""
provider = providers.Factory(Example) provider = providers.Factory(Example)
overriding_provider1 = providers.Factory(dict) overriding_provider1 = providers.Factory(dict)
overriding_provider2 = providers.Factory(list) overriding_provider2 = providers.Factory(list)
@ -339,6 +306,7 @@ class SingletonTests(unittest.TestCase):
def test_call(self): def test_call(self):
"""Test getting of instances.""" """Test getting of instances."""
provider = providers.Singleton(Example) provider = providers.Singleton(Example)
instance1 = provider() instance1 = provider()
instance2 = provider() instance2 = provider()
@ -347,10 +315,7 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example) self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_args(self): def test_call_with_init_positional_args(self):
"""Test getting of instances with init positional args. """Test getting of instances with init positional args."""
New simplified syntax.
"""
provider = providers.Singleton(Example) \ provider = providers.Singleton(Example) \
.args('i1', 'i2') .args('i1', 'i2')
@ -368,10 +333,7 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example) self.assertIsInstance(instance2, Example)
def test_call_with_init_keyword_args(self): def test_call_with_init_keyword_args(self):
"""Test getting of instances with init keyword args. """Test getting of instances with init keyword args."""
New simplified syntax.
"""
provider = providers.Singleton(Example) \ provider = providers.Singleton(Example) \
.kwargs(init_arg1='i1', init_arg2='i2') .kwargs(init_arg1='i1', init_arg2='i2')
@ -389,32 +351,7 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example) self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_and_keyword_args(self): def test_call_with_init_positional_and_keyword_args(self):
"""Test getting of instances with init positional and keyword args. """Test getting of instances with init positional and keyword args."""
Simplified syntax of positional and keyword arg injections.
"""
provider = providers.Singleton(Example) \
.args('i1') \
.kwargs(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.assertIs(instance1, instance2)
self.assertIsInstance(instance1, Example)
self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_and_keyword_args_extended_syntax(self):
"""Test getting of instances with init positional and keyword args.
Extended syntax of positional and keyword arg injections.
"""
provider = providers.Singleton(Example) \ provider = providers.Singleton(Example) \
.args('i1') \ .args('i1') \
.kwargs(init_arg2='i2') .kwargs(init_arg2='i2')
@ -453,6 +390,7 @@ class SingletonTests(unittest.TestCase):
def test_call_with_context_args(self): def test_call_with_context_args(self):
"""Test getting of instances with context args.""" """Test getting of instances with context args."""
provider = providers.Singleton(Example) provider = providers.Singleton(Example)
instance = provider(11, 22) instance = provider(11, 22)
self.assertEqual(instance.init_arg1, 11) self.assertEqual(instance.init_arg1, 11)
@ -499,32 +437,6 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(instance1, object) self.assertIsInstance(instance1, object)
self.assertIsInstance(instance2, object) self.assertIsInstance(instance2, object)
def test_provides_attr(self):
"""Test provides attribute."""
provider = providers.Singleton(Example)
self.assertIs(provider.provides, Example)
def test_args_attr(self):
"""Test args attribute."""
provider = providers.Singleton(Example) \
.args(1, 2)
self.assertEquals(len(provider._args), 2)
def test_kwargs_attr(self):
"""Test kwargs attribute."""
provider = providers.Singleton(Example) \
.kwargs(init_arg1=1, init_arg2=2)
self.assertEquals(len(provider._kwargs), 2)
def test_attributes_attr(self):
"""Test attributes attribute."""
provider = providers.Singleton(Example) \
.attributes(attribute1=1, attribute2=2)
self.assertEquals(len(provider._attributes), 2)
def test_injections(self): def test_injections(self):
"""Test getting a full list of injections using injections property.""" """Test getting a full list of injections using injections property."""
provider = providers.Singleton(Example) \ provider = providers.Singleton(Example) \

View File

@ -7,6 +7,8 @@ from dependency_injector import (
utils, utils,
) )
# TODO: move to test_base
class ObjectProviderTests(unittest.TestCase): class ObjectProviderTests(unittest.TestCase):
"""Object provider tests.""" """Object provider tests."""