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_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 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 allocate(self, line: OrderLine) -> str: try: batch = next( b for b in sorted(self.batches) if b.can_allocate(line) ) batch.allocate(line) self.events.append(events.Allocated( line.orderid, line.sku, line.qty, batch.reference )) self.version_number += 1 return batch.reference except StopIteration: self.events.append(events.OutOfStock(line.sku)) return None