jenkins-bot has submitted this change and it was merged. (
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, 53 insertions(+), 15 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/isbn_tests.py b/tests/isbn_tests.py
index df00417..ae867d2 100644
--- a/tests/isbn_tests.py
+++ b/tests/isbn_tests.py
@@ -34,10 +34,29 @@
else:
AnyIsbnValidationException = IsbnExc
+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'
+)
+
+ISBNINVALIDCHARACTERS_RE = (
+ r'The ISBN ([A-Z0-9]*) contains invalid characters'
+)
+
class TestCosmeticChangesISBN(DefaultDrySiteTestCase):
"""Test CosmeticChanges ISBN fix."""
+
+ INVALIDNUMBERLENGTH_RE = (
+ r'The number has an invalid length'
+ )
+ INVALIDNUMBERCHECKSUM_RE = (
+ r'The number\'s checksum or check digit is invalid'
+ )
def test_valid_isbn(self):
"""Test ISBN."""
@@ -54,17 +73,25 @@
cc = CosmeticChangesToolkit(self.site, namespace=0)
# Invalid characters
- self.assertRaises(AnyIsbnValidationException,
- cc.fix_ISBN, 'ISBN 0975229LOL')
+ self.assertRaisesRegex(AnyIsbnValidationException,
+ (ISBNINVALIDLENGTH_RE + '|' +
+ self.INVALIDNUMBERLENGTH_RE),
+ cc.fix_ISBN, 'ISBN 0975229LOL')
# Invalid checksum
- self.assertRaises(AnyIsbnValidationException,
- cc.fix_ISBN, 'ISBN 0975229801')
+ self.assertRaisesRegex(AnyIsbnValidationException,
+ (ISBNINVALIDCHECKSUM_RE + '|' +
+ self.INVALIDNUMBERCHECKSUM_RE),
+ cc.fix_ISBN, 'ISBN 0975229801')
# Invalid length
- self.assertRaises(AnyIsbnValidationException,
- cc.fix_ISBN, 'ISBN 09752298')
+ self.assertRaisesRegex(AnyIsbnValidationException,
+ (ISBNINVALIDLENGTH_RE + '|' +
+ self.INVALIDNUMBERLENGTH_RE),
+ cc.fix_ISBN, 'ISBN 09752298')
# X in the middle
- self.assertRaises(AnyIsbnValidationException,
- cc.fix_ISBN, 'ISBN 09752X9801')
+ self.assertRaisesRegex(AnyIsbnValidationException,
+ (ISBNINVALIDCHARACTERS_RE + '|' +
+ self.INVALIDNUMBERLENGTH_RE),
+ cc.fix_ISBN, 'ISBN 09752X9801')
def test_ignore_invalid_isbn(self):
"""Test fixing ISBN numbers with an invalid
ISBN."""
@@ -77,6 +104,10 @@
class TestIsbn(TestCase):
"""Test ISBN-related classes and helper functions."""
+
+ ISBNXINTHEMIDDLE_RE = (
+ r'ISBN ([A-Z0-9]*): X is only allowed at the end of the ISBN'
+ )
net = False
@@ -94,10 +125,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, ISBNINVALIDCHARACTERS_RE,
+ ISBN10, '0975229LOL') # Invalid characters
+ self.assertRaisesRegex(IsbnExc, ISBNINVALIDCHECKSUM_RE,
+ ISBN10, '0975229801') # Invalid checksum
+ self.assertRaisesRegex(IsbnExc, 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,9 +146,12 @@
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, ISBNINVALIDCHARACTERS_RE,
+ ISBN13, '9783161484LOL') # Invalid characters
+ self.assertRaisesRegex(IsbnExc, ISBNINVALIDCHECKSUM_RE,
+ ISBN13, '9783161484105') # Invalid checksum
+ self.assertRaisesRegex(IsbnExc, ISBNINVALIDLENGTH_RE,
+ ISBN13, '9783161484') # Invalid length
def test_general(self):
"""Test things that apply both to ISBN10 and
ISBN13."""
--
To view, visit
https://gerrit.wikimedia.org/r/404085
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iaab3472bbae926caf5d68239b3828b5691ee38b9
Gerrit-PatchSet: 14
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Divadsn <divad.nnamtdeis(a)gmail.com>
Gerrit-Reviewer: Divadsn <divad.nnamtdeis(a)gmail.com>
Gerrit-Reviewer: Framawiki <framawiki(a)tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: MtDu <justin.d128(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>