2018-01-15 19:19:30 +01:00
|
|
|
import unittest
|
|
|
|
|
|
2021-01-31 16:49:12 -05:00
|
|
|
from isbn_verifier import (
|
|
|
|
|
is_valid,
|
|
|
|
|
)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2020-10-15 12:46:24 -04:00
|
|
|
# Tests adapted from `problem-specifications//canonical-data.json`
|
2018-01-15 19:19:30 +01:00
|
|
|
|
|
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
class IsbnVerifierTest(unittest.TestCase):
|
2020-10-15 12:48:47 -04:00
|
|
|
def test_valid_isbn(self):
|
2019-10-17 13:33:43 +01:00
|
|
|
self.assertIs(is_valid("3-598-21508-8"), True)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
def test_invalid_isbn_check_digit(self):
|
|
|
|
|
self.assertIs(is_valid("3-598-21508-9"), False)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2020-10-15 12:48:47 -04:00
|
|
|
def test_valid_isbn_with_a_check_digit_of_10(self):
|
2019-10-17 13:33:43 +01:00
|
|
|
self.assertIs(is_valid("3-598-21507-X"), True)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
def test_check_digit_is_a_character_other_than_x(self):
|
|
|
|
|
self.assertIs(is_valid("3-598-21507-A"), False)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2022-05-05 19:07:22 -07:00
|
|
|
def test_invalid_check_digit_in_isbn_is_not_treated_as_zero(self):
|
|
|
|
|
self.assertIs(is_valid("4-598-21507-B"), False)
|
|
|
|
|
|
2022-01-04 15:40:35 -08:00
|
|
|
def test_invalid_character_in_isbn_is_not_treated_as_zero(self):
|
2019-10-17 13:33:43 +01:00
|
|
|
self.assertIs(is_valid("3-598-P1581-X"), False)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
def test_x_is_only_valid_as_a_check_digit(self):
|
|
|
|
|
self.assertIs(is_valid("3-598-2X507-9"), False)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
|
|
|
|
def test_valid_isbn_without_separating_dashes(self):
|
2019-10-17 13:33:43 +01:00
|
|
|
self.assertIs(is_valid("3598215088"), True)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
def test_isbn_without_separating_dashes_and_x_as_check_digit(self):
|
|
|
|
|
self.assertIs(is_valid("359821507X"), True)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
def test_isbn_without_check_digit_and_dashes(self):
|
|
|
|
|
self.assertIs(is_valid("359821507"), False)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
def test_too_long_isbn_and_no_dashes(self):
|
|
|
|
|
self.assertIs(is_valid("3598215078X"), False)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
def test_too_short_isbn(self):
|
|
|
|
|
self.assertIs(is_valid("00"), False)
|
2018-08-20 10:53:19 -04:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
def test_isbn_without_check_digit(self):
|
|
|
|
|
self.assertIs(is_valid("3-598-21507"), False)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
def test_check_digit_of_x_should_not_be_used_for_0(self):
|
|
|
|
|
self.assertIs(is_valid("3-598-21515-X"), False)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
def test_empty_isbn(self):
|
|
|
|
|
self.assertIs(is_valid(""), False)
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
def test_input_is_9_characters(self):
|
|
|
|
|
self.assertIs(is_valid("134456729"), False)
|
2018-04-11 01:23:32 +08:00
|
|
|
|
2022-01-04 15:40:35 -08:00
|
|
|
def test_invalid_characters_are_not_ignored_after_checking_length(self):
|
2019-10-17 13:33:43 +01:00
|
|
|
self.assertIs(is_valid("3132P34035"), False)
|
2018-08-20 10:53:19 -04:00
|
|
|
|
2022-01-04 15:40:35 -08:00
|
|
|
def test_invalid_characters_are_not_ignored_before_checking_length(self):
|
|
|
|
|
self.assertIs(is_valid("3598P215088"), False)
|
|
|
|
|
|
2018-08-20 10:53:19 -04:00
|
|
|
def test_input_is_too_long_but_contains_a_valid_isbn(self):
|
2019-10-17 13:33:43 +01:00
|
|
|
self.assertIs(is_valid("98245726788"), False)
|
2018-08-20 10:53:19 -04:00
|
|
|
|
2018-01-15 19:19:30 +01:00
|
|
|
|
2019-10-17 13:33:43 +01:00
|
|
|
if __name__ == "__main__":
|
2018-01-15 19:19:30 +01:00
|
|
|
unittest.main()
|