Restrict repeatedly binding of catalog providers

This commit is contained in:
Roman Mogilatov 2015-10-17 01:00:32 +03:00
parent 5799b6ff33
commit 476e867dc9
2 changed files with 22 additions and 0 deletions

View File

@ -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

View File

@ -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."""