From 0cb3bfb4abac4cd63298fea81eeed0ad51ae38a3 Mon Sep 17 00:00:00 2001 From: Niko Lindroos Date: Wed, 18 Sep 2024 15:23:31 +0300 Subject: [PATCH] feat: add order_by field to messages query KK-1214 --- messaging/schema.py | 2 ++ messaging/tests/test_api.py | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/messaging/schema.py b/messaging/schema.py index 99000395..2d0ef02a 100644 --- a/messaging/schema.py +++ b/messaging/schema.py @@ -66,6 +66,8 @@ class MessageFilter(django_filters.FilterSet): help_text="Filter by multiple occurrences.", ) + order_by = django_filters.OrderingFilter(fields=("created_at", "sent_at")) + def __init__(self, data=None, *args, **kwargs): """ Initializes the MessageFilter instance and processes occurrence global IDs. diff --git a/messaging/tests/test_api.py b/messaging/tests/test_api.py index 403f2857..e0e2e5d6 100644 --- a/messaging/tests/test_api.py +++ b/messaging/tests/test_api.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timedelta from unittest.mock import patch import pytest @@ -27,7 +27,8 @@ $limit: Int, $offset: Int, $after: String, - $first: Int + $first: Int, + $orderBy: String ) { messages( projectId: $projectId, @@ -36,7 +37,8 @@ limit: $limit, offset: $offset, after: $after, - first: $first + first: $first, + orderBy: $orderBy ) { pageInfo { startCursor @@ -280,6 +282,24 @@ def test_messages_query_pagination_with_limit_and_offset( assert executed["data"]["messages"]["pageInfo"]["hasNextPage"] is False +@pytest.mark.parametrize( + "order_by_criteria", ["created_at", "-created_at", "sent_at", "-sent_at"] +) +def test_messages_query_order_by(order_by_criteria, project_user_api_client, project): + for days in range(0, 5, 1): + t = now() - timedelta(days=days) + MessageFactory(project=project, sent_at=t, created_at=t) + executed = project_user_api_client.execute( + MESSAGES_QUERY, + variables={"orderBy": order_by_criteria}, + ) + messages = [edge["node"] for edge in executed["data"]["messages"]["edges"]] + assert len(messages) == 5 + assert [ + message.subject for message in Message.objects.all().order_by(order_by_criteria) + ] == [message["subject"] for message in messages] + + MESSAGE_QUERY = """ query Message($id: ID!) { message(id: $id){