Skip to content

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

Test changing ordered item stock view

Anna Siwike9486a5b2e77
diff --git a/tests/conftest.py b/tests/conftest.py
index cc85d6b08d..f4da9ae7ba 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -415,7 +415,8 @@ def order_with_items_and_stock(order, product_class):
quantity=3,
unit_price_net=Decimal('30.00'),
unit_price_gross=Decimal('30.00'),
- stock=stock
+ stock=stock,
+ stock_location=stock.location.name
)
product = Product.objects.create(
name='Test product 2', price=Decimal('20.00'),
@@ -432,7 +433,8 @@ def order_with_items_and_stock(order, product_class):
quantity=2,
unit_price_net=Decimal('20.00'),
unit_price_gross=Decimal('20.00'),
- stock=stock
+ stock=stock,
+ stock_location=stock.location.name
)
Order.objects.recalculate_order(order)
order.refresh_from_db()
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index eebe96be28..8ccb0dd1a5 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -7,7 +7,7 @@ from saleor.dashboard.order.forms import ChangeQuantityForm, MoveItemsForm
from saleor.order.models import (
DeliveryGroup, Order, OrderedItem, OrderHistoryEntry)
from saleor.order.utils import add_items_to_delivery_group
-from saleor.product.models import ProductVariant, Stock
+from saleor.product.models import ProductVariant, Stock, StockLocation
from tests.utils import get_redirect_location, get_url_path
@@ -347,3 +347,67 @@ def test_view_order_packing_slips(
order_with_items_and_stock.id,
order_with_items_and_stock.groups.all()[0].pk)
assert response['Content-Disposition'] == 'filename=%s' % name
+
+
+def test_view_change_order_line_stock_valid(
+ admin_client, order_with_items_and_stock):
+ order = order_with_items_and_stock
+ line = order.get_items().last()
+ old_stock = line.stock
+ variant = ProductVariant.objects.get(sku=line.product_sku)
+ stock_location = StockLocation.objects.create(name='Warehouse 2')
+ stock = Stock.objects.create(
+ variant=variant, cost_price=2, quantity=2, quantity_allocated=0,
+ location=stock_location)
+
+ url = reverse(
+ 'dashboard:orderline-change-stock', kwargs={
+ 'order_pk': order.pk,
+ 'line_pk': line.pk})
+ data = {'stock': stock.pk}
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 200
+
+ line.refresh_from_db()
+ assert line.stock == stock
+ assert line.stock_location == stock.location.name
+ assert line.stock.quantity_allocated == 2
+
+ old_stock.refresh_from_db()
+ assert old_stock.quantity_allocated == 0
+
+
+def test_view_change_order_line_stock_insufficient_stock(
+ admin_client, order_with_items_and_stock):
+ order = order_with_items_and_stock
+ line = order.get_items().last()
+ old_stock = line.stock
+ variant = ProductVariant.objects.get(sku=line.product_sku)
+ stock_location = StockLocation.objects.create(name='Warehouse 2')
+ stock = Stock.objects.create(
+ variant=variant, cost_price=2, quantity=2, quantity_allocated=1,
+ location=stock_location)
+
+ url = reverse(
+ 'dashboard:orderline-change-stock', kwargs={
+ 'order_pk': order.pk,
+ 'line_pk': line.pk})
+ data = {'stock': stock.pk}
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 400
+
+ line.refresh_from_db()
+ assert line.stock == old_stock
+ assert line.stock_location == old_stock.location.name
+
+ old_stock.refresh_from_db()
+ assert old_stock.quantity_allocated == 2
+
+ stock.refresh_from_db()
+ assert stock.quantity_allocated == 1