|
1 | 1 | from collections import Counter |
| 2 | +from datetime import datetime |
2 | 3 | from decimal import Decimal |
3 | 4 | from unittest.mock import PropertyMock, patch |
4 | 5 |
|
| 6 | +import freezegun |
5 | 7 | import pytest |
6 | 8 | from django.conf import settings |
7 | 9 | from django.test import TestCase |
| 10 | +from django.utils.timezone import make_aware |
8 | 11 | from rest_framework import status |
9 | 12 |
|
10 | 13 | from audit_log.models import AuditLogEntry |
|
34 | 37 | # === util methods === |
35 | 38 |
|
36 | 39 |
|
37 | | -def get_list(api_client: APIClient, query_string: str = None): |
| 40 | +def get_list(api_client: APIClient, query_string: str = None, *args, **kwargs): |
38 | 41 | url = reverse("registration-list") |
39 | 42 |
|
40 | 43 | if query_string: |
41 | 44 | url = "%s?%s" % (url, query_string) |
42 | 45 |
|
43 | | - return api_client.get(url) |
| 46 | + return api_client.get(url, *args, **kwargs) |
44 | 47 |
|
45 | 48 |
|
46 | 49 | def assert_registrations_in_response( |
@@ -668,6 +671,42 @@ def test_registration_id_is_audit_logged_on_get_detail(user_api_client, registra |
668 | 671 | ] |
669 | 672 |
|
670 | 673 |
|
| 674 | +@pytest.mark.django_db |
| 675 | +def test_list_should_order_by_id_by_default(user_api_client): |
| 676 | + registration1 = RegistrationFactory() |
| 677 | + registration2 = RegistrationFactory() |
| 678 | + registration3 = RegistrationFactory() |
| 679 | + expected_order = [registration1, registration2, registration3] |
| 680 | + |
| 681 | + response = get_list(user_api_client) |
| 682 | + assert response.status_code == status.HTTP_200_OK, response.json() |
| 683 | + |
| 684 | + data = response.data["data"] |
| 685 | + assert [item["id"] for item in data] == [item.pk for item in expected_order] |
| 686 | + |
| 687 | + |
| 688 | +@freezegun.freeze_time("2024-01-01") |
| 689 | +@pytest.mark.django_db |
| 690 | +def test_list_should_order_by_event_start_time(user_api_client): |
| 691 | + registration_jun = RegistrationFactory( |
| 692 | + event=EventFactory(start_time=make_aware(datetime(2024, 6, 1))) |
| 693 | + ) |
| 694 | + registration_jan = RegistrationFactory( |
| 695 | + event=EventFactory(start_time=make_aware(datetime(2024, 1, 1))) |
| 696 | + ) |
| 697 | + registration_dec = RegistrationFactory( |
| 698 | + event=EventFactory(start_time=make_aware(datetime(2024, 12, 1))) |
| 699 | + ) |
| 700 | + expected_order = [registration_dec, registration_jun, registration_jan] |
| 701 | + |
| 702 | + response = get_list(user_api_client, data={"sort": "-event__start_time"}) |
| 703 | + |
| 704 | + assert response.status_code == status.HTTP_200_OK, response.json() |
| 705 | + |
| 706 | + data = response.data["data"] |
| 707 | + assert [item["id"] for item in data] == [item.pk for item in expected_order] |
| 708 | + |
| 709 | + |
671 | 710 | @pytest.mark.django_db |
672 | 711 | def test_registration_id_is_audit_logged_on_get_list( |
673 | 712 | user_api_client, registration, registration2 |
|
0 commit comments