python-dependency-injector/tests/test_providers.py

114 lines
3.5 KiB
Python
Raw Normal View History

2015-03-12 13:46:14 +03:00
"""Objects providers unittests."""
2015-03-12 13:45:15 +03:00
2015-03-12 13:46:14 +03:00
import unittest2 as unittest
from objects.providers import Provider
2015-03-14 01:02:01 +03:00
from objects.providers import Delegate
2015-03-12 13:46:14 +03:00
from objects.providers import NewInstance
from objects.providers import Singleton
from objects.providers import Scoped
from objects.providers import ExternalDependency
from objects.providers import Class
from objects.providers import Object
from objects.providers import Function
from objects.providers import Value
from objects.providers import Callable
from objects.providers import Config
from objects.utils import is_provider
2015-03-13 18:31:07 +03:00
from objects.errors import Error
2015-03-12 13:46:14 +03:00
class ProviderTest(unittest.TestCase):
"""Provider test cases."""
def setUp(self):
"""Set test cases environment up."""
self.provider = Provider()
2015-03-12 13:46:14 +03:00
def test_is_provider(self):
"""Test `is_provider` check."""
self.assertTrue(is_provider(self.provider))
2015-03-12 13:46:14 +03:00
def test_call(self):
"""Test call."""
self.assertRaises(NotImplementedError, self.provider.__call__)
2015-03-12 13:46:14 +03:00
def test_delegate(self):
"""Test creating of provider delegation."""
delegate1 = self.provider.delegate()
2015-03-12 13:46:14 +03:00
2015-03-14 01:02:01 +03:00
self.assertIsInstance(delegate1, Delegate)
self.assertIs(delegate1.delegated, self.provider)
delegate2 = self.provider.delegate()
2015-03-14 01:02:01 +03:00
self.assertIsInstance(delegate2, Delegate)
self.assertIs(delegate2.delegated, self.provider)
self.assertIsNot(delegate1, delegate2)
2015-03-12 20:24:29 +03:00
def test_override(self):
"""Test provider overriding."""
overriding_provider = Provider()
self.provider.override(overriding_provider)
self.assertTrue(self.provider.overridden)
2015-03-12 20:24:29 +03:00
def test_override_with_not_provider(self):
"""Test provider overriding with not provider instance."""
self.assertRaises(Error, self.provider.override, object())
2015-03-12 20:24:29 +03:00
def test_last_overriding(self):
"""Test getting last overriding provider."""
overriding_provider1 = Provider()
overriding_provider2 = Provider()
self.provider.override(overriding_provider1)
self.assertIs(self.provider.last_overriding, overriding_provider1)
2015-03-12 20:24:29 +03:00
self.provider.override(overriding_provider2)
self.assertIs(self.provider.last_overriding, overriding_provider2)
2015-03-12 20:24:29 +03:00
def test_last_overriding_of_not_overridden_provider(self):
"""Test getting last overriding from not overridden provider."""
try:
self.provider.last_overriding
2015-03-13 18:31:07 +03:00
except Error:
2015-03-12 20:24:29 +03:00
pass
else:
self.fail('Got en error in {}'.format(
str(self.test_last_overriding_of_not_overridden_provider)))
class ProviderDelegateTest(unittest.TestCase):
"""ProviderDelegate test cases."""
def setUp(self):
"""Set test cases environment up."""
self.delegated = Provider()
2015-03-14 01:02:01 +03:00
self.delegate = Delegate(delegated=self.delegated)
def test_is_provider(self):
"""Test `is_provider` check."""
self.assertTrue(is_provider(self.delegate))
def test_init_with_not_provider(self):
"""Test that delegate accepts only another provider as delegated."""
2015-03-14 01:02:01 +03:00
self.assertRaises(Error, Delegate, delegated=object())
def test_call(self):
""" Test returning of delegated provider."""
delegated1 = self.delegate()
delegated2 = self.delegate()
self.assertIs(delegated1, self.delegated)
self.assertIs(delegated2, self.delegated)
2015-03-14 01:02:01 +03:00
class NewInstanceTest(unittest.TestCase):
"""NewInstance test cases."""