Skip to content

Commit

Permalink
test(source list): add tests for source list ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
dchiller committed Aug 26, 2024
1 parent ba0ff80 commit 5776ecf
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions django/cantusdb_project/main_app/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4694,6 +4694,79 @@ def test_search_by_indexing_notes(self):
response = self.client.get(reverse("source-list"), {"general": search_term})
self.assertIn(source, response.context["sources"])

def test_ordering(self) -> None:
"""
Order is currently available by country, city + institution name (parameter:
"city_institution"), and siglum + shelfmark. Siglum + shelfmark is the default.
"""
# Create a bunch of sources
sources = []
for _ in range(10):
sources.append(make_fake_source())
# Default ordering is by siglum and shelfmark, ascending
with self.subTest("Default ordering"):
response = self.client.get(reverse("source-list"))
response_sources = response.context["sources"]
expected_source_order = sorted(
sources,
key=lambda source: (
source.holding_institution.siglum,
source.shelfmark,
),
)
self.assertEqual(
list(expected_source_order),
list(response_sources),
)
response_reverse = self.client.get(reverse("source-list"), {"sort": "desc"})
response_sources_reverse = response_reverse.context["sources"]
self.assertEqual(
list(reversed(expected_source_order)),
list(response_sources_reverse),
)
with self.subTest("Order by country, ascending"):
response = self.client.get(reverse("source-list"), {"order": "country"})
response_sources = response.context["sources"]
expected_source_order = sorted(
sources, key=lambda source: source.holding_institution.country
)
self.assertEqual(
list(expected_source_order),
list(response_sources),
)
response_reverse = self.client.get(
reverse("source-list"), {"order": "country", "sort": "desc"}
)
response_sources_reverse = response_reverse.context["sources"]
self.assertEqual(
list(reversed(expected_source_order)),
list(response_sources_reverse),
)
with self.subTest("Order by city and institution name, ascending"):
response = self.client.get(
reverse("source-list"), {"order": "city_institution"}
)
response_sources = response.context["sources"]
expected_source_order = sorted(
sources,
key=lambda source: (
source.holding_institution.city,
source.holding_institution.name,
),
)
self.assertEqual(
list(expected_source_order),
list(response_sources),
)
response_reverse = self.client.get(
reverse("source-list"), {"order": "city_institution", "sort": "desc"}
)
response_sources_reverse = response_reverse.context["sources"]
self.assertEqual(
list(reversed(expected_source_order)),
list(response_sources_reverse),
)


class SourceCreateViewTest(TestCase):
@classmethod
Expand Down

0 comments on commit 5776ecf

Please sign in to comment.