From e322d3cae1720504814b2fa6afd39fdab0390d95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Thu, 23 Nov 2023 17:05:18 -0300 Subject: [PATCH 1/2] Do not create a set for already-distinct query result --- bookwyrm/book_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/book_search.py b/bookwyrm/book_search.py index ceb228f40..3012482fd 100644 --- a/bookwyrm/book_search.py +++ b/bookwyrm/book_search.py @@ -137,7 +137,7 @@ def search_title_author( # filter out multiple editions of the same work list_results = [] - for work_id in set(editions_of_work[:30]): + for work_id in editions_of_work[:30]: result = ( results.filter(parent_work=work_id) .order_by("-rank", "-edition_rank") From c997d2d44aebd258ba4f45975f16b1940a799199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Fri, 24 Nov 2023 02:18:18 -0300 Subject: [PATCH 2/2] Add test to assert distinct() clause Also, tweak other `search_title_author()` tests to verify ordering by edition rank. --- bookwyrm/tests/test_book_search.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/bookwyrm/tests/test_book_search.py b/bookwyrm/tests/test_book_search.py index db6ba8353..ad954f585 100644 --- a/bookwyrm/tests/test_book_search.py +++ b/bookwyrm/tests/test_book_search.py @@ -26,10 +26,10 @@ class BookSearch(TestCase): parent_work=self.work, isbn_10="1111111111", openlibrary_key="hello", + pages=150, ) - self.third_edition = models.Edition.objects.create( - title="Edition with annoying ISBN", + title="Another Edition with annoying ISBN", parent_work=self.work, isbn_10="022222222X", ) @@ -76,16 +76,21 @@ class BookSearch(TestCase): def test_search_title_author(self): """search by unique identifiers""" - results = book_search.search_title_author("Another", min_confidence=0) + results = book_search.search_title_author("annoying", min_confidence=0) self.assertEqual(len(results), 1) - self.assertEqual(results[0], self.second_edition) + self.assertEqual(results[0], self.third_edition) def test_search_title_author_return_first(self): - """search by unique identifiers""" - results = book_search.search_title_author( + """sorts by edition rank""" + result = book_search.search_title_author( "Another", min_confidence=0, return_first=True ) - self.assertEqual(results, self.second_edition) + self.assertEqual(result, self.second_edition) # highest edition rank + + def test_search_title_author_one_edition_per_work(self): + """at most one edition per work""" + results = book_search.search_title_author("Edition", 0) + self.assertEqual(results, [self.first_edition]) # highest edition rank def test_format_search_result(self): """format a search result"""