Skip to content

Commit 87b2437

Browse files
committed
test: Fix FATAL: sorry, too many clients already
By moving two Pytest fixtures responsible for database connectivity from the "function" scope to the "session" scope, the number of redundant invocations to `sqlalchemy.create_engine()` can be significantly reduced. Apparently, this fixes to connection pool overflow.
1 parent c446d94 commit 87b2437

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

target_postgres/tests/test_target_postgres.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def postgres_config_ssh_tunnel_fixture():
4747
return postgres_config_ssh_tunnel()
4848

4949

50-
@pytest.fixture
50+
@pytest.fixture(scope="session")
5151
def postgres_target(postgres_config) -> TargetPostgres:
5252
return TargetPostgres(config=postgres_config)
5353

@@ -81,6 +81,7 @@ class AssertionHelper:
8181
def __init__(self, target: TargetPostgres, metadata_column_prefix: str):
8282
self.target = target
8383
self.metadata_column_prefix = metadata_column_prefix
84+
self.engine = create_engine(self.target)
8485

8586
def remove_metadata_columns(self, row: dict) -> dict:
8687
new_row = {}
@@ -107,9 +108,8 @@ def verify_data(
107108
table, as determined by lowest primary_key value, or else a list of
108109
dictionaries representing every row in the table.
109110
"""
110-
engine = create_engine(self.target)
111111
full_table_name = f"{self.target.config['default_target_schema']}.{table_name}"
112-
with engine.connect() as connection:
112+
with self.engine.connect() as connection:
113113
if primary_key is not None and check_data is not None:
114114
if isinstance(check_data, dict):
115115
result = connection.execute(
@@ -154,9 +154,8 @@ def verify_schema(
154154
it is all about the `type` attribute which is compared.
155155
metadata_column_prefix: The prefix string for metadata columns. Usually `_sdc`.
156156
"""
157-
engine = create_engine(self.target)
158157
schema = self.target.config["default_target_schema"]
159-
with engine.connect() as connection:
158+
with self.engine.connect() as connection:
160159
meta = sqlalchemy.MetaData()
161160
table = sqlalchemy.Table(
162161
table_name, meta, schema=schema, autoload_with=connection
@@ -178,7 +177,7 @@ def verify_schema(
178177
)
179178

180179

181-
@pytest.fixture
180+
@pytest.fixture(scope="session")
182181
def helper(postgres_target) -> AssertionHelper:
183182
return AssertionHelper(
184183
target=postgres_target, metadata_column_prefix=METADATA_COLUMN_PREFIX

0 commit comments

Comments
 (0)