@@ -15499,76 +15499,88 @@ def test_fixed_length_binary_type(cursor, db_connection):
1549915499
1550015500def test_fetchall_with_integrity_constraint(cursor, db_connection):
1550115501 """
15502- Test that UNIQUE constraint errors are appropriately triggered for multi-row INSERT
15502+ Test that UNIQUE constraint errors are appropriately triggered for multi-row INSERT
1550315503 statements that use OUTPUT inserted.
15504-
15505- This test covers a specific case where SQL Server's protocol has error conditions
15506- that do not become apparent until rows are fetched, requiring special handling
15504+
15505+ This test covers a specific case where SQL Server's protocol has error conditions
15506+ that do not become apparent until rows are fetched, requiring special handling
1550715507 in fetchall().
1550815508 """
1550915509 try:
1551015510 # Setup table with unique constraint
1551115511 cursor.execute("DROP TABLE IF EXISTS #uniq_cons_test")
15512- cursor.execute("""
15512+ cursor.execute(
15513+ """
1551315514 CREATE TABLE #uniq_cons_test (
1551415515 id INTEGER NOT NULL IDENTITY,
1551515516 data VARCHAR(50) NULL,
1551615517 PRIMARY KEY (id),
1551715518 UNIQUE (data)
1551815519 )
15519- """)
15520-
15520+ """
15521+ )
15522+
1552115523 # Insert initial row - should work
15522- cursor.execute("INSERT INTO #uniq_cons_test (data) OUTPUT inserted.id VALUES (?)", ('the data 1',))
15524+ cursor.execute(
15525+ "INSERT INTO #uniq_cons_test (data) OUTPUT inserted.id VALUES (?)", ('the data 1',)
15526+ )
1552315527 cursor.fetchall() # Complete the operation
15524-
15528+
1552515529 # Test single row duplicate - should raise IntegrityError
1552615530 with pytest.raises(mssql_python.IntegrityError):
15527- cursor.execute("INSERT INTO #uniq_cons_test (data) OUTPUT inserted.id VALUES (?)", ('the data 1',))
15531+ cursor.execute(
15532+ "INSERT INTO #uniq_cons_test (data) OUTPUT inserted.id VALUES (?)", ('the data 1',)
15533+ )
1552815534 cursor.fetchall() # Error should be detected here
15529-
15535+
1553015536 # Insert two valid rows in one statement - should work
15531- cursor.execute("INSERT INTO #uniq_cons_test (data) OUTPUT inserted.id VALUES (?), (?)",
15532- ('the data 2', 'the data 3'))
15537+ cursor.execute(
15538+ "INSERT INTO #uniq_cons_test (data) OUTPUT inserted.id VALUES (?), (?)",
15539+ ('the data 2', 'the data 3'),
15540+ )
1553315541 cursor.fetchall()
15534-
15542+
1553515543 # Verify current state
1553615544 cursor.execute("SELECT * FROM #uniq_cons_test ORDER BY id")
1553715545 rows = cursor.fetchall()
1553815546 expected_before = [(1, "the data 1"), (3, "the data 2"), (4, "the data 3")]
1553915547 actual_before = [tuple(row) for row in rows]
1554015548 assert actual_before == expected_before
15541-
15549+
1554215550 # THE CRITICAL TEST: Multi-row INSERT with duplicate values
1554315551 # This should raise IntegrityError during fetchall()
1554415552 with pytest.raises(mssql_python.IntegrityError):
15545- cursor.execute("INSERT INTO #uniq_cons_test (data) OUTPUT inserted.id VALUES (?), (?)",
15546- ('the data 4', 'the data 4')) # Duplicate in same statement
15547-
15553+ cursor.execute(
15554+ "INSERT INTO #uniq_cons_test (data) OUTPUT inserted.id VALUES (?), (?)",
15555+ ('the data 4', 'the data 4'),
15556+ ) # Duplicate in same statement
15557+
1554815558 # The error should be detected HERE during fetchall()
1554915559 cursor.fetchall()
15550-
15560+
1555115561 # Verify table state after failed multi-row insert
1555215562 cursor.execute("SELECT * FROM #uniq_cons_test ORDER BY id")
1555315563 rows = cursor.fetchall()
1555415564 expected_after = [(1, "the data 1"), (3, "the data 2"), (4, "the data 3")]
1555515565 actual_after = [tuple(row) for row in rows]
1555615566 assert actual_after == expected_after, "Table should be unchanged after failed insert"
15557-
15567+
1555815568 # Test timing: execute() should succeed, error detection happens in fetchall()
1555915569 try:
15560- cursor.execute("INSERT INTO #uniq_cons_test (data) OUTPUT inserted.id VALUES (?), (?)",
15561- ('the data 5', 'the data 5'))
15570+ cursor.execute(
15571+ "INSERT INTO #uniq_cons_test (data) OUTPUT inserted.id VALUES (?), (?)",
15572+ ('the data 5', 'the data 5'),
15573+ )
1556215574 execute_succeeded = True
1556315575 except Exception:
1556415576 execute_succeeded = False
15565-
15577+
1556615578 assert execute_succeeded, "execute() should succeed, error detection happens in fetchall()"
15567-
15579+
1556815580 # fetchall() should raise the IntegrityError
1556915581 with pytest.raises(mssql_python.IntegrityError):
1557015582 cursor.fetchall()
15571-
15583+
1557215584 except Exception as e:
1557315585 pytest.fail(f"Integrity constraint multi-row test failed: {e}")
1557415586 finally:
0 commit comments