Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Test adding variant to existing lines

Anna Siwikc487451471d4
diff --git a/tests/conftest.py b/tests/conftest.py
index 9a5b1dc6b8..6828500344 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -441,6 +441,32 @@ def order_with_items_and_stock(order, product_class):
return order
+def order_with_variant_from_different_stocks(order_with_items_and_stock):
+ line = OrderedItem.objects.get(product_sku='SKU_A')
+ variant = ProductVariant.objects.get(sku=line.product_sku)
+ warehouse_2 = StockLocation.objects.create(name='Warehouse 2')
+ stock = Stock.objects.create(
+ variant=variant, cost_price=1, quantity=5, quantity_allocated=2,
+ location=warehouse_2)
+ OrderedItem.objects.create(
+ delivery_group=line.delivery_group,
+ product=variant.product,
+ product_name=variant.product.name,
+ product_sku=line.product_sku,
+ quantity=2,
+ unit_price_net=Decimal('30.00'),
+ unit_price_gross=Decimal('30.00'),
+ stock=stock,
+ stock_location=stock.location.name
+ )
+ warehouse_2 = StockLocation.objects.create(name='Warehouse 3')
+ Stock.objects.create(
+ variant=variant, cost_price=1, quantity=5, quantity_allocated=0,
+ location=warehouse_2)
+ return order_with_items_and_stock
+
+
@pytest.fixture()
def sale(db, default_category):
sale = Sale.objects.create(name="Sale", value=5)
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index 7e1f777839..53da305806 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -9,7 +9,8 @@ from saleor.dashboard.order.forms import ChangeQuantityForm, MoveLinesForm
from saleor.order.models import (
DeliveryGroup, Order, OrderLine, OrderHistoryEntry)
from saleor.order.utils import (
- fill_group_with_partition, change_order_line_quantity)
+ add_variant_to_existing_lines, change_order_line_quantity,
+ fill_group_with_partition)
from saleor.product.models import ProductVariant, Stock, StockLocation
from tests.utils import get_redirect_location, get_url_path
@@ -442,3 +443,64 @@ def test_view_change_order_line_stock_merges_lines(
stock.refresh_from_db()
assert stock.quantity_allocated == 0
+
+
+def test_add_variant_to_existing_lines_one_line(
+ order_with_variant_from_different_stocks):
+ order = order_with_variant_from_different_stocks
+ lines = order.get_items().filter(product_sku='SKU_A')
+ variant_lines_before = lines.count()
+ line = lines.get(stock_location='Warehouse 2')
+ quantity_before = line.quantity
+ variant = ProductVariant.objects.get(sku='SKU_A')
+
+ quantity_left = add_variant_to_existing_lines(
+ line.delivery_group, variant, 2)
+
+ lines_after = order.get_items().filter(product_sku='SKU_A').count()
+ line.refresh_from_db()
+ assert quantity_left == 0
+ assert lines_after == variant_lines_before
+ assert line.quantity == 4
+
+
+def test_add_variant_to_existing_lines_multiple_lines(
+ order_with_variant_from_different_stocks):
+ order = order_with_variant_from_different_stocks
+ lines = order.get_items().filter(product_sku='SKU_A')
+ variant_lines_before = lines.count()
+ line_1 = lines.get(stock_location='Warehouse 1')
+ line_2 = lines.get(stock_location='Warehouse 2')
+ variant = ProductVariant.objects.get(sku='SKU_A')
+
+ quantity_left = add_variant_to_existing_lines(
+ line_1.delivery_group, variant, 4)
+
+ lines_after = order.get_items().filter(product_sku='SKU_A').count()
+ line_1.refresh_from_db()
+ line_2.refresh_from_db()
+ assert quantity_left == 0
+ assert lines_after == variant_lines_before
+ assert line_1.quantity == 4
+ assert line_2.quantity == 5
+
+
+def test_add_variant_to_existing_lines_multiple_lines_with_rest(
+ order_with_variant_from_different_stocks):
+ order = order_with_variant_from_different_stocks
+ lines = order.get_items().filter(product_sku='SKU_A')
+ variant_lines_before = lines.count()
+ line_1 = lines.get(stock_location='Warehouse 1')
+ line_2 = lines.get(stock_location='Warehouse 2')
+ variant = ProductVariant.objects.get(sku='SKU_A')
+
+ quantity_left = add_variant_to_existing_lines(
+ line_1.delivery_group, variant, 7)
+
+ lines_after = order.get_items().filter(product_sku='SKU_A').count()
+ line_1.refresh_from_db()
+ line_2.refresh_from_db()
+ assert quantity_left == 2
+ assert lines_after == variant_lines_before
+ assert line_1.quantity == 5
+ assert line_2.quantity == 5