Skip to content

Commit

Permalink
Add default ordering to order discounts
Browse files Browse the repository at this point in the history
  • Loading branch information
fowczarek committed Apr 11, 2022
1 parent 89d6e52 commit 362a6c5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
17 changes: 17 additions & 0 deletions saleor/discount/migrations/0038_alter_orderdiscount_options.py
Original file line number Diff line number Diff line change
@@ -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",)},
),
]
1 change: 1 addition & 0 deletions saleor/discount/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,4 @@ class OrderDiscount(models.Model):
class Meta:
# Orders searching index
indexes = [GinIndex(fields=["name", "translated_name"])]
ordering = ("pk",)
56 changes: 24 additions & 32 deletions saleor/webhook/tests/test_webhook_payloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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": [
Expand All @@ -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),
}
],
Expand All @@ -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)


Expand Down

0 comments on commit 362a6c5

Please sign in to comment.