Testing the last 8 bags on a purchase order with SQLite
A café orders 24 five-pound bags at $33 each. The first shipment arrives with a supplier invoice for 16 bags. That bill comes to $528. Now try to enter a second bill for 9 bags. A check against the original 24 will happily accept it, leaving the café billed for 25. The interesting pair of inputs is 8 and 9 , both tried from exactly the same starting point. Eight should create a $264 bill and…
A café ordered 24 five-pound bags of coffee beans at $33 each. The supplier sent an invoice for 16 bags, totaling $528. When attempting to add a second bill for 9 bags, SQLite mistakenly accepted it, leaving the café billed for 25 bags instead of the expected 25. Interestingly, both 8 and 9 bags were tested from the same starting point.
An 8-bag bill should have generated a $264 bill and left zero bags on order. Conversely, a 9-bag bill should have failed, keeping the first bill and the remaining bags intact. Running the eight-bag case first and then reusing the database for the nine-bag case would only test the scenario of a completely filled order. This Python script, named test_partial_bill.py, uses SQLite's standard library and an in-memory database.
It involves one line of order data, whole bags, and no tax, freight, or cancellations. All amounts are given in cents. The code first updates the order line to add the requested bags, then attempts to insert a bill line based on the new bag count. The test checks if the expected number of bags and bill amounts are in place after each operation. If the number of bags exceeds the remaining quantity, a ValueError is raised.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to