|
| 1 | +from datetime import timedelta |
1 | 2 | from unittest.mock import patch |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 | from core.const.shop_error_messages import CartNotOrderableErrorMessages |
5 | 6 | from core.external_apis.portone.client import PortOneException |
6 | 7 | from core.util.testutil import to_json |
| 8 | +from django.utils import timezone |
7 | 9 | from rest_framework.status import ( |
8 | 10 | HTTP_200_OK, |
9 | 11 | HTTP_201_CREATED, |
|
15 | 17 | from shop.conftest import VALID_TICKET_INFO |
16 | 18 | from shop.order.models import CustomerInfo, Order, OrderProductRelation, SingleProductCart, TicketInfo |
17 | 19 | from shop.order.serializers.dto import OrderDto, SingleProductCartDto |
18 | | -from shop.payment_history.models import PaymentHistoryStatus |
| 20 | +from shop.payment_history.models import PaymentHistory, PaymentHistoryStatus |
19 | 21 | from shop.test.helpers import OrdersApi |
20 | 22 |
|
21 | 23 |
|
@@ -43,6 +45,22 @@ def test_order_list_excludes_other_users_orders(other_client, order_factory): |
43 | 45 | assert response.json() == [] |
44 | 46 |
|
45 | 47 |
|
| 48 | +@pytest.mark.django_db |
| 49 | +def test_order_list_orders_by_first_paid_at_desc(customer_client, order_factory): |
| 50 | + # 먼저 생성된 주문(= Order.created_at 이 더 과거)이 더 최근에 결제되도록 구성. |
| 51 | + # 이렇게 해야 created_at 기본 정렬과 first_paid_at 정렬의 결과가 달라져 검증이 유효하다. |
| 52 | + older_order = order_factory(status="completed") |
| 53 | + newer_order = order_factory(status="completed") |
| 54 | + now = timezone.now() |
| 55 | + PaymentHistory.objects.filter(order=older_order).update(created_at=now) |
| 56 | + PaymentHistory.objects.filter(order=newer_order).update(created_at=now - timedelta(days=1)) |
| 57 | + |
| 58 | + response = OrdersApi(http_client=customer_client).list() |
| 59 | + |
| 60 | + assert response.status_code == HTTP_200_OK |
| 61 | + assert [order["id"] for order in response.json()] == [str(older_order.id), str(newer_order.id)] |
| 62 | + |
| 63 | + |
46 | 64 | @pytest.mark.django_db |
47 | 65 | def test_order_retrieve_returns_full_order_dto(customer_client, order_factory): |
48 | 66 | completed_order = order_factory(status="completed") |
|
0 commit comments