Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests for fulfillments
Adam Janik32ec28ab7e0f
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.pyindex a147aecdb8..c6ee8de4b0 100644--- a/saleor/graphql/api.py+++ b/saleor/graphql/api.py@@ -289,6 +289,7 @@ class Mutations(graphene.ObjectType):draft_order_update = DraftOrderUpdate.Field()fulfillment_cancel = FulfillmentCancel.Field()fulfillment_create = FulfillmentCreate.Field()+ fulfillment_update = FulfillmentUpdate.Field()order_add_note = OrderAddNote.Field()order_cancel = OrderCancel.Field()order_capture = OrderCapture.Field()diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.pyindex 38c0bcb711..c6aa29ff2c 100644--- a/saleor/graphql/order/mutations/fulfillments.py+++ b/saleor/graphql/order/mutations/fulfillments.py@@ -86,7 +86,7 @@ class FulfillmentCreate(ModelMutation):@classmethoddef construct_instance(cls, instance, cleaned_data):- instance.order = cleaned_data['order']+ instance.order = cleaned_data.get('order') or instance.orderreturn super().construct_instance(instance, cleaned_data)@classmethoddiff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex 25a254feb6..776098b0eb 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -80,7 +80,7 @@ class OrderNote(CountableDjangoObjectType):exclude_fields = ['order']-class Fulfillment(DjangoObjectType):+class Fulfillment(CountableDjangoObjectType):status_display = graphene.String(description='User-friendly fulfillment status.')@@ -94,8 +94,9 @@ class Fulfillment(DjangoObjectType):return self.get_status_display()-class FulfillmentLine(DjangoObjectType):+class FulfillmentLine(CountableDjangoObjectType):class Meta:description = 'Represents line of the fulfillment.'+ interfaces = [relay.Node]model = models.FulfillmentLineexclude_fields = ['fulfillment']diff --git a/tests/api/test_fulfillment.py b/tests/api/test_fulfillment.pynew file mode 100644index 0000000000..93f4297474--- /dev/null+++ b/tests/api/test_fulfillment.py@@ -0,0 +1,95 @@+import json+from unittest.mock import MagicMock, Mock++import graphene+import pytest+from django.shortcuts import reverse+from tests.utils import get_graphql_content++from saleor.account.models import Address+from saleor.graphql.order.mutations.draft_orders import (+ check_for_draft_order_errors)+from saleor.order.models import Order, OrderStatus, PaymentStatus, FulfillmentStatus+++def test_create_fulfillment(admin_api_client, order_with_lines):+ order = order_with_lines+ query = """+ mutation fulfillOrder(+ $order: ID, $lines: [FulfillmentLineInput], $tracking: String) {+ fulfillmentCreate(+ input: {lines: $lines, order: $order,+ trackingNumber: $tracking}) {+ fulfillment {+ fulfillmentOrder+ status+ trackingNumber+ lines {+ totalCount+ }+ }+ }+ }+ """+ order_id = graphene.Node.to_global_id('Order', order.id)+ order_line = order.lines.first()+ order_line_id = graphene.Node.to_global_id('OrderLine', order_line.id)+ tracking = 'Flames tracking'+ variables = json.dumps(+ {'order': order_id,+ 'lines': [{'orderLineId': order_line_id, 'quantity': 1}],+ 'tracking': tracking})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ assert 'errors' not in content+ data = content['data']['fulfillmentCreate']['fulfillment']+ assert data['fulfillmentOrder'] == order.pk+ assert data['status'] == FulfillmentStatus.FULFILLED.upper()+ assert data['trackingNumber'] == tracking+ assert data['lines']['totalCount'] == 1+++def test_update_fulfillment(admin_api_client, fulfillment):+ query = """+ mutation updateFulfillment($id: ID!, $tracking: String) {+ fulfillmentUpdate(+ id: $id, input: {trackingNumber: $tracking}) {+ fulfillment {+ trackingNumber+ }+ }+ }+ """+ fulfillment_id = graphene.Node.to_global_id('Fulfillment', fulfillment.id)+ tracking = 'stationary tracking'+ variables = json.dumps(+ {'id': fulfillment_id, 'tracking': tracking})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ assert 'errors' not in content+ data = content['data']['fulfillmentUpdate']['fulfillment']+ assert data['trackingNumber'] == tracking+++def test_cancel_fulfillment(admin_api_client, fulfillment):+ query = """+ mutation cancelFulfillment($id: ID!, $restock: Boolean) {+ fulfillmentCancel(id: $id, input: {restock: $restock}) {+ fulfillment {+ status+ }+ }+ }+ """+ fulfillment_id = graphene.Node.to_global_id('Fulfillment', fulfillment.id)+ restock = True+ variables = json.dumps(+ {'id': fulfillment_id, 'restock': restock})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ assert 'errors' not in content+ data = content['data']['fulfillmentCancel']['fulfillment']+ assert data['status'] == FulfillmentStatus.CANCELED.upper()diff --git a/tests/conftest.py b/tests/conftest.pyindex ede1d97ca2..8789ee747f 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -503,6 +503,11 @@ def fulfilled_order(order_with_lines):return order+def fulfillment(fulfilled_order):+ return fulfilled_order.fulfillments.first()++@pytest.fixturedef draft_order(order_with_lines):order_with_lines.status = OrderStatus.DRAFT