2015-11-10 20:38:18 +03:00
|
|
|
"""Dependency injector catalogs unittests."""
|
2015-03-16 16:58:07 +03:00
|
|
|
|
|
|
|
import unittest2 as unittest
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
from dependency_injector import catalogs
|
|
|
|
from dependency_injector import providers
|
|
|
|
from dependency_injector import errors
|
2015-03-16 16:58:07 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
|
|
|
|
class CatalogA(catalogs.DeclarativeCatalog):
|
2015-10-11 15:34:21 +03:00
|
|
|
"""Test catalog A."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
p11 = providers.Provider()
|
|
|
|
p12 = providers.Provider()
|
2015-10-11 15:34:21 +03:00
|
|
|
|
2015-10-07 13:36:28 +03:00
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
class CatalogB(CatalogA):
|
|
|
|
"""Test catalog B."""
|
2015-10-07 13:36:28 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
p21 = providers.Provider()
|
|
|
|
p22 = providers.Provider()
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
|
2015-11-11 22:25:41 +03:00
|
|
|
class CatalogBundleTests(unittest.TestCase):
|
|
|
|
"""Catalog bundle test cases."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Set test environment up."""
|
2015-11-12 19:35:23 +03:00
|
|
|
self.bundle = CatalogB.Bundle(CatalogB.p11,
|
|
|
|
CatalogB.p12)
|
2015-11-11 22:25:41 +03:00
|
|
|
|
|
|
|
def test_get_attr_from_bundle(self):
|
|
|
|
"""Test get providers (attribute) from catalog bundle."""
|
2015-11-12 19:35:23 +03:00
|
|
|
self.assertIs(self.bundle.p11, CatalogA.p11)
|
|
|
|
self.assertIs(self.bundle.p12, CatalogA.p12)
|
2015-11-11 22:25:41 +03:00
|
|
|
|
|
|
|
def test_get_attr_not_from_bundle(self):
|
|
|
|
"""Test get providers (attribute) that are not in bundle."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertRaises(errors.Error, getattr, self.bundle, 'p21')
|
|
|
|
self.assertRaises(errors.Error, getattr, self.bundle, 'p22')
|
2015-11-11 22:25:41 +03:00
|
|
|
|
|
|
|
def test_get_method_from_bundle(self):
|
|
|
|
"""Test get providers (get() method) from bundle."""
|
2015-11-12 19:35:23 +03:00
|
|
|
self.assertIs(self.bundle.get_provider('p11'), CatalogB.p11)
|
|
|
|
self.assertIs(self.bundle.get_provider('p12'), CatalogB.p12)
|
2015-11-11 22:25:41 +03:00
|
|
|
|
|
|
|
def test_get_method_not_from_bundle(self):
|
|
|
|
"""Test get providers (get() method) that are not in bundle."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertRaises(errors.Error, self.bundle.get_provider, 'p21')
|
|
|
|
self.assertRaises(errors.Error, self.bundle.get_provider, 'p22')
|
2015-11-11 22:25:41 +03:00
|
|
|
|
|
|
|
def test_has(self):
|
|
|
|
"""Test checks of providers availability in bundle."""
|
|
|
|
self.assertTrue(self.bundle.has_provider('p11'))
|
|
|
|
self.assertTrue(self.bundle.has_provider('p12'))
|
|
|
|
|
|
|
|
self.assertFalse(self.bundle.has_provider('p21'))
|
|
|
|
self.assertFalse(self.bundle.has_provider('p22'))
|
|
|
|
|
|
|
|
def test_hasattr(self):
|
|
|
|
"""Test checks of providers availability in bundle."""
|
|
|
|
self.assertTrue(hasattr(self.bundle, 'p11'))
|
|
|
|
self.assertTrue(hasattr(self.bundle, 'p12'))
|
|
|
|
|
|
|
|
self.assertFalse(hasattr(self.bundle, 'p21'))
|
|
|
|
self.assertFalse(hasattr(self.bundle, 'p22'))
|
|
|
|
|
|
|
|
def test_create_bundle_with_unbound_provider(self):
|
|
|
|
"""Test that bundle is not created with unbound provider."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertRaises(errors.Error, CatalogB.Bundle, providers.Provider())
|
2015-11-11 22:25:41 +03:00
|
|
|
|
|
|
|
def test_create_bundle_with_another_catalog_provider(self):
|
|
|
|
"""Test that bundle can not contain another catalog's provider."""
|
2015-11-23 22:45:58 +03:00
|
|
|
class TestCatalog(catalogs.DeclarativeCatalog):
|
2015-11-11 22:25:41 +03:00
|
|
|
"""Test catalog."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
provider = providers.Provider()
|
2015-11-11 22:25:41 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertRaises(errors.Error,
|
2015-11-12 19:35:23 +03:00
|
|
|
CatalogB.Bundle, CatalogB.p21, TestCatalog.provider)
|
2015-11-11 22:25:41 +03:00
|
|
|
|
|
|
|
def test_create_bundle_with_another_catalog_provider_with_same_name(self):
|
|
|
|
"""Test that bundle can not contain another catalog's provider."""
|
2015-11-23 22:45:58 +03:00
|
|
|
class TestCatalog(catalogs.DeclarativeCatalog):
|
2015-11-11 22:25:41 +03:00
|
|
|
"""Test catalog."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
p21 = providers.Provider()
|
2015-11-11 22:25:41 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertRaises(errors.Error,
|
2015-11-12 19:35:23 +03:00
|
|
|
CatalogB.Bundle, CatalogB.p21, TestCatalog.p21)
|
2015-11-11 22:25:41 +03:00
|
|
|
|
|
|
|
def test_is_bundle_owner(self):
|
|
|
|
"""Test that catalog bundle is owned by catalog."""
|
2015-11-12 19:35:23 +03:00
|
|
|
self.assertTrue(CatalogB.is_bundle_owner(self.bundle))
|
2015-11-11 22:25:41 +03:00
|
|
|
self.assertFalse(CatalogA.is_bundle_owner(self.bundle))
|
|
|
|
|
|
|
|
def test_is_bundle_owner_with_not_bundle_instance(self):
|
|
|
|
"""Test that check of bundle ownership raises error with not bundle."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertRaises(errors.Error, CatalogB.is_bundle_owner, object())
|
2015-11-12 19:35:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
class DynamicCatalogTests(unittest.TestCase):
|
|
|
|
"""Dynamic catalog tests."""
|
|
|
|
|
|
|
|
catalog = None
|
|
|
|
""":type: di.DynamicCatalog"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Set test environment up."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.catalog = catalogs.DynamicCatalog(p1=providers.Provider(),
|
|
|
|
p2=providers.Provider())
|
2015-11-12 19:35:23 +03:00
|
|
|
self.catalog.name = 'TestCatalog'
|
|
|
|
|
|
|
|
def test_providers(self):
|
|
|
|
"""Test `di.DeclarativeCatalog.inherited_providers` contents."""
|
|
|
|
self.assertDictEqual(self.catalog.providers,
|
|
|
|
dict(p1=self.catalog.p1,
|
|
|
|
p2=self.catalog.p2))
|
|
|
|
|
|
|
|
def test_bind_provider(self):
|
|
|
|
"""Test setting of provider via bind_provider() to catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
px = providers.Provider()
|
|
|
|
py = providers.Provider()
|
2015-11-12 19:35:23 +03:00
|
|
|
|
|
|
|
self.catalog.bind_provider('px', px)
|
|
|
|
self.catalog.bind_provider('py', py)
|
|
|
|
|
|
|
|
self.assertIs(self.catalog.px, px)
|
|
|
|
self.assertIs(self.catalog.get_provider('px'), px)
|
|
|
|
|
|
|
|
self.assertIs(self.catalog.py, py)
|
|
|
|
self.assertIs(self.catalog.get_provider('py'), py)
|
|
|
|
|
2015-12-14 12:22:55 +03:00
|
|
|
def test_bind_provider_with_valid_provided_type(self):
|
|
|
|
"""Test setting of provider with provider type restriction."""
|
|
|
|
class SomeProvider(providers.Provider):
|
|
|
|
"""Some provider."""
|
|
|
|
|
|
|
|
class SomeCatalog(catalogs.DynamicCatalog):
|
|
|
|
"""Some catalog with provider type restriction."""
|
|
|
|
|
|
|
|
provider_type = SomeProvider
|
|
|
|
|
|
|
|
px = SomeProvider()
|
|
|
|
py = SomeProvider()
|
|
|
|
catalog = SomeCatalog()
|
|
|
|
|
|
|
|
catalog.bind_provider('px', px)
|
|
|
|
catalog.py = py
|
|
|
|
|
|
|
|
self.assertIs(catalog.px, px)
|
|
|
|
self.assertIs(catalog.get_provider('px'), px)
|
|
|
|
|
|
|
|
self.assertIs(catalog.py, py)
|
|
|
|
self.assertIs(catalog.get_provider('py'), py)
|
|
|
|
|
|
|
|
def test_bind_provider_with_invalid_provided_type(self):
|
|
|
|
"""Test setting of provider with provider type restriction."""
|
|
|
|
class SomeProvider(providers.Provider):
|
|
|
|
"""Some provider."""
|
|
|
|
|
|
|
|
class SomeCatalog(catalogs.DynamicCatalog):
|
|
|
|
"""Some catalog with provider type restriction."""
|
|
|
|
|
|
|
|
provider_type = SomeProvider
|
|
|
|
|
|
|
|
px = providers.Provider()
|
|
|
|
catalog = SomeCatalog()
|
|
|
|
|
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
catalog.bind_provider('px', px)
|
|
|
|
|
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
catalog.px = px
|
|
|
|
|
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
catalog.bind_providers(dict(px=px))
|
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
def test_bind_providers(self):
|
|
|
|
"""Test setting of provider via bind_providers() to catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
px = providers.Provider()
|
|
|
|
py = providers.Provider()
|
2015-11-12 19:35:23 +03:00
|
|
|
|
|
|
|
self.catalog.bind_providers(dict(px=px, py=py))
|
|
|
|
|
|
|
|
self.assertIs(self.catalog.px, px)
|
|
|
|
self.assertIs(self.catalog.get_provider('px'), px)
|
|
|
|
|
|
|
|
self.assertIs(self.catalog.py, py)
|
|
|
|
self.assertIs(self.catalog.get_provider('py'), py)
|
|
|
|
|
|
|
|
def test_setattr(self):
|
|
|
|
"""Test setting of providers via attributes to catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
px = providers.Provider()
|
|
|
|
py = providers.Provider()
|
2015-11-12 19:35:23 +03:00
|
|
|
|
|
|
|
self.catalog.px = px
|
|
|
|
self.catalog.py = py
|
|
|
|
|
|
|
|
self.assertIs(self.catalog.px, px)
|
|
|
|
self.assertIs(self.catalog.get_provider('px'), px)
|
|
|
|
|
|
|
|
self.assertIs(self.catalog.py, py)
|
|
|
|
self.assertIs(self.catalog.get_provider('py'), py)
|
|
|
|
|
|
|
|
def test_unbind_provider(self):
|
|
|
|
"""Test that catalog unbinds provider correct."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.catalog.px = providers.Provider()
|
2015-11-12 19:35:23 +03:00
|
|
|
self.catalog.unbind_provider('px')
|
|
|
|
self.assertFalse(self.catalog.has_provider('px'))
|
|
|
|
|
|
|
|
def test_unbind_via_delattr(self):
|
|
|
|
"""Test that catalog unbinds provider correct."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.catalog.px = providers.Provider()
|
2015-11-12 19:35:23 +03:00
|
|
|
del self.catalog.px
|
|
|
|
self.assertFalse(self.catalog.has_provider('px'))
|
|
|
|
|
|
|
|
def test_provider_is_bound(self):
|
|
|
|
"""Test that providers are bound to the catalogs."""
|
|
|
|
self.assertTrue(self.catalog.is_provider_bound(self.catalog.p1))
|
|
|
|
self.assertEquals(
|
|
|
|
self.catalog.get_provider_bind_name(self.catalog.p1), 'p1')
|
|
|
|
self.assertTrue(self.catalog.is_provider_bound(self.catalog.p2))
|
|
|
|
self.assertEquals(
|
|
|
|
self.catalog.get_provider_bind_name(self.catalog.p2), 'p2')
|
|
|
|
|
|
|
|
def test_provider_binding_to_different_catalogs(self):
|
|
|
|
"""Test that provider could be bound to different catalogs."""
|
|
|
|
p1 = self.catalog.p1
|
|
|
|
p2 = self.catalog.p2
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
catalog_a = catalogs.DynamicCatalog(pa1=p1, pa2=p2)
|
|
|
|
catalog_b = catalogs.DynamicCatalog(pb1=p1, pb2=p2)
|
2015-11-12 19:35:23 +03:00
|
|
|
|
|
|
|
self.assertTrue(self.catalog.is_provider_bound(p1))
|
|
|
|
self.assertTrue(catalog_a.is_provider_bound(p1))
|
|
|
|
self.assertTrue(catalog_b.is_provider_bound(p1))
|
|
|
|
self.assertEquals(self.catalog.get_provider_bind_name(p1), 'p1')
|
|
|
|
self.assertEquals(catalog_a.get_provider_bind_name(p1), 'pa1')
|
|
|
|
self.assertEquals(catalog_b.get_provider_bind_name(p1), 'pb1')
|
|
|
|
|
|
|
|
self.assertTrue(self.catalog.is_provider_bound(p2))
|
|
|
|
self.assertTrue(catalog_a.is_provider_bound(p2))
|
|
|
|
self.assertTrue(catalog_b.is_provider_bound(p2))
|
|
|
|
self.assertEquals(self.catalog.get_provider_bind_name(p2), 'p2')
|
|
|
|
self.assertEquals(catalog_a.get_provider_bind_name(p2), 'pa2')
|
|
|
|
self.assertEquals(catalog_b.get_provider_bind_name(p2), 'pb2')
|
|
|
|
|
|
|
|
def test_provider_rebinding_to_the_same_catalog(self):
|
|
|
|
"""Test provider rebinding to the same catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
with self.assertRaises(errors.Error):
|
2015-11-12 19:35:23 +03:00
|
|
|
self.catalog.p3 = self.catalog.p1
|
|
|
|
|
|
|
|
def test_provider_binding_with_the_same_name(self):
|
|
|
|
"""Test binding of provider with the same name."""
|
2015-11-23 22:45:58 +03:00
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
self.catalog.bind_provider('p1', providers.Provider())
|
2015-11-12 19:35:23 +03:00
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
"""Test getting of providers using get() method."""
|
|
|
|
self.assertIs(self.catalog.get_provider('p1'), self.catalog.p1)
|
|
|
|
self.assertIs(self.catalog.get_provider('p2'), self.catalog.p2)
|
|
|
|
|
|
|
|
def test_get_undefined(self):
|
|
|
|
"""Test getting of undefined providers using get() method."""
|
2015-11-23 22:45:58 +03:00
|
|
|
with self.assertRaises(errors.UndefinedProviderError):
|
2015-11-12 19:35:23 +03:00
|
|
|
self.catalog.get_provider('undefined')
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
with self.assertRaises(errors.UndefinedProviderError):
|
2015-11-12 19:35:23 +03:00
|
|
|
self.catalog.undefined
|
|
|
|
|
|
|
|
def test_has_provider(self):
|
|
|
|
"""Test checks of providers availability in catalog."""
|
|
|
|
self.assertTrue(self.catalog.has_provider('p1'))
|
|
|
|
self.assertTrue(self.catalog.has_provider('p2'))
|
|
|
|
self.assertFalse(self.catalog.has_provider('undefined'))
|
|
|
|
|
|
|
|
def test_filter_all_providers_by_type(self):
|
|
|
|
"""Test getting of all catalog providers of specific type."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertTrue(len(self.catalog.filter(providers.Provider)) == 2)
|
|
|
|
self.assertTrue(len(self.catalog.filter(providers.Value)) == 0)
|
2015-11-12 19:35:23 +03:00
|
|
|
|
|
|
|
def test_repr(self):
|
|
|
|
"""Test catalog representation."""
|
|
|
|
self.assertIn('TestCatalog', repr(self.catalog))
|
|
|
|
self.assertIn('p1', repr(self.catalog))
|
|
|
|
self.assertIn('p2', repr(self.catalog))
|
2015-11-11 22:25:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
class DeclarativeCatalogTests(unittest.TestCase):
|
|
|
|
"""Declarative catalog tests."""
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
def test_cls_providers(self):
|
2015-11-10 11:42:29 +03:00
|
|
|
"""Test `di.DeclarativeCatalog.cls_providers` contents."""
|
2015-10-11 15:34:21 +03:00
|
|
|
self.assertDictEqual(CatalogA.cls_providers,
|
|
|
|
dict(p11=CatalogA.p11,
|
|
|
|
p12=CatalogA.p12))
|
|
|
|
self.assertDictEqual(CatalogB.cls_providers,
|
|
|
|
dict(p21=CatalogB.p21,
|
|
|
|
p22=CatalogB.p22))
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
def test_inherited_providers(self):
|
2015-11-10 11:42:29 +03:00
|
|
|
"""Test `di.DeclarativeCatalog.inherited_providers` contents."""
|
2015-10-11 15:34:21 +03:00
|
|
|
self.assertDictEqual(CatalogA.inherited_providers, dict())
|
|
|
|
self.assertDictEqual(CatalogB.inherited_providers,
|
|
|
|
dict(p11=CatalogA.p11,
|
|
|
|
p12=CatalogA.p12))
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
def test_providers(self):
|
2015-11-10 11:42:29 +03:00
|
|
|
"""Test `di.DeclarativeCatalog.inherited_providers` contents."""
|
2015-10-11 15:34:21 +03:00
|
|
|
self.assertDictEqual(CatalogA.providers,
|
|
|
|
dict(p11=CatalogA.p11,
|
|
|
|
p12=CatalogA.p12))
|
|
|
|
self.assertDictEqual(CatalogB.providers,
|
|
|
|
dict(p11=CatalogA.p11,
|
|
|
|
p12=CatalogA.p12,
|
|
|
|
p21=CatalogB.p21,
|
|
|
|
p22=CatalogB.p22))
|
|
|
|
|
2015-11-12 17:56:00 +03:00
|
|
|
def test_bind_provider(self):
|
|
|
|
"""Test setting of provider via bind_provider() to catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
px = providers.Provider()
|
|
|
|
py = providers.Provider()
|
2015-11-12 17:56:00 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
CatalogA.bind_provider('px', px)
|
|
|
|
CatalogA.bind_provider('py', py)
|
2015-11-12 17:56:00 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
self.assertIs(CatalogA.px, px)
|
|
|
|
self.assertIs(CatalogA.get_provider('px'), px)
|
2015-11-12 17:56:00 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
self.assertIs(CatalogA.py, py)
|
|
|
|
self.assertIs(CatalogA.get_provider('py'), py)
|
2015-11-12 17:56:00 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
del CatalogA.px
|
|
|
|
del CatalogA.py
|
2015-11-12 17:56:00 +03:00
|
|
|
|
2015-12-14 12:22:55 +03:00
|
|
|
def test_bind_provider_with_valid_provided_type(self):
|
|
|
|
"""Test setting of provider with provider type restriction."""
|
|
|
|
class SomeProvider(providers.Provider):
|
|
|
|
"""Some provider."""
|
|
|
|
|
|
|
|
class SomeCatalog(catalogs.DeclarativeCatalog):
|
|
|
|
"""Some catalog with provider type restriction."""
|
|
|
|
|
|
|
|
provider_type = SomeProvider
|
|
|
|
|
|
|
|
px = SomeProvider()
|
|
|
|
py = SomeProvider()
|
|
|
|
|
|
|
|
SomeCatalog.bind_provider('px', px)
|
|
|
|
SomeCatalog.py = py
|
|
|
|
|
|
|
|
self.assertIs(SomeCatalog.px, px)
|
|
|
|
self.assertIs(SomeCatalog.get_provider('px'), px)
|
|
|
|
|
|
|
|
self.assertIs(SomeCatalog.py, py)
|
|
|
|
self.assertIs(SomeCatalog.get_provider('py'), py)
|
|
|
|
|
|
|
|
def test_bind_provider_with_invalid_provided_type(self):
|
|
|
|
"""Test setting of provider with provider type restriction."""
|
|
|
|
class SomeProvider(providers.Provider):
|
|
|
|
"""Some provider."""
|
|
|
|
|
|
|
|
class SomeCatalog(catalogs.DeclarativeCatalog):
|
|
|
|
"""Some catalog with provider type restriction."""
|
|
|
|
|
|
|
|
provider_type = SomeProvider
|
|
|
|
|
|
|
|
px = providers.Provider()
|
|
|
|
|
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
SomeCatalog.bind_provider('px', px)
|
|
|
|
|
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
SomeCatalog.px = px
|
|
|
|
|
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
SomeCatalog.bind_providers(dict(px=px))
|
|
|
|
|
2015-11-12 17:56:00 +03:00
|
|
|
def test_bind_providers(self):
|
|
|
|
"""Test setting of provider via bind_providers() to catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
px = providers.Provider()
|
|
|
|
py = providers.Provider()
|
2015-11-12 17:56:00 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
CatalogB.bind_providers(dict(px=px, py=py))
|
2015-11-12 17:56:00 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
self.assertIs(CatalogB.px, px)
|
|
|
|
self.assertIs(CatalogB.get_provider('px'), px)
|
2015-11-12 17:56:00 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
self.assertIs(CatalogB.py, py)
|
|
|
|
self.assertIs(CatalogB.get_provider('py'), py)
|
2015-11-12 17:56:00 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
del CatalogB.px
|
|
|
|
del CatalogB.py
|
2015-11-12 17:56:00 +03:00
|
|
|
|
2015-11-12 14:43:54 +03:00
|
|
|
def test_setattr(self):
|
2015-11-12 17:56:00 +03:00
|
|
|
"""Test setting of providers via attributes to catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
px = providers.Provider()
|
|
|
|
py = providers.Provider()
|
2015-11-12 14:43:54 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
CatalogB.px = px
|
|
|
|
CatalogB.py = py
|
2015-11-12 14:43:54 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
self.assertIs(CatalogB.px, px)
|
|
|
|
self.assertIs(CatalogB.get_provider('px'), px)
|
2015-11-12 14:43:54 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
self.assertIs(CatalogB.py, py)
|
|
|
|
self.assertIs(CatalogB.get_provider('py'), py)
|
2015-11-12 14:43:54 +03:00
|
|
|
|
2015-11-12 19:35:23 +03:00
|
|
|
del CatalogB.px
|
|
|
|
del CatalogB.py
|
2015-11-12 17:56:00 +03:00
|
|
|
|
|
|
|
def test_unbind_provider(self):
|
|
|
|
"""Test that catalog unbinds provider correct."""
|
2015-11-23 22:45:58 +03:00
|
|
|
CatalogB.px = providers.Provider()
|
2015-11-12 19:35:23 +03:00
|
|
|
CatalogB.unbind_provider('px')
|
|
|
|
self.assertFalse(CatalogB.has_provider('px'))
|
2015-11-12 17:56:00 +03:00
|
|
|
|
|
|
|
def test_unbind_via_delattr(self):
|
|
|
|
"""Test that catalog unbinds provider correct."""
|
2015-11-23 22:45:58 +03:00
|
|
|
CatalogB.px = providers.Provider()
|
2015-11-12 19:35:23 +03:00
|
|
|
del CatalogB.px
|
|
|
|
self.assertFalse(CatalogB.has_provider('px'))
|
2015-11-12 17:56:00 +03:00
|
|
|
|
2015-10-17 01:00:32 +03:00
|
|
|
def test_provider_is_bound(self):
|
|
|
|
"""Test that providers are bound to the catalogs."""
|
2015-11-10 16:01:06 +03:00
|
|
|
self.assertTrue(CatalogA.is_provider_bound(CatalogA.p11))
|
|
|
|
self.assertEquals(CatalogA.get_provider_bind_name(CatalogA.p11), 'p11')
|
2015-10-17 01:00:32 +03:00
|
|
|
|
2015-11-10 16:01:06 +03:00
|
|
|
self.assertTrue(CatalogA.is_provider_bound(CatalogA.p12))
|
|
|
|
self.assertEquals(CatalogA.get_provider_bind_name(CatalogA.p12), 'p12')
|
2015-10-17 01:00:32 +03:00
|
|
|
|
2015-11-10 16:01:06 +03:00
|
|
|
def test_provider_binding_to_different_catalogs(self):
|
|
|
|
"""Test that provider could be bound to different catalogs."""
|
|
|
|
p11 = CatalogA.p11
|
|
|
|
p12 = CatalogA.p12
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
class CatalogD(catalogs.DeclarativeCatalog):
|
2015-11-10 16:01:06 +03:00
|
|
|
"""Test catalog."""
|
|
|
|
|
|
|
|
pd1 = p11
|
|
|
|
pd2 = p12
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
class CatalogE(catalogs.DeclarativeCatalog):
|
2015-11-10 16:01:06 +03:00
|
|
|
"""Test catalog."""
|
|
|
|
|
|
|
|
pe1 = p11
|
|
|
|
pe2 = p12
|
|
|
|
|
|
|
|
self.assertTrue(CatalogA.is_provider_bound(p11))
|
|
|
|
self.assertTrue(CatalogD.is_provider_bound(p11))
|
|
|
|
self.assertTrue(CatalogE.is_provider_bound(p11))
|
|
|
|
self.assertEquals(CatalogA.get_provider_bind_name(p11), 'p11')
|
|
|
|
self.assertEquals(CatalogD.get_provider_bind_name(p11), 'pd1')
|
|
|
|
self.assertEquals(CatalogE.get_provider_bind_name(p11), 'pe1')
|
|
|
|
|
|
|
|
self.assertTrue(CatalogA.is_provider_bound(p12))
|
|
|
|
self.assertTrue(CatalogD.is_provider_bound(p12))
|
|
|
|
self.assertTrue(CatalogE.is_provider_bound(p12))
|
|
|
|
self.assertEquals(CatalogA.get_provider_bind_name(p12), 'p12')
|
|
|
|
self.assertEquals(CatalogD.get_provider_bind_name(p12), 'pd2')
|
|
|
|
self.assertEquals(CatalogE.get_provider_bind_name(p12), 'pe2')
|
|
|
|
|
|
|
|
def test_provider_rebinding_to_the_same_catalog(self):
|
|
|
|
"""Test provider rebinding to the same catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
class TestCatalog(catalogs.DeclarativeCatalog):
|
2015-11-10 16:01:06 +03:00
|
|
|
"""Test catalog."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
p1 = providers.Provider()
|
2015-11-10 16:01:06 +03:00
|
|
|
p2 = p1
|
|
|
|
|
|
|
|
def test_provider_rebinding_to_the_same_catalogs_hierarchy(self):
|
|
|
|
"""Test provider rebinding to the same catalogs hierarchy."""
|
2015-11-23 22:45:58 +03:00
|
|
|
class TestCatalog1(catalogs.DeclarativeCatalog):
|
2015-11-10 16:01:06 +03:00
|
|
|
"""Test catalog."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
p1 = providers.Provider()
|
2015-11-10 16:01:06 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
with self.assertRaises(errors.Error):
|
2015-11-10 16:01:06 +03:00
|
|
|
class TestCatalog2(TestCatalog1):
|
|
|
|
"""Test catalog."""
|
|
|
|
|
|
|
|
p2 = TestCatalog1.p1
|
2015-10-17 01:00:32 +03:00
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
def test_get(self):
|
|
|
|
"""Test getting of providers using get() method."""
|
2015-11-12 19:35:23 +03:00
|
|
|
self.assertIs(CatalogB.get('p11'), CatalogB.p11)
|
|
|
|
self.assertIs(CatalogB.get('p12'), CatalogB.p12)
|
|
|
|
self.assertIs(CatalogB.get('p22'), CatalogB.p22)
|
|
|
|
self.assertIs(CatalogB.get('p22'), CatalogB.p22)
|
|
|
|
|
|
|
|
self.assertIs(CatalogB.get_provider('p11'), CatalogB.p11)
|
|
|
|
self.assertIs(CatalogB.get_provider('p12'), CatalogB.p12)
|
|
|
|
self.assertIs(CatalogB.get_provider('p22'), CatalogB.p22)
|
|
|
|
self.assertIs(CatalogB.get_provider('p22'), CatalogB.p22)
|
2015-11-12 14:43:54 +03:00
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
def test_get_undefined(self):
|
|
|
|
"""Test getting of undefined providers using get() method."""
|
2015-11-23 22:45:58 +03:00
|
|
|
with self.assertRaises(errors.UndefinedProviderError):
|
2015-11-12 19:35:23 +03:00
|
|
|
CatalogB.get('undefined')
|
2015-11-12 14:43:54 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
with self.assertRaises(errors.UndefinedProviderError):
|
2015-11-12 19:35:23 +03:00
|
|
|
CatalogB.get_provider('undefined')
|
2015-11-12 14:43:54 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
with self.assertRaises(errors.UndefinedProviderError):
|
2015-11-12 19:35:23 +03:00
|
|
|
CatalogB.undefined
|
2015-10-11 15:34:21 +03:00
|
|
|
|
|
|
|
def test_has(self):
|
2015-10-15 20:15:38 +03:00
|
|
|
"""Test checks of providers availability in catalog."""
|
2015-11-12 19:35:23 +03:00
|
|
|
self.assertTrue(CatalogB.has('p11'))
|
|
|
|
self.assertTrue(CatalogB.has('p12'))
|
|
|
|
self.assertTrue(CatalogB.has('p21'))
|
|
|
|
self.assertTrue(CatalogB.has('p22'))
|
|
|
|
self.assertFalse(CatalogB.has('undefined'))
|
|
|
|
|
|
|
|
self.assertTrue(CatalogB.has_provider('p11'))
|
|
|
|
self.assertTrue(CatalogB.has_provider('p12'))
|
|
|
|
self.assertTrue(CatalogB.has_provider('p21'))
|
|
|
|
self.assertTrue(CatalogB.has_provider('p22'))
|
|
|
|
self.assertFalse(CatalogB.has_provider('undefined'))
|
2015-11-12 14:43:54 +03:00
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
def test_filter_all_providers_by_type(self):
|
2015-03-16 16:58:07 +03:00
|
|
|
"""Test getting of all catalog providers of specific type."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertTrue(len(CatalogB.filter(providers.Provider)) == 4)
|
|
|
|
self.assertTrue(len(CatalogB.filter(providers.Value)) == 0)
|
2015-07-17 19:31:44 +03:00
|
|
|
|
2015-11-11 22:25:41 +03:00
|
|
|
def test_repr(self):
|
2015-11-12 19:35:23 +03:00
|
|
|
"""Test catalog representation."""
|
2015-11-12 02:00:27 +03:00
|
|
|
self.assertIn('CatalogA', repr(CatalogA))
|
|
|
|
self.assertIn('p11', repr(CatalogA))
|
|
|
|
self.assertIn('p12', repr(CatalogA))
|
|
|
|
|
|
|
|
self.assertIn('CatalogB', repr(CatalogB))
|
|
|
|
self.assertIn('p11', repr(CatalogB))
|
|
|
|
self.assertIn('p12', repr(CatalogB))
|
|
|
|
self.assertIn('p21', repr(CatalogB))
|
|
|
|
self.assertIn('p22', repr(CatalogB))
|
|
|
|
|
2015-11-11 22:25:41 +03:00
|
|
|
def test_abstract_catalog_backward_compatibility(self):
|
|
|
|
"""Test that di.AbstractCatalog is available."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertIs(catalogs.DeclarativeCatalog, catalogs.AbstractCatalog)
|
2015-08-03 12:57:42 +03:00
|
|
|
|
|
|
|
|
2015-11-11 22:25:41 +03:00
|
|
|
class OverrideTests(unittest.TestCase):
|
|
|
|
"""Catalog overriding and override decorator test cases."""
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2015-10-23 16:23:12 +03:00
|
|
|
def tearDown(self):
|
|
|
|
"""Tear test environment down."""
|
2015-11-11 22:25:41 +03:00
|
|
|
CatalogA.reset_override()
|
2015-10-23 16:23:12 +03:00
|
|
|
|
2015-08-03 12:57:42 +03:00
|
|
|
def test_overriding(self):
|
|
|
|
"""Test catalog overriding with another catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
@catalogs.override(CatalogA)
|
|
|
|
class OverridingCatalog(catalogs.DeclarativeCatalog):
|
2015-08-03 12:57:42 +03:00
|
|
|
"""Overriding catalog."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
p11 = providers.Value(1)
|
|
|
|
p12 = providers.Value(2)
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2015-11-11 22:25:41 +03:00
|
|
|
self.assertEqual(CatalogA.p11(), 1)
|
|
|
|
self.assertEqual(CatalogA.p12(), 2)
|
|
|
|
self.assertEqual(len(CatalogA.overridden_by), 1)
|
2015-11-10 18:58:04 +03:00
|
|
|
|
2015-11-26 16:11:46 +03:00
|
|
|
def test_override_declarative_catalog_with_itself(self):
|
|
|
|
"""Test catalog overriding of declarative catalog with itself."""
|
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
CatalogA.override(CatalogA)
|
|
|
|
|
|
|
|
def test_override_declarative_catalog_with_subclass(self):
|
|
|
|
"""Test catalog overriding of declarative catalog with subclass."""
|
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
CatalogB.override(CatalogA)
|
|
|
|
|
|
|
|
def test_override_dynamic_catalog_with_itself(self):
|
|
|
|
"""Test catalog overriding of dynamic catalog with itself."""
|
|
|
|
catalog = catalogs.DynamicCatalog(p11=providers.Value(1),
|
|
|
|
p12=providers.Value(2))
|
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
catalog.override(catalog)
|
|
|
|
|
2015-11-10 18:58:04 +03:00
|
|
|
def test_overriding_with_dynamic_catalog(self):
|
|
|
|
"""Test catalog overriding with another dynamic catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
CatalogA.override(catalogs.DynamicCatalog(p11=providers.Value(1),
|
|
|
|
p12=providers.Value(2)))
|
2015-11-11 22:25:41 +03:00
|
|
|
self.assertEqual(CatalogA.p11(), 1)
|
|
|
|
self.assertEqual(CatalogA.p12(), 2)
|
|
|
|
self.assertEqual(len(CatalogA.overridden_by), 1)
|
2015-10-23 16:23:12 +03:00
|
|
|
|
|
|
|
def test_is_overridden(self):
|
|
|
|
"""Test catalog is_overridden property."""
|
2015-11-11 22:25:41 +03:00
|
|
|
self.assertFalse(CatalogA.is_overridden)
|
2015-10-23 16:23:12 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@catalogs.override(CatalogA)
|
|
|
|
class OverridingCatalog(catalogs.DeclarativeCatalog):
|
2015-10-23 16:23:12 +03:00
|
|
|
"""Overriding catalog."""
|
|
|
|
|
2015-11-11 22:25:41 +03:00
|
|
|
self.assertTrue(CatalogA.is_overridden)
|
2015-10-23 16:23:12 +03:00
|
|
|
|
|
|
|
def test_last_overriding(self):
|
|
|
|
"""Test catalog last_overriding property."""
|
2015-11-23 22:45:58 +03:00
|
|
|
@catalogs.override(CatalogA)
|
|
|
|
class OverridingCatalog1(catalogs.DeclarativeCatalog):
|
2015-10-23 16:23:12 +03:00
|
|
|
"""Overriding catalog."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@catalogs.override(CatalogA)
|
|
|
|
class OverridingCatalog2(catalogs.DeclarativeCatalog):
|
2015-10-23 16:23:12 +03:00
|
|
|
"""Overriding catalog."""
|
|
|
|
|
2015-11-11 22:25:41 +03:00
|
|
|
self.assertIs(CatalogA.last_overriding, OverridingCatalog2)
|
2015-10-23 16:23:12 +03:00
|
|
|
|
|
|
|
def test_last_overriding_on_not_overridden(self):
|
|
|
|
"""Test catalog last_overriding property on not overridden catalog."""
|
2015-11-25 14:39:50 +03:00
|
|
|
self.assertIsNone(CatalogA.last_overriding)
|
2015-10-23 16:23:12 +03:00
|
|
|
|
|
|
|
def test_reset_last_overriding(self):
|
|
|
|
"""Test resetting last overriding catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
@catalogs.override(CatalogA)
|
|
|
|
class OverridingCatalog1(catalogs.DeclarativeCatalog):
|
2015-10-23 16:23:12 +03:00
|
|
|
"""Overriding catalog."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
p11 = providers.Value(1)
|
|
|
|
p12 = providers.Value(2)
|
2015-10-23 16:23:12 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@catalogs.override(CatalogA)
|
|
|
|
class OverridingCatalog2(catalogs.DeclarativeCatalog):
|
2015-10-23 16:23:12 +03:00
|
|
|
"""Overriding catalog."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
p11 = providers.Value(3)
|
|
|
|
p12 = providers.Value(4)
|
2015-10-23 16:23:12 +03:00
|
|
|
|
2015-11-11 22:25:41 +03:00
|
|
|
CatalogA.reset_last_overriding()
|
2015-10-23 16:23:12 +03:00
|
|
|
|
2015-11-11 22:25:41 +03:00
|
|
|
self.assertEqual(CatalogA.p11(), 1)
|
|
|
|
self.assertEqual(CatalogA.p12(), 2)
|
2015-10-23 16:23:12 +03:00
|
|
|
|
|
|
|
def test_reset_last_overriding_when_not_overridden(self):
|
|
|
|
"""Test resetting last overriding catalog when it is not overridden."""
|
2015-11-23 22:45:58 +03:00
|
|
|
with self.assertRaises(errors.Error):
|
2015-11-11 22:25:41 +03:00
|
|
|
CatalogA.reset_last_overriding()
|
2015-10-23 16:23:12 +03:00
|
|
|
|
|
|
|
def test_reset_override(self):
|
|
|
|
"""Test resetting all catalog overrides."""
|
2015-11-23 22:45:58 +03:00
|
|
|
@catalogs.override(CatalogA)
|
|
|
|
class OverridingCatalog1(catalogs.DeclarativeCatalog):
|
2015-10-23 16:23:12 +03:00
|
|
|
"""Overriding catalog."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
p11 = providers.Value(1)
|
|
|
|
p12 = providers.Value(2)
|
2015-10-23 16:23:12 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@catalogs.override(CatalogA)
|
|
|
|
class OverridingCatalog2(catalogs.DeclarativeCatalog):
|
2015-10-23 16:23:12 +03:00
|
|
|
"""Overriding catalog."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
p11 = providers.Value(3)
|
|
|
|
p12 = providers.Value(4)
|
2015-10-23 16:23:12 +03:00
|
|
|
|
2015-11-11 22:25:41 +03:00
|
|
|
CatalogA.reset_override()
|
2015-11-10 11:42:29 +03:00
|
|
|
|
2015-11-11 22:25:41 +03:00
|
|
|
self.assertFalse(CatalogA.p11.is_overridden)
|
|
|
|
self.assertFalse(CatalogA.p12.is_overridden)
|
2015-11-23 21:57:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CatalogModuleBackwardCompatibility(unittest.TestCase):
|
|
|
|
"""Backward compatibility test of catalog module."""
|
|
|
|
|
|
|
|
def test_import_catalog(self):
|
|
|
|
"""Test that module `catalog` is the same as `catalogs`."""
|
|
|
|
from dependency_injector import catalog
|
|
|
|
from dependency_injector import catalogs
|
|
|
|
|
|
|
|
self.assertIs(catalog, catalogs)
|