Adds test for invalid isbns and handle isbns with dashes

This commit is contained in:
Mouse Reeve 2023-11-02 19:01:40 -07:00
parent 95ba38524b
commit 285c513211
2 changed files with 13 additions and 1 deletions

View file

@ -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

View file

@ -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")