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
defsetUpClass(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')
ifcls._postgresql:
logger.info('removing test-database instance at %s',cls.dbcreds.stringWithHiddenPassword())
cls._postgresql.stop()
logger.info('test-database instance removed')
exceptExceptionase:
logger.info('error while removing test-database instance at %s: %s',cls.dbcreds.stringWithHiddenPassword(),e)
...
...
@@ -105,8 +111,9 @@ class PostgresTestMixin():
defclose_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()
ifcls.db:
logger.info('Closing PostgresDatabaseConnection to test-database %s...',cls.db)
cls.db.disconnect()
exceptExceptionase:
logger.error("Error while closing PostgresDatabaseConnection to test-database: %s",e)
...
...
@@ -114,10 +121,11 @@ class PostgresTestMixin():
defprint_database_instance_log(cls):
'''print the log of the testing-database instance (can help debugging sql statements)'''