mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 09:57:37 +03:00 
			
		
		
		
	Restrict repeatedly binding of catalog providers
This commit is contained in:
		
							parent
							
								
									5799b6ff33
								
							
						
					
					
						commit
						476e867dc9
					
				| 
						 | 
					@ -86,6 +86,11 @@ class CatalogMetaClass(type):
 | 
				
			||||||
        cls.Bundle = mcs.bundle_cls_factory(cls)
 | 
					        cls.Bundle = mcs.bundle_cls_factory(cls)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for name, provider in six.iteritems(cls_providers):
 | 
					        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)
 | 
					            provider.bind = ProviderBinding(cls, name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return cls
 | 
					        return cls
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -71,6 +71,23 @@ class CatalogsInheritanceTests(unittest.TestCase):
 | 
				
			||||||
                                  p32=CatalogC.p32))
 | 
					                                  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):
 | 
					class CatalogBundleTests(unittest.TestCase):
 | 
				
			||||||
    """Catalog bundle test cases."""
 | 
					    """Catalog bundle test cases."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user