M4tx has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/181427

Change subject: Add unit test for isbn script
......................................................................

Add unit test for isbn script

Bug: T72336
Change-Id: I006570c68e83c52ae5cb0051ad7336eb5e02f93f
---
A tests/isbn_tests.py
1 file changed, 81 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/27/181427/1

diff --git a/tests/isbn_tests.py b/tests/isbn_tests.py
new file mode 100644
index 0000000..bf15f15
--- /dev/null
+++ b/tests/isbn_tests.py
@@ -0,0 +1,81 @@
+# -*- coding: utf-8  -*-
+"""Tests for isbn script."""
+#
+# (C) Pywikibot team, 2014
+#
+# Distributed under the terms of the MIT license.
+#
+__version__ = '$Id$'
+
+import unittest
+from scripts.isbn import ISBN10, ISBN13, InvalidIsbnException as IsbnExc, \
+    getIsbn, hyphenateIsbnNumbers, convertIsbn10toIsbn13
+from tests.aspects import TestCase
+
+
+class TestIsbn(TestCase):
+    net = False
+
+    """Test ISBN-related classes and helper functions."""
+
+    def test_isbn10(self):
+        """Test ISBN10."""
+        # Test general features
+        isbn = ISBN10('097522980x')
+        isbn.format()
+        self.assertEqual(isbn.code, '0-9752298-0-X')
+        self.assertEqual(isbn.digits(),
+                         ['0', '9', '7', '5', '2', '2', '9', '8', '0', 'X'])
+
+        # Converting to ISBN13
+        isbn13 = isbn.toISBN13()
+        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
+
+    def test_isbn13(self):
+        """Test ISBN13."""
+        # Test general features
+        isbn = ISBN13('9783161484100')
+        isbn.format()
+        self.assertEqual(isbn.code, '978-3-16-148410-0')
+        self.assertEqual(isbn.digits(),
+                         [9, 7, 8, 3, 1, 6, 1, 4, 8, 4, 1, 0, 0])
+
+        # Errors
+        self.assertRaises(IsbnExc, ISBN13, '9783161484LOL')  # Invalid chars
+        self.assertRaises(IsbnExc, ISBN13, '9783161484105')  # Invalid checksum
+        self.assertRaises(IsbnExc, 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.assertRaises(IsbnExc, getIsbn, '097522')
+
+        # hyphenateIsbnNumbers
+        self.assertEqual(hyphenateIsbnNumbers('ISBN 097522980x'),
+                         'ISBN 0-9752298-0-X')
+        self.assertEqual(hyphenateIsbnNumbers('ISBN 0975229801'),
+                         'ISBN 0975229801')  # Invalid ISBN - no changes
+
+        # convertIsbn10toIsbn13
+        self.assertEqual(convertIsbn10toIsbn13('ISBN 0-9752298-0-X'),
+                         'ISBN 978-0-9752298-0-4')
+        self.assertEqual(convertIsbn10toIsbn13('ISBN 0-9752298-0-1'),
+                         'ISBN 0-9752298-0-1')  # Invalid ISBN - no changes
+
+        # Errors
+        isbn = ISBN10('9492098059')
+        self.assertRaises(IsbnExc, isbn.format)  # Invalid group number
+        isbn = ISBN10('9095012042')
+        self.assertRaises(IsbnExc, isbn.format)  # Invalid publisher number
+
+
+if __name__ == "__main__":
+    unittest.main()

-- 
To view, visit https://gerrit.wikimedia.org/r/181427
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I006570c68e83c52ae5cb0051ad7336eb5e02f93f
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: M4tx <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to