Skip to content

Commit 10e6d8b

Browse files
committed
Tests: Refactor test cases to use generic db helpers
1 parent 0588bb1 commit 10e6d8b

16 files changed

Lines changed: 276 additions & 336 deletions

tests/stream/test_kafka.py

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from concurrent.futures import ThreadPoolExecutor
33

44
import pytest
5-
import sqlalchemy
65
from confluent_kafka import Producer
76

87
from tests.util import invoke_ingest_command
8+
from tests.util.db import get_query_result
99
from tests.warehouse.settings import DESTINATIONS
1010

1111
# Marked explicitly (not auto-marked by path) because this module lives outside tests/warehouse.
@@ -43,13 +43,10 @@ def run():
4343
assert res.exit_code == 0
4444

4545
def get_output_table():
46-
dest_engine = sqlalchemy.create_engine(dest_uri)
47-
with dest_engine.connect() as conn:
48-
res = conn.exec_driver_sql(
49-
f"select _kafka__data from {topic}.output order by _kafka_msg_id asc"
50-
).fetchall()
51-
dest_engine.dispose()
52-
return res
46+
return get_query_result(
47+
dest_uri,
48+
f"select _kafka__data from {topic}.output order by _kafka_msg_id asc",
49+
)
5350

5451
run()
5552

@@ -118,17 +115,11 @@ def run():
118115
assert res.exit_code == 0
119116

120117
def get_output_table():
121-
dest_engine = sqlalchemy.create_engine(dest_uri)
122-
with dest_engine.connect() as conn:
123-
res = (
124-
conn.exec_driver_sql( # ty: ignore[no-matching-overload, unused-ignore-comment, unused-ignore-comment]
125-
f"SELECT id, temperature, humidity FROM {topic}.output WHERE temperature >= 38.00 ORDER BY id ASC"
126-
)
127-
.mappings()
128-
.fetchall()
129-
)
130-
dest_engine.dispose()
131-
return res
118+
return get_query_result(
119+
dest_uri,
120+
f"SELECT id, temperature, humidity FROM {topic}.output WHERE temperature >= 38.00 ORDER BY id ASC",
121+
mappings=True,
122+
)
132123

133124
run()
134125

@@ -174,17 +165,11 @@ def run():
174165
assert res.exit_code == 0
175166

176167
def get_output_table():
177-
dest_engine = sqlalchemy.create_engine(dest_uri)
178-
with dest_engine.connect() as conn:
179-
res = (
180-
conn.exec_driver_sql(
181-
f'SELECT "partition", "topic", "key", "offset" FROM {topic}.output ORDER BY "partition" ASC, "offset" ASC'
182-
)
183-
.mappings()
184-
.fetchall()
185-
)
186-
dest_engine.dispose()
187-
return res
168+
return get_query_result(
169+
dest_uri,
170+
f'SELECT "partition", "topic", "key", "offset" FROM {topic}.output ORDER BY "partition" ASC, "offset" ASC',
171+
mappings=True,
172+
)
188173

189174
run()
190175

tests/stream/test_mqbridge_kafka.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
import duckdb
1818
import pytest
19-
import sqlalchemy
2019
from confluent_kafka import Producer
2120

2221
from tests.util import invoke_ingest_command
22+
from tests.util.db import get_query_result
2323
from tests.warehouse.settings import DESTINATIONS
2424

2525
# Marked explicitly (not auto-marked by path) because this module lives outside tests/warehouse.
@@ -64,13 +64,11 @@ def run():
6464
assert res.exit_code == 0
6565

6666
def rows():
67-
engine = sqlalchemy.create_engine(dest_uri)
68-
with engine.connect() as conn:
69-
out = conn.exec_driver_sql(
70-
f"select order_id, amount from {topic}.output order by order_id asc"
71-
).fetchall()
72-
engine.dispose()
73-
return [tuple(r) for r in out]
67+
res = get_query_result(
68+
dest_uri,
69+
f"select order_id, amount from {topic}.output order by order_id asc",
70+
)
71+
return [tuple(r) for r in res]
7472

7573
run()
7674
assert rows() == EXPECTED

tests/util/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def as_datetime(date_str: str) -> date:
1414
return datetime.strptime(date_str, "%Y-%m-%d").replace(tzinfo=timezone.utc).date()
1515

1616

17-
def as_datetime2(date_str: str) -> datetime:
17+
def as_datetime_notz(date_str: str) -> datetime:
1818
return datetime.strptime(date_str, "%Y-%m-%d")
1919

2020

tests/util/db.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Any, Sequence, Union
2+
3+
import sqlalchemy as sa
4+
5+
6+
def dbquery(
7+
uri: str, query: str, fetch: bool = False, mappings: bool = False
8+
) -> Union[Sequence[Union[sa.Row[Any], sa.RowMapping]], None]:
9+
"""Query database using SQLAlchemy and optionally return results."""
10+
11+
# CrateDB needs a relaxed SQLAlchemy dialect for querying.
12+
# It will not support advanced features of CrateDB,
13+
# but that's okay in this case.
14+
if uri.startswith("cratedb://"):
15+
uri = uri.replace("cratedb://", "postgresql+psycopg_relaxed://")
16+
17+
engine = sa.create_engine(uri, poolclass=sa.NullPool)
18+
response = None
19+
with engine.connect() as conn:
20+
res = conn.exec_driver_sql(query)
21+
if fetch:
22+
if mappings:
23+
response = res.mappings().fetchall()
24+
else:
25+
response = res.fetchall()
26+
engine.dispose()
27+
return response
28+
29+
30+
def get_query_result(uri: str, query: str, fetch: bool = True, mappings: bool = False):
31+
"""Query database using SQLAlchemy and return results."""
32+
return dbquery(uri, query, fetch=True, mappings=mappings)

0 commit comments

Comments
 (0)