def test_prefers_current_stock_batches_to_shipments(): in_stock_batch = Batch("in-stock-batch", "RETRO-CLOCK", 100, eta=None) shipment_batch = Batch("shipment-batch", "RETRO-CLOCK", 100, eta=tomorrow) line = OrderLine("oref", "RETRO-CLOCK", 10) allocate(line, [in_stock_batch, shipment_batch]) assert in_stock_batch.available_quantity == 90 assert shipment_batch.available_quantity == 100
def test_prefers_earlier_batches(): earliest = Batch("speedy-batch", "MINIMALIST-SPOON", 100, eta=today) medium = Batch("normal-batch", "MINIMALIST-SPOON", 100, eta=tomorrow) latest = Batch("slow-batch", "MINIMALIST-SPOON", 100, eta=later) line = OrderLine("order1", "MINIMALIST-SPOON", 10) allocate(line, [medium, earliest, latest]) assert earliest.available_quantity == 90 assert medium.available_quantity == 100 assert latest.available_quantity == 100
def test_prefers_warehouse_batches_to_shipments(): batch_in_warehouse = Batch("batch-001", "DESK-LAMP", 100, None) batch_tomorrow = Batch("batch-002", "DESK-LAMP", 100, tomorrow) line = OrderLine("oref", "DESK-LAMP", 10) batches = [batch_in_warehouse, batch_tomorrow] selected_batch_ref = allocate(line, batches) assert selected_batch_ref == batch_in_warehouse.reference and batch_in_warehouse.available_quantity == 90
def test_returns_allocated_ref(): in_stock_batch = Batch("in-stock-batch", "RETRO-CLOCK", 100, eta=None) shipment_batch = Batch("shipment-batch", "RETRO-CLOCK", 100, eta=tomrrow) line = OrderLine("oref", "RETRO-CLOCK", 10) allocations = allocate(line, [in_stock_batch, shipment_batch]) assert allocations == in_stock_batch.reference
def allocate(orderid: str, sku: str, qty: int, uow: unit_of_work.AbstractUnitOfWork) -> str: line = model.OrderLine(orderid, sku, qty) with uow: batches = uow.batches.list() if not is_valid_sku(sku, batches): raise InvalidSku(f"Invalid sku {sku}") batchref = model.allocate(line, batches) uow.commit() return batchref
def test_returns_allocated_batch_ref(): in_stock_batch = Batch("in-stock-batch-ref", "HIGHBROW-POSTER", 100, eta=None) shipment_batch = Batch("shipment-batch-ref", "HIGHBROW-POSTER", 100, eta=tomorrow) line = OrderLine("oref", "HIGHBROW-POSTER", 10) allocation = allocate(line, [in_stock_batch, shipment_batch]) assert allocation == in_stock_batch.reference
def test_raises_out_of_stock_exception_if_cannot_allocate(): batch = Batch('batch1', 'SMALL-FORK', 10, eta=today) allocate(OrderLine('order1', 'SMALL-FORK', 10), [batch]) with pytest.raises(OutOfStock, match='SMALL-FORK'): allocate(OrderLine('order2', 'SMALL-FORK', 1), [batch])
def test_raises_out_of_stock_exception_if_cannot_allocate(): batch = Batch("batch1", "SMALL-FORK", 10, eta=today) allocate(OrderLine("order1", "SMALL-FORK", 10), [batch]) with pytest.raises(OutOfStock, match="SMALL-FORK"): allocate(OrderLine("order2", "SMALL-FORK", 1), [batch])