python-dependency-injector/tests/test_catalog.py

90 lines
2.6 KiB
Python
Raw Normal View History

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
class CatalogTests(unittest.TestCase):
"""Catalog test cases."""
2015-09-01 15:02:00 +03:00
class Catalog(di.AbstractCatalog):
2015-03-16 16:58:07 +03:00
"""Test catalog."""
2015-09-01 15:02:00 +03:00
obj = di.Object(object())
another_obj = di.Object(object())
2015-03-16 16:58:07 +03:00
def test_get_used(self):
"""Test retrieving used provider."""
catalog = self.Catalog(self.Catalog.obj)
self.assertIsInstance(catalog.obj(), object)
def test_get_unused(self):
"""Test retrieving unused provider."""
catalog = self.Catalog()
2015-09-01 15:02:00 +03:00
self.assertRaises(di.Error, getattr, catalog, 'obj')
2015-03-16 16:58:07 +03:00
def test_all_providers(self):
"""Test getting of all catalog providers."""
2015-07-17 19:31:44 +03:00
self.assertTrue(len(self.Catalog.providers) == 2)
2015-03-16 16:58:07 +03:00
2015-07-17 19:31:44 +03:00
self.assertIn('obj', self.Catalog.providers)
self.assertIn(self.Catalog.obj, self.Catalog.providers.values())
2015-03-16 16:58:07 +03:00
2015-07-17 19:31:44 +03:00
self.assertIn('another_obj', self.Catalog.providers)
self.assertIn(self.Catalog.another_obj,
self.Catalog.providers.values())
2015-03-16 16:58:07 +03:00
def test_all_providers_by_type(self):
"""Test getting of all catalog providers of specific type."""
2015-09-01 15:02:00 +03:00
self.assertTrue(len(self.Catalog.filter(di.Object)) == 2)
self.assertTrue(len(self.Catalog.filter(di.Value)) == 0)
2015-07-17 19:31:44 +03:00
def test_metaclass_with_several_catalogs(self):
"""Test that metaclass work well with several catalogs."""
2015-09-01 15:02:00 +03:00
class Catalog1(di.AbstractCatalog):
2015-07-17 19:31:44 +03:00
"""Catalog1."""
2015-09-01 15:02:00 +03:00
provider = di.Object(object())
2015-07-17 19:31:44 +03:00
2015-09-01 15:02:00 +03:00
class Catalog2(di.AbstractCatalog):
2015-07-17 19:31:44 +03:00
"""Catalog2."""
2015-09-01 15:02:00 +03:00
provider = di.Object(object())
2015-07-17 19:31:44 +03:00
self.assertTrue(len(Catalog1.providers) == 1)
self.assertIs(Catalog1.provider, Catalog1.providers['provider'])
self.assertTrue(len(Catalog2.providers) == 1)
self.assertIs(Catalog2.provider, Catalog2.providers['provider'])
self.assertIsNot(Catalog1.provider, Catalog2.provider)
class OverrideTests(unittest.TestCase):
"""Override decorator test cases."""
2015-09-01 15:02:00 +03:00
class Catalog(di.AbstractCatalog):
"""Test catalog."""
2015-09-01 15:02:00 +03:00
obj = di.Object(object())
another_obj = di.Object(object())
def test_overriding(self):
"""Test catalog overriding with another catalog."""
2015-09-01 15:02:00 +03:00
@di.override(self.Catalog)
class OverridingCatalog(self.Catalog):
"""Overriding catalog."""
2015-09-01 15:02:00 +03:00
obj = di.Value(1)
another_obj = di.Value(2)
self.assertEqual(self.Catalog.obj(), 1)
self.assertEqual(self.Catalog.another_obj(), 2)