mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix calibre.library.test.DBTest
- fix testSorting by making the author_sort field on the test data unique. - make tests less brittle by declaratively defining the data to be compared by testSorting
This commit is contained in:
parent
8522ac55af
commit
a517cec2cd
@ -15,33 +15,40 @@ from calibre.library.database2 import LibraryDatabase2
|
|||||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
from calibre.ptempfile import PersistentTemporaryDirectory
|
||||||
from calibre.utils.run_tests import find_tests_in_dir, run_tests
|
from calibre.utils.run_tests import find_tests_in_dir, run_tests
|
||||||
|
|
||||||
|
|
||||||
class DBTest(unittest.TestCase):
|
class DBTest(unittest.TestCase):
|
||||||
|
|
||||||
img = '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00d\x00d\x00\x00\xff\xdb\x00C\x00\x05\x03\x04\x04\x04\x03\x05\x04\x04\x04\x05\x05\x05\x06\x07\x0c\x08\x07\x07\x07\x07\x0f\x0b\x0b\t\x0c\x11\x0f\x12\x12\x11\x0f\x11\x11\x13\x16\x1c\x17\x13\x14\x1a\x15\x11\x11\x18!\x18\x1a\x1d\x1d\x1f\x1f\x1f\x13\x17"$"\x1e$\x1c\x1e\x1f\x1e\xff\xdb\x00C\x01\x05\x05\x05\x07\x06\x07\x0e\x08\x08\x0e\x1e\x14\x11\x14\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\xff\xc0\x00\x11\x08\x00\x01\x00\x01\x03\x01\x11\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x14\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xc4\x00\x14\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\xff\xc4\x00\x14\x11\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00?\x00p\xf9+\xff\xd9' # noqa
|
img = '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00d\x00d\x00\x00\xff\xdb\x00C\x00\x05\x03\x04\x04\x04\x03\x05\x04\x04\x04\x05\x05\x05\x06\x07\x0c\x08\x07\x07\x07\x07\x0f\x0b\x0b\t\x0c\x11\x0f\x12\x12\x11\x0f\x11\x11\x13\x16\x1c\x17\x13\x14\x1a\x15\x11\x11\x18!\x18\x1a\x1d\x1d\x1f\x1f\x1f\x13\x17"$"\x1e$\x1c\x1e\x1f\x1e\xff\xdb\x00C\x01\x05\x05\x05\x07\x06\x07\x0e\x08\x08\x0e\x1e\x14\x11\x14\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\xff\xc0\x00\x11\x08\x00\x01\x00\x01\x03\x01\x11\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x14\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xc4\x00\x14\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\xff\xc4\x00\x14\x11\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00?\x00p\xf9+\xff\xd9' # noqa
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.tdir = PersistentTemporaryDirectory('_calibre_dbtest')
|
self.tdir = PersistentTemporaryDirectory('_calibre_dbtest')
|
||||||
self.db = LibraryDatabase2(self.tdir)
|
self.db = LibraryDatabase2(self.tdir)
|
||||||
f = open(os.path.join(self.tdir, 'test.txt'), 'w+b')
|
f = open(os.path.join(self.tdir, 'test.txt'), 'w+b')
|
||||||
f.write('test')
|
f.write('test')
|
||||||
paths = list(repeat(f, 3))
|
paths = list(repeat(f, 3))
|
||||||
formats = list(repeat('txt', 3))
|
formats = list(repeat('txt', 3))
|
||||||
m1 = MetaInformation('Test Ebook 1', ['Test Author 1'])
|
|
||||||
|
# sorted tuple of tuples of (author-sort author name, full author name)
|
||||||
|
self.authors = sorted((('as1', 'Test Author 1'), ('as2', 'Test Author 2')))
|
||||||
|
self.ratings = sorted((3, 2), reverse=True)
|
||||||
|
|
||||||
|
m1 = MetaInformation('Test Ebook 1', [self.authors[0][1]])
|
||||||
m1.tags = ['tag1', 'tag2']
|
m1.tags = ['tag1', 'tag2']
|
||||||
m1.publisher = 'Test Publisher 1'
|
m1.publisher = 'Test Publisher 1'
|
||||||
m1.rating = 2
|
m1.rating = self.ratings[1]
|
||||||
m1.series = 'Test Series 1'
|
m1.series = 'Test Series 1'
|
||||||
m1.series_index = 3
|
m1.series_index = 3
|
||||||
m1.author_sort = 'as1'
|
m1.author_sort = self.authors[0][0]
|
||||||
m1.isbn = 'isbn1'
|
m1.isbn = 'isbn1'
|
||||||
m1.cover_data = ('jpg', self.img)
|
m1.cover_data = ('jpg', self.img)
|
||||||
m2 = MetaInformation('Test Ebook 2', ['Test Author 2'])
|
|
||||||
|
m2 = MetaInformation('Test Ebook 2', [self.authors[1][1]])
|
||||||
m2.tags = ['tag3', 'tag4']
|
m2.tags = ['tag3', 'tag4']
|
||||||
m2.publisher = 'Test Publisher 2'
|
m2.publisher = 'Test Publisher 2'
|
||||||
m2.rating = 3
|
m2.rating = self.ratings[0]
|
||||||
m2.series = 'Test Series 2'
|
m2.series = 'Test Series 2'
|
||||||
m2.series_index = 1
|
m2.series_index = 1
|
||||||
m2.author_sort = 'as1'
|
m2.author_sort = self.authors[1][0]
|
||||||
m2.isbn = 'isbn1'
|
m2.isbn = 'isbn1'
|
||||||
self.db.add_books(paths, formats, [m1, m2, m2], add_duplicates=True)
|
self.db.add_books(paths, formats, [m1, m2, m2], add_duplicates=True)
|
||||||
self.m1, self.m2 = m1, m2
|
self.m1, self.m2 = m1, m2
|
||||||
@ -71,7 +78,8 @@ class DBTest(unittest.TestCase):
|
|||||||
|
|
||||||
def testMetadata(self):
|
def testMetadata(self):
|
||||||
self.db.refresh('timestamp', True)
|
self.db.refresh('timestamp', True)
|
||||||
for x in ('title', 'author_sort', 'series', 'publisher', 'isbn', 'series_index', 'rating'):
|
for x in ('title', 'author_sort', 'series', 'publisher', 'isbn',
|
||||||
|
'series_index', 'rating'):
|
||||||
val = 3 if x in ['rating', 'series_index'] else 'dummy'
|
val = 3 if x in ['rating', 'series_index'] else 'dummy'
|
||||||
getattr(self.db, 'set_'+x)(3, val)
|
getattr(self.db, 'set_'+x)(3, val)
|
||||||
self.db.refresh_ids([3])
|
self.db.refresh_ids([3])
|
||||||
@ -84,9 +92,9 @@ class DBTest(unittest.TestCase):
|
|||||||
|
|
||||||
def testSorting(self):
|
def testSorting(self):
|
||||||
self.db.sort('authors', True)
|
self.db.sort('authors', True)
|
||||||
self.assertEqual(self.db.authors(0), 'Test Author 1')
|
self.assertEqual(self.db.authors(0), self.authors[0][1])
|
||||||
self.db.sort('rating', False)
|
self.db.sort('rating', False)
|
||||||
self.assertEqual(self.db.rating(0), 3)
|
self.assertEqual(self.db.rating(0), self.ratings[0])
|
||||||
|
|
||||||
|
|
||||||
def find_tests():
|
def find_tests():
|
||||||
@ -98,9 +106,10 @@ def test():
|
|||||||
run_tests(find_tests())
|
run_tests(find_tests())
|
||||||
|
|
||||||
|
|
||||||
def main(args=sys.argv):
|
def main():
|
||||||
test()
|
test()
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user