def test_records_out_of_stock_event_if_cannot_allocate(): batch = Batch('batch1', 'SMALL-FORK', 10, eta=today) product = Product('SMALL-FORK', [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_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('oref1', 'MINIMALIST-SPOON', 10) allocate(line, [earliest, medium, latest]) assert earliest.available_quantity == 90 assert medium.available_quantity == 100 assert latest.available_quantity == 100
def test_returns_allocated_batch_ref(): in_stock_batch = Batch('in-stock-batch', 'HIGHBROW-POSTER', 100, eta=None) shipment_batch = Batch('shipment-batch', '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 assert in_stock_batch.available_quantity == 90 assert shipment_batch.available_quantity == 100
def test_repository_can_retrieve_a_batch_wit_allocations(session): orderline_id = insert_order_line(session) batch1_id = insert_batch(session, 'batch1') insert_batch(session, 'batch2') insert_allocation(session, orderline_id, batch1_id) repo = repository.SqlAlchemyRepository(session) retrieved = repo.get('batch1') expected = Batch('batch1', 'GENERIC-SOFA', 100, eta=None) assert retrieved == expected #batch.__eq__ only compares reference assert retrieved.sku == expected.sku assert retrieved._purchased_quantity == expected._purchased_quantity assert retrieved._allocations == { OrderLine('order1', 'GENERIC-SOFA', 12), }
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 make_bath_and_line(sku, batch_qty, line_qty): return (Batch('batch-001', sku, qty=batch_qty, eta=date.today), OrderLine('order-123', sku, line_qty))
def test_cannot_allocate_if_skus_do_not_match(): batch = Batch('batch-001', 'ELEGANTE-LAMP', qty=2, eta=date.today) line = OrderLine('order-123', 'SMALL-TABLE', 2) assert batch.can_allocate(line) is False