@@ -1188,3 +1188,85 @@ async def test_connection_close_no_rollback_with_autocommit_on(
11881188 await check_data_visibility_async (
11891189 table_name , 1 , connection_factory , True , [1 , "autocommit_close_test" ]
11901190 )
1191+
1192+
1193+ async def test_database_switching_with_same_engine_preserves_database_context (
1194+ database_name : str ,
1195+ connection_factory : Callable [..., Connection ],
1196+ ) -> None :
1197+ """
1198+ Async integration test for database context preservation with caching on Firebolt.
1199+
1200+ This test verifies against a live Firebolt instance:
1201+ 1. Connect with database1 + engine1 (cache entry created)
1202+ 2. Connect with database2 + engine1 (should add database2 to cache)
1203+ 3. Cursors from second connection should have database2, not database1
1204+ """
1205+ first_db_name = database_name
1206+ second_db_name = f"{ database_name } _second_async"
1207+
1208+ # Create a system connection to set up test databases
1209+ async with await connection_factory () as system_connection :
1210+ system_cursor = system_connection .cursor ()
1211+
1212+ try :
1213+ # Create the second test database
1214+ await system_cursor .execute (
1215+ f'CREATE DATABASE IF NOT EXISTS "{ second_db_name } "'
1216+ )
1217+
1218+ # First connection: database1 + engine1
1219+ async with await connection_factory (database = first_db_name ) as connection1 :
1220+ cursor1 = connection1 .cursor ()
1221+ await cursor1 .execute ("SELECT current_database()" )
1222+ result1 = await cursor1 .fetchone ()
1223+
1224+ # Verify first connection has correct database
1225+ assert (
1226+ result1 [0 ] == first_db_name
1227+ ), f"First cursor should have database { first_db_name } "
1228+ assert (
1229+ cursor1 .database == first_db_name
1230+ ), f"First cursor database property should be { first_db_name } "
1231+
1232+ # Second connection: database2 + engine1 (same engine)
1233+ async with await connection_factory (database = second_db_name ) as connection2 :
1234+ cursor2 = connection2 .cursor ()
1235+ await cursor2 .execute ("SELECT current_database()" )
1236+ result2 = await cursor2 .fetchone ()
1237+
1238+ # Verify second connection has correct database
1239+ assert result2 [0 ] == second_db_name , (
1240+ f"Second cursor should have database { second_db_name } , "
1241+ f"but got { result2 [0 ]} . This indicates the database context was overwritten."
1242+ )
1243+ assert cursor2 .database == second_db_name , (
1244+ f"Second cursor database property should be { second_db_name } , "
1245+ f"but has { cursor2 .database } . This indicates the database context was overwritten."
1246+ )
1247+
1248+ # Third connection: back to database1 + engine1 (should use cache)
1249+ async with await connection_factory (database = first_db_name ) as connection3 :
1250+ cursor3 = connection3 .cursor ()
1251+ await cursor3 .execute ("SELECT current_database()" )
1252+ result3 = await cursor3 .fetchone ()
1253+
1254+ # Verify third connection has correct database (should be from cache)
1255+ assert result3 [0 ] == first_db_name , (
1256+ f"Third cursor should have database { first_db_name } , "
1257+ f"but got { result3 [0 ]} . This indicates cached database context is incorrect."
1258+ )
1259+ assert cursor3 .database == first_db_name , (
1260+ f"Third cursor database property should be { first_db_name } , "
1261+ f"but has { cursor3 .database } . This indicates cached database context is incorrect."
1262+ )
1263+
1264+ finally :
1265+ # Clean up: Drop the test database
1266+ try :
1267+ await system_cursor .execute (
1268+ f'DROP DATABASE IF EXISTS "{ second_db_name } "'
1269+ )
1270+ except Exception :
1271+ # Ignore cleanup errors to avoid masking the real test failure
1272+ pass
0 commit comments