|
| 1 | +import pytest |
| 2 | + |
| 3 | +from datetime import date, timedelta |
| 4 | +from model import allocate, OrderLine, Batch, OutOfStock |
| 5 | + |
| 6 | +today = date.today() |
| 7 | +tomorrow = today + timedelta(days=1) |
| 8 | +later = today + timedelta(days=10) |
| 9 | + |
| 10 | +def test_prefers_current_stock_batches_to_shipments(): |
| 11 | + in_stock_batch = Batch("in-stock-batch", "RETRO-CLOCK", 100, eta=None) |
| 12 | + shipment_batch = Batch("shipment-batch", "RETRO-CLOCK", 100, eta=tomorrow) |
| 13 | + line = OrderLine("oref", "RETRO-CLOCK", 10) |
| 14 | + |
| 15 | + allocate(line, [in_stock_batch, shipment_batch]) |
| 16 | + |
| 17 | + assert in_stock_batch.available_quantity == 90 |
| 18 | + assert shipment_batch.available_quantity == 100 |
| 19 | + |
| 20 | +def test_prefers_earlier_batches(): |
| 21 | + earliest = Batch("speedy-batch", "MINIMALIST-SPOON", 100, eta=today) |
| 22 | + medium = Batch("normal-batch", "MINIMALIST-SPOON", 100, eta=tomorrow) |
| 23 | + latest = Batch("slow-batch", "MINIMALIST-SPOON", 100, eta=later) |
| 24 | + line = OrderLine("order1", "MINIMALIST-SPOON", 10) |
| 25 | + |
| 26 | + allocate(line, [medium, earliest, latest]) |
| 27 | + |
| 28 | + assert earliest.available_quantity == 90 |
| 29 | + assert medium.available_quantity == 100 |
| 30 | + assert latest.available_quantity == 100 |
| 31 | + |
| 32 | +def test_returns_allocated_batch_ref(): |
| 33 | + in_stock_batch = Batch("in-stock-batch-ref", "HIGHBROW-POSTER", 100, eta=None) |
| 34 | + shipment_batch = Batch("shipment-batch-ref", "HIGHBROW-POSTER", 100, eta=tomorrow) |
| 35 | + line = OrderLine("oref", "HIGHBROW-POSTER", 10) |
| 36 | + |
| 37 | + allocation = allocate(line, [in_stock_batch, shipment_batch]) |
| 38 | + |
| 39 | + assert allocation == in_stock_batch.reference |
| 40 | + |
| 41 | +def test_raises_out_of_stock_exception_if_cannot_allocate(): |
| 42 | + batch = Batch("batch1", "SMALL-FORK", 10, eta=today) |
| 43 | + line = OrderLine("order1", "SMALL-FORK", 10) |
| 44 | + |
| 45 | + allocate(line, [batch]) |
| 46 | + |
| 47 | + with pytest.raises(OutOfStock, match="SMALL-FORK"): |
| 48 | + other_line = OrderLine("order2", "SMALL-FORK", 1) |
| 49 | + |
| 50 | + allocate(other_line, [batch]) |
0 commit comments