def setUp(self): """Create temporary directory and clean database instance.""" if os.path.isdir(TMP_DIR): shutil.rmtree(TMP_DIR) os.makedirs(TMP_DIR) # Create fresh database instance DatabaseDriver.init_db(connect_string=CONNECT)
def init(self, base_dir): """Initialize the BASEDIR environment variable. Create a fresh database and return an open connection. """ os.environ[config.ENV_BASEDIR] = os.path.abspath(str(base_dir)) connect_string = 'sqlite:{}/auth.db'.format(str(base_dir)) DatabaseDriver.init_db(connect_string=connect_string) return DatabaseDriver.connect(connect_string=connect_string)
def setUp(self): """Create empty directory.""" if os.path.isdir(TMP_DIR): shutil.rmtree(TMP_DIR) os.mkdir(TMP_DIR) os.environ[config.ENV_DATABASE] = CONNECT DatabaseDriver.init_db() os.environ[config.ENV_BASEDIR] = os.path.join(TMP_DIR) self.engine = EngineApi()
def connect(self, base_dir): """Create empty database and open connection.""" connect_string = 'sqlite:{}/auth.db'.format(str(base_dir)) DatabaseDriver.init_db(connect_string=connect_string) con = DatabaseDriver.connect(connect_string=connect_string) sql = 'INSERT INTO registered_user(id, email, secret, active) VALUES(?, ?, ?, ?)' con.execute(sql, (USER_1, USER_1, pbkdf2_sha256.hash(USER_1), 1)) con.execute(sql, (USER_2, USER_2, pbkdf2_sha256.hash(USER_2), 1)) con.execute(sql, (USER_3, USER_3, pbkdf2_sha256.hash(USER_3), 0)) con.commit() return con
def test_init_db(self, tmpdir): """Test initializing the database using the default database.""" if ENV_DATABASE in os.environ: del os.environ[ENV_DATABASE] filename = '{}/my.db'.format(str(tmpdir)) connect_string = 'sqlite:{}'.format(filename) os.environ[ENV_DATABASE] = connect_string # Call the init_db method to create all database tables DatabaseDriver.init_db() # Connect to the database and ensure we can run a simple query without # and SQL error con = DatabaseDriver.connect() assert con.execute('SELECT * from team').fetchone() is None con.close()
def test_run_benchmark(self, tmpdir): """Test running a benchmarks.""" # Initialize the BASEDIR environment variable os.environ[config.ENV_BASEDIR] = os.path.abspath(str(tmpdir)) # Create a new database and open a connection connect_string = 'sqlite:{}/auth.db'.format(str(tmpdir)) DatabaseDriver.init_db(connect_string=connect_string) con = DatabaseDriver.connect(connect_string=connect_string) # Create repository and engine instances repository = BenchmarkRepository( con=con, template_store=TemplateRepository( base_dir=config.get_template_dir(), loader=BenchmarkTemplateLoader(), filenames=['benchmark', 'template', 'workflow'])) engine = BenchmarkEngine(con) # Add with minimal information benchmark = repository.add_benchmark(name='My benchmark', src_dir=TEMPLATE_DIR) template = benchmark.template arguments = { 'names': TemplateArgument(parameter=template.get_parameter('names'), value=FileHandle(DATA_FILE)), 'sleeptime': TemplateArgument(parameter=template.get_parameter('sleeptime'), value=1), 'greeting': TemplateArgument(parameter=template.get_parameter('greeting'), value='Welcome') } run_id, state = engine.run(benchmark, arguments, 'USERID') assert state.is_success() sql = 'SELECT * FROM benchmark_run WHERE run_id = ?' rs = con.execute(sql, (run_id, )).fetchone() assert rs['benchmark_id'] == benchmark.identifier assert rs['user_id'] == 'USERID' assert rs['state'] == state.type_id table_name = bm.PREFIX_RESULT_TABLE + benchmark.identifier sql = 'SELECT * FROM {} WHERE run_id = ?'.format(table_name) rs = con.execute(sql, (run_id, )).fetchone() assert rs['max_line'] == 'Welcome Alice!'
def __init__(self, con=None, backend=None, base_dir=None, urls=None): """Initialize the database connection, the engine backend, the base directory for uploaded files. and the factory for API Urls. If connection or backend are not provided the instances will be created using the respective drivers and the configuration that is given in the environment variables. If the base directory is not given the value is expected to be contained in the environment variable 'benchengine_BASEDIR'. If the base directory does not exist it will be created. If the Url factory is not given it will be instatiated using the base Url that is expected to be present in the respective environment variable. Parameters ---------- con: DB-API 2.0 database connection, optional Connection to underlying database backend: benchengine.benchmark.engine.BenchmarkEngine, optional Workflow execution backend base_dir: string, optional Path to directory to store uploaded and downloaded files urls: benchengine.api.route.UrlFactory, optional Factory for Urls to access API resources Raises ------ ValueError """ # Use database driver to get connection if the connection is not given self.con = con if not con is None else DatabaseDriver.connect() # Set the base directory (either from given argument value or from the # value of the environment variable). Raise error if the base directory # value is None. Otherwise, create the directory if it does not exist. self.base_dir = base_dir if not base_dir is None else config.get_base_dir( ) if self.base_dir is None: raise ValueError('no base directory given') util.create_dir(self.base_dir) # Use default benchmark engine if no backend is given self.backend = backend if not backend is None else BenchmarkEngine( self.con) # Create subfolder to store uploaded files for individual teams self.team_files_dir = config.get_upload_dir() util.create_dir(self.team_files_dir) # Set Url factory and serialized self.urls = urls if not urls is None else UrlFactory() self.serialize = Serializer(self.urls)
def test_connect_sqlite(self, tmpdir): """Test connecting to SQLite3 database.""" # Set the connect string filename = '{}/my.db'.format(str(tmpdir)) connect_string = 'sqlite:{}'.format(filename) # Connect by passing the connect sting (clear environment first) if ENV_DATABASE in os.environ: del os.environ[ENV_DATABASE] con = DatabaseDriver.connect(connect_string=connect_string) self.validate_database(con, filename) # Make sure that database file has been deleted assert not os.path.isfile(filename) # Repeat with the environment variable set os.environ[ENV_DATABASE] = connect_string con.close() con = DatabaseDriver.connect() self.validate_database(con, filename) connect_info = DatabaseDriver.info() assert connect_info.startswith('sqlite3 @ ') os.environ[ENV_DATABASE] = 'unknown' with pytest.raises(ValueError): DatabaseDriver.info() con.close()
def test_connect_invalid_string(self): """Ensure that an exception is thrown if an invalid connections tring is provided to the driver. """ with pytest.raises(ValueError): DatabaseDriver.connect('not a valid connect string')
def connect(self, base_dir): """Create a fresh database and return connected user manager.""" connect_string = 'sqlite:{}/auth.db'.format(str(base_dir)) DatabaseDriver.init_db(connect_string=connect_string) con = DatabaseDriver.connect(connect_string=connect_string) return UserManager(con)