Skip to content

Commit

Permalink
Add test for ordering AJAX datatables
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvanrun committed Dec 9, 2024
1 parent 60caa1a commit d0539ea
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
61 changes: 60 additions & 1 deletion app/tests/core_tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
AccessRequestHandlingOptions,
)
from grandchallenge.core.views import RedirectPath
from grandchallenge.datatables.views import PaginatedTableListView
from grandchallenge.datatables.views import Column, PaginatedTableListView
from grandchallenge.subdomains.utils import reverse
from tests.algorithms_tests.factories import AlgorithmFactory
from tests.archives_tests.factories import ArchiveFactory
Expand Down Expand Up @@ -69,6 +69,65 @@ def test_paginated_table_list_view_post():
}


@pytest.mark.django_db
def test_paginated_table_list_view_ordering():
view = PaginatedTableListView()

request = HttpRequest()
request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"
request.POST["length"] = 50
request.POST["draw"] = 1

view.model = Algorithm
view.row_template = "datatable_row_template.html"

view.columns = [
Column(
title="Created",
sort_field="created",
),
Column(
title="Title",
sort_field="title",
),
]

AlgorithmFactory(title="BbbbbB")
AlgorithmFactory(title="AaaaaA")

view.request = request

resp = view.post(request)
json_resp = json.loads(resp.content)

assert json_resp["draw"] == 1
assert json_resp["recordsTotal"] == 2

# No ordering via AJAX, check defaults
assert view.default_sort_column == 0
assert view.default_sort_order == "desc"

assert "AaaaaA" == json_resp["data"][0][1]
assert "BbbbbB" in json_resp["data"][1][1]

# Swap direction
request.POST["order[0][dir]"] = "asc"
resp = view.post(request)
json_resp = json.loads(resp.content)
# Also swaped rows
assert "BbbbbB" == json_resp["data"][0][1]
assert "AaaaaA" == json_resp["data"][1][1]

# Change order column
request.POST["order[0][column]"] = "1"
resp = view.post(request)
json_resp = json.loads(resp.content)

# Also changes the rows
assert "AaaaaA" == json_resp["data"][0][1]
assert "BbbbbB" == json_resp["data"][1][1]


@pytest.mark.django_db
def test_healthcheck(client, django_assert_num_queries):
with django_assert_num_queries(7):
Expand Down
1 change: 1 addition & 0 deletions app/tests/templates/datatable_row_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ object.created|date:"Y-m-d H:i:s.u" }}<split></split>{{ object.title }}

0 comments on commit d0539ea

Please sign in to comment.