@@ -7162,13 +7162,7 @@ def test_executemany_uuid_insert_and_select(cursor, db_connection):
71627162 db_connection .commit ()
71637163
71647164 # Generate data for insertion
7165- data_to_insert = []
7166- uuids_to_check = {}
7167- for i in range (5 ):
7168- new_uuid = uuid .uuid4 ()
7169- description = f"Item { i } "
7170- data_to_insert .append ((new_uuid , description ))
7171- uuids_to_check [description ] = new_uuid
7165+ data_to_insert = [(uuid .uuid4 (), f"Item { i } " ) for i in range (5 )]
71727166
71737167 # Insert all data with a single call to executemany
71747168 sql = f"INSERT INTO { table_name } (id, description) VALUES (?, ?)"
@@ -7179,23 +7173,23 @@ def test_executemany_uuid_insert_and_select(cursor, db_connection):
71797173 assert cursor .rowcount == 5 , f"Expected 5 rows inserted, but got { cursor .rowcount } "
71807174
71817175 # Fetch all data from the table
7182- cursor .execute (f"SELECT id, description FROM { table_name } " )
7176+ cursor .execute (f"SELECT id, description FROM { table_name } ORDER BY description " )
71837177 rows = cursor .fetchall ()
71847178
71857179 # Verify the number of fetched rows
71867180 assert len (rows ) == len (data_to_insert ), "Number of fetched rows does not match."
7187-
7188- # Verify each fetched row's data and type
7189- for row in rows :
7190- retrieved_uuid , retrieved_desc = row
7191-
7181+
7182+ # Compare inserted and retrieved rows by index
7183+ for i , ( retrieved_uuid , retrieved_desc ) in enumerate ( rows ) :
7184+ expected_uuid , expected_desc = data_to_insert [ i ]
7185+
71927186 # Assert the type is correct
7187+ if isinstance (retrieved_uuid , str ):
7188+ retrieved_uuid = uuid .UUID (retrieved_uuid ) # convert if driver returns str
7189+
71937190 assert isinstance (retrieved_uuid , uuid .UUID ), f"Expected uuid.UUID, got { type (retrieved_uuid )} "
7194-
7195- # Assert the value matches the original data
7196- expected_uuid = uuids_to_check .get (retrieved_desc )
7197- assert expected_uuid is not None , f"Retrieved description '{ retrieved_desc } ' was not in the original data."
71987191 assert retrieved_uuid == expected_uuid , f"UUID mismatch for '{ retrieved_desc } ': expected { expected_uuid } , got { retrieved_uuid } "
7192+ assert retrieved_desc == expected_desc , f"Description mismatch: expected { expected_desc } , got { retrieved_desc } "
71997193
72007194 finally :
72017195 # Clean up the temporary table
@@ -10640,6 +10634,9 @@ def test_executemany_with_uuids(cursor, db_connection):
1064010634 [None , "Item 5" ]
1064110635 ]
1064210636
10637+ # Map descriptions to original UUIDs for O(1) lookup
10638+ uuid_map = {desc : uid for uid , desc in test_data }
10639+
1064310640 # Execute batch insert
1064410641 cursor .executemany (f"INSERT INTO { table_name } (id, description) VALUES (?, ?)" , test_data )
1064510642 cursor .connection .commit ()
@@ -10650,16 +10647,19 @@ def test_executemany_with_uuids(cursor, db_connection):
1065010647
1065110648 assert len (rows ) == len (test_data ), "Number of fetched rows does not match inserted rows."
1065210649
10653- for row in rows :
10654- retrieved_uuid , retrieved_desc = row
10655- for original_uuid , original_desc in test_data :
10656- if original_desc == retrieved_desc :
10657- if original_uuid is None :
10658- assert retrieved_uuid is None , f"Expected None for '{ retrieved_desc } ', got { retrieved_uuid } "
10659- else :
10660- assert isinstance (retrieved_uuid , uuid .UUID ), f"Expected UUID, got { type (retrieved_uuid )} "
10661- assert retrieved_uuid == original_uuid , f"UUID mismatch for '{ retrieved_desc } '"
10662- break
10650+ for retrieved_uuid , retrieved_desc in rows :
10651+ expected_uuid = uuid_map [retrieved_desc ]
10652+
10653+ if expected_uuid is None :
10654+ assert retrieved_uuid is None , f"Expected None for '{ retrieved_desc } ', got { retrieved_uuid } "
10655+ else :
10656+ # Convert string to UUID if needed
10657+ if isinstance (retrieved_uuid , str ):
10658+ retrieved_uuid = uuid .UUID (retrieved_uuid )
10659+
10660+ assert isinstance (retrieved_uuid , uuid .UUID ), f"Expected UUID, got { type (retrieved_uuid )} "
10661+ assert retrieved_uuid == expected_uuid , f"UUID mismatch for '{ retrieved_desc } '"
10662+
1066310663 finally :
1066410664 cursor .execute (f"DROP TABLE IF EXISTS { table_name } " )
1066510665 db_connection .commit ()
0 commit comments