Skip to content
Snippets Groups Projects
Commit 1fab7b8c authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

SW-816: major speedup of test database setup and preparations for each test

parent 4525fc32
No related branches found
No related tags found
2 merge requests!59Merge LOFAR-Release-4_0 into master,!57Resolve SW-816
......@@ -39,7 +39,7 @@ class RADBCommonTestMixin():
@classmethod
def setUpClass(cls):
logger.info('setting up test RA database...')
logger.info('setting up test database instance...')
# connect to shared test db
cls.postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)()
cls.dbcreds = Credentials()
......@@ -67,21 +67,24 @@ class RADBCommonTestMixin():
conn.commit()
conn.close()
logger.info('Finished setting up test RA database. It is avaiblable at: %s', cls.dbcreds.stringWithHiddenPassword())
logger.info('Finished setting up test database instance. It is avaiblable at: %s', cls.dbcreds.stringWithHiddenPassword())
def setUp(self):
# set up a fresh copy of the RADB sql schema
self._setup_database()
cls.radb = RADatabase(cls.dbcreds)
cls.radb.connect()
# set up radb python module
self.radb = RADatabase(self.dbcreds)
self.radb.connect()
# set up a fresh copy of the RADB sql schema
cls._setup_database(cls.radb)
def tearDown(self):
self.radb.disconnect()
def setUp(self):
# wipe all tables by truncating specification which cascades into the rest.
logger.debug("setUp: Wiping radb tables for each unittest.")
self.radb.executeQuery("TRUNCATE TABLE resource_allocation.specification CASCADE;")
self.radb.commit()
@classmethod
def tearDownClass(cls):
cls.radb.disconnect()
db_log_file_name = os.path.join(cls.postgresql.base_dir, '%s.log' % cls.postgresql.name)
logger.info('Printing test-postgress-database server log: %s', db_log_file_name)
with open(db_log_file_name, 'r') as db_log_file:
......@@ -92,25 +95,25 @@ class RADBCommonTestMixin():
cls.postgresql.stop()
logger.info('test RA removed')
def _setup_database(self):
logger.info('applying RADB sql schema to %s', self.dbcreds.stringWithHiddenPassword())
with PostgresDatabaseConnection(self.dbcreds) as db:
# populate db tables
# These are applied in given order to set up test db
# Note: cannot use create_and_populate_database.sql since '\i' is not understood by cursor.execute()
sql_basepath = os.environ['LOFARROOT'] + "/share/radb/sql/"
sql_createdb_paths = [sql_basepath + "create_database.sql",
sql_basepath + "/add_resource_allocation_statics.sql",
sql_basepath + "/add_virtual_instrument.sql",
sql_basepath + "/add_notifications.sql",
sql_basepath + "/add_functions_and_triggers.sql"]
for sql_path in sql_createdb_paths:
logger.debug("setting up database. applying sql file: %s", sql_path)
with open(sql_path) as sql:
db.executeQuery(sql.read())
@staticmethod
def _setup_database(db: PostgresDatabaseConnection):
logger.info('applying RADB sql schema to %s', db)
# populate db tables
# These are applied in given order to set up test db
# Note: cannot use create_and_populate_database.sql since '\i' is not understood by cursor.execute()
sql_basepath = os.environ['LOFARROOT'] + "/share/radb/sql/"
sql_createdb_paths = [sql_basepath + "create_database.sql",
sql_basepath + "/add_resource_allocation_statics.sql",
sql_basepath + "/add_virtual_instrument.sql",
sql_basepath + "/add_notifications.sql",
sql_basepath + "/add_functions_and_triggers.sql"]
for sql_path in sql_createdb_paths:
logger.debug("setting up database. applying sql file: %s", sql_path)
with open(sql_path) as sql:
db.executeQuery(sql.read())
db.commit()
class RADBCommonTest(RADBCommonTestMixin, unittest.TestCase):
# database created?
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment