Skip to content

Commit

Permalink
♻️(backend) api resources list ordering
Browse files Browse the repository at this point in the history
Give the possibility to order the resources
list by creation date (documents / templates).
By default the list is ordered by
creation date descending.
  • Loading branch information
AntoLC committed Apr 16, 2024
1 parent 6f985af commit 60d85e6
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/backend/core/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from rest_framework import (
decorators,
exceptions,
filters,
mixins,
pagination,
status,
Expand Down Expand Up @@ -138,6 +139,10 @@ def get_me(self, request):
class ResourceViewsetMixin:
"""Mixin with methods common to all resource viewsets that are managed with accesses."""

filter_backends = [filters.OrderingFilter]
ordering_fields = ["created_at"]
ordering = ["-created_at"]

def get_queryset(self):
"""Custom queryset to get user related resources."""
queryset = super().get_queryset()
Expand Down
56 changes: 56 additions & 0 deletions src/backend/core/tests/documents/test_api_documents_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,59 @@ def test_api_documents_list_authenticated_distinct():
content = response.json()
assert len(content["results"]) == 1
assert content["results"][0]["id"] == str(document.id)


def test_api_documents_order():
"""
Test that the endpoint GET documents is sorted in 'created_at' descending order by default.
"""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)

document_ids = [
str(document.id)
for document in factories.DocumentFactory.create_batch(5, users=[user])
]

response = client.get(
"/api/v1.0/documents/",
)

assert response.status_code == 200

response_data = response.json()
response_document_ids = [document["id"] for document in response_data["results"]]

document_ids.reverse()
assert (
response_document_ids == document_ids
), "created_at values are not sorted from newest to oldest"


def test_api_documents_order_param():
"""
Test that the 'created_at' field is sorted in ascending order
when the 'ordering' query parameter is set.
"""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)

documents_ids = [
str(document.id)
for document in factories.DocumentFactory.create_batch(5, users=[user])
]

response = APIClient().get(
"/api/v1.0/documents/?ordering=created_at",
)
assert response.status_code == 200

response_data = response.json()

response_document_ids = [document["id"] for document in response_data["results"]]

assert (
response_document_ids == documents_ids
), "created_at values are not sorted from oldest to newest"
56 changes: 56 additions & 0 deletions src/backend/core/tests/templates/test_api_templates_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,59 @@ def test_api_templates_list_authenticated_distinct():
content = response.json()
assert len(content["results"]) == 1
assert content["results"][0]["id"] == str(template.id)


def test_api_templates_order():
"""
Test that the endpoint GET templates is sorted in 'created_at' descending order by default.
"""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)

template_ids = [
str(template.id)
for template in factories.DocumentFactory.create_batch(5, users=[user])
]

response = APIClient().get(
"/api/v1.0/templates/",
)

assert response.status_code == 200

response_data = response.json()
response_template_ids = [template["id"] for template in response_data["results"]]

template_ids.reverse()
assert (
response_template_ids == template_ids
), "created_at values are not sorted from newest to oldest"


def test_api_templates_order_param():
"""
Test that the 'created_at' field is sorted in ascending order
when the 'ordering' query parameter is set.
"""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)

templates_ids = [
str(template.id)
for template in factories.DocumentFactory.create_batch(5, users=[user])
]

response = APIClient().get(
"/api/v1.0/templates/?ordering=created_at",
)
assert response.status_code == 200

response_data = response.json()

response_template_ids = [template["id"] for template in response_data["results"]]

assert (
response_template_ids == templates_ids
), "created_at values are not sorted from oldest to newest"

0 comments on commit 60d85e6

Please sign in to comment.