Divadsn has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/404085 )
Change subject: Replace assertRaises with assertRaisesRegex in isbn_tests.py ...................................................................... Replace assertRaises with assertRaisesRegex in isbn_tests.py assertRaises is not as good of a test as asserRaisesRegex. The latter has an extra parameter to match the exception message, allowing more more precision when checking an error. Bug: T154281 Change-Id: Iaab3472bbae926caf5d68239b3828b5691ee38b9 --- M tests/isbn_tests.py 1 file changed, 54 insertions(+), 19 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/85/404085/1 diff --git a/tests/isbn_tests.py b/tests/isbn_tests.py index df00417..216a1f8 100644 --- a/tests/isbn_tests.py +++ b/tests/isbn_tests.py @@ -39,6 +39,19 @@ """Test CosmeticChanges ISBN fix.""" + ISBNINVALIDCHECKSUM_RE = ( + r"ISBN-13: The ISBN ([A-Z0-9]*) is not 13 digits long\. / " + r"ISBN-10: The ISBN checksum of ([A-Z0-9]*) is incorrect\." + ) + ISBNINVALIDLENGTH_RE = ( + r"ISBN-13: The ISBN ([A-Z0-9]*) is not 13 digits long\. / " + r"ISBN-10: The ISBN ([A-Z0-9]*) is not 10 digits long\." + ) + ISBNXINTHEMIDDLE_RE = ( + r"ISBN-13: The ISBN ([A-Z0-9]*) contains invalid characters\. / " + r"ISBN-10: The ISBN ([A-Z0-9]*) is not 10 digits long\." + ) + def test_valid_isbn(self): """Test ISBN.""" cc = CosmeticChangesToolkit(self.site, namespace=0) @@ -54,17 +67,17 @@ cc = CosmeticChangesToolkit(self.site, namespace=0) # Invalid characters - self.assertRaises(AnyIsbnValidationException, - cc.fix_ISBN, 'ISBN 0975229LOL') + self.assertRaisesRegex(AnyIsbnValidationException, self.ISBNINVALIDLENGTH_RE, + cc.fix_ISBN, 'ISBN 0975229LOL') # Invalid checksum - self.assertRaises(AnyIsbnValidationException, - cc.fix_ISBN, 'ISBN 0975229801') + self.assertRaisesRegex(AnyIsbnValidationException, self.ISBNINVALIDCHECKSUM_RE, + cc.fix_ISBN, 'ISBN 0975229801') # Invalid length - self.assertRaises(AnyIsbnValidationException, - cc.fix_ISBN, 'ISBN 09752298') + self.assertRaisesRegex(AnyIsbnValidationException, self.ISBNINVALIDLENGTH_RE, + cc.fix_ISBN, 'ISBN 09752298') # X in the middle - self.assertRaises(AnyIsbnValidationException, - cc.fix_ISBN, 'ISBN 09752X9801') + self.assertRaisesRegex(AnyIsbnValidationException, self.ISBNXINTHEMIDDLE_RE, + cc.fix_ISBN, 'ISBN 09752X9801') def test_ignore_invalid_isbn(self): """Test fixing ISBN numbers with an invalid ISBN.""" @@ -77,6 +90,23 @@ class TestIsbn(TestCase): """Test ISBN-related classes and helper functions.""" + + ISBNINVALIDCHARACTERS_RE = ( + r"The ISBN ([A-Z0-9]*) contains invalid characters" + ) + ISBNINVALIDCHECKSUM_RE = ( + r"The ISBN checksum of ([A-Z0-9]*) is incorrect" + ) + ISBNINVALIDLENGTH_RE = ( + r"The ISBN ([A-Z0-9]*) is not (10|13) digits long" + ) + ISBNBOTHINVALIDLENGTH_RE = ( + r"ISBN-13: The ISBN ([A-Z0-9]*) is not 13 digits long\. / " + r"ISBN-10: The ISBN ([A-Z0-9]*) is not 10 digits long\." + ) + ISBNXINTHEMIDDLE_RE = ( + r"ISBN ([A-Z0-9]*): X is only allowed at the end of the ISBN" + ) net = False @@ -94,10 +124,14 @@ self.assertEqual(isbn13.code, '978-0-9752298-0-4') # Errors - self.assertRaises(IsbnExc, ISBN10, '0975229LOL') # Invalid characters - self.assertRaises(IsbnExc, ISBN10, '0975229801') # Invalid checksum - self.assertRaises(IsbnExc, ISBN10, '09752298') # Invalid length - self.assertRaises(IsbnExc, ISBN10, '09752X9801') # X in the middle + self.assertRaisesRegex(IsbnExc, self.ISBNINVALIDCHARACTERS_RE, + ISBN10, '0975229LOL') # Invalid characters + self.assertRaisesRegex(IsbnExc, self.ISBNINVALIDCHECKSUM_RE, + ISBN10, '0975229801') # Invalid checksum + self.assertRaisesRegex(IsbnExc, self.ISBNINVALIDLENGTH_RE, + ISBN10, '09752298') # Invalid length + self.assertRaisesRegex(IsbnExc, self.ISBNXINTHEMIDDLE_RE, + ISBN10, '09752X9801') # X in the middle def test_isbn13(self): """Test ISBN13.""" @@ -111,19 +145,20 @@ self.assertEqual(isbn.code, '9788090273412') # Errors - self.assertRaises(IsbnExc, ISBN13, '9783161484LOL') # Invalid chars - self.assertRaises(IsbnExc, ISBN13, '9783161484105') # Invalid checksum - self.assertRaises(IsbnExc, ISBN13, '9783161484') # Invalid length + self.assertRaisesRegex(IsbnExc, self.ISBNINVALIDCHARACTERS_RE, + ISBN13, '9783161484LOL') # Invalid characters + self.assertRaisesRegex(IsbnExc, self.ISBNINVALIDCHECKSUM_RE, + ISBN13, '9783161484105') # Invalid checksum + self.assertRaisesRegex(IsbnExc, self.ISBNINVALIDLENGTH_RE, + ISBN13, '9783161484') # Invalid length def test_general(self): """Test things that apply both to ISBN10 and ISBN13.""" # getIsbn self.assertIsInstance(getIsbn('097522980x'), ISBN10) self.assertIsInstance(getIsbn('9783161484100'), ISBN13) - self.assertRaisesRegex(IsbnExc, - 'ISBN-13: The ISBN 097522 is not 13 digits ' - 'long. / ISBN-10: The ISBN 097522 is not 10 ' - 'digits long.', getIsbn, '097522') + self.assertRaisesRegex(IsbnExc, self.ISBNBOTHINVALIDLENGTH_RE , + getIsbn, '097522') # hyphenateIsbnNumbers self.assertEqual(hyphenateIsbnNumbers('ISBN 097522980x'), -- To view, visit https://gerrit.wikimedia.org/r/404085 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iaab3472bbae926caf5d68239b3828b5691ee38b9 Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Divadsn <divad.nnamtd...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits