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_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_returns_allocated_batch_ref(): 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) result = allocate(line, [in_stock_batch, shipment_batch]) assert result == in_stock_batch.reference
def test_prefers_current_stock_batches_to_shipments(): in_stock_batch = Batch(ref="in-stock-batch", sku="RETRO-CLOCK", purchased_quantity=100, eta=datetime.datetime.now()) shipment_batch = Batch(ref="shipment-batch", sku="RETRO-CLOCK", purchased_quantity=100, eta=datetime.datetime.now()+datetime.timedelta(days=1)) line = OrderLine(orderid="oref", sku="RETRO-CLOCK", qty=50) allocate(line, [in_stock_batch, shipment_batch]) print(in_stock_batch) print(in_stock_batch.allocations)
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", "RETRO-CLOCK", 100, eta=today) medium = Batch("normal-batch", "RETRO-CLOCK", 100, eta=tomorrow) latest = Batch("slow-batch", "RETRO-CLOCK", 100, eta=later) line = OrderLine("oref", "RETRO-CLOCK", 10) allocate(line, [medium, latest, earliest]) assert earliest.available_quantity == 90 assert medium.available_quantity == 100 assert latest.available_quantity == 100
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) repo = FakeRepository([in_stock_batch, shipment_batch]) session = FakeSession() line = OrderLine('oref', "RETRO-CLOCK", 10) handlers.allocate(line, repo, session) assert in_stock_batch.available_quantity == 90 assert shipment_batch.available_quantity == 100
def test_records_out_of_stock_event_if_cannot_allocate(): batch = Batch('batch1', 'SMALL-FORK', 10, eta=date.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-WORK") assert allocation is None
def test_repository_can_save_a_batch(): batch1 = Batch(ref="in-stock-batch1", sku="RETRO-CLOCK", purchased_quantity=100, eta=datetime.datetime.now()) batch2 = Batch(ref="in-stock-batch2", sku="RETRO-CLOCK", purchased_quantity=110, eta=datetime.datetime.now()) batch3 = Batch(ref="in-stock-batch3", sku="RETRO-CLOCK", purchased_quantity=120, eta=datetime.datetime.now()) batch4 = Batch(ref="in-stock-batch4", sku="RETRO-CLOCK", purchased_quantity=130, eta=datetime.datetime.now()) repo = repository.FakeRepository() repo.add(batch1) repo.add(batch2) repo.add(batch3) repo.add(batch4) print(repo._batches) data=repo.get("in-stock-batch1") # print(repo.list()) # test_repository_can_save_a_batch()
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_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])
async def get(self, id_: UUID) -> Batch: data = database[id_] print("GET, data", data) batch_data = Batch(**data) return batch_data
def test_allocating_to_a_batch_reduces_the_available_quantity(): batch = Batch(ref="batch-001", sku="SMALL-TABLE", purchased_quantity=20) line = OrderLine(orderid="order-ref", sku="SMALLs-TABLE", qty=2) batch.allocate(line) print(batch.allocations)