Design an inventory management system
medium 80 minCount goods in several places, promise them to orders without ever promising too much, ship or let go, and explain every count.
Watch lesson4:28
Verify
Trace it before you say done
The point: run the code by hand on one good day and on every bad one, and write the two counts after each call.
Trace
The program from the code tab
| Call | cellar plum | shed plum | cellar fig | Answer |
|---|---|---|---|---|
| deliveries | 6 / 0 | 3 / 0 | 4 / 0 | |
| reserve a: plum 2, fig 1 | 6 / 0 | 3 / 2 | 4 / 1 | Picks from shed and cellar; alert for the shed |
| reserve b: fig 1, plum 9 | 6 / 0 | 3 / 2 | 4 / 1 | OutOfStock, nothing changed |
| ship a | 6 / 0 | 1 / 0 | 3 / 0 | Two movements |
| reserve c: plum 5 | 6 / 5 | 1 / 0 | 3 / 0 | Cellar only; alert for the cellar (1 left) |
| expire at 1000 | 6 / 0 | 1 / 0 | 3 / 0 | 1 promise closed |
| ship c | 6 / 0 | 1 / 0 | 3 / 0 | NotHeld |
Edges
Small inputs that break large designs
| Input | Expected | Which line does it |
|---|---|---|
| Want exactly what is available | Held, available 0 | qty <= self.available |
| Want 0 or -1 | ValueError | First lines of reserve |
| Want nothing at all | ValueError | not wanted |
| Unknown product | OutOfStock | The policy sees no lines and answers None |
Unknown place in receive | KeyError | Goods are never invented for a place |
| Ship twice | NotHeld | Status is SHIPPED |
| Release after ship | NotHeld | Same check |
| Ship in the second of the end | NotHeld | expires_at <= now |
| Listener raises an exception | The promise stands | It is told after the block |
Invariants
Four sentences, checked by a program
Python
def check(shop): for (sku, place), line in shop._lines.items(): assert 0 <= line.held <= line.on_hand moved = sum(m.change for m in shop._movements if (m.sku, m.place) == (sku, place)) assert moved == line.on_hand promised = sum(p.qty for r in shop._reservations.values() if r.status is Status.HELD for p in r.picks if (p.sku, p.place) == (sku, place)) assert promised == line.heldWhich rule broke tells you where to look