def setUpClass(cls): if not CHAINERUI_ENV == 'test': raise NotInTestEnvironmentException( 'set environment variable CHAINERUI_ENV=test ' 'when you run this test') db.init_db() db.setup(test_mode=True)
def func_init_db(func_dir): url = 'sqlite:///' + os.path.join(func_dir, 'chainerui.db') db.setup(url=url, echo=False) yield if db._session is not None: db.session.remove() db.remove_db()
def project(func_dir): db.setup(test_mode=True) db.upgrade() project_path = os.path.join(func_dir, 'test_project') _setup_test_project(project_path) project_name = 'my-project' Project.create(project_path, project_name) yield (project_path, project_name) db.session.remove() db.remove_db()
def db_handler(args): """db_handler.""" if args.type == 'create': if args.db is None: db.init_db() return if not db.setup(url=args.db, echo=args.db_echo): return if args.type == 'status': current_rev = db_revision.current_db_revision() print('The current DB schema version:', current_rev) if args.type == 'upgrade': db.upgrade() if args.type == 'revision': db_revision.new_revision() if args.type == 'drop': if args.db is not None: db.downgrade() db.remove_db()
def test_setup_import_error(self): try: import fdb # NOQA pytest.skip( 'expected that fdb is not installed on testing environment') except (ImportError, TypeError): pass from chainerui import db db_url = 'firebird+fdb://user:password@host:3050/path/to/db' assert not db.setup(url=db_url) assert not db._initialized with pytest.raises(ValueError) as e: db.url assert 'DB URL' in str(e.value) with pytest.raises(ValueError) as e: db.engine assert 'database engine' in str(e.value) with pytest.raises(ValueError) as e: db.session assert 'database session' in str(e.value) with pytest.raises(ValueError) as e: db.alembic_config assert 'migration configuration' in str(e.value)
def setUpClass(cls): if not CHAINERUI_ENV == 'test': raise NotInTestEnvironmentException( 'set environment variable CHAINERUI_ENV=test ' 'when you run this test' ) db.init_db() db.setup(test_mode=True) cls.db_file_path = db._sqlite_db_file_path config = db.alembic_config cls._config = config script_dir = alembic.script.ScriptDirectory.from_config(config) revisions = [sc.revision for sc in script_dir.walk_revisions('base', target.revision)][::-1] for rev in revisions[:-1]: alembic.command.upgrade(config, rev)
def test_setup_default_db(self): from chainerui import db db.init_db() assert db.setup(url=None) assert db._initialized assert db.url == 'sqlite:///' + os.path.join(self.dir, 'db', 'chainerui.db') assert db.engine is not None assert db.session is not None assert db.alembic_config is not None
def test_setup_external_db_file(self): from chainerui import db db_dir = os.path.join(self.dir, 'exdb') os.makedirs(db_dir) db_url = 'sqlite:///' + os.path.join(db_dir, 'sqlite.db') assert db.setup(url=db_url) assert db._initialized assert db.url == db_url assert db.engine is not None assert db.session is not None assert db.alembic_config is not None
def result_path(func_dir): db.setup(test_mode=True) db.upgrade() info_path = os.path.join(func_dir, '.chainerui_images') with open(os.path.join(func_dir, 'img1_1.png'), 'w') as f: f.write('text') with open(os.path.join(func_dir, 'img1_2.png'), 'w') as f: f.write('text2') with open(os.path.join(func_dir, 'img2.png'), 'w') as f: f.write('text3') test_data = [{ 'iteration': 1000, 'epoch': 1, 'images': OrderedDict([ ('0', 'img1_1.png'), ('1', 'img1_2.png'), ]) }, { 'iteration': 2000, 'epoch': 2, 'custom': 'test', 'images': OrderedDict([ ('seg', 'img2.png'), ]) }] with open(info_path, 'w') as f: json.dump(test_data, f) yield (func_dir) db.session.remove() db.remove_db()
def project_create_handler(args): """project_create_handler.""" if not db.setup(url=args.db, echo=args.db_echo): return if not _check_db_revision(): return project_path = os.path.abspath(args.project_dir) project_name = args.project_name project = db.session.query(Project).\ filter_by(path_name=project_path).first() if project is None: project = Project.create(project_path, project_name) else: print("Path '{}' has already registered.".format(project.path_name))
def test_setup_not_init_error(self): from chainerui import db assert not db.setup(url=None) assert not db._initialized with pytest.raises(ValueError) as e: db.url assert 'DB URL' in str(e.value) with pytest.raises(ValueError) as e: db.engine assert 'database engine' in str(e.value) with pytest.raises(ValueError) as e: db.session assert 'database session' in str(e.value) with pytest.raises(ValueError) as e: db.alembic_config assert 'migration configuration' in str(e.value)
def server_handler(args): """server_handler.""" if not db.setup(url=args.db, echo=args.db_echo): return if not _check_db_revision(): return app = create_app() listener = '{:s}:{:d}'.format(args.host, args.port) if args.debug: logging.getLogger('werkzeug').disabled = True set_loglevel(logging.DEBUG) app.config['ENV'] = 'development' app.debug = True _show_banner_debug(app, listener) from werkzeug.serving import run_simple run_simple(args.host, args.port, app, use_reloader=True, use_debugger=True, threaded=True) else: app.config['ENV'] = 'production' import gevent from gevent.pywsgi import WSGIServer http_server = WSGIServer(listener, application=app, log=None) def stop_server(): if http_server.started: http_server.stop() gevent.signal(signal.SIGTERM, stop_server) gevent.signal(signal.SIGINT, stop_server) logger.info(' * Environment: {}'.format(app.config['ENV'])) logger.info( ' * Running on http://{}/ (Press CTRL+C to quit)'.format(listener)) try: http_server.serve_forever() except (KeyboardInterrupt, SystemExit): stop_server()
def test_setup_connection_error(self): from chainerui import db db_dir = os.path.join(self.dir, 'not_exist') db_url = 'sqlite:///' + os.path.join(db_dir, 'sqlite.db') assert not db.setup(url=db_url) assert not db._initialized with pytest.raises(ValueError) as e: db.url assert 'DB URL' in str(e.value) with pytest.raises(ValueError) as e: db.engine assert 'database engine' in str(e.value) with pytest.raises(ValueError) as e: db.session assert 'database session' in str(e.value) with pytest.raises(ValueError) as e: db.alembic_config assert 'migration configuration' in str(e.value)
def _setup_db(db_url): test_mode = CHAINERUI_ENV == 'test' echo = CHAINERUI_ENV == 'development' return db.setup(url=db_url, test_mode=test_mode, echo=echo)