diff --git a/dependency_injector/catalog.py b/dependency_injector/catalog.py index 2725f7c3..eb674756 100644 --- a/dependency_injector/catalog.py +++ b/dependency_injector/catalog.py @@ -86,6 +86,11 @@ class CatalogMetaClass(type): cls.Bundle = mcs.bundle_cls_factory(cls) for name, provider in six.iteritems(cls_providers): + if provider.is_bound: + raise Error('Provider {0} has been already bound to catalog' + '{1} as "{2}"'.format(provider, + provider.bind.catalog, + provider.bind.name)) provider.bind = ProviderBinding(cls, name) return cls diff --git a/tests/test_catalog.py b/tests/test_catalog.py index fc7e031a..c5e1e182 100644 --- a/tests/test_catalog.py +++ b/tests/test_catalog.py @@ -71,6 +71,23 @@ class CatalogsInheritanceTests(unittest.TestCase): 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.assertIs(CatalogA.p11.bind.catalog, CatalogA) + self.assertEquals(CatalogA.p11.bind.name, 'p11') + + self.assertIs(CatalogA.p12.bind.catalog, CatalogA) + self.assertEquals(CatalogA.p12.bind.name, 'p12') + + def test_provider_rebinding(self): + """Test that provider could not be bound twice.""" + self.assertRaises(di.Error, type, 'TestCatalog', (di.AbstractCatalog,), + dict(some_name=CatalogA.p11)) + + class CatalogBundleTests(unittest.TestCase): """Catalog bundle test cases."""