2015-08-31 16:31:38 +03:00
|
|
|
"""Dependency injector catalog unittests."""
|
2015-03-16 16:58:07 +03:00
|
|
|
|
|
|
|
import unittest2 as unittest
|
2015-09-01 15:02:00 +03:00
|
|
|
import dependency_injector as di
|
2015-03-16 16:58:07 +03:00
|
|
|
|
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
class CatalogA(di.AbstractCatalog):
|
|
|
|
"""Test catalog A."""
|
|
|
|
|
|
|
|
p11 = di.Provider()
|
|
|
|
p12 = di.Provider()
|
|
|
|
|
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-10-11 15:34:21 +03:00
|
|
|
p21 = di.Provider()
|
|
|
|
p22 = di.Provider()
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
class CatalogC(CatalogB):
|
|
|
|
"""Test catalog C."""
|
2015-10-07 13:36:28 +03:00
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
p31 = di.Provider()
|
|
|
|
p32 = di.Provider()
|
2015-10-07 13:36:28 +03:00
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
|
2015-10-15 20:15:38 +03:00
|
|
|
class CatalogD(di.AbstractCatalog):
|
|
|
|
"""Test catalog D."""
|
|
|
|
|
|
|
|
p11 = di.Provider()
|
|
|
|
p12 = di.Provider()
|
|
|
|
|
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
class CatalogsInheritanceTests(unittest.TestCase):
|
|
|
|
"""Catalogs inheritance tests."""
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
def test_cls_providers(self):
|
|
|
|
"""Test `di.AbstractCatalog.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))
|
|
|
|
self.assertDictEqual(CatalogC.cls_providers,
|
|
|
|
dict(p31=CatalogC.p31,
|
|
|
|
p32=CatalogC.p32))
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
def test_inherited_providers(self):
|
|
|
|
"""Test `di.AbstractCatalog.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))
|
|
|
|
self.assertDictEqual(CatalogC.inherited_providers,
|
|
|
|
dict(p11=CatalogA.p11,
|
|
|
|
p12=CatalogA.p12,
|
|
|
|
p21=CatalogB.p21,
|
|
|
|
p22=CatalogB.p22))
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
def test_providers(self):
|
|
|
|
"""Test `di.AbstractCatalog.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))
|
|
|
|
self.assertDictEqual(CatalogC.providers,
|
|
|
|
dict(p11=CatalogA.p11,
|
|
|
|
p12=CatalogA.p12,
|
|
|
|
p21=CatalogB.p21,
|
|
|
|
p22=CatalogB.p22,
|
|
|
|
p31=CatalogC.p31,
|
|
|
|
p32=CatalogC.p32))
|
|
|
|
|
|
|
|
|
2015-10-15 20:15:38 +03:00
|
|
|
class CatalogPackTests(unittest.TestCase):
|
|
|
|
"""Catalog pack test cases."""
|
2015-10-14 18:07:28 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Set test environment up."""
|
2015-10-15 20:15:38 +03:00
|
|
|
self.pack = CatalogC.Pack(CatalogC.p11,
|
|
|
|
CatalogC.p12)
|
|
|
|
|
|
|
|
def test_get_attr_from_pack(self):
|
|
|
|
"""Test get providers (attribute) from pack."""
|
|
|
|
self.assertIs(self.pack.p11, CatalogC.p11)
|
|
|
|
self.assertIs(self.pack.p12, CatalogC.p12)
|
|
|
|
|
|
|
|
def test_get_attr_not_from_pack(self):
|
|
|
|
"""Test get providers (attribute) that are not in pack."""
|
|
|
|
self.assertRaises(di.Error, getattr, self.pack, 'p21')
|
|
|
|
self.assertRaises(di.Error, getattr, self.pack, 'p22')
|
|
|
|
self.assertRaises(di.Error, getattr, self.pack, 'p31')
|
|
|
|
self.assertRaises(di.Error, getattr, self.pack, 'p32')
|
|
|
|
|
|
|
|
def test_get_method_from_pack(self):
|
|
|
|
"""Test get providers (get() method) from pack."""
|
|
|
|
self.assertIs(self.pack.get('p11'), CatalogC.p11)
|
|
|
|
self.assertIs(self.pack.get('p12'), CatalogC.p12)
|
|
|
|
|
|
|
|
def test_get_method_not_from_pack(self):
|
|
|
|
"""Test get providers (get() method) that are not in pack."""
|
|
|
|
self.assertRaises(di.Error, self.pack.get, 'p21')
|
|
|
|
self.assertRaises(di.Error, self.pack.get, 'p22')
|
|
|
|
self.assertRaises(di.Error, self.pack.get, 'p31')
|
|
|
|
self.assertRaises(di.Error, self.pack.get, 'p32')
|
2015-10-14 18:07:28 +03:00
|
|
|
|
2015-10-15 20:15:38 +03:00
|
|
|
def test_has(self):
|
|
|
|
"""Test checks of providers availability in pack."""
|
|
|
|
self.assertTrue(self.pack.has('p11'))
|
|
|
|
self.assertTrue(self.pack.has('p12'))
|
2015-10-14 18:07:28 +03:00
|
|
|
|
2015-10-15 20:15:38 +03:00
|
|
|
self.assertFalse(self.pack.has('p21'))
|
|
|
|
self.assertFalse(self.pack.has('p22'))
|
|
|
|
self.assertFalse(self.pack.has('p31'))
|
|
|
|
self.assertFalse(self.pack.has('p32'))
|
2015-10-14 18:07:28 +03:00
|
|
|
|
2015-10-15 20:15:38 +03:00
|
|
|
def test_create_pack_with_another_catalog_provider(self):
|
|
|
|
"""Test that pack is not created with provider from another catalog."""
|
|
|
|
self.assertRaises(di.Error, CatalogC.Pack, CatalogC.p31, CatalogD.p11)
|
|
|
|
|
|
|
|
def test_create_pack_with_unbound_provider(self):
|
|
|
|
"""Test that pack is not created with unbound provider."""
|
|
|
|
self.assertRaises(di.Error, CatalogC.Pack, di.Provider())
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
|
2015-03-16 16:58:07 +03:00
|
|
|
class CatalogTests(unittest.TestCase):
|
|
|
|
"""Catalog test cases."""
|
|
|
|
|
2015-10-11 15:34:21 +03:00
|
|
|
def test_get(self):
|
|
|
|
"""Test getting of providers using get() method."""
|
|
|
|
self.assertIs(CatalogC.get('p11'), CatalogC.p11)
|
|
|
|
self.assertIs(CatalogC.get('p12'), CatalogC.p12)
|
|
|
|
self.assertIs(CatalogC.get('p22'), CatalogC.p22)
|
|
|
|
self.assertIs(CatalogC.get('p22'), CatalogC.p22)
|
|
|
|
self.assertIs(CatalogC.get('p32'), CatalogC.p32)
|
|
|
|
self.assertIs(CatalogC.get('p32'), CatalogC.p32)
|
|
|
|
|
|
|
|
def test_get_undefined(self):
|
|
|
|
"""Test getting of undefined providers using get() method."""
|
|
|
|
self.assertRaises(di.Error, CatalogC.get, 'undefined')
|
|
|
|
|
|
|
|
def test_has(self):
|
2015-10-15 20:15:38 +03:00
|
|
|
"""Test checks of providers availability in catalog."""
|
2015-10-11 15:34:21 +03:00
|
|
|
self.assertTrue(CatalogC.has('p11'))
|
|
|
|
self.assertTrue(CatalogC.has('p12'))
|
|
|
|
self.assertTrue(CatalogC.has('p21'))
|
|
|
|
self.assertTrue(CatalogC.has('p22'))
|
|
|
|
self.assertTrue(CatalogC.has('p31'))
|
|
|
|
self.assertTrue(CatalogC.has('p32'))
|
|
|
|
self.assertFalse(CatalogC.has('undefined'))
|
|
|
|
|
|
|
|
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-10-11 15:34:21 +03:00
|
|
|
self.assertTrue(len(CatalogC.filter(di.Provider)) == 6)
|
|
|
|
self.assertTrue(len(CatalogC.filter(di.Value)) == 0)
|
2015-07-17 19:31:44 +03:00
|
|
|
|
2015-08-03 12:57:42 +03:00
|
|
|
|
|
|
|
class OverrideTests(unittest.TestCase):
|
|
|
|
"""Override decorator test cases."""
|
|
|
|
|
2015-09-01 15:02:00 +03:00
|
|
|
class Catalog(di.AbstractCatalog):
|
2015-08-03 12:57:42 +03:00
|
|
|
"""Test catalog."""
|
|
|
|
|
2015-09-01 15:02:00 +03:00
|
|
|
obj = di.Object(object())
|
|
|
|
another_obj = di.Object(object())
|
2015-08-03 12:57:42 +03:00
|
|
|
|
|
|
|
def test_overriding(self):
|
|
|
|
"""Test catalog overriding with another catalog."""
|
2015-09-01 15:02:00 +03:00
|
|
|
@di.override(self.Catalog)
|
2015-08-03 12:57:42 +03:00
|
|
|
class OverridingCatalog(self.Catalog):
|
|
|
|
"""Overriding catalog."""
|
|
|
|
|
2015-09-01 15:02:00 +03:00
|
|
|
obj = di.Value(1)
|
|
|
|
another_obj = di.Value(2)
|
2015-08-03 12:57:42 +03:00
|
|
|
|
|
|
|
self.assertEqual(self.Catalog.obj(), 1)
|
|
|
|
self.assertEqual(self.Catalog.another_obj(), 2)
|