Skip to content

Commit 776443e

Browse files
committed
integration tests
1 parent fc4fe11 commit 776443e

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,52 @@ async def test_select_quoted_bigint(
696696
assert result[0][0] == int(
697697
long_bigint_value
698698
), "Invalid data returned by fetchall"
699+
700+
701+
async def test_transaction_commit(
702+
connection: Connection, create_drop_test_table_setup_teardown_async: Callable
703+
) -> None:
704+
"""Test transaction SQL statements with COMMIT."""
705+
async with connection.cursor() as c:
706+
# Test successful transaction with COMMIT
707+
result = await c.execute("BEGIN TRANSACTION")
708+
assert result == 0, "BEGIN TRANSACTION should return 0 rows"
709+
710+
await c.execute("INSERT INTO \"test_tbl\" VALUES (1, 'committed')")
711+
712+
result = await c.execute("COMMIT TRANSACTION")
713+
assert result == 0, "COMMIT TRANSACTION should return 0 rows"
714+
715+
# Verify the data was committed
716+
await c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
717+
data = await c.fetchall()
718+
assert len(data) == 1, "Committed data should be present"
719+
assert data[0] == [
720+
1,
721+
"committed",
722+
], "Committed data should match inserted values"
723+
724+
725+
async def test_transaction_rollback(
726+
connection: Connection, create_drop_test_table_setup_teardown_async: Callable
727+
) -> None:
728+
"""Test transaction SQL statements with ROLLBACK."""
729+
async with connection.cursor() as c:
730+
# Test transaction with ROLLBACK
731+
result = await c.execute("BEGIN") # Test short form
732+
assert result == 0, "BEGIN should return 0 rows"
733+
734+
await c.execute("INSERT INTO \"test_tbl\" VALUES (1, 'rolled_back')")
735+
736+
# Verify data is visible within transaction
737+
await c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
738+
data = await c.fetchall()
739+
assert len(data) == 1, "Data should be visible within transaction"
740+
741+
result = await c.execute("ROLLBACK") # Test short form
742+
assert result == 0, "ROLLBACK should return 0 rows"
743+
744+
# Verify the data was rolled back
745+
await c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
746+
data = await c.fetchall()
747+
assert len(data) == 0, "Rolled back data should not be present"

tests/integration/dbapi/conftest.py

Lines changed: 1 addition & 3 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

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,52 @@ def test_select_quoted_bigint(
697697
assert result[0][0] == int(
698698
long_bigint_value
699699
), "Invalid data returned by fetchall"
700+
701+
702+
def test_transaction_commit(
703+
connection: Connection, create_drop_test_table_setup_teardown: Callable
704+
) -> None:
705+
"""Test transaction SQL statements with COMMIT."""
706+
with connection.cursor() as c:
707+
# Test successful transaction with COMMIT
708+
result = c.execute("BEGIN TRANSACTION")
709+
assert result == 0, "BEGIN TRANSACTION should return 0 rows"
710+
711+
c.execute("INSERT INTO \"test_tbl\" VALUES (1, 'committed')")
712+
713+
result = c.execute("COMMIT TRANSACTION")
714+
assert result == 0, "COMMIT TRANSACTION should return 0 rows"
715+
716+
# Verify the data was committed
717+
c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
718+
data = c.fetchall()
719+
assert len(data) == 1, "Committed data should be present"
720+
assert data[0] == [
721+
1,
722+
"committed",
723+
], "Committed data should match inserted values"
724+
725+
726+
def test_transaction_rollback(
727+
connection: Connection, create_drop_test_table_setup_teardown: Callable
728+
) -> None:
729+
"""Test transaction SQL statements with ROLLBACK."""
730+
with connection.cursor() as c:
731+
# Test transaction with ROLLBACK
732+
result = c.execute("BEGIN") # Test short form
733+
assert result == 0, "BEGIN should return 0 rows"
734+
735+
c.execute("INSERT INTO \"test_tbl\" VALUES (1, 'rolled_back')")
736+
737+
# Verify data is visible within transaction
738+
c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
739+
data = c.fetchall()
740+
assert len(data) == 1, "Data should be visible within transaction"
741+
742+
result = c.execute("ROLLBACK") # Test short form
743+
assert result == 0, "ROLLBACK should return 0 rows"
744+
745+
# Verify the data was rolled back
746+
c.execute('SELECT * FROM "test_tbl" WHERE id = 1')
747+
data = c.fetchall()
748+
assert len(data) == 0, "Rolled back data should not be present"

0 commit comments

Comments
 (0)