Skip to content

Commit

Permalink
feat: add order_by field to messages query
Browse files Browse the repository at this point in the history
KK-1214
  • Loading branch information
nikomakela committed Sep 18, 2024
1 parent c9a4415 commit 0cb3bfb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions messaging/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 23 additions & 3 deletions messaging/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -27,7 +27,8 @@
$limit: Int,
$offset: Int,
$after: String,
$first: Int
$first: Int,
$orderBy: String
) {
messages(
projectId: $projectId,
Expand All @@ -36,7 +37,8 @@
limit: $limit,
offset: $offset,
after: $after,
first: $first
first: $first,
orderBy: $orderBy
) {
pageInfo {
startCursor
Expand Down Expand Up @@ -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){
Expand Down

0 comments on commit 0cb3bfb

Please sign in to comment.