def setUpClass(cls): """ Set up sqlite database in a temp directory. Databse created in this step is restored for each test. """ super(DatabaseCase, cls).setUpClass() cls.pgdir = os.path.join(TEST_DIR, "pg") cls.postgresql = testing.postgresql.Postgresql(base_dir=cls.pgdir) # load semver extension conn = psycopg2.connect(**cls.postgresql.dsn()) conn.set_isolation_level(0) cur = conn.cursor() try: cur.execute("CREATE EXTENSION IF NOT EXISTS semver;") except: # older PostgreSQL doesn't support CREATE EXTENSION # load semver type manually with open("/usr/share/pgsql/contrib/semver.sql") as f: sql = f.read() cur.execute(sql) conn.close() _set_up_db_conf(cls.postgresql) cls.dbpath = os.path.join(cls.pgdir, "data") cls.clean_dbpath = os.path.join(TEST_DIR, "pg_clean_data") cls.reports_path = os.path.abspath( os.path.join(cpath, "..", "sample_reports")) cls.db = storage.Database(session_kwargs={ "autoflush": False, "autocommit": False }, create_schema=True)
def setUp(self): """ Restore database from clean version. Delete lobs. """ if not os.path.isdir(self.clean_dbpath): # no .clean version, load data and save .clean self.prepare() # required due to mixing of sqlalchemy and flask-sqlalchemy # fixed in flask-sqlalchemy >= 2.0 self.db.session._model_changes = {} self.db.session.commit() self.db.close() shutil.copytree(self.dbpath, self.clean_dbpath) self.postgresql.stop() shutil.rmtree(self.pgdir) self.postgresql = testing.postgresql.Postgresql( base_dir=self.pgdir, copy_data_from=self.clean_dbpath) _set_up_db_conf(self.postgresql) # reinit DB with new version storage.Database.__instance__ = None self.db = storage.Database(session_kwargs={ "autoflush": False, "autocommit": False }) # required due to mixing of sqlalchemy and flask-sqlalchemy # fixed in flask-sqlalchemy >= 2.0 self.db.session._model_changes = {} lobdir = os.path.join(TEST_DIR, "lob") if os.path.exists(lobdir): shutil.rmtree(lobdir)