From f844abcad93f6754fd3c18c267656829f537c709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Mon, 8 Apr 2024 16:08:52 -0300 Subject: [PATCH 1/4] test_quotation_page_serialization: use strings for page numbers This follows from #3273, "Allow page numbers to be text, instead of integers". --- bookwyrm/tests/models/test_status_model.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index e97febbfa..b2ca63854 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -1,4 +1,5 @@ """ testing models """ +from unittest import expectedFailure from unittest.mock import patch import pathlib import re @@ -337,11 +338,14 @@ class Status(TestCase): activity["attachment"][0]["name"], "Author Name: Test Edition (worm)" ) + @expectedFailure def test_quotation_page_serialization(self, *_): """serialization of quotation page position""" tests = [ - ("single pos", 7, None, "p. 7"), - ("page range", 7, 10, "pp. 7-10"), + ("single pos", "7", "", "p. 7"), + ("page range", "7", "10", "pp. 7-10"), + ("page range roman", "xv", "xvi", "pp. xv-xvi"), + ("page range reverse", "14", "10", "pp. 14-10"), ] for desc, beg, end, pages in tests: with self.subTest(desc): From df78cc64a661a73e0e7cbafa9cac90d3d1f6b4e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Mon, 8 Apr 2024 16:10:00 -0300 Subject: [PATCH 2/4] Quotation._format_position: do not treat page numbers as integers Fixes: #3352 --- bookwyrm/models/status.py | 2 +- bookwyrm/tests/models/test_status_model.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index 546a8d6c8..dc0ab45a6 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -395,7 +395,7 @@ class Quotation(BookStatus): end = self.endposition or 0 if self.position_mode != "PG" or not beg: return None - return f"pp. {beg}-{end}" if end > beg else f"p. {beg}" + return f"pp. {beg}-{end}" if end else f"p. {beg}" @property def pure_content(self): diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index b2ca63854..5a7f0429b 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -1,5 +1,4 @@ """ testing models """ -from unittest import expectedFailure from unittest.mock import patch import pathlib import re @@ -338,7 +337,6 @@ class Status(TestCase): activity["attachment"][0]["name"], "Author Name: Test Edition (worm)" ) - @expectedFailure def test_quotation_page_serialization(self, *_): """serialization of quotation page position""" tests = [ From 873336960595a5d2da5157f4fac143e442a1b1a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Mon, 8 Apr 2024 16:15:50 -0300 Subject: [PATCH 3/4] test_quotation_page_serialization: add test with no position --- bookwyrm/models/status.py | 2 +- bookwyrm/tests/models/test_status_model.py | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index dc0ab45a6..d0c1e639b 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -392,7 +392,7 @@ class Quotation(BookStatus): def _format_position(self) -> Optional[str]: """serialize page position""" beg = self.position - end = self.endposition or 0 + end = self.endposition if self.position_mode != "PG" or not beg: return None return f"pp. {beg}-{end}" if end else f"p. {beg}" diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index 5a7f0429b..c266999a7 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -341,6 +341,7 @@ class Status(TestCase): """serialization of quotation page position""" tests = [ ("single pos", "7", "", "p. 7"), + ("missing beg", "", "10", None), ("page range", "7", "10", "pp. 7-10"), ("page range roman", "xv", "xvi", "pp. xv-xvi"), ("page range reverse", "14", "10", "pp. 14-10"), @@ -357,10 +358,11 @@ class Status(TestCase): position_mode="PG", ) activity = status.to_activity(pure=True) - self.assertRegex( - activity["content"], - f'^

"my quote"

, {pages}

$', - ) + if pages: + expect_re = f'^

"my quote"

, {pages}

$' + else: + expect_re = '^

"my quote"

$' + self.assertRegex(activity["content"], expect_re) def test_review_to_activity(self, *_): """subclass of the base model version with a "pure" serializer""" From 4304cd4a791b9175e0d33a68350fc0ec95c48b0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Tue, 9 Apr 2024 19:18:02 -0300 Subject: [PATCH 4/4] use re.escape --- bookwyrm/tests/models/test_status_model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index c266999a7..6323eeca3 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -359,7 +359,8 @@ class Status(TestCase): ) activity = status.to_activity(pure=True) if pages: - expect_re = f'^

"my quote"

, {pages}

$' + pages_re = re.escape(pages) + expect_re = f'^

"my quote"

, {pages_re}

$' else: expect_re = '^

"my quote"

$' self.assertRegex(activity["content"], expect_re)