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 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)