def test_records_out_of_stock_event_if_cannot_allocate(): batch = Batch('batch1', 'SMALL-FORK', 10, eta=today) product = Product(sku="SMALL-FORK", batches=[batch]) product.allocate(OrderLine('order1', 'SMALL-FORK', 10)) allocation = product.allocate(OrderLine('order2', 'SMALL-FORK', 1)) assert product.events[-1] == events.OutOfStock(sku="SMALL-FORK") assert allocation is None
def test_allocating_to_a_batch_reduces_the_available_quantity(): batch = Batch("batch-001", "SMALL-TABLE", qty=20, eta=date.today()) line = OrderLine('order-ref', "SMALL-TABLE", 2) batch.allocate(line) assert batch.available_quantity == 18
def test_returns_allocated_batch_id(): warehouse_batch = Batch('wh-batch', 'sku1', 100, eta=None) shipment_batch = Batch('sh-batch', 'sku1', 100, eta=tomorrow) line = OrderLine('oref', 'sku1', 10) product = Product(sku='sku1', batches=[warehouse_batch, shipment_batch]) allocation = product.allocate(line, ) assert allocation == 'wh-batch'
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) product = Product(sku="HIGHBROW-POSTER", batches=[in_stock_batch, shipment_batch]) allocation = product.allocate(line) assert allocation == in_stock_batch.reference
def test_increments_version_number(): line = OrderLine('oref', "SCANDI-PEN", 10) product = Product(sku="SCANDI-PEN", batches=[Batch('b1', "SCANDI-PEN", 100, eta=None)]) product.version_number = 7 product.allocate(line) assert product.version_number == 8
def test_records_out_of_stock_event_if_cannot_allocate(): batch = Batch("batch1", "HEAVY-SPOON", 100, eta=today) different_sku_line = OrderLine("oref", "SMALL-FORK", 10) product = Product(sku="HEAVY-SPOON", batches=[batch]) allocation = product.allocate(different_sku_line) assert product.events[-1] == events.OutOfStock(sku="SMALL-FORK") assert allocation is None
def allocate(cmd: commands.Allocate, uow: unit_of_work.AbstractUnitOfWork): line = OrderLine(cmd.orderid, cmd.sku, cmd.qty) with uow: product = uow.products.get(sku=line.sku) if product is None: raise exceptions.InvalidSku(f'Invalid sku {line.sku}') product.allocate(line) uow.commit()
def test_records_out_of_stock_event_if_cannot_allocate(): sku1_batch = Batch('batch1', 'sku1', 100, eta=today) sku2_line = OrderLine('oref', 'sku2', 10) product = Product(sku='sku1', batches=[sku1_batch]) allocation = product.allocate(sku2_line) assert product.events[-1] == events.OutOfStock(sku='sku2') assert allocation is None
def test_outputs_allocated_event(): batch = Batch("batchref", "RETRO-LAMPSHADE", 100, eta=None) line = OrderLine("oref", "RETRO-LAMPSHADE", 10) product = Product(sku="RETRO-LAMPSHADE", batches=[batch]) product.allocate(line) expected = events.Allocated( orderid="oref", sku="RETRO-LAMPSHADE", qty=10, batchref=batch.reference ) assert product.events[-1] == expected
def test_prefers_warehouse_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) product = Product(sku="RETRO-CLOCK", batches=[in_stock_batch, shipment_batch]) line = OrderLine("oref", "RETRO-CLOCK", 10) product.allocate(line) assert in_stock_batch.available_quantity == 90 assert shipment_batch.available_quantity == 100
def test_prefers_warehouse_batches_to_shipments(): warehouse_batch = Batch('wh-batch', 'sku1', 100, eta=None) shipment_batch = Batch('sh-batch', 'sku1', 100, eta=tomorrow) product = Product(sku='sku1', batches=[warehouse_batch, shipment_batch]) line = OrderLine('oref', 'sku1', 10) product.allocate(line) assert warehouse_batch.available_quantity == 90 assert shipment_batch.available_quantity == 100
def test_prefers_earlier_batches(): earliest = Batch('sh-batch', 'sku1', 100, eta=today) medium = Batch('sh-batch', 'sku1', 100, eta=tomorrow) latest = Batch('sh-batch', 'sku1', 100, eta=later) product = Product(sku='sku1', batches=[medium, earliest, latest]) line = OrderLine('oref', 'sku1', 10) product.allocate(line) assert earliest.available_quantity == 90 assert medium.available_quantity == 100 assert latest.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) product = Product(sku="MINIMALIST-SPOON", batches=[medium, earliest, latest]) line = OrderLine("order1", "MINIMALIST-SPOON", 10) product.allocate(line) assert earliest.available_quantity == 90 assert medium.available_quantity == 100 assert latest.available_quantity == 100
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=date.today() + timedelta(days=1)) 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=date.today()) medium = Batch("normal-batch", "MINIMALIST-SPOON", 100, eta=date.today() + timedelta(days=1)) latest = Batch("slow-batch", "MINIMALIST-SPOON", 100, eta=date.today() + timedelta(days=2)) 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_cannot_allocate_if_skus_do_not_match(): batch = Batch("batch-001", "UNCOMFORTABLE-CHAIR", 100, eta=None) different_sku_line = OrderLine("order-123", "EXPENSIVE-TOASTER", 10) assert batch.can_allocate(different_sku_line) is False
def make_batch_and_line(sku, batch_qty, line_qty): return (Batch("batch-001", sku, batch_qty, eta=date.today()), OrderLine("order-123", sku, line_qty))
def test_cannot_allocate_if_skus_do_not_match(): batch = Batch("batch-001", 'sku1', 100, eta=None) line = OrderLine("order-123", 'sku2', 10) assert batch.can_allocate(line) is False
def test_raises_out_of_stock_exception_if_cannot_allocate(): batch = Batch("batch1", "SMALL-FORK", 10, eta=date.today()) allocate(OrderLine("order1", "SMALL-FORK", 10), [batch]) with pytest.raises(OutOfStock, match="SMALL-FORK"): allocate(OrderLine("order2", "SMALL-FORK", 1), [batch])