Skip to content

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

Test transitions to shipped and cancelled

Anna Siwik4daee3ee6e66
diff --git a/tests/conftest.py b/tests/conftest.py
index ebc9ad1073..d17713f521 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -480,6 +480,33 @@ def order_with_variant_from_different_stocks(order_with_lines_and_stock):
return order_with_lines_and_stock
+def delivery_group(order, product_class):
+ group = DeliveryGroup.objects.create(order=order)
+ product = Product.objects.create(
+ name='Test product', price=Decimal('10.00'),
+ product_class=product_class)
+ variant = ProductVariant.objects.create(product=product, sku='SKU_A')
+ warehouse = StockLocation.objects.create(name='Warehouse 1')
+ stock = Stock.objects.create(
+ variant=variant, cost_price=1, quantity=5, quantity_allocated=3,
+ location=warehouse)
+ OrderLine.objects.create(
+ delivery_group=group,
+ product=product,
+ product_name=product.name,
+ product_sku='SKU_A',
+ quantity=3,
+ unit_price_net=Decimal('30.00'),
+ unit_price_gross=Decimal('30.00'),
+ stock=stock,
+ stock_location=stock.location.name
+ )
+ recalculate_order(order)
+ order.refresh_from_db()
+ return group
+
+
@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 d49e451681..b78e9ca645 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -2,11 +2,12 @@ from decimal import Decimal
from django.urls import reverse
import pytest
+from django_fsm import TransitionNotAllowed
from tests.utils import get_redirect_location, get_url_path
from saleor.dashboard.order.forms import ChangeQuantityForm
-from saleor.order import OrderStatus
+from saleor.order import GroupStatus, OrderStatus
from saleor.order.models import (
DeliveryGroup, Order, OrderHistoryEntry, OrderLine)
from saleor.order.transitions import process_delivery_group
@@ -526,3 +527,78 @@ def test_view_add_variant_to_delivery_group(
assert get_redirect_location(response) == reverse(
'dashboard:order-details', kwargs={'order_pk': order.pk})
assert line.quantity == 4
+
+
+def test_ship_new_delivery_group(delivery_group):
+ line = delivery_group.lines.first()
+ quantity = line.quantity
+ stock = line.stock
+ stock_quantity = stock.quantity
+ stock_quantity_allocated = stock.quantity_allocated
+
+ delivery_group.ship()
+ delivery_group.save()
+
+ stock.refresh_from_db()
+ assert delivery_group.status == GroupStatus.SHIPPED
+ assert stock.quantity == stock_quantity - quantity
+ assert stock.quantity_allocated == stock_quantity_allocated - quantity
+
+
+def test_cant_ship_cancelled_delivery_group(delivery_group):
+ delivery_group.cancel()
+ delivery_group.save()
+
+ with pytest.raises(TransitionNotAllowed):
+ delivery_group.ship()
+
+
+def test_cant_ship_shipped_delivery_group(delivery_group):
+ delivery_group.ship()
+ delivery_group.save()
+
+ with pytest.raises(TransitionNotAllowed):
+ delivery_group.ship()
+
+
+def test_cancel_new_delivery_group(delivery_group):
+ line = delivery_group.lines.first()
+ quantity = line.quantity
+ stock = line.stock
+ stock_quantity = stock.quantity
+ stock_quantity_allocated = stock.quantity_allocated
+
+ delivery_group.cancel()
+ delivery_group.save()
+
+ stock.refresh_from_db()
+ assert delivery_group.status == GroupStatus.CANCELLED
+ assert stock.quantity == stock_quantity
+ assert stock.quantity_allocated == stock_quantity_allocated - quantity
+
+
+def test_cancel_shipped_delivery_group(delivery_group):
+ delivery_group.ship()
+ delivery_group.save()
+
+ line = delivery_group.lines.first()
+ quantity = line.quantity
+ stock = line.stock
+ stock_quantity = stock.quantity
+ stock_quantity_allocated = stock.quantity_allocated
+
+ delivery_group.cancel()
+ delivery_group.save()
+
+ stock.refresh_from_db()
+ assert delivery_group.status == GroupStatus.CANCELLED
+ assert line.stock.quantity == stock_quantity + quantity
+ assert line.stock.quantity_allocated == stock_quantity_allocated
+
+
+def test_cant_cancel_cancelled_delivery_group(delivery_group):
+ delivery_group.cancel()
+ delivery_group.save()
+
+ with pytest.raises(TransitionNotAllowed):
+ delivery_group.cancel()