@@ -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"
0 commit comments