Commit 1b6bc0ea authored by Jorrit Schaap's avatar Jorrit Schaap

TMSS-142: try to use the same <preferred_port> if available

parent 90825a55
......@@ -29,6 +29,7 @@ import testing.postgresql
from lofar.common.dbcredentials import Credentials
from lofar.common.postgres import PostgresDatabaseConnection
from lofar.common.testing.dbcredentials import TemporaryCredentials
from lofar.common.util import find_free_port
class PostgresTestDatabaseInstance():
''' A helper class which instantiates a running postgres server (not interfering with any other test/production postgres servers)
......@@ -36,9 +37,10 @@ class PostgresTestDatabaseInstance():
Derive your own sub-class and implement apply_database_schema with your own sql schema to setup your type of database.
'''
def __init__(self, user: str = 'test_user') -> None:
def __init__(self, user: str = 'test_user', preferred_port: int=5444) -> None:
self._postgresql = None
self.tmp_creds = TemporaryCredentials(user=user)
self.tmp_creds.dbcreds.port = preferred_port
def __enter__(self):
'''create/instantiate the postgres server'''
......@@ -66,7 +68,9 @@ class PostgresTestDatabaseInstance():
'''instantiate the isolated postgres server'''
logger.info('creating test-database instance...')
self._postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)()
factory = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)
factory.settings['port'] = find_free_port(self.tmp_creds.dbcreds.port)
self._postgresql = factory()
# fill credentials with the dynamically created postgress instance (e.g. port changes for each time)
dsn = self._postgresql.dsn()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment