From 2fcc03fcb63fc9947c6cb0a27c7037eb7af87298 Mon Sep 17 00:00:00 2001 From: Ben Silverman Date: Mon, 23 Sep 2024 13:00:08 -0400 Subject: [PATCH] Fix failing CollectionDetailView tests --- apps/readux/tests/tests.py | 69 +++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/apps/readux/tests/tests.py b/apps/readux/tests/tests.py index 4f22b696..7fb995ba 100644 --- a/apps/readux/tests/tests.py +++ b/apps/readux/tests/tests.py @@ -1,6 +1,8 @@ from django.test import TestCase, Client from django.test import RequestFactory from django.conf import settings + +from apps.readux.forms import AllVolumesForm from ..annotations import Annotations, AnnotationCrud from django.contrib.auth import get_user_model from django.urls import reverse @@ -417,27 +419,64 @@ def test_user_annotations_on_canvas(self): assert serialized_canvas['label'] == str(self.canvas.position) assert len(serialized_canvas['otherContent']) == 1 - # TODO are the volumes actually sorted? def test_collection_detail_view_sort_and_order(self): - view = CollectionDetail() - for sort in view.SORT_OPTIONS: - for order in view.ORDER_OPTIONS: - kwargs = {'sort': sort, 'order': order } - url = reverse('collection', kwargs={ 'collection': self.collection.pid }) - response = self.client.get(url, data=kwargs) - context = response.context_data - assert context['sort'] == sort - assert context['order'] == order - assert context['order_url_params'] == urlencode({'sort': sort, 'order': order}) - assert context['manifest_query_set'].ordered + descrizione = self.collection.manifests.first() + vol2 = Manifest.objects.create(pid="test1", author="xyz", label="zyx", published_date_edtf="1000") + self.collection.manifests.add(vol2) + url = reverse('collection', kwargs={ 'collection': self.collection.pid }) + + # test all different sort options: title + kwargs = {'sort': 'title', 'order': 'asc' } + response = self.client.get(url, data=kwargs) + context = response.context_data + assert context['volumes'][0].pid == descrizione.pid + kwargs = {'sort': 'title', 'order': 'desc' } + response = self.client.get(url, data=kwargs) + context = response.context_data + assert context['volumes'][0].pid == vol2.pid + + # author + kwargs = {'sort': 'author', 'order': 'asc' } + response = self.client.get(url, data=kwargs) + context = response.context_data + assert context['volumes'][0].pid == descrizione.pid + kwargs = {'sort': 'author', 'order': 'desc' } + response = self.client.get(url, data=kwargs) + context = response.context_data + assert context['volumes'][0].pid == vol2.pid + + # edtf date published + kwargs = {'sort': 'date', 'order': 'asc' } + response = self.client.get(url, data=kwargs) + context = response.context_data + assert context['volumes'][0].pid == vol2.pid + # the other one doesn't have an edtf published date, so it should be sorted last + # (nulls last) and therefore vol2 should still be first + kwargs = {'sort': 'date', 'order': 'desc' } + response = self.client.get(url, data=kwargs) + context = response.context_data + assert context['volumes'][0].pid == vol2.pid + + # date added + kwargs = {'sort': 'added', 'order': 'asc' } + response = self.client.get(url, data=kwargs) + context = response.context_data + assert context['volumes'][0].pid == descrizione.pid + kwargs = {'sort': 'added', 'order': 'desc' } + response = self.client.get(url, data=kwargs) + context = response.context_data + assert context['volumes'][0].pid == vol2.pid def test_collection_detail_view_with_no_sort_or_order_specified(self): + descrizione = self.collection.manifests.first() + vol2 = Manifest.objects.create(pid="test1", author="xyz", label="zyx", published_date_edtf="1000") + self.collection.manifests.add(vol2) url = reverse('collection', kwargs={ 'collection': self.collection.pid }) response = self.client.get(url) context = response.context_data - assert context['sort'] == 'title' - assert context['order'] == 'asc' - assert context['manifest_query_set'].ordered + # should order by title, asc + assert context['volumes'][0].pid == descrizione.pid + assert context['volumes'][1].pid == vol2.pid def test_volume_detail_view(self): url = reverse('volume', kwargs={'volume': self.manifest.pid})