Update DeclarativeCatalog and Override tests

This commit is contained in:
Roman Mogilatov 2015-11-11 21:25:41 +02:00
parent ed898f7fe2
commit 3fc5f044f9

View File

@ -25,117 +25,6 @@ class CatalogC(CatalogB):
p32 = di.Provider() p32 = di.Provider()
class CatalogsInheritanceTests(unittest.TestCase):
"""Catalogs inheritance tests."""
def test_cls_providers(self):
"""Test `di.DeclarativeCatalog.cls_providers` contents."""
self.assertDictEqual(CatalogA.cls_providers,
dict(p11=CatalogA.p11,
p12=CatalogA.p12))
self.assertDictEqual(CatalogB.cls_providers,
dict(p21=CatalogB.p21,
p22=CatalogB.p22))
self.assertDictEqual(CatalogC.cls_providers,
dict(p31=CatalogC.p31,
p32=CatalogC.p32))
def test_inherited_providers(self):
"""Test `di.DeclarativeCatalog.inherited_providers` contents."""
self.assertDictEqual(CatalogA.inherited_providers, dict())
self.assertDictEqual(CatalogB.inherited_providers,
dict(p11=CatalogA.p11,
p12=CatalogA.p12))
self.assertDictEqual(CatalogC.inherited_providers,
dict(p11=CatalogA.p11,
p12=CatalogA.p12,
p21=CatalogB.p21,
p22=CatalogB.p22))
def test_providers(self):
"""Test `di.DeclarativeCatalog.inherited_providers` contents."""
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))
self.assertDictEqual(CatalogC.providers,
dict(p11=CatalogA.p11,
p12=CatalogA.p12,
p21=CatalogB.p21,
p22=CatalogB.p22,
p31=CatalogC.p31,
p32=CatalogC.p32))
class CatalogProvidersBindingTests(unittest.TestCase):
"""Catalog providers binding test cases."""
def test_provider_is_bound(self):
"""Test that providers are bound to the catalogs."""
self.assertTrue(CatalogA.is_provider_bound(CatalogA.p11))
self.assertEquals(CatalogA.get_provider_bind_name(CatalogA.p11), 'p11')
self.assertTrue(CatalogA.is_provider_bound(CatalogA.p12))
self.assertEquals(CatalogA.get_provider_bind_name(CatalogA.p12), 'p12')
def test_provider_binding_to_different_catalogs(self):
"""Test that provider could be bound to different catalogs."""
p11 = CatalogA.p11
p12 = CatalogA.p12
class CatalogD(di.DeclarativeCatalog):
"""Test catalog."""
pd1 = p11
pd2 = p12
class CatalogE(di.DeclarativeCatalog):
"""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."""
with self.assertRaises(di.Error):
class TestCatalog(di.DeclarativeCatalog):
"""Test catalog."""
p1 = di.Provider()
p2 = p1
def test_provider_rebinding_to_the_same_catalogs_hierarchy(self):
"""Test provider rebinding to the same catalogs hierarchy."""
class TestCatalog1(di.DeclarativeCatalog):
"""Test catalog."""
p1 = di.Provider()
with self.assertRaises(di.Error):
class TestCatalog2(TestCatalog1):
"""Test catalog."""
p2 = TestCatalog1.p1
class CatalogBundleTests(unittest.TestCase): class CatalogBundleTests(unittest.TestCase):
"""Catalog bundle test cases.""" """Catalog bundle test cases."""
@ -223,8 +112,111 @@ class CatalogBundleTests(unittest.TestCase):
self.assertRaises(di.Error, CatalogC.is_bundle_owner, object()) self.assertRaises(di.Error, CatalogC.is_bundle_owner, object())
class CatalogTests(unittest.TestCase): class DeclarativeCatalogTests(unittest.TestCase):
"""Catalog test cases.""" """Declarative catalog tests."""
def test_cls_providers(self):
"""Test `di.DeclarativeCatalog.cls_providers` contents."""
self.assertDictEqual(CatalogA.cls_providers,
dict(p11=CatalogA.p11,
p12=CatalogA.p12))
self.assertDictEqual(CatalogB.cls_providers,
dict(p21=CatalogB.p21,
p22=CatalogB.p22))
self.assertDictEqual(CatalogC.cls_providers,
dict(p31=CatalogC.p31,
p32=CatalogC.p32))
def test_inherited_providers(self):
"""Test `di.DeclarativeCatalog.inherited_providers` contents."""
self.assertDictEqual(CatalogA.inherited_providers, dict())
self.assertDictEqual(CatalogB.inherited_providers,
dict(p11=CatalogA.p11,
p12=CatalogA.p12))
self.assertDictEqual(CatalogC.inherited_providers,
dict(p11=CatalogA.p11,
p12=CatalogA.p12,
p21=CatalogB.p21,
p22=CatalogB.p22))
def test_providers(self):
"""Test `di.DeclarativeCatalog.inherited_providers` contents."""
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))
self.assertDictEqual(CatalogC.providers,
dict(p11=CatalogA.p11,
p12=CatalogA.p12,
p21=CatalogB.p21,
p22=CatalogB.p22,
p31=CatalogC.p31,
p32=CatalogC.p32))
def test_provider_is_bound(self):
"""Test that providers are bound to the catalogs."""
self.assertTrue(CatalogA.is_provider_bound(CatalogA.p11))
self.assertEquals(CatalogA.get_provider_bind_name(CatalogA.p11), 'p11')
self.assertTrue(CatalogA.is_provider_bound(CatalogA.p12))
self.assertEquals(CatalogA.get_provider_bind_name(CatalogA.p12), 'p12')
def test_provider_binding_to_different_catalogs(self):
"""Test that provider could be bound to different catalogs."""
p11 = CatalogA.p11
p12 = CatalogA.p12
class CatalogD(di.DeclarativeCatalog):
"""Test catalog."""
pd1 = p11
pd2 = p12
class CatalogE(di.DeclarativeCatalog):
"""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."""
with self.assertRaises(di.Error):
class TestCatalog(di.DeclarativeCatalog):
"""Test catalog."""
p1 = di.Provider()
p2 = p1
def test_provider_rebinding_to_the_same_catalogs_hierarchy(self):
"""Test provider rebinding to the same catalogs hierarchy."""
class TestCatalog1(di.DeclarativeCatalog):
"""Test catalog."""
p1 = di.Provider()
with self.assertRaises(di.Error):
class TestCatalog2(TestCatalog1):
"""Test catalog."""
p2 = TestCatalog1.p1
def test_get(self): def test_get(self):
"""Test getting of providers using get() method.""" """Test getting of providers using get() method."""
@ -254,130 +246,112 @@ class CatalogTests(unittest.TestCase):
self.assertTrue(len(CatalogC.filter(di.Provider)) == 6) self.assertTrue(len(CatalogC.filter(di.Provider)) == 6)
self.assertTrue(len(CatalogC.filter(di.Value)) == 0) self.assertTrue(len(CatalogC.filter(di.Value)) == 0)
def test_repr(self):
"""Test declarative catalog representation."""
def test_abstract_catalog_backward_compatibility(self):
"""Test that di.AbstractCatalog is available."""
self.assertIs(di.DeclarativeCatalog, di.AbstractCatalog)
class OverrideTests(unittest.TestCase): class OverrideTests(unittest.TestCase):
"""Catalog overriding and override decorator test cases.""" """Catalog overriding and override decorator test cases."""
class Catalog(di.DeclarativeCatalog):
"""Test catalog."""
obj = di.Object(object())
another_obj = di.Object(object())
def tearDown(self): def tearDown(self):
"""Tear test environment down.""" """Tear test environment down."""
self.Catalog.reset_override() CatalogA.reset_override()
def test_overriding(self): def test_overriding(self):
"""Test catalog overriding with another catalog.""" """Test catalog overriding with another catalog."""
@di.override(self.Catalog) @di.override(CatalogA)
class OverridingCatalog(di.DeclarativeCatalog): class OverridingCatalog(di.DeclarativeCatalog):
"""Overriding catalog.""" """Overriding catalog."""
obj = di.Value(1) p11 = di.Value(1)
another_obj = di.Value(2) p12 = di.Value(2)
self.assertEqual(self.Catalog.obj(), 1) self.assertEqual(CatalogA.p11(), 1)
self.assertEqual(self.Catalog.another_obj(), 2) self.assertEqual(CatalogA.p12(), 2)
self.assertEqual(len(self.Catalog.overridden_by), 1) self.assertEqual(len(CatalogA.overridden_by), 1)
def test_overriding_with_dynamic_catalog(self): def test_overriding_with_dynamic_catalog(self):
"""Test catalog overriding with another dynamic catalog.""" """Test catalog overriding with another dynamic catalog."""
self.Catalog.override(di.DynamicCatalog(obj=di.Value(1), CatalogA.override(di.DynamicCatalog(p11=di.Value(1),
another_obj=di.Value(2))) p12=di.Value(2)))
self.assertEqual(self.Catalog.obj(), 1) self.assertEqual(CatalogA.p11(), 1)
self.assertEqual(self.Catalog.another_obj(), 2) self.assertEqual(CatalogA.p12(), 2)
self.assertEqual(len(self.Catalog.overridden_by), 1) self.assertEqual(len(CatalogA.overridden_by), 1)
def test_is_overridden(self): def test_is_overridden(self):
"""Test catalog is_overridden property.""" """Test catalog is_overridden property."""
self.assertFalse(self.Catalog.is_overridden) self.assertFalse(CatalogA.is_overridden)
@di.override(self.Catalog) @di.override(CatalogA)
class OverridingCatalog(di.DeclarativeCatalog): class OverridingCatalog(di.DeclarativeCatalog):
"""Overriding catalog.""" """Overriding catalog."""
self.assertTrue(self.Catalog.is_overridden) self.assertTrue(CatalogA.is_overridden)
def test_last_overriding(self): def test_last_overriding(self):
"""Test catalog last_overriding property.""" """Test catalog last_overriding property."""
@di.override(self.Catalog) @di.override(CatalogA)
class OverridingCatalog1(di.DeclarativeCatalog): class OverridingCatalog1(di.DeclarativeCatalog):
"""Overriding catalog.""" """Overriding catalog."""
@di.override(self.Catalog) @di.override(CatalogA)
class OverridingCatalog2(di.DeclarativeCatalog): class OverridingCatalog2(di.DeclarativeCatalog):
"""Overriding catalog.""" """Overriding catalog."""
self.assertIs(self.Catalog.last_overriding, OverridingCatalog2) self.assertIs(CatalogA.last_overriding, OverridingCatalog2)
def test_last_overriding_on_not_overridden(self): def test_last_overriding_on_not_overridden(self):
"""Test catalog last_overriding property on not overridden catalog.""" """Test catalog last_overriding property on not overridden catalog."""
with self.assertRaises(di.Error): with self.assertRaises(di.Error):
self.Catalog.last_overriding CatalogA.last_overriding
def test_reset_last_overriding(self): def test_reset_last_overriding(self):
"""Test resetting last overriding catalog.""" """Test resetting last overriding catalog."""
@di.override(self.Catalog) @di.override(CatalogA)
class OverridingCatalog1(di.DeclarativeCatalog): class OverridingCatalog1(di.DeclarativeCatalog):
"""Overriding catalog.""" """Overriding catalog."""
obj = di.Value(1) p11 = di.Value(1)
another_obj = di.Value(2) p12 = di.Value(2)
@di.override(self.Catalog) @di.override(CatalogA)
class OverridingCatalog2(di.DeclarativeCatalog): class OverridingCatalog2(di.DeclarativeCatalog):
"""Overriding catalog.""" """Overriding catalog."""
obj = di.Value(3) p11 = di.Value(3)
another_obj = di.Value(4) p12 = di.Value(4)
self.Catalog.reset_last_overriding() CatalogA.reset_last_overriding()
self.assertEqual(self.Catalog.obj(), 1) self.assertEqual(CatalogA.p11(), 1)
self.assertEqual(self.Catalog.another_obj(), 2) self.assertEqual(CatalogA.p12(), 2)
def test_reset_last_overriding_when_not_overridden(self): def test_reset_last_overriding_when_not_overridden(self):
"""Test resetting last overriding catalog when it is not overridden.""" """Test resetting last overriding catalog when it is not overridden."""
with self.assertRaises(di.Error): with self.assertRaises(di.Error):
self.Catalog.reset_last_overriding() CatalogA.reset_last_overriding()
def test_reset_override(self): def test_reset_override(self):
"""Test resetting all catalog overrides.""" """Test resetting all catalog overrides."""
@di.override(self.Catalog) @di.override(CatalogA)
class OverridingCatalog1(di.DeclarativeCatalog): class OverridingCatalog1(di.DeclarativeCatalog):
"""Overriding catalog.""" """Overriding catalog."""
obj = di.Value(1) p11 = di.Value(1)
another_obj = di.Value(2) p12 = di.Value(2)
@di.override(self.Catalog) @di.override(CatalogA)
class OverridingCatalog2(di.DeclarativeCatalog): class OverridingCatalog2(di.DeclarativeCatalog):
"""Overriding catalog.""" """Overriding catalog."""
obj = di.Value(3) p11 = di.Value(3)
another_obj = di.Value(4) p12 = di.Value(4)
self.Catalog.reset_override() CatalogA.reset_override()
self.assertIsInstance(self.Catalog.obj(), object) self.assertFalse(CatalogA.p11.is_overridden)
self.assertIsInstance(self.Catalog.another_obj(), object) self.assertFalse(CatalogA.p12.is_overridden)
class DeclarativeCatalogReprTest(unittest.TestCase):
"""Tests for declarative catalog representation."""
def test_repr(self):
"""Test declarative catalog representation."""
class TestCatalog(di.DeclarativeCatalog):
"""Test catalog."""
self.assertIn('TestCatalog', repr(TestCatalog))
class AbstractCatalogCompatibilityTest(unittest.TestCase):
"""Test backward compatibility with di.AbstractCatalog."""
def test_compatibility(self):
"""Test that di.AbstractCatalog is available."""
self.assertIs(di.DeclarativeCatalog, di.AbstractCatalog)