Skip to content

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

Add default ordering to order discounts

fowczrek362a6c54ff6a
diff --git a/saleor/discount/migrations/0038_alter_orderdiscount_options.py b/saleor/discount/migrations/0038_alter_orderdiscount_options.py
new file mode 100644
index 0000000000..c48757ce37
--- /dev/null
+++ b/saleor/discount/migrations/0038_alter_orderdiscount_options.py
@@ -0,0 +1,17 @@
+# Generated by Django 3.2.13 on 2022-04-11 10:17
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("discount", "0037_rewrite_discount_order_relations"),
+ ]
+
+ operations = [
+ migrations.AlterModelOptions(
+ name="orderdiscount",
+ options={"ordering": ("pk",)},
+ ),
+ ]
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index d2bdf8a6b0..5aa2ecc7e2 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -392,3 +392,4 @@ class OrderDiscount(models.Model):
class Meta:
# Orders searching index
indexes = [GinIndex(fields=["name", "translated_name"])]
+ ordering = ("pk",)
diff --git a/saleor/webhook/tests/test_webhook_payloads.py b/saleor/webhook/tests/test_webhook_payloads.py
index e856cd7dec..4805a87848 100644
--- a/saleor/webhook/tests/test_webhook_payloads.py
+++ b/saleor/webhook/tests/test_webhook_payloads.py
@@ -54,10 +54,6 @@ def parse_django_datetime(date):
return json.loads(json.dumps(date, cls=DjangoJSONEncoder))
-def sort_by_id(dicts):
- return sorted(dicts, key=lambda d: d["id"])
-
-
@freeze_time()
@mock.patch("saleor.webhook.payloads.generate_order_lines_payload")
@mock.patch("saleor.webhook.payloads.generate_fulfillment_lines_payload")
@@ -204,7 +200,28 @@ def test_generate_order_payload(
"country_area": order.billing_address.country_area,
"phone": str(order.billing_address.phone),
},
- "discounts": ANY,
+ "discounts": [
+ {
+ "id": graphene.Node.to_global_id("OrderDiscount", discount_1.pk),
+ "type": discount_1.type,
+ "value_type": discount_1.value_type,
+ "value": "20.000",
+ "amount_value": str(quantize_price(discount_1.amount_value, currency)),
+ "name": discount_1.name,
+ "translated_name": discount_1.translated_name,
+ "reason": discount_1.reason,
+ },
+ {
+ "id": graphene.Node.to_global_id("OrderDiscount", discount_2.pk),
+ "type": discount_2.type,
+ "value_type": discount_2.value_type,
+ "value": "10.000",
+ "amount_value": str(quantize_price(discount_2.amount_value, currency)),
+ "name": discount_2.name,
+ "translated_name": discount_2.translated_name,
+ "reason": discount_2.reason,
+ },
+ ],
"original": graphene.Node.to_global_id("Order", order.original_id),
"lines": json.loads(order_lines),
"fulfillments": [
@@ -214,8 +231,8 @@ def test_generate_order_payload(
"status": fulfillment.status,
"tracking_number": fulfillment.tracking_number,
"created": parse_django_datetime(fulfillment.created),
- "shipping_refund_amount": ANY,
- "total_refund_amount": ANY,
+ "shipping_refund_amount": "0.00",
+ "total_refund_amount": "0.00",
"lines": json.loads(fulfillment_lines),
}
],
@@ -238,31 +255,6 @@ def test_generate_order_payload(
"meta": generate_meta(requestor_data=generate_requestor(customer_user)),
}
- assert sort_by_id(payload["discounts"]) == sort_by_id(
- [
- {
- "id": graphene.Node.to_global_id("OrderDiscount", discount_1.pk),
- "type": discount_1.type,
- "value_type": discount_1.value_type,
- "value": ANY,
- "amount_value": str(quantize_price(discount_1.amount_value, currency)),
- "name": discount_1.name,
- "translated_name": discount_1.translated_name,
- "reason": discount_1.reason,
- },
- {
- "id": graphene.Node.to_global_id("OrderDiscount", discount_2.pk),
- "type": discount_2.type,
- "value_type": discount_2.value_type,
- "value": ANY,
- "amount_value": str(quantize_price(discount_2.amount_value, currency)),
- "name": discount_2.name,
- "translated_name": discount_2.translated_name,
- "reason": discount_2.reason,
- },
- ],
- )
-
mocked_fulfillment_lines.assert_called_with(fulfillment)