def test_raises_out_of_stock_exception_if_cannot_allocate(self): batch = Batch('batch1', 'SMALL-FORK', 10, eta=datetime.datetime.now()) line = OrderLine('order1', 'SMALL-FORK', 10) allocate(line, [batch]) with pytest.raises(OutOfStock, match='SMALL-FORK'): line = OrderLine('order2', 'SMALL-FORK', 1) allocate(line, [batch])
def test_raises_out_of_stock_exception_if_cannot_allocate(): batch = Batch("05", "SMALL_FORK", 10, eta=today) line1 = OrderLine("05", "SMALL_FORK", 10) line2 = OrderLine("05", "SMALL_FORK", 1) allocate(line1, [batch]) with pytest.raises(OutOfStock, match="SMALL_FORK"): allocate(line2, [batch])
def test_raises_out_of_stock_exception_if_cannot_allocate(): batch = Batch("batch-ref", "SPOON", 100, eta=today) line = OrderLine("order1", "SPOON", 100) allocate(line, [batch]) with pytest.raises(OutOfStock, match="SPOON"): line2 = OrderLine("order2", "SPOON", 10) allocate(line2, [batch])
def test_cannot_allocate_if_sku_do_not_match(): batch = Batch("batch-001", "UNCOMFORTABLE-CHAIR", 100, manufacture_date=None) different_sku_line = OrderLine("order-123", "EXPENSIVE-TOASTER", 10) assert batch.can_allocate(different_sku_line) is False
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 allocate(): batch, orderline = make_batch_and_line("Lamp", 10, 5) batch.allocate(orderline) batch.allocate(OrderLine("ab", "Lamp", 2)) print(batch.allocated_quantity) print(batch._allocations) print(batch.available_quantity)
def test_returns_allocated_batch_ref(): in_stock_batch = Batch("in-stock-batch-ref", "HIGHBROW-POSTER", 100, manufacture_date=None) shipment_batch = Batch("shipment-batch-ref", "HIGHBROW-POSTER", 100, manufacture_date=tommorrow) line = OrderLine("oref", "HIGHBROW-POSTER", 10) allocation = allocate(line, [in_stock_batch, shipment_batch]) assert allocation == in_stock_batch.reference
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_warehouse_batches_to_shipments(): in_stock_batch = Batch("02", "RETRO_CLOCK", 100, eta=None) shipment_batch = Batch("03", "RETRO_CLOCK", 100, eta=tomorrow) line = OrderLine("02", "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_allocating_to_a_batch_rquantity(): batch = Batch("batch-001", "SMALL-TABLE", quantity=20, manufacture_date=date.today()) line = OrderLine("order-ref", "SMALL-TABLE", 2) batch.allocate(line) assert batch.available_quantity == 18
def test_allocating_to_a_batch_reduces_the_available_quantity(): # Arrange batch = Batch("batch-001", "SMALL-TABLE", qty=20, eta=date.today()) line = OrderLine('order-ref', "SMALL-TABLE", 2) # Act - take action batch.allocate(line) # Assert assert batch.available_quantity == 18
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_earlier_batches(): today_batch = Batch("04", "MINIMALIST-SPOON", 100, eta=today) tomorrow_batch = Batch("05", "MINIMALIST-SPOON", 100, eta=tomorrow) later_batch = Batch("06", "MINIMALIST-SPOON", 100, eta=later) line = OrderLine("03", "MINIMALIST-SPOON", 10) allocate(line, [today_batch, tomorrow_batch, later_batch]) assert today_batch.available_quantity == 90 assert tomorrow_batch.available_quantity == 100 assert later_batch.available_quantity == 100
def test_prefers_current_stock_batches_to_shipments(self=None): """batchesの最初のひとつめにしか処理がかからないことを確認""" tomorrow = (datetime.datetime.now() + datetime.timedelta(days=1)) 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]) self.assertEqual(90, in_stock_batch.available_quantity) self.assertEqual(100, shipment_batch.available_quantity)
def test_prefers_earlier_batches(self): """batchesをソートした上で先頭の要素にallocateが実行されることを確認""" today = datetime.datetime.now() tomorrow = (datetime.datetime.now() + datetime.timedelta(days=1)) later = (datetime.datetime.now() + datetime.timedelta(weeks=4)) 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]) # 順番に注目earliestは2番目 self.assertEqual( 90, earliest.available_quantity) # 2番目にあるearliestにオーダーがかかった self.assertEqual(100, medium.available_quantity) self.assertEqual(100, latest.available_quantity)
def test_returns_allocated_batch_ref(self): """Batchとallocateの返却が一致することを確認""" tomorrow = (datetime.datetime.now() + datetime.timedelta(days=1)) 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]) self.assertTrue(allocation == in_stock_batch.reference)
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("order1", "HIGHBROW-POSTER", 10) ref = allocate(line, [in_stock_batch, shipment_batch]) assert ref == in_stock_batch.reference
def create_order(self, rq_user_id, products): session = self.Session() try: user = session.query(User).get(rq_user_id) if user is None: raise Exception('Unable to find user with id = %s' % rq_user_id) order_line_list = [] user_order = Order(user_id=rq_user_id, order_total=0) for prod_def in products: db_product = session.query(Product).get(prod_def['productId']) if db_product is None: raise Exception('Product not found with ProductID: %s' % prod_def['productId']) order_line = OrderLine(product=db_product, order=user_order, quantity=prod_def['quantity']) order_line_list.append(order_line) user_order.order_total = user_order.order_total + db_product.price * prod_def[ 'quantity'] session.add(user_order) session.add_all(order_line_list) session.commit() the_order = session.query(Order).get(user_order.order_id) return the_order.to_json() except Exception as e: logging.error('*** Exception in create_order: %s' % e) traceback.print_exc() session.rollback() raise finally: session.close()
def test_allocatint_to_a_batch_reduces_available_quantity(): batch = Batch("batch-001", "SMALL-TABLE", qty=20, eta=date.today()) line = OrderLine("order-ref", "SMALL-TABLE", 2) batch.allocate(line) print(batch._allocations)
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_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_allocating_to_a_batch_reduces_the_available_quantity(self): batch = Batch('batch-001', 'SMALL-TABLE', qty=20, eta=date.today()) line = OrderLine('order-ref', "SMALL-TABLE", 2) batch.allocate(line) self.assertEqual(18, batch.available_quantity)
def make_batch_and_line(sku: str, batch_qty: int, line_qty: int): return (Batch("Batch-001", sku, batch_qty, eta=date.today()), OrderLine("Order-123", sku, line_qty))
def test_allocating_to_a_batch_reduces_the_available_quantity(): batch = Batch("1", "SMALL_TABLE", qty=20, eta=today) line = OrderLine("1", "SMALL_TABLE", 2) batch.allocate(line) assert batch.available_quantity == 18
large_batch, small_line = make_batch_and_line("Lamp", 20, 20) print(large_batch.can_allocate(small_line)) print(large_batch.available_quantity) def test_cant_allocate_if_sku_doesnt_match(): batch = Batch("batch-001", "UNCOMFORTABLE-CHAIR", 100, eta=None) different_sku_line = OrderLine("order-123", "EXPENSIVE-TOASTER", 10) print(batch.can_allocate(different_sku_line)) def allocate(): batch, orderline = make_batch_and_line("Lamp", 10, 5) batch.allocate(orderline) batch.allocate(OrderLine("ab", "Lamp", 2)) print(batch.allocated_quantity) print(batch._allocations) print(batch.available_quantity) def test_allocation_is_idempotent(): batch, line = make_batch_and_line("ANGULAR-DESK", 20, 2) batch.allocate(line) batch.allocate(line) return batch order = OrderLine("a", "abc", 1) print(order) order.orderid = "b" print(order)
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_cant_allocate_if_sku_doesnt_match(): batch = Batch("batch-001", "UNCOMFORTABLE-CHAIR", 100, eta=None) different_sku_line = OrderLine("order-123", "EXPENSIVE-TOASTER", 10) print(batch.can_allocate(different_sku_line))
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))