From 9a617479cc040d77784aca8e2c4f6c6dda4e71de Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Wed, 11 Nov 2015 15:51:42 +0200 Subject: [PATCH] Refactor CatalogBundle --- dependency_injector/catalogs.py | 4 ++-- tests/test_catalogs.py | 34 +++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/dependency_injector/catalogs.py b/dependency_injector/catalogs.py index 2796fe46..f0bfc079 100644 --- a/dependency_injector/catalogs.py +++ b/dependency_injector/catalogs.py @@ -38,7 +38,7 @@ class CatalogBundle(object): """ return type('BundleSubclass', (cls,), dict(catalog=catalog)) - def get(self, name): + def get_provider(self, name): """Return provider with specified name or raise an error.""" try: return self.providers[name] @@ -46,7 +46,7 @@ class CatalogBundle(object): raise Error('Provider "{0}" is not a part of {1}'.format(name, self)) - def has(self, name): + def has_provider(self, name): """Check if there is provider with certain name.""" return name in self.providers diff --git a/tests/test_catalogs.py b/tests/test_catalogs.py index fed33ab8..bfddcfb5 100644 --- a/tests/test_catalogs.py +++ b/tests/test_catalogs.py @@ -158,25 +158,35 @@ class CatalogBundleTests(unittest.TestCase): def test_get_method_from_bundle(self): """Test get providers (get() method) from bundle.""" - self.assertIs(self.bundle.get('p11'), CatalogC.p11) - self.assertIs(self.bundle.get('p12'), CatalogC.p12) + self.assertIs(self.bundle.get_provider('p11'), CatalogC.p11) + self.assertIs(self.bundle.get_provider('p12'), CatalogC.p12) def test_get_method_not_from_bundle(self): """Test get providers (get() method) that are not in bundle.""" - self.assertRaises(di.Error, self.bundle.get, 'p21') - self.assertRaises(di.Error, self.bundle.get, 'p22') - self.assertRaises(di.Error, self.bundle.get, 'p31') - self.assertRaises(di.Error, self.bundle.get, 'p32') + self.assertRaises(di.Error, self.bundle.get_provider, 'p21') + self.assertRaises(di.Error, self.bundle.get_provider, 'p22') + self.assertRaises(di.Error, self.bundle.get_provider, 'p31') + self.assertRaises(di.Error, self.bundle.get_provider, 'p32') def test_has(self): """Test checks of providers availability in bundle.""" - self.assertTrue(self.bundle.has('p11')) - self.assertTrue(self.bundle.has('p12')) + self.assertTrue(self.bundle.has_provider('p11')) + self.assertTrue(self.bundle.has_provider('p12')) - self.assertFalse(self.bundle.has('p21')) - self.assertFalse(self.bundle.has('p22')) - self.assertFalse(self.bundle.has('p31')) - self.assertFalse(self.bundle.has('p32')) + self.assertFalse(self.bundle.has_provider('p21')) + self.assertFalse(self.bundle.has_provider('p22')) + self.assertFalse(self.bundle.has_provider('p31')) + self.assertFalse(self.bundle.has_provider('p32')) + + def test_hasattr(self): + """Test checks of providers availability in bundle.""" + self.assertTrue(hasattr(self.bundle, 'p11')) + self.assertTrue(hasattr(self.bundle, 'p12')) + + self.assertFalse(hasattr(self.bundle, 'p21')) + self.assertFalse(hasattr(self.bundle, 'p22')) + self.assertFalse(hasattr(self.bundle, 'p31')) + self.assertFalse(hasattr(self.bundle, 'p31')) def test_create_bundle_with_unbound_provider(self): """Test that bundle is not created with unbound provider."""