From 1fab7b8c3bc9c4fc2b11f77cef2ecd4ac3854928 Mon Sep 17 00:00:00 2001 From: Jorrit Schaap <schaap@astron.nl> Date: Thu, 19 Sep 2019 12:14:37 +0200 Subject: [PATCH] SW-816: major speedup of test database setup and preparations for each test --- .../tests/radb_common_testing.py | 61 ++++++++++--------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/SAS/ResourceAssignment/ResourceAssignmentDatabase/tests/radb_common_testing.py b/SAS/ResourceAssignment/ResourceAssignmentDatabase/tests/radb_common_testing.py index 8dbcf72906b..e1d89daaf30 100755 --- a/SAS/ResourceAssignment/ResourceAssignmentDatabase/tests/radb_common_testing.py +++ b/SAS/ResourceAssignment/ResourceAssignmentDatabase/tests/radb_common_testing.py @@ -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? -- GitLab