def test_registry_base(self): Registry(app=self.app) self.app.extensions['registry']['myns'] = RegistryBase() self.assertRaises(NotImplementedError, self.app.extensions['registry']['myns'].register) self.assertRaises(NotImplementedError, self.app.extensions['registry']['myns'].unregister)
def test_registration_iter(self): Registry(app=self.app) self.app.extensions['registry']['mynamespace'] = RegistryBase() self.app.extensions['registry']['myothernamespace'] = RegistryBase() assert len(self.app.extensions['registry']) == 2 assert 'mynamespace' in self.app.extensions['registry'] assert 'myothernamespace' in self.app.extensions['registry'] # Double registration try: self.app.extensions['registry']['mynamespace'] = RegistryBase() raise AssertionError("No exception raise for double registration") except RegistryError: pass # Registered object assert isinstance(self.app.extensions['registry']['mynamespace'], RegistryBase) assert isinstance(self.app.extensions['registry']['myothernamespace'], RegistryBase) # Iteration assert set(self.app.extensions['registry']) == \ set(['myothernamespace', 'mynamespace', ]) for ns, r in self.app.extensions['registry'].items(): assert ns in ['mynamespace', 'myothernamespace'] # Removal myregistry = self.app.extensions['registry']['mynamespace'] assert myregistry.namespace del self.app.extensions['registry']['mynamespace'] assert myregistry.namespace is None
def test_registration(self): r = Registry(app=self.app) r['myns'] = DictRegistry() r['myns'].register('key1', 'item1') r['myns']['key2'] = 'item2' self.assertRaises( RegistryError, r['myns'].register, 'key2', 'item3' ) assert len(r['myns']) == 2 assert len(r['myns'].items()) == 2 assert 'key2' in r['myns'] assert 'notin' not in r['myns'] assert list(r['myns']) assert r['myns'].get("key1") == "item1" assert r['myns'].get("key1", default='na') == "item1" assert r['myns'].get("unknownkey", default='na') == "na" assert r['myns'].get("unknownkey") is None assert r['myns']['key2'] == 'item2' r['myns'].unregister('key2') assert len(r['myns']) == 1 assert 'key1' in r['myns'] assert 'key2' not in r['myns'] del r['myns']['key1'] assert len(r['myns']) == 0
def test_regsitration(self): Registry(app=self.app) self.app.extensions['registry']['myns'] = \ EntryPointRegistry('flask_registry.test_entry') self.assertEqual(len(self.app.extensions['registry']['myns']), 1) assert 'testcase' in self.app.extensions['registry']['myns']
def test_registration_noapp(self): Registry(app=self.app) self.app.extensions['registry']['myns'] = ModuleDiscoveryRegistry( 'notimportant', ) self.assertRaises(RegistryError, self.app.extensions['registry']['myns'].discover)
def test_load_modules(self): Registry(app=self.app) self.app.extensions['registry']['impns'] = ImportPathRegistry( initial=['flask_registry.registries.*'], load_modules=True ) assert len(self.app.extensions['registry']['impns']) == 4 assert not isinstance( self.app.extensions['registry']['impns'][0], six.string_types )
def test_load(self): Registry(app=self.app) self.app.extensions['registry']['myns'] = \ EntryPointRegistry('flask_registry.test_entry', load=True) self.assertEqual(len(self.app.extensions['registry']['myns']), 1) assert RegistryBase == \ self.app.extensions['registry']['myns']['testcase'][0]
def test_creation(self): Registry(app=self.app) self.app.extensions['registry']['modns'] = ModuleRegistry() moda = MockModule() self.app.extensions['registry']['modns'].register(moda) self.app.extensions['registry']['modns'].unregister(moda) moda.assert_called()
def test_exclude(self): Registry(app=self.app) self.app.extensions['registry']['impns'] = ImportPathRegistry( initial=['flask_registry.registries.*'], exclude=['flask_registry.registries.pkgresources'], ) assert len(self.app.extensions['registry']['impns']) == 3 assert 'flask_registry.registries.pkgresources' not in \ self.app.extensions['registry']['impns']
def test_six_usage(self): r = Registry(app=self.app) r['myns'] = DictRegistry() r['myns'].register('key1', 'item1') r['myns']['key2'] = 'item2' assert list(six.iterkeys(r['myns'])) == list(r['myns'].keys()) assert list(six.itervalues(r['myns'])) == list(r['myns'].values()) assert list(six.iteritems(r['myns'])) == list(r['myns'].items())
def test_registration(self): Registry(app=self.app) self.app.extensions['registry'].update(pathns=ImportPathRegistry( initial=['tests'])) self.app.extensions['registry'].update( myns=PkgResourcesDirDiscoveryRegistry( 'resources', app=self.app, registry_namespace='pathns')) self.assertEquals(1, len(self.app.extensions['registry']['myns']))
def test_missing_folder(self): Registry(app=self.app) self.app.extensions['registry'].update( pathns=ImportPathRegistry(initial=['tests'])) self.app.extensions['registry'].update( myns=PkgResourcesDirDiscoveryRegistry('non_existing_folder', app=self.app, registry_namespace='pathns')) self.assertEquals(0, len(self.app.extensions['registry']['myns']))
def test_creation_setup(self): Registry(app=self.app) self.app.extensions['registry']['modns'] = ModuleRegistry( with_setup=False ) moda = MockModule() self.app.extensions['registry']['modns'].register(moda) self.app.extensions['registry']['modns'].unregister(moda) moda.assert_not_called()
def test_unregister(self): Registry(app=self.app) self.app.extensions['registry']['impns'] = ImportPathRegistry( initial=['flask_registry.registries.*'] ) self.assertRaises( NotImplementedError, self.app.extensions['registry']['impns'].unregister, 'flask_registry.*' )
def test_proxy(self): Registry(app=self.app) proxy = RegistryProxy('prxns', RegistryBase) assert 'prxns' not in self.app.extensions['registry'] with self.app.app_context(): self.assertRaises(NotImplementedError, proxy.register) assert 'prxns' in self.app.extensions['registry'] assert isinstance(self.app.extensions['registry']['prxns'], RegistryBase)
def test_missing_module(self): Registry(app=self.app) self.app.extensions['registry'].update( pathns=ImportPathRegistry(initial=['flask_registry.*']), myns=ModuleDiscoveryRegistry('some_non_existing_module', registry_namespace='pathns')) with self.app.app_context(): self.app.extensions['registry']['myns'].discover() assert len(self.app.extensions['registry']['myns']) == 0
def test_namespace_injection(self): Registry(app=self.app) self.app.extensions['registry']['mynamespace'] = RegistryBase() assert self.app.extensions['registry']['mynamespace'].namespace == \ 'mynamespace' try: self.app.extensions['registry']['mynamespace'].namespace = "na" raise AssertionError("RegistryError not raised.") except RegistryError: pass
def test_registration(self): Registry(app=self.app) self.app.extensions['registry']['impns'] = ImportPathRegistry() self.app.extensions['registry']['impns'].register('flask_registry') self.app.extensions['registry']['impns'].register( 'flask_registry.registries.*' ) assert len(self.app.extensions['registry']['impns']) == 5 assert 'flask_registry.registries.core' in \ self.app.extensions['registry']['impns']
def test_broken_module(self): Registry(app=self.app) self.app.extensions['registry'].update( pathns=ImportPathRegistry(initial=['tests']), myns=ModuleDiscoveryRegistry('broken_module', registry_namespace='pathns')) with self.app.app_context(): self.assertRaises(ImportError, self.app.extensions['registry']['myns'].discover) assert len(self.app.extensions['registry']['myns']) == 0
def test_registration(self): r = Registry(app=self.app) r['myns'] = ListRegistry() r['myns'].register('item1') r['myns'].register('item2') assert len(r['myns']) == 2 assert 'item1' in r['myns'] assert 'notin' not in r['myns'] assert list(r['myns']) r['myns'].unregister('item2') assert len(r['myns']) == 1 assert 'item1' in r['myns'] assert 'item2' not in r['myns']
def test_registration(self): Registry(app=self.app) self.app.extensions['registry']['pathns'] = \ ImportPathRegistry(initial=['flask_registry.*']) self.assertEqual(3, len(self.app.extensions['registry']['pathns'])) self.app.extensions['registry']['myns'] = \ ModuleAutoDiscoveryRegistry('appdiscovery', app=self.app, registry_namespace='pathns') assert len(self.app.extensions['registry']['myns']) == 1 from flask_registry.registries import appdiscovery assert self.app.extensions['registry']['myns'][0] == appdiscovery
def test_registry_proxy(self): Registry(app=self.app) self.app.extensions['registry']['pathns'] = \ ImportPathRegistry(initial=['flask_registry.*']) myns = RegistryProxy('myns', ModuleAutoDiscoveryRegistry, 'appdiscovery', registry_namespace='pathns') with self.app.app_context(): self.assertEqual(3, len(self.app.extensions['registry']['pathns'])) self.assertEqual(1, len(list(myns))) from flask_registry.registries import appdiscovery self.assertEqual(appdiscovery, myns[0])
def test_modules_namespace(self): from flask_registry import registries Registry(app=self.app) self.app.extensions['registry']['pathns'] = ModuleRegistry() self.app.extensions['registry']['pathns'].register(registries) self.app.extensions['registry']['myns'] = \ ModuleDiscoveryRegistry('appdiscovery', registry_namespace='pathns') with self.app.app_context(): self.app.extensions['registry']['myns'].discover() assert len(self.app.extensions['registry']['myns']) == 1 from flask_registry.registries import appdiscovery assert self.app.extensions['registry']['myns'][0] == appdiscovery
def test_exclude(self): Registry(app=self.app) # Set exclude config self.app.config['PATH_NS_APPDISCOVERY_EXCLUDE'] = [ 'flask_registry.registries', ] self.app.extensions['registry'].update({ 'path.ns': ImportPathRegistry(initial=['flask_registry.*']), 'myns': ModuleDiscoveryRegistry('appdiscovery', registry_namespace='path.ns') }) with self.app.app_context(): self.app.extensions['registry']['myns'].discover() assert len(self.app.extensions['registry']['myns']) == 0
def test_registration(self): r = Registry(app=self.app) r['myns'] = SingletonRegistry() r['myns'].register('item1') assert r['myns'].get() == 'item1' self.assertRaises( RegistryError, r['myns'].register, 'item 2' ) r['myns'].unregister() assert r['myns'].get() is None self.assertRaises( RegistryError, r['myns'].unregister, )
def test_registration(self): Registry(app=self.app) self.app.extensions['registry'].update(pathns=ImportPathRegistry( initial=['flask_registry.*'])) self.assertEquals(3, len(self.app.extensions['registry']['pathns'])) self.app.extensions['registry']['myns'] = \ ModuleDiscoveryRegistry( 'appdiscovery', registry_namespace='pathns') with self.app.app_context(): self.app.extensions['registry']['myns'].discover() assert len(self.app.extensions['registry']['myns']) == 1 from flask_registry.registries import appdiscovery assert self.app.extensions['registry']['myns'][0] == appdiscovery self.app.extensions['registry']['myns'].discover(app=self.app)
def test_syntaxerror_module(self): Registry(app=self.app) self.app.extensions['registry'].update( pathns=ImportPathRegistry(initial=['tests']), myns=ModuleDiscoveryRegistry('syntaxerror_module', registry_namespace='pathns')) with self.app.app_context(): self.assertRaises(SyntaxError, self.app.extensions['registry']['myns'].discover) assert len(self.app.extensions['registry']['myns']) == 0 # Silence the error self.app.extensions['registry']['myns_silent'] = \ ModuleDiscoveryRegistry('syntaxerror_module', registry_namespace='pathns', silent=True) with self.app.app_context(): self.app.extensions['registry']['myns_silent'].discover() assert len(self.app.extensions['registry']['myns_silent']) == 0
def test_proxy_ns(self): Registry(app=self.app) proxy = RegistryProxy('pathns', ImportPathRegistry, initial=['flask_registry.*']) with self.app.app_context(): assert 'pathns' not in self.app.extensions['registry'] self.app.extensions['registry']['myns'] = \ ModuleDiscoveryRegistry('appdiscovery', registry_namespace=proxy) assert 'pathns' in self.app.extensions['registry'] self.assertEqual(3, len(self.app.extensions['registry']['pathns'])) self.app.extensions['registry']['myns'].discover() assert len(self.app.extensions['registry']['myns']) == 1 from flask_registry.registries import appdiscovery assert self.app.extensions['registry']['myns'][0] == appdiscovery
def test_creation_init(self): assert 'registry' not in self.app.extensions r = Registry() r.init_app(app=self.app) assert isinstance(self.app.extensions['registry'], Registry)
def test_double_creation(self): Registry(app=self.app) self.assertRaises(RegistryError, Registry, app=self.app)
def test_creation_init(self): assert 'registry' not in self.app.extensions r = Registry() r.init_app(app=self.app) assert isinstance(self.app.extensions['registry'], Registry)