Skip to content

Commit 4c075bf

Browse files
committed
improve test isolation
1 parent e27fbee commit 4c075bf

File tree

3 files changed

+38
-42
lines changed

3 files changed

+38
-42
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -773,18 +773,19 @@ async def test_transaction_commit(
773773
connection: Connection, create_drop_test_table_setup_teardown_async: Callable
774774
) -> None:
775775
"""Test transaction SQL statements with COMMIT."""
776+
table_name = create_drop_test_table_setup_teardown_async
776777
async with connection.cursor() as c:
777778
# Test successful transaction with COMMIT
778779
result = await c.execute("BEGIN TRANSACTION")
779780
assert result == 0, "BEGIN TRANSACTION should return 0 rows"
780781

781-
await c.execute("INSERT INTO \"test_tbl\" VALUES (1, 'committed')")
782+
await c.execute(f"INSERT INTO \"{table_name}\" VALUES (1, 'committed')")
782783

783784
result = await c.execute("COMMIT TRANSACTION")
784785
assert result == 0, "COMMIT TRANSACTION should return 0 rows"
785786

786787
# Verify the data was committed
787-
await c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
788+
await c.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
788789
data = await c.fetchall()
789790
assert len(data) == 1, "Committed data should be present"
790791
assert data[0] == [
@@ -797,23 +798,24 @@ async def test_transaction_rollback(
797798
connection: Connection, create_drop_test_table_setup_teardown_async: Callable
798799
) -> None:
799800
"""Test transaction SQL statements with ROLLBACK."""
801+
table_name = create_drop_test_table_setup_teardown_async
800802
async with connection.cursor() as c:
801803
# Test transaction with ROLLBACK
802804
result = await c.execute("BEGIN") # Test short form
803805
assert result == 0, "BEGIN should return 0 rows"
804806

805-
await c.execute("INSERT INTO \"test_tbl\" VALUES (1, 'rolled_back')")
807+
await c.execute(f"INSERT INTO \"{table_name}\" VALUES (1, 'rolled_back')")
806808

807809
# Verify data is visible within transaction
808-
await c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
810+
await c.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
809811
data = await c.fetchall()
810812
assert len(data) == 1, "Data should be visible within transaction"
811813

812814
result = await c.execute("ROLLBACK") # Test short form
813815
assert result == 0, "ROLLBACK should return 0 rows"
814816

815817
# Verify the data was rolled back
816-
await c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
818+
await c.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
817819
data = await c.fetchall()
818820
assert len(data) == 0, "Rolled back data should not be present"
819821

@@ -822,23 +824,24 @@ async def test_transaction_cursor_isolation(
822824
connection: Connection, create_drop_test_table_setup_teardown_async: Callable
823825
) -> None:
824826
"""Test that one cursor can't see another's data until it commits."""
827+
table_name = create_drop_test_table_setup_teardown_async
825828
cursor1 = connection.cursor()
826829
cursor2 = connection.cursor()
827830

828831
# Start transaction in cursor1 and insert data
829832
result = await cursor1.execute("BEGIN TRANSACTION")
830833
assert result == 0, "BEGIN TRANSACTION should return 0 rows"
831834

832-
await cursor1.execute("INSERT INTO \"test_tbl\" VALUES (1, 'isolated_data')")
835+
await cursor1.execute(f"INSERT INTO \"{table_name}\" VALUES (1, 'isolated_data')")
833836

834837
# Verify cursor1 can see its own uncommitted data
835-
await cursor1.execute('SELECT * FROM "test_tbl" WHERE id = 1')
838+
await cursor1.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
836839
data1 = await cursor1.fetchall()
837840
assert len(data1) == 1, "Cursor1 should see its own uncommitted data"
838841
assert data1[0] == [1, "isolated_data"], "Cursor1 data should match inserted values"
839842

840843
# Verify cursor2 cannot see cursor1's uncommitted data
841-
await cursor2.execute('SELECT * FROM "test_tbl" WHERE id = 1')
844+
await cursor2.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
842845
data2 = await cursor2.fetchall()
843846
assert len(data2) == 0, "Cursor2 should not see cursor1's uncommitted data"
844847

@@ -847,7 +850,7 @@ async def test_transaction_cursor_isolation(
847850
assert result == 0, "COMMIT TRANSACTION should return 0 rows"
848851

849852
# Now cursor2 should be able to see the committed data
850-
await cursor2.execute('SELECT * FROM "test_tbl" WHERE id = 1')
853+
await cursor2.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
851854
data2 = await cursor2.fetchall()
852855
assert len(data2) == 1, "Cursor2 should see committed data after commit"
853856
assert data2[0] == [1, "isolated_data"], "Cursor2 should see the committed data"

tests/integration/dbapi/conftest.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import uuid
23
from datetime import date, datetime, timedelta, timezone
34
from decimal import Decimal
45
from logging import getLogger
@@ -12,8 +13,8 @@
1213

1314
LOGGER = getLogger(__name__)
1415

15-
CREATE_TEST_TABLE = 'CREATE TABLE IF NOT EXISTS "test_tbl" (id int, name string)'
16-
DROP_TEST_TABLE = 'DROP TABLE IF EXISTS "test_tbl" CASCADE'
16+
CREATE_TEST_TABLE = 'CREATE TABLE IF NOT EXISTS "{table}" (id int, name string)'
17+
DROP_TEST_TABLE = 'DROP TABLE IF EXISTS "{table}" CASCADE'
1718

1819
LONG_SELECT_DEFAULT_V1 = 250000000000
1920
LONG_SELECT_DEFAULT_V2 = 350000000000
@@ -29,38 +30,27 @@ def long_test_value_with_default(default: int = 0) -> int:
2930
return long_test_value_with_default
3031

3132

32-
@fixture
33-
def create_drop_test_table_setup_teardown(connection: Connection) -> None:
34-
with connection.cursor() as c:
35-
c.execute(CREATE_TEST_TABLE)
36-
yield c
37-
c.execute(DROP_TEST_TABLE)
38-
39-
40-
@fixture
41-
async def create_server_side_test_table_setup_teardown_async(
42-
connection: Connection,
43-
) -> None:
44-
with connection.cursor() as c:
45-
await c.execute(CREATE_TEST_TABLE)
46-
yield c
47-
await c.execute(DROP_TEST_TABLE)
33+
def generate_unique_table_name() -> str:
34+
"""Generate a unique table name for testing purposes."""
35+
return f"test_table_{uuid.uuid4().hex}"
4836

4937

5038
@fixture
5139
def create_drop_test_table_setup_teardown(connection: Connection) -> None:
40+
table = generate_unique_table_name()
5241
with connection.cursor() as c:
53-
c.execute(CREATE_TEST_TABLE)
54-
yield c
55-
c.execute(DROP_TEST_TABLE)
42+
c.execute(CREATE_TEST_TABLE.format(table=table))
43+
yield table
44+
c.execute(DROP_TEST_TABLE.format(table=table))
5645

5746

5847
@fixture
5948
async def create_drop_test_table_setup_teardown_async(connection: Connection) -> None:
49+
table = generate_unique_table_name()
6050
async with connection.cursor() as c:
61-
await c.execute(CREATE_TEST_TABLE)
62-
yield c
63-
await c.execute(DROP_TEST_TABLE)
51+
await c.execute(CREATE_TEST_TABLE.format(table=table))
52+
yield table
53+
await c.execute(DROP_TEST_TABLE.format(table=table))
6454

6555

6656
@fixture

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -774,18 +774,19 @@ def test_transaction_commit(
774774
connection: Connection, create_drop_test_table_setup_teardown: Callable
775775
) -> None:
776776
"""Test transaction SQL statements with COMMIT."""
777+
table_name = create_drop_test_table_setup_teardown
777778
with connection.cursor() as c:
778779
# Test successful transaction with COMMIT
779780
result = c.execute("BEGIN TRANSACTION")
780781
assert result == 0, "BEGIN TRANSACTION should return 0 rows"
781782

782-
c.execute("INSERT INTO \"test_tbl\" VALUES (1, 'committed')")
783+
c.execute(f"INSERT INTO \"{table_name}\" VALUES (1, 'committed')")
783784

784785
result = c.execute("COMMIT TRANSACTION")
785786
assert result == 0, "COMMIT TRANSACTION should return 0 rows"
786787

787788
# Verify the data was committed
788-
c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
789+
c.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
789790
data = c.fetchall()
790791
assert len(data) == 1, "Committed data should be present"
791792
assert data[0] == [
@@ -798,23 +799,24 @@ def test_transaction_rollback(
798799
connection: Connection, create_drop_test_table_setup_teardown: Callable
799800
) -> None:
800801
"""Test transaction SQL statements with ROLLBACK."""
802+
table_name = create_drop_test_table_setup_teardown
801803
with connection.cursor() as c:
802804
# Test transaction with ROLLBACK
803805
result = c.execute("BEGIN") # Test short form
804806
assert result == 0, "BEGIN should return 0 rows"
805807

806-
c.execute("INSERT INTO \"test_tbl\" VALUES (1, 'rolled_back')")
808+
c.execute(f"INSERT INTO \"{table_name}\" VALUES (1, 'rolled_back')")
807809

808810
# Verify data is visible within transaction
809-
c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
811+
c.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
810812
data = c.fetchall()
811813
assert len(data) == 1, "Data should be visible within transaction"
812814

813815
result = c.execute("ROLLBACK") # Test short form
814816
assert result == 0, "ROLLBACK should return 0 rows"
815817

816818
# Verify the data was rolled back
817-
c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
819+
c.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
818820
data = c.fetchall()
819821
assert len(data) == 0, "Rolled back data should not be present"
820822

@@ -823,23 +825,24 @@ def test_transaction_cursor_isolation(
823825
connection: Connection, create_drop_test_table_setup_teardown: Callable
824826
) -> None:
825827
"""Test that one cursor can't see another's data until it commits."""
828+
table_name = create_drop_test_table_setup_teardown
826829
cursor1 = connection.cursor()
827830
cursor2 = connection.cursor()
828831

829832
# Start transaction in cursor1 and insert data
830833
result = cursor1.execute("BEGIN TRANSACTION")
831834
assert result == 0, "BEGIN TRANSACTION should return 0 rows"
832835

833-
cursor1.execute("INSERT INTO \"test_tbl\" VALUES (1, 'isolated_data')")
836+
cursor1.execute(f"INSERT INTO \"{table_name}\" VALUES (1, 'isolated_data')")
834837

835838
# Verify cursor1 can see its own uncommitted data
836-
cursor1.execute('SELECT * FROM "test_tbl" WHERE id = 1')
839+
cursor1.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
837840
data1 = cursor1.fetchall()
838841
assert len(data1) == 1, "Cursor1 should see its own uncommitted data"
839842
assert data1[0] == [1, "isolated_data"], "Cursor1 data should match inserted values"
840843

841844
# Verify cursor2 cannot see cursor1's uncommitted data
842-
cursor2.execute('SELECT * FROM "test_tbl" WHERE id = 1')
845+
cursor2.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
843846
data2 = cursor2.fetchall()
844847
assert len(data2) == 0, "Cursor2 should not see cursor1's uncommitted data"
845848

@@ -848,7 +851,7 @@ def test_transaction_cursor_isolation(
848851
assert result == 0, "COMMIT TRANSACTION should return 0 rows"
849852

850853
# Now cursor2 should be able to see the committed data
851-
cursor2.execute('SELECT * FROM "test_tbl" WHERE id = 1')
854+
cursor2.execute(f'SELECT * FROM "{table_name}" WHERE id = 1')
852855
data2 = cursor2.fetchall()
853856
assert len(data2) == 1, "Cursor2 should see committed data after commit"
854857
assert data2[0] == [1, "isolated_data"], "Cursor2 should see the committed data"

0 commit comments

Comments
 (0)