Prove the Database Behaves · practice
Prove an Import Rolls Back Mid-Batch
An import rollback is meaningful only when the failure comes after new work has begun. Put a genuinely new CSV row first, a disagreement second, and then prove the earlier row did not survive.
Continue in tests/test_catalog.py. Complete test_failed_import_rolls_back_every_new_item(tmp_path) with import-rollback.db and late-conflict.csv, both below tmp_path. In the database, arrange one stored item such as asset tag KEPT-7 with its original name and commit it. Close that connection before invoking the path-owning import boundary.
Write a valid five-column CSV. Its first row should have a new ID and tag. Its second row should reuse KEPT-7 but change one CSV-owned field, such as the name. The reader and validator should accept the whole file; the disagreement appears only while the staged batch is compared with stored state.
Call exactly catalog_import.import_items_csv(database_path, csv_path) inside pytest.raises(ValueError). Compare the exact message CSV item KEPT-7 disagrees with the stored item.. Then reopen the database and select all five item fields in ID order. The complete result should contain only the original stored row. That assertion proves both halves of the
If the conflicting row comes first, no write has happened before failure, so even a broken per-row commit can pass. A row-count-only assertion is also too weak because one deleted row and one leaked row could still produce the same count. Compare the values that matter.
Press Run. The fixed entry invokes pytest.main(["-q", "-p", "no:cacheprovider", "tests/test_catalog.py"]) and should report:
Database tests passed: 6
Submit checks two broken transaction timings separately: committing each insert and committing before the next row is classified. Each run begins in a fresh project and collects the same six tests. Your late-conflict arrangement should expose both without relying on IDs or text from the starter catalog.
Task
Complete test_failed_import_rolls_back_every_new_item(tmp_path) in tests/test_catalog.py. Put a new CSV row before a later KEPT-7 disagreement, require the exact