Skip to content

Commit e561be2

Browse files
committed
add integration tests
1 parent 97b5c56 commit e561be2

File tree

3 files changed

+128
-46
lines changed

3 files changed

+128
-46
lines changed

tests/integration/dbapi/async/V2/test_queries_async.py

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from random import randint
55
from typing import Callable, List, Tuple
66

7+
import trio
78
from pytest import mark, raises
89

910
import firebolt.async_db
@@ -283,34 +284,73 @@ async def test_parameterized_query_with_special_chars(connection: Connection) ->
283284
], "Invalid data in table after parameterized insert"
284285

285286

286-
async def test_executemany_bulk_insert(
287-
connection: Connection, fb_numeric_paramstyle: None
287+
@mark.parametrize(
288+
"paramstyle,query,test_data",
289+
[
290+
(
291+
"fb_numeric",
292+
'INSERT INTO "test_tbl" VALUES ($1, $2)',
293+
[(1, "alice"), (2, "bob"), (3, "charlie")],
294+
),
295+
(
296+
"qmark",
297+
'INSERT INTO "test_tbl" VALUES (?, ?)',
298+
[(4, "david"), (5, "eve"), (6, "frank")],
299+
),
300+
],
301+
)
302+
async def test_executemany_bulk_insert_paramstyles(
303+
connection: Connection,
304+
paramstyle: str,
305+
query: str,
306+
test_data: List[Tuple],
307+
create_drop_test_table_setup_teardown_async: Callable,
288308
) -> None:
289-
"""executemany with bulk_insert=True inserts data correctly."""
309+
"""executemany with bulk_insert=True works correctly for both paramstyles."""
310+
# Set the paramstyle for this test
311+
original_paramstyle = firebolt.async_db.paramstyle
312+
firebolt.async_db.paramstyle = paramstyle
313+
# Generate a unique label for this test execution
314+
unique_label = f"test_bulk_insert_async_{paramstyle}_{randint(100000, 999999)}"
315+
table_name = "test_tbl"
316+
290317
try:
291-
firebolt.async_db.paramstyle = "fb_numeric"
318+
c = connection.cursor()
292319

293-
async with connection.cursor() as c:
294-
await c.execute('DROP TABLE IF EXISTS "test_bulk_insert_async"')
295-
await c.execute(
296-
'CREATE FACT TABLE "test_bulk_insert_async"(id int, name string) primary index id'
297-
)
320+
# Can't do this for fb_numeric yet - FIR-49970
321+
if paramstyle != "fb_numeric":
322+
await c.execute(f"SET query_label = '{unique_label}'")
298323

299-
await c.executemany(
300-
'INSERT INTO "test_bulk_insert_async" VALUES ($1, $2)',
301-
[(1, "alice"), (2, "bob"), (3, "charlie")],
302-
bulk_insert=True,
303-
)
324+
# Execute bulk insert
325+
await c.executemany(
326+
query,
327+
test_data,
328+
bulk_insert=True,
329+
)
304330

305-
await c.execute('SELECT * FROM "test_bulk_insert_async" ORDER BY id')
306-
data = await c.fetchall()
307-
assert len(data) == 3
308-
assert data[0] == [1, "alice"]
309-
assert data[1] == [2, "bob"]
310-
assert data[2] == [3, "charlie"]
331+
# Verify the data was inserted correctly
332+
await c.execute(f'SELECT * FROM "{table_name}" ORDER BY id')
333+
data = await c.fetchall()
334+
assert len(data) == len(test_data)
335+
for i, (expected_id, expected_name) in enumerate(test_data):
336+
assert data[i] == [expected_id, expected_name]
337+
338+
# Verify that only one INSERT query was executed with our unique label
339+
# Can't do this for fb_numeric yet - FIR-49970
340+
if paramstyle != "fb_numeric":
341+
# Wait a moment to ensure query history is updated
342+
await trio.sleep(10)
343+
await c.execute(
344+
"SELECT COUNT(*) FROM information_schema.engine_query_history "
345+
f"WHERE query_label = '{unique_label}' AND query_text LIKE 'INSERT INTO%'"
346+
" AND status = 'ENDED_SUCCESSFULLY'"
347+
)
348+
query_count = (await c.fetchone())[0]
349+
assert (
350+
query_count == 1
351+
), f"Expected 1 INSERT query with label '{unique_label}', but found {query_count}"
311352
finally:
312-
async with connection.cursor() as c:
313-
await c.execute('DROP TABLE IF EXISTS "test_bulk_insert_async"')
353+
firebolt.async_db.paramstyle = original_paramstyle
314354

315355

316356
async def test_multi_statement_query(connection: Connection) -> None:

tests/integration/dbapi/conftest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
LOGGER = getLogger(__name__)
1414

15-
CREATE_TEST_TABLE = (
16-
'CREATE DIMENSION TABLE IF NOT EXISTS "test_tbl" (id int, name string)'
17-
)
15+
CREATE_TEST_TABLE = 'CREATE TABLE IF NOT EXISTS "test_tbl" (id int, name string)'
1816
DROP_TEST_TABLE = 'DROP TABLE IF EXISTS "test_tbl" CASCADE'
1917

2018
LONG_SELECT_DEFAULT_V1 = 250000000000
@@ -59,7 +57,7 @@ def create_drop_test_table_setup_teardown(connection: Connection) -> None:
5957

6058
@fixture
6159
async def create_drop_test_table_setup_teardown_async(connection: Connection) -> None:
62-
with connection.cursor() as c:
60+
async with connection.cursor() as c:
6361
await c.execute(CREATE_TEST_TABLE)
6462
yield c
6563
await c.execute(DROP_TEST_TABLE)

tests/integration/dbapi/sync/V2/test_queries.py

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import math
2+
import time
23
from datetime import date, datetime
34
from decimal import Decimal
45
from random import randint
@@ -7,6 +8,7 @@
78

89
from pytest import mark, raises
910

11+
import firebolt.db
1012
from firebolt.client.auth import Auth
1113
from firebolt.common._types import ColType
1214
from firebolt.common.row_set.types import Column
@@ -283,31 +285,73 @@ def test_empty_query(c: Cursor, query: str, params: tuple) -> None:
283285
)
284286

285287

286-
def test_executemany_bulk_insert(
287-
connection: Connection, fb_numeric_paramstyle: None
288+
@mark.parametrize(
289+
"paramstyle,query,test_data",
290+
[
291+
(
292+
"fb_numeric",
293+
'INSERT INTO "test_tbl" VALUES ($1, $2)',
294+
[(1, "alice"), (2, "bob"), (3, "charlie")],
295+
),
296+
(
297+
"qmark",
298+
'INSERT INTO "test_tbl" VALUES (?, ?)',
299+
[(4, "david"), (5, "eve"), (6, "frank")],
300+
),
301+
],
302+
)
303+
def test_executemany_bulk_insert_paramstyles(
304+
connection: Connection,
305+
paramstyle: str,
306+
query: str,
307+
test_data: List[Tuple],
308+
create_drop_test_table_setup_teardown: Callable,
288309
) -> None:
289-
"""executemany with bulk_insert=True inserts data correctly."""
310+
"""executemany with bulk_insert=True works correctly for both paramstyles."""
311+
# Set the paramstyle for this test
312+
original_paramstyle = firebolt.db.paramstyle
313+
firebolt.db.paramstyle = paramstyle
314+
# Generate a unique label for this test execution
315+
unique_label = f"test_bulk_insert_{paramstyle}_{randint(100000, 999999)}"
316+
table_name = "test_tbl"
317+
290318
try:
291-
with connection.cursor() as c:
292-
c.execute('DROP TABLE IF EXISTS "test_bulk_insert"')
319+
c = connection.cursor()
320+
321+
# Can't do this for fb_numeric yet - FIR-49970
322+
if paramstyle != "fb_numeric":
323+
c.execute(f"SET query_label = '{unique_label}'")
324+
325+
# Execute bulk insert
326+
c.executemany(
327+
query,
328+
test_data,
329+
bulk_insert=True,
330+
)
331+
332+
# Verify the data was inserted correctly
333+
c.execute(f'SELECT * FROM "{table_name}" ORDER BY id')
334+
data = c.fetchall()
335+
assert len(data) == len(test_data)
336+
for i, (expected_id, expected_name) in enumerate(test_data):
337+
assert data[i] == [expected_id, expected_name]
338+
339+
# Verify that only one INSERT query was executed with our unique label
340+
# Can't do this for fb_numeric yet - FIR-49970
341+
if paramstyle != "fb_numeric":
342+
# Wait a moment to ensure query history is updated
343+
time.sleep(10)
293344
c.execute(
294-
'CREATE FACT TABLE "test_bulk_insert"(id int, name string) primary index id'
295-
)
296-
c.executemany(
297-
'INSERT INTO "test_bulk_insert" VALUES ($1, $2)',
298-
[(1, "alice"), (2, "bob"), (3, "charlie")],
299-
bulk_insert=True,
345+
"SELECT COUNT(*) FROM information_schema.engine_query_history "
346+
f"WHERE query_label = '{unique_label}' AND query_text LIKE 'INSERT INTO%'"
347+
" AND status = 'ENDED_SUCCESSFULLY'"
300348
)
301-
302-
c.execute('SELECT * FROM "test_bulk_insert" ORDER BY id')
303-
data = c.fetchall()
304-
assert len(data) == 3
305-
assert data[0] == [1, "alice"]
306-
assert data[1] == [2, "bob"]
307-
assert data[2] == [3, "charlie"]
349+
query_count = c.fetchone()[0]
350+
assert (
351+
query_count == 1
352+
), f"Expected 1 INSERT query with label '{unique_label}', but found {query_count}"
308353
finally:
309-
with connection.cursor() as c:
310-
c.execute('DROP TABLE IF EXISTS "test_bulk_insert"')
354+
firebolt.db.paramstyle = original_paramstyle
311355

312356

313357
def test_multi_statement_query(connection: Connection) -> None:

0 commit comments

Comments
 (0)