From c7386e4d0ae1fed612cf87ba71d82705c2757449 Mon Sep 17 00:00:00 2001 From: Nicolas Clerc Date: Fri, 20 Dec 2024 16:17:59 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B(backend)=20fix=20order=20created?= =?UTF-8?q?=5Fon=20date=20filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Order filtering is matching created_on date exactly, which only matches times at 0:00, as query parameter only contains a date. --- .../joanie/core/filters/admin/__init__.py | 2 +- .../tests/core/api/admin/orders/test_list.py | 20 +++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/backend/joanie/core/filters/admin/__init__.py b/src/backend/joanie/core/filters/admin/__init__.py index 72eda7762..a2f2e7f27 100755 --- a/src/backend/joanie/core/filters/admin/__init__.py +++ b/src/backend/joanie/core/filters/admin/__init__.py @@ -280,7 +280,7 @@ class Meta: field_name="product__type", choices=enums.PRODUCT_TYPE_CHOICES, ) - created_on = filters.DateFilter(field_name="created_on", lookup_expr="exact") + created_on = filters.DateFilter(field_name="created_on", lookup_expr="date") created_on_date_range = filters.DateFromToRangeFilter(field_name="created_on") def filter_by_query(self, queryset, _name, value): diff --git a/src/backend/joanie/tests/core/api/admin/orders/test_list.py b/src/backend/joanie/tests/core/api/admin/orders/test_list.py index 5c816a7eb..27d36e77d 100644 --- a/src/backend/joanie/tests/core/api/admin/orders/test_list.py +++ b/src/backend/joanie/tests/core/api/admin/orders/test_list.py @@ -22,12 +22,20 @@ class OrdersAdminApiListTestCase(TestCase): @staticmethod def generate_orders_created_on(number: int, created_on=None): """Generate a batch of orders with a specific creation date.""" - if created_on: - created_on = datetime.combine(created_on, datetime.min.time(), tzinfo=timezone.utc) - with mock.patch( - "django.utils.timezone.now", return_value=created_on or datetime.now() - ): - return factories.OrderGeneratorFactory.create_batch(number) + orders = [] + for _ in range(number): + if created_on: + created_on = datetime.combine( + created_on, datetime.now().time(), tzinfo=timezone.utc + ) + with mock.patch( + "django.utils.timezone.now", + return_value=created_on or datetime.now(), + ): + orders.append(factories.OrderFactory()) + # orders default orderings are by creation date + orders.sort(key=lambda x: x.created_on, reverse=True) + return orders def test_api_admin_orders_request_without_authentication(self): """