From 285c513211da7fef58c6f333ec80ea6566ecebc6 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 2 Nov 2023 19:01:40 -0700 Subject: [PATCH] Adds test for invalid isbns and handle isbns with dashes --- bookwyrm/isbn/isbn.py | 7 ++++++- bookwyrm/tests/test_isbn.py | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bookwyrm/isbn/isbn.py b/bookwyrm/isbn/isbn.py index 4cc7f47dd..56062ff7b 100644 --- a/bookwyrm/isbn/isbn.py +++ b/bookwyrm/isbn/isbn.py @@ -40,7 +40,12 @@ class IsbnHyphenator: self.__element_tree = ElementTree.parse(self.__range_file_path) gs1_prefix = isbn_13[:3] - reg_group = self.__find_reg_group(isbn_13, gs1_prefix) + try: + reg_group = self.__find_reg_group(isbn_13, gs1_prefix) + except ValueError: + # if the reg groups are invalid, just return the original isbn + return isbn_13 + if reg_group is None: return isbn_13 # failed to hyphenate diff --git a/bookwyrm/tests/test_isbn.py b/bookwyrm/tests/test_isbn.py index b528e9210..5486c7151 100644 --- a/bookwyrm/tests/test_isbn.py +++ b/bookwyrm/tests/test_isbn.py @@ -29,3 +29,10 @@ class TestISBN(TestCase): self.assertEqual(hyphenator.hyphenate("9786769533251"), "9786769533251") # 979-8 (United States) 2300000-3499999 (unassigned) self.assertEqual(hyphenator.hyphenate("9798311111111"), "9798311111111") + + def test_isbn_hyphenation_invalid_data(self): + """Make sure not to throw an error when a bad ISBN is found""" + # no action taken + self.assertEqual(hyphenator.hyphenate("978-0-4633461-1-2"), "978-0-4633461-1-2") + self.assertEqual(hyphenator.hyphenate("9-0-4633461-1-2"), "9-0-4633461-1-2") + self.assertEqual(hyphenator.hyphenate("90463346112"), "90463346112")