♻️(backend) api resources list ordering
Give the possibility to order the resources list by creation date (documents / templates). By default the list is ordered by creation date descending.
This commit is contained in:
@@ -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, is_public=True)
|
||||
]
|
||||
|
||||
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, is_public=True)
|
||||
]
|
||||
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user