Пример #1
0
def iter_suites(packages=None):
    """Yield all testsuites."""
    from werkzeug.utils import import_string, find_modules
    from flask_registry import ModuleAutoDiscoveryRegistry, \
        ImportPathRegistry

    app = create_app()

    if packages is None:
        testsuite = ModuleAutoDiscoveryRegistry('testsuite', app=app)
        from invenio import testsuite as testsuite_invenio
        from invenio.base import testsuite as testsuite_base
        from invenio.celery import testsuite as testsuite_celery
        testsuite.register(testsuite_invenio)
        testsuite.register(testsuite_base)
        testsuite.register(testsuite_celery)
    else:
        exclude = map(lambda x: x + '.testsuite',
                      app.config.get('PACKAGES_EXCLUDE', []))
        testsuite = ImportPathRegistry(initial=packages,
                                       exclude=exclude,
                                       load_modules=True)

    for package in testsuite:
        for name in find_modules(package.__name__):
            module = import_string(name)
            if not module.__name__.split('.')[-1].startswith('test_'):
                continue
            if hasattr(module, 'TEST_SUITE'):
                yield module.TEST_SUITE
            else:
                app.logger.warning("%s: No test suite defined." %
                                   module.__name__)
Пример #2
0
 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']
Пример #3
0
 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
     )
Пример #4
0
    def test_import_error(self):
        Registry(app=self.app)

        self.app.extensions['registry']['packages'] = \
            ImportPathRegistry(initial=['flask_registry'])

        self.app.extensions['registry']['blueprints'] = \
            BlueprintAutoDiscoveryRegistry(app=self.app)

        self.assertEqual(len(self.app.extensions['registry']['blueprints']), 0)
Пример #5
0
    def test_registration(self):
        Registry(app=self.app)

        self.app.extensions['registry'].update(pathns=ImportPathRegistry(
            initial=['registry_module']))
        self.app.extensions['registry'].update(
            myns=PkgResourcesDirDiscoveryRegistry(
                'resources', app=self.app, registry_namespace='pathns'))

        self.assertEquals(1, len(self.app.extensions['registry']['myns']))
Пример #6
0
    def test_missing_folder(self):
        Registry(app=self.app)

        self.app.extensions['registry'].update(pathns=ImportPathRegistry(
            initial=['registry_module']))
        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_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
Пример #8
0
    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_broken_module(self):
        Registry(app=self.app)

        self.app.extensions['registry'].update(
            pathns=ImportPathRegistry(initial=['registry_module']),
            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
Пример #10
0
 def create_registries(self):
     """Create registries for testing."""
     self.app.extensions['registry']['workflows.tests'] = \
         ImportPathRegistry(initial=TEST_PACKAGES)
     self.app.extensions['registry']['workflows'] = \
         WorkflowsRegistry(
             'workflows', app=self.app, registry_namespace='workflows.tests'
         )
     self.app.extensions['registry']['workflows.actions'] = \
         WorkflowsRegistry(
             'actions', app=self.app, registry_namespace='workflows.tests'
         )
Пример #11
0
    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']
Пример #12
0
    def test_registration(self):
        Registry(app=self.app)

        self.app.extensions['registry']['packages'] = \
            ImportPathRegistry(initial=['registry_module'])

        self.app.extensions['registry']['blueprints'] = \
            BlueprintAutoDiscoveryRegistry(app=self.app)

        self.assertEqual(
            len(self.app.extensions['registry']['blueprints']),
            3
        )
    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(4, 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_registration(self):
        Registry(app=self.app)

        self.app.extensions['registry']['pathns'] = \
            ImportPathRegistry(initial=['flask_registry.*'])

        self.assertEqual(4, 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
Пример #15
0
    def test_syntax_error(self):
        Registry(app=self.app)

        self.app.extensions['registry']['packages'] = \
            ImportPathRegistry(initial=['tests'])

        self.assertRaises(SyntaxError,
                          BlueprintAutoDiscoveryRegistry,
                          app=self.app,
                          module_name='syntaxerror_views')

        self.app.extensions['registry'].update(
            blueprints=BlueprintAutoDiscoveryRegistry(
                app=self.app, module_name='syntaxerror_views', silent=True))

        self.assertEqual(len(self.app.extensions['registry']['blueprints']), 0)
    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
Пример #17
0
    def test_registration(self):
        r = self.app.extensions['registry']

        r['testpkgs'] = ImportPathRegistry(
            initial=['invenio.testsuite.test_apps'])
        assert len(r['testpkgs']) == 1

        r['myns'] = \
            DictModuleAutoDiscoverySubRegistry(
                'last',
                keygetter=lambda k, v, new_v: k if k else v.__name__,
                app=self.app,
                registry_namespace='testpkgs'
            )
        assert len(r['myns']) == 1

        from invenio.testsuite.test_apps.last import views
        assert r['myns']['invenio.testsuite.test_apps.last.views'] == \
            views

        self.assertRaises(RegistryError,
                          DictModuleAutoDiscoverySubRegistry,
                          'last',
                          app=self.app,
                          registry_namespace='testpkgs')

        # Register simple object
        class TestObject(object):
            pass

        r['myns'].register(TestObject)

        # Identical keys raises RegistryError
        self.assertRaises(RegistryError, r['myns'].register, TestObject)

        r['myns'].unregister('TestObject')
        assert 'TestObject' not in r['myns']

        r['myns']['mykey'] = TestObject
        assert TestObject == r['myns']['mykey']

        assert len(r['myns'].items()) == 2
    def test_registration(self):
        Registry(app=self.app)

        self.app.extensions['registry'].update(
            pathns=ImportPathRegistry(initial=['flask_registry.*'])
        )

        self.assertEquals(4, 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=['registry_module']),
            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