diff --git a/LCS/PyCommon/test/postgres.py b/LCS/PyCommon/test/postgres.py index ccf44425ae5e53edb3bdd47484b27f04c5fb2afa..2e2a2b792d155b0ce72ec5dff334ac55ac59593a 100755 --- a/LCS/PyCommon/test/postgres.py +++ b/LCS/PyCommon/test/postgres.py @@ -34,6 +34,11 @@ class PostgresTestMixin(): It implements the unittest setUpClass/tearDownClass methods and uses them as a template method pattern to do all the testing-database setup/teardown work for you. ''' + # class variables are initialized in setUpClass + _postgresql = None + dbcreds = None + db = None + @classmethod def setUpClass(cls): logger.info('setting up test-database instance...') @@ -80,9 +85,10 @@ class PostgresTestMixin(): cls.print_database_instance_log() try: - logger.info('removing test-database instance at %s', cls.dbcreds.stringWithHiddenPassword()) - cls._postgresql.stop() - logger.info('test-database instance removed') + if cls._postgresql: + logger.info('removing test-database instance at %s', cls.dbcreds.stringWithHiddenPassword()) + cls._postgresql.stop() + logger.info('test-database instance removed') except Exception as e: logger.info('error while removing test-database instance at %s: %s', cls.dbcreds.stringWithHiddenPassword(), e) @@ -105,8 +111,9 @@ class PostgresTestMixin(): def close_database_connection(cls): ''' close the database connection created in create_database_connection''' try: - logger.info('Closing PostgresDatabaseConnection to test-database %s...', cls.db) - cls.db.disconnect() + if cls.db: + logger.info('Closing PostgresDatabaseConnection to test-database %s...', cls.db) + cls.db.disconnect() except Exception as e: logger.error("Error while closing PostgresDatabaseConnection to test-database: %s", e) @@ -114,10 +121,11 @@ class PostgresTestMixin(): def print_database_instance_log(cls): '''print the log of the testing-database instance (can help debugging sql statements)''' try: - db_log_file_name = os.path.join(cls._postgresql.base_dir, '%s.log' % cls._postgresql.name) - logger.info('Printing test-postgress-database server log for reference: %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) + if cls._postgresql: + db_log_file_name = os.path.join(cls._postgresql.base_dir, '%s.log' % cls._postgresql.name) + logger.info('Printing test-postgress-database server log for reference: %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) except Exception as e: logger.error("Error while printing test-postgress-database server log: %s", e)