def anyblok_nose(): """Run nose unit test for the registry """ warnings.simplefilter('default') registry = anyblok.start('nose', useseparator=True, unittest=True) defaultTest = [] if registry: installed_bloks = registry.System.Blok.list_by_state("installed") selected_bloks = return_list( Configuration.get('selected_bloks')) or installed_bloks unwanted_bloks = return_list( Configuration.get('unwanted_bloks')) or [] defaultTest = [] for blok in installed_bloks: if blok not in selected_bloks or blok in unwanted_bloks: continue startpath = BlokManager.getPath(blok) for root, dirs, _ in walk(startpath): if 'tests' in dirs: defaultTest.append(join(root, 'tests')) registry.close() # free the registry to force create it again sys.exit(main(defaultTest=defaultTest))
def test_merge_label(self): Configuration.add('new-group', label="Label 1", function_=fnct_other_configuration) Configuration.add('old-group', label="Label 2", part='other', function_=fnct_configuration) labels = Configuration._merge_labels('bloks') self.assertEqual(labels, {'new-group': "Label 1"})
def getRemoteUserAuthenticationPolicy(): """Define the authentication policy for remote user server""" return RemoteUserAuthenticationPolicy( environ_key=Configuration.get('pyramid_remoteuser_environ_key'), callback=Configuration.get('pyramid_authentication_callback'), debug=Configuration.get('pyramid_authentication_debug') )
def list_databases(): """ return the name of the databases found in the BDD the result can be filtering by the Configuration entry ``db_filter`` ..warning:: For the moment only the ``prostgresql`` dialect is available :rtype: list of the database's names """ url = Configuration.get('get_url')() db_filter = Configuration.get('db_filter') text = None if url.drivername in ('postgres', 'postgresql'): url = Configuration.get('get_url')(db_name='postgres') text = "SELECT datname FROM pg_database" if db_filter: db_filter = db_filter.replace('%', '%%') text += " where datname like '%s'" % db_filter if text is None: return [] engine = create_engine(url) return [x[0] for x in engine.execute(text).fetchall() if x[0] not in ('template1', 'template0', 'postgres')]
def run_exit(application, configuration_groups, **kwargs): """Run nose unit test for the registry :param application: name of the application :param configuration_groups: list configuration groupe to load :param \**kwargs: ArgumentParser named arguments """ format_configuration(configuration_groups, 'unittest') registry = anyblok.start(application, configuration_groups=configuration_groups, useseparator=True, unittest=True, **kwargs) defaultTest = [] if registry: installed_bloks = registry.System.Blok.list_by_state("installed") selected_bloks = Configuration.get('selected_bloks') if not selected_bloks: selected_bloks = installed_bloks unwanted_bloks = Configuration.get('unwanted_bloks') or [] defaultTest = [path for blok in installed_bloks if blok in selected_bloks and blok not in unwanted_bloks for path in [join(BlokManager.getPath(blok), 'tests')] if exists(path)] registry.close() # free the registry to force create it again sys.exit(main(defaultTest=defaultTest))
def anyblok_wsgi(application, configuration_groups, **kwargs): """ :param application: name of the application :param configuration_groups: list configuration groupe to load :param \**kwargs: ArgumentParser named arguments """ format_configuration(configuration_groups, 'preload', 'pyramid-debug', 'wsgi') load_init_function_from_entry_points() Configuration.load(application, configuration_groups=configuration_groups, **kwargs) BlokManager.load() config = Configurator() config.include_from_entry_point() config.load_config_bloks() wsgi_host = Configuration.get('wsgi_host') wsgi_port = int(Configuration.get('wsgi_port')) app = config.make_wsgi_app() server = make_server(wsgi_host, wsgi_port, app) preload_databases() logger.info("Serve forever on %r:%r" % (wsgi_host, wsgi_port)) server.serve_forever()
def test_db_exists_with_ro_url(self): db_name = Configuration.get('db_name') Registry = Configuration.get('Registry') with DBTestCase.Configuration( db_ro_urls=['postgresql:///'], db_url='', db_wo_url='' ): self.assertTrue(Registry.db_exists(db_name=db_name))
def load_registry(self): if self.enabled and self.registryLoaded is False: # Load the registry here not in configuration, # because the configuration are not load in order of score self.registryLoaded = True BlokManager.load() Configuration.parse_options(self.AnyBlokOptions, ('bloks',)) db_name = Configuration.get('db_name') if not db_name: raise Exception("No database defined to run Test") registry = RegistryManager.get(db_name) if registry: installed_bloks = registry.System.Blok.list_by_state( "installed") selected_bloks = Configuration.get('selected_bloks') if not selected_bloks: selected_bloks = installed_bloks unwanted_bloks = Configuration.get('unwanted_bloks') if unwanted_bloks is None: unwanted_bloks = [] self.bloks_path = [BlokManager.getPath(x) for x in installed_bloks] self.authoried_bloks_test_files = [ path for blok in installed_bloks if blok in selected_bloks and blok not in unwanted_bloks for path in [join(BlokManager.getPath(blok), 'tests')] if exists(path)] registry.close() # free the registry to force create it again
def parser(self): # Don't call super to user the Parser of anyblok kwargs = { "usage": self.usage, "prog": self.prog } parser = getParser(**kwargs) parser.add_argument("-v", "--version", action="version", default=argparse.SUPPRESS, version="%(prog)s (version " + __version__ + ")\n", help="show program's version number and exit") parser.add_argument("args", nargs="*", help=argparse.SUPPRESS) keys = sorted(self.settings, key=self.settings.__getitem__) for k in keys: self.settings[k].add_option(parser) description = {} if self.application in Configuration.applications: description.update(Configuration.applications[self.application]) else: description.update(Configuration.applications['default']) configuration_groups = description.pop('configuration_groups', ['gunicorn', 'database']) if 'plugins' not in configuration_groups: configuration_groups.append('plugins') Configuration._load(parser, configuration_groups) return parser
def db_exists(cls, db_name=None): if not db_name: raise RegistryException('db_name is required') urls = [] url = Configuration.get('db_url') if url: urls.append(url) wo_url = Configuration.get('db_wo_url') if wo_url: urls.append(wo_url) for ro_url in Configuration.get('db_ro_urls', []) or []: urls.append(ro_url) gurl = Configuration.get('get_url', get_url) for url in urls: url = gurl(db_name=db_name, url=url) if not database_exists(url): return False else: return database_exists(gurl(db_name=db_name)) return True
def getSessionAuthenticationPolicy(): """Define the authentication policy for a session""" return SessionAuthenticationPolicy( prefix=Configuration.get('pyramid_session_prefix'), callback=Configuration.get('pyramid_authentication_callback'), debug=Configuration.get('pyramid_authentication_debug') )
def test_need_anyblok_registry_ko(self): def __get_db_name(request): return 'wrong_db_name' Configuration.update(get_db_name=__get_db_name) self.includemes.append(self.add_route_and_views2) webserver = self.init_web_server() webserver.get('/test/', status=404)
def init(self, parser, opts, args): Configuration.parse_options(opts, ('gunicorn',)) # get the configuration save in AnyBlok configuration in # gunicorn configuration for name in Configuration.configuration.keys(): if name in self.cfg.settings: value = Configuration.get(name) if value: self.cfg.settings[name].set(value)
def test_update_query(self): registry = RegistryManager.get(Configuration.get('db_name')) registry.upgrade(install=('test-me-blok2',)) registry.commit() self.assertTrue(registry.System.Blok.query().all_name()) self.assertTrue(registry.System.Blok.query().all()) registry.close() registry = RegistryManager.get(Configuration.get('db_name')) self.assertTrue(registry.System.Blok.query().all_name()) self.assertTrue(registry.System.Blok.query().all())
def test_get_url5(self): db_url = 'postgres:///anyblok' Configuration.update( db_driver_name=None, db_host=None, db_user_name='jssuzanne', db_password='******', db_port=None) url = get_url(db_name='anyblok3', url=db_url) self.check_url(url, 'postgres://*****:*****@/anyblok3')
def test_get_url2(self): Configuration.update( db_name='anyblok', db_driver_name='postgres', db_host='localhost', db_user_name=None, db_password=None, db_port=None,) url = Configuration.get_url(db_name='anyblok2') self.check_url(url, 'postgres://localhost/anyblok2')
def filterModel(cls, query): Model = cls.registry.System.Model wanted_models = Configuration.get('doc_wanted_models') if wanted_models: query = query.filter(Model.name.in_(wanted_models)) unwanted_models = Configuration.get('doc_unwanted_models') if unwanted_models: query = query.filter(Model.name.notin_(unwanted_models)) return query
def test_get_url_without_drivername(self): Configuration.update( db_name=None, db_driver_name=None, db_host=None, db_user_name=None, db_password=None, db_port=None) with self.assertRaises(ConfigurationException): Configuration.get_url()
def test_registry_by_default_method(self): self.includemes.append(self.add_route_and_views) webserver = self.init_web_server() res = webserver.get('/test/', status=200) self.assertEqual(Configuration.get('db_name'), res.body.decode('utf8')) webserver.get('/test/login/') res = webserver.get('/test/', status=200) self.assertEqual('other_db_name', res.body.decode('utf8')) webserver.get('/test/logout/') res = webserver.get('/test/', status=200) self.assertEqual(Configuration.get('db_name'), res.body.decode('utf8'))
def test_start_function(self): BlokManager.unload() db_name = Configuration.get('db_name') or 'test_anyblok' db_driver_name = Configuration.get('db_driver_name') or 'postgresql' testargs = ['default', '--db-name', db_name, '--db-driver-name', db_driver_name] with patch.object(sys, 'argv', testargs): registry = start('default') self.assertIsNotNone(registry)
def test_get_url3(self): Configuration.update( db_url='postgres:///anyblok', db_name=None, db_driver_name=None, db_host=None, db_user_name=None, db_password=None, db_port=None) url = Configuration.get_url() self.check_url(url, 'postgres:///anyblok')
def test_string_with_encrypt_key_defined_by_configuration(self): Configuration.set('default_encrypt_key', 'secretkey') registry = self.init_registry(simple_column, ColumnType=String, encrypt_key=True) test = registry.Test.insert(col='col') registry.session.commit() self.assertEqual(test.col, 'col') res = registry.execute('select col from test where id = %s' % test.id) res = res.fetchall()[0][0] self.assertNotEqual(res, 'col') del Configuration.configuration['default_encrypt_key']
def test_parse_option_configuration(self): args = MockArgParseArguments(configfile="mock_configuration_file.cfg") Configuration.parse_options(args, ()) self.assertConfig({ 'db_name': 'anyblok', 'db_driver_name': 'postgres', 'db_user_name': '', 'db_password': '', 'db_host': 'localhost', 'db_port': '', 'configfile': 'mock_configuration_file.cfg', })
def init_engine(self, db_name=None): """Overload the initiation of engine to create more than one engine use the Configuration option to create engines: * db_url: read and write engine * db_ro_urls: read only engines (list) * db_wo_url: write only engines .. warning:: All the engines use the same database name not at the same location :param db_name: name of the database for the engines """ kwargs = self.init_engine_options() gurl = Configuration.get('get_url', get_url) self.engines = {'ro': [], 'wo': None} self._engine = None for url in Configuration.get('db_ro_urls', []) or []: url = gurl(db_name=db_name, url=url) engine = create_engine(url, **kwargs) self.engines['ro'].append(engine) wo_url = Configuration.get('db_wo_url') if wo_url: url = gurl(db_name=db_name, url=wo_url) engine = create_engine(url, **kwargs) self.engines['wo'] = engine url = Configuration.get('db_url') if url and wo_url: raise RegistryException( "You have not to use the both Configuration option " "--get-wo-url [%s] and --get-url [%s], chose only one of them " "because only one master can be chose" % (wo_url, url)) elif url: url = gurl(db_name=db_name, url=url) engine = create_engine(url, **kwargs) self.engines['wo'] = engine self.engines['ro'].append(engine) if not self.engines['ro'] and not self.engines['wo']: url = gurl(db_name=db_name) engine = create_engine(url, **kwargs) self.engines['wo'] = engine self.engines['ro'].append(engine) elif not self.engines['wo']: logger.debug('No WRITE engine defined use READ ONLY mode') self.loadwithoutmigration = True
def test_merge_for_more_parts(self): Configuration.add('new-group', function_=fnct_configuration) Configuration.add('new-group', function_=fnct_other_configuration) Configuration.add('old-group', function_=fnct_configuration) Configuration.add('old-group', part='other', function_=fnct_other_configuration) groups = Configuration._merge_groups('bloks', 'other') self.assertEqual(groups, { 'new-group': [fnct_configuration, fnct_other_configuration], 'old-group': [fnct_configuration, fnct_other_configuration]})
def registry2doc(application, configuration_groups, **kwargs): """Return auto documentation for the registry :param application: name of the application :param configuration_groups: list configuration groupe to load :param \**kwargs: ArgumentParser named arguments """ format_configuration(configuration_groups, 'doc', 'schema') registry = anyblok.start(application, configuration_groups=configuration_groups, **kwargs) if registry: registry.commit() doc = registry.Documentation() doc.auto_doc() if Configuration.get('doc_format') == 'RST': with open(Configuration.get('doc_output'), 'w') as fp: doc.toRST(fp) elif Configuration.get('doc_format') == 'UML': format_ = Configuration.get('schema_format') name_ = Configuration.get('schema_output') dot = ModelSchema(name_, format=format_) doc.toUML(dot) dot.save() elif Configuration.get('doc_format') == 'SQL': format_ = Configuration.get('schema_format') name_ = Configuration.get('schema_output') dot = SQLSchema(name_, format=format_) doc.toSQL(dot) dot.save()
def createdb(application, configuration_groups, **kwargs): """ Create a database and install blok from config :param application: name of the application :param configuration_groups: list configuration groupe to load :param \**kwargs: ArgumentParser named arguments """ format_configuration(configuration_groups, 'create_db', 'install-bloks', 'install-or-update-bloks') load_init_function_from_entry_points() Configuration.load(application, configuration_groups=configuration_groups, **kwargs) BlokManager.load() db_name = Configuration.get('db_name') db_template_name = Configuration.get('db_template_name', None) url = Configuration.get('get_url')(db_name=db_name) create_database(url, template=db_template_name) registry = RegistryManager.get(db_name) if registry is None: return if Configuration.get('install_all_bloks'): bloks = registry.System.Blok.list_by_state('uninstalled') else: install_bloks = Configuration.get('install_bloks') or [] iou_bloks = Configuration.get('install_or_update_bloks') or [] bloks = list(set(install_bloks + iou_bloks)) registry.upgrade(install=bloks) registry.commit() registry.close()
def test_parse_option(self): kwargs = {'test': 'value'} args = MockArgParseArguments(configfile="mock_configuration_file.cfg", kwargs=kwargs) Configuration.parse_options(args, ()) kwargs.update({ 'db_name': 'anyblok', 'db_driver_name': 'postgres', 'db_user_name': '', 'db_password': '', 'db_host': 'localhost', 'db_port': '', }) self.assertConfig(kwargs)
def test_add_application_properties(self): self.assertIsNone( Configuration.applications.get('test_add_application_properties')) Configuration.add_application_properties( 'test_add_application_properties', ['logging'], description='Just a test') self.assertIsNotNone( Configuration.applications.get('test_add_application_properties')) self.assertEqual( Configuration.applications['test_add_application_properties'], { 'configuration_groups': ['config', 'database', 'logging'], 'description': 'Just a test' })
def create_database(database): """ Create a new database, initialize it and return an AnyBlok registry :param: database's name :rtype: AnyBlok registry instance """ url = Configuration.get('get_url')(db_name=database) if database_exists(url): raise Exception("Database %r already exist") db_template_name = Configuration.get('db_template_name', None) SU_create_database(url, template=db_template_name) registry = RegistryManager.get(database) return registry
def test_class_plugins_config(self): option = 'anyblok.tests.test_config:MockPluginClass' Configuration.configuration['option'] = ConfigOption( option, AnyBlokPlugin) res = Configuration.get('option') assert MockPluginClass is res
def test_update2(self): Configuration.update(dict(one_option=1)) assert Configuration.get('one_option') == 1
def test_set_float(self): Configuration.set('value', 1.) assert Configuration.configuration['value'].type == float assert Configuration.get('value') == 1.
def test_add_argument_float(self): parser = self.get_parser() parser.add_argument('--value', dest='value', type=float, default=1) assert Configuration.configuration['value'].type == float assert Configuration.get('value') == 1.
def test_add_argument_list(self): parser = self.get_parser() parser.add_argument('--value', dest='value', nargs="+", default='1, 2') assert Configuration.get('value') == ['1', '2']
def test_add_application_properties_and_load_it(self): Configuration.add_application_properties( 'test_add_application_properties', ['logging']) parser = MockArgumentParser() # add twice to check doublon Configuration.add('logging', function_=add_logging, label='Logging') Configuration.add('logging', function_=add_logging, label='Logging') Configuration.add('database', function_=add_database, label='Database') Configuration.add('config', function_=add_configuration_file) Configuration.add('install-bloks', function_=add_install_bloks) Configuration._load(parser, ['config', 'database', 'logging'])
def clean(): url = Configuration.get('get_url')() drop_database(url) db_template_name = Configuration.get('db_template_name', None) create_database(url, template=db_template_name)
def test_add(self): Configuration.add('new-group', function_=fnct_configuration) self.assertAdded('new-group', function_=fnct_configuration)
def test_has(self): assert Configuration.has('option') is False Configuration.configuration['option'] = ConfigOption('option', str) assert Configuration.has('option') is True
def test_set_dict(self): Configuration.set('value', {'a': 1}) assert Configuration.configuration['value'].type == dict assert Configuration.get('value') == {'a': 1}
def test_load_without_configuration_groupes(self): assert Configuration.load('default') is None
def beaker_settings(settings): """Add in settings the default value for beaker configuration :param settings: dict of the existing settings """ settings.update({ 'beaker.session.data_dir': Configuration.get('beaker.session.data_dir'), 'beaker.session.lock_dir': Configuration.get('beaker.session.lock_dir'), 'beaker.session.memcache_module': Configuration.get('beaker.session.memcache_module'), 'beaker.session.type': Configuration.get('beaker.session.type'), 'beaker.session.url': Configuration.get('beaker.session.url'), 'beaker.session.cookie_expires': Configuration.get('beaker.session.cookie_expires'), 'beaker.session.cookie_domain': Configuration.get('beaker.session.cookie_domain'), 'beaker.session.key': Configuration.get('beaker.session.key'), 'beaker.session.secret': Configuration.get('beaker.session.secret'), 'beaker.session.secure': Configuration.get('beaker.session.secure'), 'beaker.session.timeout': Configuration.get('beaker.session.timeout'), 'beaker.session.encrypt_key': Configuration.get('beaker.session.encrypt_key'), 'beaker.session.validate_key': Configuration.get('beaker.session.validate_key'), })
def test_initialize_logging(self): Configuration.add('logging', function_=add_logging, label='Logging') Configuration.set('logging_level', 'DEBUG') Configuration.set('logging_level_qualnames', ['test']) Configuration.initialize_logging()
def load_configuration(): load_init_function_from_entry_points(unittest=True) Configuration.load_config_for_test() Configuration.parse_options(MockParser()) configuration_post_load(unittest=True)
def test_default_str(self): parser = self.get_parser() parser.add_argument('--value', dest='value', default='1') parser.set_defaults(value='2') assert Configuration.get('value') == '2'
def test_load_with_bad_configuration_groupes(self): Configuration.load('default', configuration_groups=['bad-groups']) self.assertConfig(MockArguments.vals)
def test_set_str(self): Configuration.set('value', '1') assert Configuration.configuration['value'].type == str assert Configuration.get('value') == '1'
def test_parse_option_kwargs(self): kwargs = {'test': 'value'} args = MockArgParseArguments(kwargs=kwargs) Configuration.parse_options(args) self.assertConfig(kwargs)
def test_add_argument_str(self): parser = self.get_parser() parser.add_argument('--value', dest='value', default='1') assert Configuration.configuration['value'].type == str assert Configuration.get('value') == '1'
def test_set_int(self): Configuration.set('value', 1) assert Configuration.configuration['value'].type == int assert Configuration.get('value') == 1
def test_load_with_configuration_groupes(self): Configuration.load('default', configuration_groups=['install-bloks']) self.assertConfig(MockArguments.vals)
def getBasicAuthAuthenticationPolicy(): """Define basic auth authentication policy""" return BasicAuthAuthenticationPolicy( Configuration.get('pyramid_basicauth_check'), debug=Configuration.get('pyramid_authentication_debug'))
def test_parse_option_args(self): args = ('test', ) args = MockArgParseArguments(args=args) with pytest.raises(ConfigurationException): Configuration.parse_options(args)
def getSessionAuthenticationPolicy(): """Define the session based authentication policy""" return SessionAuthenticationPolicy( prefix=Configuration.get('pyramid_session_prefix'), callback=Configuration.get('pyramid_authentication_callback'), debug=Configuration.get('pyramid_authentication_debug'))
def test_empty_parse_option(self): args = MockArgParseArguments() Configuration.parse_options(args) assert Configuration.configuration == {}
def getRemoteUserAuthenticationPolicy(): """Define the authentication policy for remote user server""" return RemoteUserAuthenticationPolicy( environ_key=Configuration.get('pyramid_remoteuser_environ_key'), callback=Configuration.get('pyramid_authentication_callback'), debug=Configuration.get('pyramid_authentication_debug'))
def test_set_list(self): Configuration.set('value', [1]) assert Configuration.configuration['value'].type == list assert Configuration.get('value') == [1]
def test_get(self): option = 'My option' Configuration.configuration['option'] = ConfigOption(option, str) res = Configuration.get('option') assert option == res
def test_set_tuple(self): Configuration.set('value', (1, )) assert Configuration.configuration['value'].type == tuple assert Configuration.get('value') == (1, )
def test_remove_more_function(self): Configuration.add('new-group', function_=fnct_configuration) Configuration.add('new-group', function_=fnct_other_configuration) Configuration.remove('new-group', function_=fnct_configuration) assert Configuration.groups['new-group'] == [fnct_other_configuration]