Example #1
0
def test_allocate_stock_many_stocks(order_line, variant_with_many_stocks):
    variant = variant_with_many_stocks
    stocks = variant.stocks.all()

    allocate_stock(order_line, COUNTRY_CODE, 5)

    allocations = Allocation.objects.filter(order_line=order_line, stock__in=stocks)
    assert allocations[0].quantity_allocated == 4
    assert allocations[1].quantity_allocated == 1
def test_allocate_stock_insufficient_stocks(order_line,
                                            variant_with_many_stocks):
    variant = variant_with_many_stocks
    stocks = variant.stocks.all()

    with pytest.raises(InsufficientStock):
        allocate_stock(order_line, COUNTRY_CODE, 10)

    assert not Allocation.objects.filter(order_line=order_line,
                                         stock__in=stocks).exists()
def test_allocate_stock(order_line, stock):
    stock.quantity = 100
    stock.save(update_fields=["quantity"])

    allocate_stock(order_line, COUNTRY_CODE, 50)

    stock.refresh_from_db()
    assert stock.quantity == 100
    allocation = Allocation.objects.get(order_line=order_line, stock=stock)
    assert allocation.quantity_allocated == 50
def test_allocate_stock_partially_allocated_insufficient_stocks(
        order_line, order_line_with_allocation_in_many_stocks):
    allocated_line = order_line_with_allocation_in_many_stocks
    variant = allocated_line.variant
    stocks = variant.stocks.all()

    with pytest.raises(InsufficientStock):
        allocate_stock(order_line, COUNTRY_CODE, 6)

    assert not Allocation.objects.filter(order_line=order_line,
                                         stock__in=stocks).exists()
def test_allocate_stock_many_stocks_partially_allocated(
        order_line, order_line_with_allocation_in_many_stocks):
    allocated_line = order_line_with_allocation_in_many_stocks
    variant = allocated_line.variant
    stocks = variant.stocks.all()

    allocate_stock(order_line, COUNTRY_CODE, 4)

    allocations = Allocation.objects.filter(order_line=order_line,
                                            stock__in=stocks)
    assert allocations[0].quantity_allocated == 2
    assert allocations[1].quantity_allocated == 2