diff --git a/SAS/ResourceAssignment/ResourceAssignmentDatabase/tests/radb_common_testing.py b/SAS/ResourceAssignment/ResourceAssignmentDatabase/tests/radb_common_testing.py index 9c24b56865a008d59420900e01cc9d7a5ab6a1e8..8dbcf72906b8f12c8aa28eb9f819f1afe3774527 100755 --- a/SAS/ResourceAssignment/ResourceAssignmentDatabase/tests/radb_common_testing.py +++ b/SAS/ResourceAssignment/ResourceAssignmentDatabase/tests/radb_common_testing.py @@ -36,75 +36,80 @@ class RADBCommonTestMixin(): ''' A common test mixin class from which you can derive to get a freshly setup postgres testing instance with the latest RADB sql setup scripts applied. ''' - def setUp(self): + + @classmethod + def setUpClass(cls): logger.info('setting up test RA database...') # connect to shared test db - self.postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)() - self.dbcreds = Credentials() - - # set up fixtures - # Note: In theory, this can be moved to the PostgresqlFactory call as kwarg 'on_initialized=populatedb' - # ...but for some reason that was much slower than keeping it here. - self._setup_database() + cls.postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)() + cls.dbcreds = Credentials() # update credentials (e.g. port changes for each test) - self.dbcreds.host = self.postgresql.dsn()['host'] - self.dbcreds.database = self.postgresql.dsn()['database'] - self.dbcreds.port = self.postgresql.dsn()['port'] + cls.dbcreds.host = cls.postgresql.dsn()['host'] + cls.dbcreds.database = cls.postgresql.dsn()['database'] + cls.dbcreds.port = cls.postgresql.dsn()['port'] + + # connect to db as root + conn = psycopg2.connect(**cls.postgresql.dsn()) + cursor = conn.cursor() + + # set credentials to be used during tests + cls.dbcreds.user = 'resourceassignment' + cls.dbcreds.password = 'secret' # cannot be empty... + + # create user role + # Note: NOSUPERUSER currently raises "permission denied for schema virtual_instrument" + # Maybe we want to sort out user creation and proper permissions in the sql scripts? + query = "CREATE USER %s WITH SUPERUSER PASSWORD '%s'" % (cls.dbcreds.user, cls.dbcreds.password) + cursor.execute(query) + + cursor.close() + conn.commit() + conn.close() + + logger.info('Finished setting up test RA database. It is avaiblable at: %s', cls.dbcreds.stringWithHiddenPassword()) + + def setUp(self): + # set up a fresh copy of the RADB sql schema + self._setup_database() # set up radb python module self.radb = RADatabase(self.dbcreds) self.radb.connect() - logger.info('...finished setting up test RA database') def tearDown(self): self.radb.disconnect() - db_log_file_name = os.path.join(self.postgresql.base_dir, '%s.log' % self.postgresql.name) + @classmethod + def tearDownClass(cls): + 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: for line in db_log_file.readlines(): print(" postgres log: %s" % line.strip(), file=sys.stderr) - logger.info('removing test RA database...') - self.postgresql.stop() + logger.info('removing test RA database at %s', cls.dbcreds.stringWithHiddenPassword()) + cls.postgresql.stop() + logger.info('test RA removed') def _setup_database(self): - - # connect to db as root - conn = psycopg2.connect(**self.postgresql.dsn()) - cursor = conn.cursor() - - # set credentials to be used during tests - self.dbcreds.user = 'resourceassignment' - self.dbcreds.password = 'blabla' # cannot be empty... - - # create user role - # Note: NOSUPERUSER currently raises "permission denied for schema virtual_instrument" - # Maybe we want to sort out user creation and proper permissions in the sql scripts? - query = "CREATE USER %s WITH SUPERUSER PASSWORD '%s'" % ( - self.dbcreds.user, - self.dbcreds.password) - cursor.execute(query) - - # 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: - cursor.execute(sql.read()) - - cursor.close() - conn.commit() - conn.close() + 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()) class RADBCommonTest(RADBCommonTestMixin, unittest.TestCase):