From 8ccb974a6130af172b84f214f23b3f6177fe9af4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 13 Dec 2020 17:26:18 +0530 Subject: [PATCH] Dont import unittest when not running tests --- src/calibre/ebooks/metadata/opf2.py | 138 ++++++++++++++-------------- 1 file changed, 70 insertions(+), 68 deletions(-) diff --git a/src/calibre/ebooks/metadata/opf2.py b/src/calibre/ebooks/metadata/opf2.py index 1832f073b2..810fcf8a45 100644 --- a/src/calibre/ebooks/metadata/opf2.py +++ b/src/calibre/ebooks/metadata/opf2.py @@ -8,7 +8,7 @@ __docformat__ = 'restructuredtext en' lxml based OPF parser. ''' -import re, sys, unittest, functools, os, uuid, glob, io, json, copy +import re, sys, functools, os, uuid, glob, io, json, copy from lxml import etree @@ -1742,78 +1742,80 @@ def test_m2o(): print('!=', newmi.get_identifiers()) -class OPFTest(unittest.TestCase): - - def setUp(self): - self.stream = io.BytesIO( -b'''\ - - - - A Cool & © ß Title - Monkey Kitchen - Next - OneTwo - 123456789 - dummy - - - - - - - - - -''' - ) - self.opf = OPF(self.stream, getcwd()) - - def testReading(self, opf=None): - if opf is None: - opf = self.opf - self.assertEqual(opf.title, u'A Cool & \xa9 \xdf Title') - self.assertEqual(opf.authors, u'Monkey Kitchen,Next'.split(',')) - self.assertEqual(opf.author_sort, 'Monkey') - self.assertEqual(opf.title_sort, 'Wow') - self.assertEqual(opf.tags, ['One', 'Two']) - self.assertEqual(opf.isbn, '123456789') - self.assertEqual(opf.series, 'A one book series') - self.assertEqual(opf.series_index, 2.5) - self.assertEqual(opf.rating, 4) - self.assertEqual(opf.publication_type, 'test') - self.assertEqual(list(opf.itermanifest())[0].get('href'), 'a ~ b') - self.assertEqual(opf.get_identifiers(), {'isbn':'123456789', - 'dummy':'dummy'}) - - def testWriting(self): - for test in [('title', 'New & Title'), ('authors', ['One', 'Two']), - ('author_sort', "Kitchen"), ('tags', ['Three']), - ('isbn', 'a'), ('rating', 3), ('series_index', 1), - ('title_sort', 'ts')]: - setattr(self.opf, *test) - attr, val = test - self.assertEqual(getattr(self.opf, attr), val) - - self.opf.render() - - def testCreator(self): - opf = OPFCreator(getcwd(), self.opf) - buf = io.BytesIO() - opf.render(buf) - raw = buf.getvalue() - self.testReading(opf=OPF(io.BytesIO(raw), getcwd())) - - def testSmartUpdate(self): - self.opf.smart_update(MetaInformation(self.opf)) - self.testReading() - - def suite(): + import unittest + + class OPFTest(unittest.TestCase): + + def setUp(self): + self.stream = io.BytesIO( + b'''\ + + + + A Cool & © ß Title + Monkey Kitchen + Next + OneTwo + 123456789 + dummy + + + + + + + + + + ''' + ) + self.opf = OPF(self.stream, getcwd()) + + def testReading(self, opf=None): + if opf is None: + opf = self.opf + self.assertEqual(opf.title, u'A Cool & \xa9 \xdf Title') + self.assertEqual(opf.authors, u'Monkey Kitchen,Next'.split(',')) + self.assertEqual(opf.author_sort, 'Monkey') + self.assertEqual(opf.title_sort, 'Wow') + self.assertEqual(opf.tags, ['One', 'Two']) + self.assertEqual(opf.isbn, '123456789') + self.assertEqual(opf.series, 'A one book series') + self.assertEqual(opf.series_index, 2.5) + self.assertEqual(opf.rating, 4) + self.assertEqual(opf.publication_type, 'test') + self.assertEqual(list(opf.itermanifest())[0].get('href'), 'a ~ b') + self.assertEqual(opf.get_identifiers(), {'isbn':'123456789', + 'dummy':'dummy'}) + + def testWriting(self): + for test in [('title', 'New & Title'), ('authors', ['One', 'Two']), + ('author_sort', "Kitchen"), ('tags', ['Three']), + ('isbn', 'a'), ('rating', 3), ('series_index', 1), + ('title_sort', 'ts')]: + setattr(self.opf, *test) + attr, val = test + self.assertEqual(getattr(self.opf, attr), val) + + self.opf.render() + + def testCreator(self): + opf = OPFCreator(getcwd(), self.opf) + buf = io.BytesIO() + opf.render(buf) + raw = buf.getvalue() + self.testReading(opf=OPF(io.BytesIO(raw), getcwd())) + + def testSmartUpdate(self): + self.opf.smart_update(MetaInformation(self.opf)) + self.testReading() + return unittest.TestLoader().loadTestsFromTestCase(OPFTest) def test(): + import unittest unittest.TextTestRunner(verbosity=2).run(suite())