def test_decrease_stock_many_allocations_partially(
    order_line_with_allocation_in_many_stocks, ):
    order_line = order_line_with_allocation_in_many_stocks

    decrease_stock(order_line, 2)

    allocations = order_line.allocations.all()
    assert allocations[0].quantity_allocated == 0
    assert allocations[0].stock.quantity == 2
    assert allocations[1].quantity_allocated == 1
    assert allocations[1].stock.quantity == 3
Example #2
0
def test_decrease_stock_many_allocations(order_line_with_allocation_in_many_stocks,):
    order_line = order_line_with_allocation_in_many_stocks
    allocations = order_line.allocations.all()
    warehouse_pk = allocations[1].stock.warehouse.pk

    decrease_stock(order_line, 3, warehouse_pk)

    assert allocations[0].quantity_allocated == 0
    assert allocations[1].quantity_allocated == 0
    assert allocations[0].stock.quantity == 4
    assert allocations[1].stock.quantity == 0
def test_decrease_stock_partially(allocation):
    stock = allocation.stock
    stock.quantity = 100
    stock.save(update_fields=["quantity"])
    allocation.quantity_allocated = 80
    allocation.save(update_fields=["quantity_allocated"])

    decrease_stock(allocation.order_line, 80)

    stock.refresh_from_db()
    assert stock.quantity == 20
    allocation.refresh_from_db()
    assert allocation.quantity_allocated == 0
Example #4
0
def test_decrease_stock(allocation):
    stock = allocation.stock
    stock.quantity = 100
    stock.save(update_fields=["quantity"])
    allocation.quantity_allocated = 80
    allocation.save(update_fields=["quantity_allocated"])
    warehouse_pk = allocation.stock.warehouse.pk

    decrease_stock(allocation.order_line, 50, warehouse_pk)

    stock.refresh_from_db()
    assert stock.quantity == 50
    allocation.refresh_from_db()
    assert allocation.quantity_allocated == 30
Example #5
0
def test_decrease_stock_more_then_allocated(
    order_line_with_allocation_in_many_stocks, ):
    order_line = order_line_with_allocation_in_many_stocks
    allocations = order_line.allocations.all()
    warehouse_pk = allocations[0].stock.warehouse.pk
    quantity_allocated = allocations.aggregate(quantity_allocated=Coalesce(
        Sum("quantity_allocated"), 0))["quantity_allocated"]
    assert quantity_allocated < 4

    decrease_stock(order_line, 4, warehouse_pk)

    assert allocations[0].quantity_allocated == 0
    assert allocations[1].quantity_allocated == 0
    assert allocations[0].stock.quantity == 0
    assert allocations[1].stock.quantity == 3