Prove the Database Behaves · practice
Prove a Commit by Reopening
A successful checkout can look correct on its writing connection even when it was never committed. Closing that writer and reading through a new connection makes durability observable.
Edit tests/test_catalog.py and complete test_checkout_commit_survives_reopening(tmp_path). Create durable-checkout.db below tmp_path, open it through catalog_db.open_database, and insert one unfamiliar item plus one member who has room to borrow. Commit only this arrangement. Then call catalog_db.checkout_item(connection, 701, 501, 601, "2034-06-12") and assert that it returns the explicit loan ID 701.
Do not call connection.commit() after checkout_item. The finally, open the same path with sqlite3.connect, and read id, item_id, member_id, checked_out_on, and returned_on for loan 701. Compare the exact stored None for the active loan’s
Keep the two connection lifetimes visually separate in the test. That structure makes the durability claim easy to read and prevents a later edit from accidentally reusing the writer.
This is a transaction behavior test. The action is the public checkout call. The decisive assertion comes from a later connection lifetime. A test that reads through the writer can pass while its insert is still pending. A test that commits for the program quietly repairs the very defect it should reveal. Checking only the returned 701 proves the Python return value, not the database outcome.
Press Run. run_tests.py again calls exactly pytest.main(["-q", "-p", "no:cacheprovider", "tests/test_catalog.py"]). Five independent tests should produce:
Database tests passed: 5
Submit runs the correct suite twice, then checks it against a checkout implementation that performs the insert without its transaction context. Your writer’s close rolls that pending row back, so the reopened query should find no loan and the test should fail at the durable observation. That failure says exactly what to inspect: who owns the commit and whether the read happened on a truly new connection.
Task
Complete test_checkout_commit_survives_reopening(tmp_path) in tests/test_catalog.py. Arrange and commit the parents, call catalog_db.checkout_item, close the writer without another commit, reopen the same tmp_path database, and compare all five stored loan fields.