Debian Codegraph @

src/calibre/db/tests/legacy.py

raw
1 #!/usr/bin/env python
2 # License: GPLv3 Copyright: 2013, Kovid Goyal <kovid at kovidgoyal.net>
3
4 import inspect
5 import numbers
6 import reprlib
7 import time
8 from functools import partial
9 from io import BytesIO
10 from operator import itemgetter
11
12 from calibre.db.constants import NOTES_DIR_NAME
13 from calibre.db.tests.base import BaseTest
14 from calibre.library.field_metadata import fm_as_dict
15 from calibre.utils.localization import _
16
17 # Utils {{{
18
19
20 class ET:
21 def __init__(self, func_name, args, kwargs={}, old=None, legacy=None):
22 self.func_name = func_name
23 self.args, self.kwargs = args, kwargs
24 self.old, self.legacy = old, legacy
25
26 def __call__(self, test):
27 old = self.old or test.init_old(test.cloned_library)
28 legacy = self.legacy or test.init_legacy(test.cloned_library)
29 oldres = getattr(old, self.func_name)(*self.args, **self.kwargs)
30 newres = getattr(legacy, self.func_name)(*self.args, **self.kwargs)
31 test.assertEqual(
32 oldres,
33 newres,
34 f'Equivalence test for {self.func_name} with args: {reprlib.repr(self.args)} and kwargs: {reprlib.repr(self.kwargs)} failed',
35 )
36 self.retval = newres
37 return newres
38
39
40 def get_defaults(spec):
41 num = len(spec.defaults or ())
42 if not num:
43 return {}
44 return dict(zip(spec.args[-num:], spec.defaults))
45
46
47 def compare_argspecs(old, new, attr):
48 # We don't compare the names of the non-keyword arguments as they are often
49 # different and they don't affect the usage of the API.
50
51 ok = len(old.args) == len(new.args) and get_defaults(old) == get_defaults(new)
52 if not ok:
53 raise AssertionError(f'The argspec for {attr} does not match. {old!r} != {new!r}')
54
55
56 def run_funcs(self, db, ndb, funcs):
57 for func in funcs:
58 meth, args = func[0], func[1:]
59 if callable(meth):
60 meth(*args)
61 else:
62 if meth[0] in {'!', '@', '#', '+', '$', '-', '%'}:
63 if meth[0] != '+':
64 fmt = {
65 '!': dict,
66 '@': lambda x: frozenset(x or ()),
67 '#': lambda x: set((x or '').split(',')),
68 '$': lambda x: {tuple(y) for y in x},
69 '-': lambda x: None,
70 '%': lambda x: set((x or '').split(',')),
71 }[meth[0]]
72 else:
73 fmt = args[-1]
74 args = args[:-1]
75 meth = meth[1:]
76 else:
77
78 def fmt(x):
79 return x
80
81 res1, res2 = fmt(getattr(db, meth)(*args)), fmt(getattr(ndb, meth)(*args))
82 self.assertEqual(res1, res2, f'The method: {meth}() returned different results for argument {args}')
83
84
85 # }}}
86
87
88 class LegacyTest(BaseTest):
89 """Test the emulation of the legacy interface."""
90
91 def test_library_wide_properties(self): # {{{
92 "Test library wide properties"
93
94 def to_unicode(x):
95 if isinstance(x, bytes):
96 return x.decode('utf-8')
97 if isinstance(x, dict):
98 # We ignore the key rec_index, since it is not stable for
99 # custom columns (it is created by iterating over a dict)
100 return {k.decode('utf-8') if isinstance(k, bytes) else k: to_unicode(v) for k, v in x.items() if k != 'rec_index'}
101 return x
102
103 def get_props(db):
104 props = (
105 'user_version',
106 'is_second_db',
107 'library_id',
108 'custom_column_label_map',
109 'custom_column_num_map',
110 'library_path',
111 'dbpath',
112 )
113 fprops = ('last_modified',)
114 ans = {x: getattr(db, x) for x in props}
115 ans.update({x: getattr(db, x)() for x in fprops})
116 ans['all_ids'] = frozenset(db.all_ids())
117 ans['field_metadata'] = fm_as_dict(db.field_metadata)
118 return to_unicode(ans)
119
120 old = self.init_old()
121 oldvals = get_props(old)
122 old.close()
123 del old
124 db = self.init_legacy()
125 newvals = get_props(db)
126 self.assertEqual(oldvals, newvals)
127 db.close()
128
129 # }}}
130
131 def test_get_property(self): # {{{
132 "Test the get_property interface for reading data"
133
134 def get_values(db):
135 ans = {}
136 for label, loc in db.FIELD_MAP.items():
137 if isinstance(label, numbers.Integral):
138 label = '#' + db.custom_column_num_map[label]['label']
139 label = str(label)
140 ans[label] = tuple(db.get_property(i, index_is_id=True, loc=loc) for i in db.all_ids())
141 if label in ('id', 'title', '#tags'):
142 with self.assertRaises(IndexError):
143 db.get_property(9999, loc=loc)
144 with self.assertRaises(IndexError):
145 db.get_property(9999, index_is_id=True, loc=loc)
146 if label in {'tags', 'formats'}:
147 # Order is random in the old db for these
148 ans[label] = tuple(set(x.split(',')) if x else x for x in ans[label])
149 if label == 'series_sort':
150 # The old db code did not take book language into account
151 # when generating series_sort values
152 ans[label] = None
153 return ans
154
155 db = self.init_legacy()
156 new_vals = get_values(db)
157 db.close()
158
159 old = self.init_old()
160 old_vals = get_values(old)
161 old.close()
162 old = None
163 self.assertEqual(old_vals, new_vals)
164
165 # }}}
166
167 def test_refresh(self): # {{{
168 "Test refreshing the view after a change to metadata.db"
169 db = self.init_legacy()
170 db2 = self.init_legacy()
171 # Ensure that the following change will actually update the timestamp
172 # on filesystems with one second resolution (OS X)
173 time.sleep(1)
174 self.assertEqual(db2.data.cache.set_field('title', {1: 'xxx'}), {1})
175 db2.close()
176 del db2
177 self.assertNotEqual(db.title(1, index_is_id=True), 'xxx')
178 db.check_if_modified()
179 self.assertEqual(db.title(1, index_is_id=True), 'xxx')
180
181 # }}}
182
183 def test_legacy_getters(self): # {{{
184 "Test various functions to get individual bits of metadata"
185 old = self.init_old()
186 getters = (
187 'path',
188 'abspath',
189 'title',
190 'title_sort',
191 'authors',
192 'series',
193 'publisher',
194 'author_sort',
195 'authors',
196 'comments',
197 'comment',
198 'publisher',
199 'rating',
200 'series_index',
201 'tags',
202 'timestamp',
203 'uuid',
204 'pubdate',
205 'ondevice',
206 'metadata_last_modified',
207 'languages',
208 )
209 oldvals = {g: tuple(getattr(old, g)(x) for x in range(3)) + tuple(getattr(old, g)(x, True) for x in (1, 2, 3)) for g in getters}
210 old_rows = {tuple(r)[:5] for r in old}
211 old.close()
212 db = self.init_legacy()
213 newvals = {g: tuple(getattr(db, g)(x) for x in range(3)) + tuple(getattr(db, g)(x, True) for x in (1, 2, 3)) for g in getters}
214 new_rows = {tuple(r)[:5] for r in db}
215 for x in (oldvals, newvals):
216 x['tags'] = tuple(set(y.split(',')) if y else y for y in x['tags'])
217 self.assertEqual(oldvals, newvals)
218 self.assertEqual(old_rows, new_rows)
219
220 # }}}
221
222 def test_legacy_direct(self): # {{{
223 "Test read-only methods that are directly equivalent in the old and new interface"
224 from datetime import timedelta
225
226 from calibre.ebooks.metadata.book.base import Metadata
227
228 ndb = self.init_legacy(self.cloned_library)
229 db = self.init_old()
230 newstag = ndb.new_api.get_item_id('tags', 'news')
231
232 self.assertEqual(dict(db.prefs), dict(ndb.prefs))
233
234 for meth, args in {
235 'find_identical_books': [
236 (Metadata('title one', ['author one']),),
237 (Metadata('unknown'),),
238 (Metadata('xxxx'),),
239 ],
240 'get_books_for_category': [('tags', newstag), ('#formats', 'FMT1')],
241 'get_next_series_num_for': [('A Series One',)],
242 'get_id_from_uuid': [('ddddd',), (db.uuid(1, True),)],
243 'cover': [(0,), (1,), (2,)],
244 'get_author_id': [('author one',), ('unknown',), ('xxxxx',)],
245 'series_id': [(0,), (1,), (2,)],
246 'publisher_id': [(0,), (1,), (2,)],
247 '@tags_older_than': [
248 ('News', None),
249 ('Tag One', None),
250 ('xxxx', None),
251 ('Tag One', None, 'News'),
252 ('News', None, 'xxxx'),
253 ('News', None, None, ['xxxxxxx']),
254 ('News', None, 'Tag One', ['Author Two', 'Author One']),
255 ('News', timedelta(0), None, None),
256 ('News', timedelta(100000)),
257 ],
258 'format': [(1, 'FMT1', True), (2, 'FMT1', True), (0, 'xxxxxx')],
259 'has_format': [(1, 'FMT1', True), (2, 'FMT1', True), (0, 'xxxxxx')],
260 'sizeof_format': [(1, 'FMT1', True), (2, 'FMT1', True), (0, 'xxxxxx')],
261 '@format_files': [(0,), (1,), (2,)],
262 'formats': [(0,), (1,), (2,)],
263 'max_size': [(0,), (1,), (2,)],
264 'format_hash': [(1, 'FMT1'), (1, 'FMT2'), (2, 'FMT1')],
265 'author_sort_from_authors': [(['Author One', 'Author Two', 'Unknown'],)],
266 'has_book': [(Metadata('title one'),), (Metadata('xxxx1111'),)],
267 'has_id': [(1,), (2,), (3,), (9999,)],
268 'id': [
269 (1,),
270 (2,),
271 (0,),
272 ],
273 'index': [
274 (1,),
275 (2,),
276 (3,),
277 ],
278 'row': [
279 (1,),
280 (2,),
281 (3,),
282 ],
283 'is_empty': [()],
284 'count': [()],
285 'all_author_names': [()],
286 'all_tag_names': [()],
287 'all_series_names': [()],
288 'all_publisher_names': [()],
289 '!all_authors': [()],
290 '!all_tags2': [()],
291 '@all_tags': [()],
292 '@get_all_identifier_types': [()],
293 '!all_publishers': [()],
294 '!all_titles': [()],
295 '!all_series': [()],
296 'standard_field_keys': [()],
297 'all_field_keys': [()],
298 'searchable_fields': [()],
299 'search_term_to_field_key': [('author',), ('tag',)],
300 'metadata_for_field': [('title',), ('tags',)],
301 'sortable_field_keys': [()],
302 'custom_field_keys': [(True,), (False,)],
303 '!get_usage_count_by_id': [
304 ('authors',),
305 ('tags',),
306 ('series',),
307 ('publisher',),
308 ('#tags',),
309 ('languages',),
310 ],
311 'get_field': [(1, 'title'), (2, 'tags'), (0, 'rating'), (1, 'authors'), (2, 'series'), (1, '#tags')],
312 'all_formats': [()],
313 'get_authors_with_ids': [()],
314 '!get_tags_with_ids': [()],
315 '!get_series_with_ids': [()],
316 '!get_publishers_with_ids': [()],
317 '!get_ratings_with_ids': [()],
318 '!get_languages_with_ids': [()],
319 'tag_name': [(3,)],
320 'author_name': [(3,)],
321 'series_name': [(3,)],
322 'authors_sort_strings': [(0,), (1,), (2,)],
323 'author_sort_from_book': [(0,), (1,), (2,)],
324 'authors_with_sort_strings': [(0,), (1,), (2,)],
325 'book_on_device_string': [(1,), (2,), (3,)],
326 'books_in_series_of': [(0,), (1,), (2,)],
327 'books_with_same_title': [(Metadata(db.title(0)),), (Metadata(db.title(1)),), (Metadata('1234'),)],
328 }.items():
329 if meth[0] in {'!', '@'}:
330 fmt = {'!': dict, '@': frozenset}[meth[0]]
331 meth = meth[1:]
332 elif meth == 'get_authors_with_ids':
333
334 def fmt(val):
335 return {x[0]: tuple(x[1:]) for x in val}
336
337 else:
338
339 def fmt(x):
340 return x
341
342 for a in args:
343 self.assertEqual(
344 fmt(getattr(db, meth)(*a)),
345 fmt(getattr(ndb, meth)(*a)),
346 f'The method: {meth}() returned different results for argument {a}',
347 )
348
349 def f(x, y): # get_top_level_move_items is broken in the old db on case-insensitive file systems
350 x.discard('metadata_db_prefs_backup.json')
351 y.pop('full-text-search.db', None)
352 x.discard(NOTES_DIR_NAME)
353 y.pop(NOTES_DIR_NAME, None)
354 return x, y
355
356 self.assertEqual(f(*db.get_top_level_move_items()), f(*ndb.get_top_level_move_items()))
357 d1, d2 = BytesIO(), BytesIO()
358 db.copy_cover_to(1, d1, True)
359 ndb.copy_cover_to(1, d2, True)
360 self.assertTrue(d1.getvalue() == d2.getvalue())
361 d1, d2 = BytesIO(), BytesIO()
362 db.copy_format_to(1, 'FMT1', d1, True)
363 ndb.copy_format_to(1, 'FMT1', d2, True)
364 self.assertTrue(d1.getvalue() == d2.getvalue())
365 old = db.get_data_as_dict(prefix='test-prefix')
366 new = ndb.get_data_as_dict(prefix='test-prefix')
367 for o, n in zip(old, new):
368 o = {str(k) if isinstance(k, bytes) else k: set(v) if isinstance(v, list) else v for k, v in o.items()}
369 n = {k: set(v) if isinstance(v, list) else v for k, v in n.items()}
370 self.assertEqual(o, n)
371
372 ndb.search('title:Unknown')
373 db.search('title:Unknown')
374 self.assertEqual(db.row(3), ndb.row(3))
375 self.assertRaises(ValueError, ndb.row, 2)
376 self.assertRaises(ValueError, db.row, 2)
377 db.close()
378 # }}}
379
380 def test_legacy_conversion_options(self): # {{{
381 "Test conversion options API"
382 ndb = self.init_legacy()
383 db = self.init_old()
384 all_ids = ndb.new_api.all_book_ids()
385 op1 = {'xx': 'yy'}
386
387 def decode(x):
388 if isinstance(x, bytes):
389 x = x.decode('utf-8')
390 return x
391
392 for x in (
393 ('has_conversion_options', all_ids),
394 ('conversion_options', 1, 'PIPE'),
395 ('set_conversion_options', 1, 'PIPE', op1),
396 ('has_conversion_options', all_ids),
397 ('conversion_options', 1, 'PIPE'),
398 ('delete_conversion_options', 1, 'PIPE'),
399 ('has_conversion_options', all_ids),
400 ):
401 meth, args = x[0], x[1:]
402 self.assertEqual(
403 decode(getattr(db, meth)(*args)),
404 decode(getattr(ndb, meth)(*args)),
405 f'The method: {meth}() returned different results for argument {args}',
406 )
407 db.close()
408
409 # }}}
410
411 def test_legacy_delete_using(self): # {{{
412 "Test delete_using() API"
413 ndb = self.init_legacy()
414 db = self.init_old()
415 cache = ndb.new_api
416 tmap = cache.get_id_map('tags')
417 t = next(iter(tmap))
418 pmap = cache.get_id_map('publisher')
419 p = next(iter(pmap))
420 run_funcs(
421 self,
422 db,
423 ndb,
424 (
425 ('delete_tag_using_id', t),
426 ('delete_publisher_using_id', p),
427 (db.refresh,),
428 ('all_tag_names',),
429 ('tags', 0),
430 ('tags', 1),
431 ('tags', 2),
432 ('all_publisher_names',),
433 ('publisher', 0),
434 ('publisher', 1),
435 ('publisher', 2),
436 ),
437 )
438 db.close()
439
440 # }}}
441
442 def test_legacy_adding_books(self): # {{{
443 "Test various adding/deleting books methods"
444 import sqlite3
445
446 con = sqlite3.connect(':memory:')
447 try:
448 con.execute('create virtual table recipe using fts5(name, ingredients)')
449 except Exception:
450 self.skipTest('python sqlite3 module does not have FTS5 support')
451 con.close()
452 del con
453 from calibre.ebooks.metadata.book.base import Metadata
454 from calibre.ptempfile import TemporaryFile
455
456 legacy, old = self.init_legacy(self.cloned_library), self.init_old(self.cloned_library)
457 mi = Metadata('Added Book0', authors=('Added Author',))
458 with TemporaryFile(suffix='.aff') as name:
459 with open(name, 'wb') as f:
460 f.write(b'xxx')
461 T = partial(ET, 'add_books', ([name], ['AFF'], [mi]), old=old, legacy=legacy)
462 T()(self)
463 book_id = T(kwargs={'return_ids': True})(self)[1][0]
464 self.assertEqual(legacy.new_api.formats(book_id), ('AFF',))
465 T(kwargs={'add_duplicates': False})(self)
466 mi.title = 'Added Book1'
467 mi.uuid = 'uuu'
468 T = partial(ET, 'import_book', (mi, [name]), old=old, legacy=legacy)
469 book_id = T()(self)
470 self.assertNotEqual(legacy.uuid(book_id, index_is_id=True), old.uuid(book_id, index_is_id=True))
471 book_id = T(kwargs={'preserve_uuid': True})(self)
472 self.assertEqual(legacy.uuid(book_id, index_is_id=True), old.uuid(book_id, index_is_id=True))
473 self.assertEqual(legacy.new_api.formats(book_id), ('AFF',))
474
475 T = partial(ET, 'add_format', old=old, legacy=legacy)
476 T((0, 'AFF', BytesIO(b'fffff')))(self)
477 T((0, 'AFF', BytesIO(b'fffff')))(self)
478 T((0, 'AFF', BytesIO(b'fffff')), {'replace': True})(self)
479 with TemporaryFile(suffix='.opf') as name:
480 with open(name, 'wb') as f:
481 f.write(b'zzzz')
482 T = partial(ET, 'import_book', (mi, [name]), old=old, legacy=legacy)
483 book_id = T()(self)
484 self.assertFalse(legacy.new_api.formats(book_id))
485
486 mi.title = 'Added Book2'
487 T = partial(ET, 'create_book_entry', (mi,), old=old, legacy=legacy)
488 T()
489 T({'add_duplicates': False})
490 T({'force_id': 1000})
491
492 with TemporaryFile(suffix='.txt') as name:
493 with open(name, 'wb') as f:
494 f.write(b'tttttt')
495 bid = legacy.add_catalog(name, 'My Catalog')
496 self.assertEqual(old.add_catalog(name, 'My Catalog'), bid)
497 cache = legacy.new_api
498 self.assertEqual(cache.formats(bid), ('TXT',))
499 self.assertEqual(cache.field_for('title', bid), 'My Catalog')
500 self.assertEqual(cache.field_for('authors', bid), ('calibre',))
501 self.assertEqual(cache.field_for('tags', bid), (_('Catalog'),))
502 self.assertTrue(bid < legacy.add_catalog(name, 'Something else'))
503 self.assertEqual(legacy.add_catalog(name, 'My Catalog'), bid)
504 self.assertEqual(old.add_catalog(name, 'My Catalog'), bid)
505
506 bid = legacy.add_news(name, {'title': 'Events', 'add_title_tag': True, 'custom_tags': ('one', 'two')})
507 self.assertEqual(cache.formats(bid), ('TXT',))
508 self.assertEqual(cache.field_for('authors', bid), ('calibre',))
509 self.assertEqual(cache.field_for('tags', bid), (_('News'), 'Events', 'one', 'two'))
510
511 self.assertTrue(legacy.cover(1, index_is_id=True))
512 origcov = legacy.cover(1, index_is_id=True)
513 self.assertTrue(legacy.has_cover(1))
514 legacy.remove_cover(1)
515 self.assertFalse(legacy.has_cover(1))
516 self.assertFalse(legacy.cover(1, index_is_id=True))
517 legacy.set_cover(3, origcov)
518 self.assertEqual(legacy.cover(3, index_is_id=True), origcov)
519 self.assertTrue(legacy.has_cover(3))
520
521 self.assertTrue(legacy.format(1, 'FMT1', index_is_id=True))
522 legacy.remove_format(1, 'FMT1', index_is_id=True)
523 self.assertIsNone(legacy.format(1, 'FMT1', index_is_id=True))
524
525 legacy.delete_book(1)
526 old.delete_book(1)
527 self.assertNotIn(1, legacy.all_ids())
528 legacy.dump_metadata((2, 3))
529 old.close()
530
531 # }}}
532
533 def test_legacy_coverage(self): # {{{
534 "Check that the emulation of the legacy interface is (almost) total"
535 cl = self.cloned_library
536 db = self.init_old(cl)
537 ndb = self.init_legacy()
538
539 SKIP_ATTRS = {
540 'TCat_Tag',
541 '_add_newbook_tag',
542 '_clean_identifier',
543 '_library_id_',
544 '_set_authors',
545 '_set_title',
546 '_set_custom',
547 '_update_author_in_cache',
548 # Feeds are now stored in the config folder
549 'get_feeds',
550 'get_feed',
551 'update_feed',
552 'remove_feeds',
553 'add_feed',
554 'set_feeds',
555 # Obsolete/broken methods
556 'author_id', # replaced by get_author_id
557 'books_for_author', # broken
558 'books_in_old_database',
559 'sizeof_old_database', # unused
560 'migrate_old', # no longer supported
561 'remove_unused_series', # superseded by clean API
562 'move_library_to', # API changed, no code uses old API
563 # Added compiled_rules() for calibredb add
564 'find_books_in_directory',
565 'import_book_directory',
566 'import_book_directory_multiple',
567 'recursive_import',
568 # Internal API
569 'clean_user_categories',
570 'cleanup_tags',
571 'books_list_filter',
572 'conn',
573 'connect',
574 'construct_file_name',
575 'construct_path_name',
576 'clear_dirtied',
577 'initialize_database',
578 'initialize_dynamic',
579 'run_import_plugins',
580 'vacuum',
581 'set_path',
582 'row_factory',
583 'rows',
584 'rmtree',
585 'series_index_pat',
586 'import_old_database',
587 'dirtied_lock',
588 'dirtied_cache',
589 'dirty_books_referencing',
590 'windows_check_if_files_in_use',
591 'get_metadata_for_dump',
592 'get_a_dirtied_book',
593 'dirtied_sequence',
594 'format_filename_cache',
595 'format_metadata_cache',
596 'filter',
597 'create_version1',
598 'normpath',
599 'custom_data_adapters',
600 'custom_table_names',
601 'custom_columns_in_meta',
602 'custom_tables',
603 }
604 SKIP_ARGSPEC = {
605 '__init__',
606 }
607
608 missing = []
609
610 try:
611 total = 0
612 for attr in dir(db):
613 if attr in SKIP_ATTRS or attr.startswith('upgrade_version'):
614 continue
615 total += 1
616 if not hasattr(ndb, attr):
617 missing.append(attr)
618 continue
619 obj, nobj = getattr(db, attr), getattr(ndb, attr)
620 if attr not in SKIP_ARGSPEC:
621 try:
622 argspec = inspect.getfullargspec(obj)
623 nargspec = inspect.getfullargspec(nobj)
624 except TypeError, ValueError:
625 pass
626 else:
627 compare_argspecs(argspec, nargspec, attr)
628 finally:
629 for db in (ndb, db):
630 db.close()
631 db.break_cycles()
632
633 if missing:
634 pc = len(missing) / total
635 raise AssertionError('{0:.1%} of API ({2} attrs) are missing: {1}'.format(pc, ', '.join(missing), len(missing)))
636
637 # }}}
638
639 def test_legacy_custom_data(self): # {{{
640 "Test the API for custom data storage"
641 legacy, old = self.init_legacy(self.cloned_library), self.init_old(self.cloned_library)
642 for name in ('name1', 'name2', 'name3'):
643 T = partial(ET, 'add_custom_book_data', old=old, legacy=legacy)
644 T((1, name, 'val1'))(self)
645 T((2, name, 'val2'))(self)
646 T((3, name, 'val3'))(self)
647 T = partial(ET, 'get_ids_for_custom_book_data', old=old, legacy=legacy)
648 T((name,))(self)
649 T = partial(ET, 'get_custom_book_data', old=old, legacy=legacy)
650 T((1, name, object()))
651 T((9, name, object()))
652 T = partial(ET, 'get_all_custom_book_data', old=old, legacy=legacy)
653 T((name, object()))
654 T((name + '!', object()))
655 T = partial(ET, 'delete_custom_book_data', old=old, legacy=legacy)
656 T((name, 1))
657 T = partial(ET, 'get_all_custom_book_data', old=old, legacy=legacy)
658 T((name, object()))
659 T = partial(ET, 'delete_all_custom_book_data', old=old, legacy=legacy)
660 T(name)
661 T = partial(ET, 'get_all_custom_book_data', old=old, legacy=legacy)
662 T((name, object()))
663
664 T = partial(ET, 'add_multiple_custom_book_data', old=old, legacy=legacy)
665 T(('n', {1: 'val1', 2: 'val2'}))(self)
666 T = partial(ET, 'get_all_custom_book_data', old=old, legacy=legacy)
667 T(('n', object()))
668 old.close()
669
670 # }}}
671
672 def test_legacy_setters(self): # {{{
673 "Test methods that are directly equivalent in the old and new interface"
674 from calibre.ebooks.metadata.book.base import Metadata
675 from calibre.utils.date import now
676
677 n = now()
678 ndb = self.init_legacy(self.cloned_library)
679 amap = ndb.new_api.get_id_map('authors')
680 sorts = [(aid, f's{aid}') for aid in amap]
681 db = self.init_old(self.cloned_library)
682 run_funcs(
683 self,
684 db,
685 ndb,
686 (
687 ('+format_metadata', 1, 'FMT1', itemgetter('size')),
688 ('+format_metadata', 1, 'FMT2', itemgetter('size')),
689 ('+format_metadata', 2, 'FMT1', itemgetter('size')),
690 ('get_tags', 0),
691 ('get_tags', 1),
692 ('get_tags', 2),
693 ('is_tag_used', 'News'),
694 ('is_tag_used', 'xchkjgfh'),
695 ('bulk_modify_tags', (1,), ['t1'], ['News']),
696 ('bulk_modify_tags', (2,), ['t1'], ['Tag One', 'Tag Two']),
697 ('bulk_modify_tags', (3,), ['t1', 't2', 't3']),
698 (db.clean,),
699 ('@all_tags',),
700 ('@tags', 0),
701 ('@tags', 1),
702 ('@tags', 2),
703 ('unapply_tags', 1, ['t1']),
704 ('unapply_tags', 2, ['xxxx']),
705 ('unapply_tags', 3, ['t2', 't3']),
706 (db.clean,),
707 ('@all_tags',),
708 ('@tags', 0),
709 ('@tags', 1),
710 ('@tags', 2),
711 ('update_last_modified', (1,), True, n),
712 ('update_last_modified', (3,), True, n),
713 ('metadata_last_modified', 1, True),
714 ('metadata_last_modified', 3, True),
715 ('set_sort_field_for_author', sorts[0][0], sorts[0][1]),
716 ('set_sort_field_for_author', sorts[1][0], sorts[1][1]),
717 ('set_sort_field_for_author', sorts[2][0], sorts[2][1]),
718 ('set_link_field_for_author', sorts[0][0], sorts[0][1]),
719 ('set_link_field_for_author', sorts[1][0], sorts[1][1]),
720 ('set_link_field_for_author', sorts[2][0], sorts[2][1]),
721 (db.refresh,),
722 ('author_sort', 0),
723 ('author_sort', 1),
724 ('author_sort', 2),
725 ),
726 )
727 omi = [db.get_metadata(x) for x in (0, 1, 2)]
728 nmi = [ndb.get_metadata(x) for x in (0, 1, 2)]
729 self.assertEqual([x.author_sort_map for x in omi], [x.author_sort_map for x in nmi])
730 db.close()
731
732 ndb = self.init_legacy(self.cloned_library)
733 db = self.init_old(self.cloned_library)
734
735 run_funcs(
736 self,
737 db,
738 ndb,
739 (
740 (
741 'set_authors',
742 1,
743 ('author one',),
744 ),
745 ('set_authors', 2, ('author two',), True, True, True),
746 ('set_author_sort', 3, 'new_aus'),
747 ('set_comment', 1, ''),
748 ('set_comment', 2, None),
749 ('set_comment', 3, '<p>a comment</p>'),
750 ('set_has_cover', 1, True),
751 ('set_has_cover', 2, True),
752 ('set_has_cover', 3, 1),
753 ('set_identifiers', 2, {'test': '', 'a': 'b'}),
754 ('set_identifiers', 3, {'id': '1', 'isbn': '9783161484100'}),
755 ('set_identifiers', 1, {}),
756 ('set_languages', 1, ('en',)),
757 ('set_languages', 2, ()),
758 ('set_languages', 3, ('deu', 'spa', 'fra')),
759 ('set_pubdate', 1, None),
760 ('set_pubdate', 2, '2011-1-7'),
761 ('set_series', 1, 'a series one'),
762 ('set_series', 2, 'another series [7]'),
763 ('set_series', 3, 'a third series'),
764 ('set_publisher', 1, 'publisher two'),
765 ('set_publisher', 2, None),
766 ('set_publisher', 3, 'a third puB'),
767 ('set_rating', 1, 2.3),
768 ('set_rating', 2, 0),
769 ('set_rating', 3, 8),
770 ('set_timestamp', 1, None),
771 ('set_timestamp', 2, '2011-1-7'),
772 ('set_uuid', 1, None),
773 ('set_uuid', 2, 'a test uuid'),
774 ('set_title', 1, 'title two'),
775 ('set_title', 2, None),
776 ('set_title', 3, 'The Test Title'),
777 ('set_tags', 1, ['a1', 'a2'], True),
778 ('set_tags', 2, ['b1', 'tag one'], False, False, False, True),
779 ('set_tags', 3, ['A1']),
780 (db.refresh,),
781 ('title', 0),
782 ('title', 1),
783 ('title', 2),
784 ('title_sort', 0),
785 ('title_sort', 1),
786 ('title_sort', 2),
787 ('authors', 0),
788 ('authors', 1),
789 ('authors', 2),
790 ('author_sort', 0),
791 ('author_sort', 1),
792 ('author_sort', 2),
793 ('has_cover', 3),
794 ('has_cover', 1),
795 ('has_cover', 2),
796 ('get_identifiers', 0),
797 ('get_identifiers', 1),
798 ('get_identifiers', 2),
799 ('pubdate', 0),
800 ('pubdate', 1),
801 ('pubdate', 2),
802 ('timestamp', 0),
803 ('timestamp', 1),
804 ('timestamp', 2),
805 ('publisher', 0),
806 ('publisher', 1),
807 ('publisher', 2),
808 ('rating', 0),
809 ('+rating', 1, lambda x: x or 0),
810 ('rating', 2),
811 ('series', 0),
812 ('series', 1),
813 ('series', 2),
814 ('series_index', 0),
815 ('series_index', 1),
816 ('series_index', 2),
817 ('uuid', 0),
818 ('uuid', 1),
819 ('uuid', 2),
820 ('isbn', 0),
821 ('isbn', 1),
822 ('isbn', 2),
823 ('@tags', 0),
824 ('@tags', 1),
825 ('@tags', 2),
826 ('@all_tags',),
827 ('@get_all_identifier_types',),
828 ('set_title_sort', 1, 'Title Two'),
829 ('set_title_sort', 2, None),
830 ('set_title_sort', 3, 'The Test Title_sort'),
831 ('set_series_index', 1, 2.3),
832 ('set_series_index', 2, 0),
833 ('set_series_index', 3, 8),
834 ('set_identifier', 1, 'moose', 'val'),
835 ('set_identifier', 2, 'test', ''),
836 ('set_identifier', 3, '', ''),
837 (db.refresh,),
838 ('series_index', 0),
839 ('series_index', 1),
840 ('series_index', 2),
841 ('title_sort', 0),
842 ('title_sort', 1),
843 ('title_sort', 2),
844 ('get_identifiers', 0),
845 ('get_identifiers', 1),
846 ('get_identifiers', 2),
847 ('@get_all_identifier_types',),
848 ('set_metadata', 1, Metadata('title', ('a1',)), False, False, False, True, True),
849 ('set_metadata', 3, Metadata('title', ('a1',))),
850 (db.refresh,),
851 ('title', 0),
852 ('title', 1),
853 ('title', 2),
854 ('title_sort', 0),
855 ('title_sort', 1),
856 ('title_sort', 2),
857 ('authors', 0),
858 ('authors', 1),
859 ('authors', 2),
860 ('author_sort', 0),
861 ('author_sort', 1),
862 ('author_sort', 2),
863 ('@tags', 0),
864 ('@tags', 1),
865 ('@tags', 2),
866 ('@all_tags',),
867 ('@get_all_identifier_types',),
868 ),
869 )
870 db.close()
871
872 ndb = self.init_legacy(self.cloned_library)
873 db = self.init_old(self.cloned_library)
874
875 run_funcs(
876 self,
877 db,
878 ndb,
879 (
880 ('set', 0, 'title', 'newtitle'),
881 ('set', 0, 'tags', 't1,t2,tag one', True),
882 ('set', 0, 'authors', 'author one & Author Two', True),
883 ('set', 0, 'rating', 3.2),
884 ('set', 0, 'publisher', 'publisher one', False),
885 (db.refresh,),
886 ('title', 0),
887 ('rating', 0),
888 ('#tags', 0),
889 ('#tags', 1),
890 ('#tags', 2),
891 ('authors', 0),
892 ('authors', 1),
893 ('authors', 2),
894 ('publisher', 0),
895 ('publisher', 1),
896 ('publisher', 2),
897 ('delete_tag', 'T1'),
898 ('delete_tag', 'T2'),
899 ('delete_tag', 'Tag one'),
900 ('delete_tag', 'News'),
901 (db.clean,),
902 (db.refresh,),
903 ('@all_tags',),
904 ('#tags', 0),
905 ('#tags', 1),
906 ('#tags', 2),
907 ),
908 )
909 db.close()
910
911 ndb = self.init_legacy(self.cloned_library)
912 db = self.init_old(self.cloned_library)
913 run_funcs(
914 self,
915 db,
916 ndb,
917 (
918 ('remove_all_tags', (1, 2, 3)),
919 (db.clean,),
920 ('@all_tags',),
921 ('@tags', 0),
922 ('@tags', 1),
923 ('@tags', 2),
924 ),
925 )
926 db.close()
927
928 ndb = self.init_legacy(self.cloned_library)
929 db = self.init_old(self.cloned_library)
930 a = {v: k for k, v in ndb.new_api.get_id_map('authors').items()}['Author One']
931 t = {v: k for k, v in ndb.new_api.get_id_map('tags').items()}['Tag One']
932 s = {v: k for k, v in ndb.new_api.get_id_map('series').items()}['A Series One']
933 p = {v: k for k, v in ndb.new_api.get_id_map('publisher').items()}['Publisher One']
934 run_funcs(
935 self,
936 db,
937 ndb,
938 (
939 ('rename_author', a, 'Author Two'),
940 ('rename_tag', t, 'News'),
941 ('rename_series', s, 'ss'),
942 ('rename_publisher', p, 'publisher one'),
943 (db.clean,),
944 (db.refresh,),
945 ('@all_tags',),
946 ('tags', 0),
947 ('tags', 1),
948 ('tags', 2),
949 ('series', 0),
950 ('series', 1),
951 ('series', 2),
952 ('publisher', 0),
953 ('publisher', 1),
954 ('publisher', 2),
955 ('series_index', 0),
956 ('series_index', 1),
957 ('series_index', 2),
958 ('authors', 0),
959 ('authors', 1),
960 ('authors', 2),
961 ('author_sort', 0),
962 ('author_sort', 1),
963 ('author_sort', 2),
964 ),
965 )
966 db.close()
967
968 # }}}
969
970 def test_legacy_custom(self): # {{{
971 "Test the legacy API for custom columns"
972 ndb = self.init_legacy(self.cloned_library)
973 db = self.init_old(self.cloned_library)
974 # Test getting
975 run_funcs(
976 self,
977 db,
978 ndb,
979 (
980 ('all_custom', 'series'),
981 ('all_custom', 'tags'),
982 ('all_custom', 'rating'),
983 ('all_custom', 'authors'),
984 ('all_custom', None, 7),
985 ('get_next_cc_series_num_for', 'My Series One', 'series'),
986 ('get_next_cc_series_num_for', 'My Series Two', 'series'),
987 ('is_item_used_in_multiple', 'My Tag One', 'tags'),
988 ('is_item_used_in_multiple', 'My Series One', 'series'),
989 ('$get_custom_items_with_ids', 'series'),
990 ('$get_custom_items_with_ids', 'tags'),
991 ('$get_custom_items_with_ids', 'float'),
992 ('$get_custom_items_with_ids', 'rating'),
993 ('$get_custom_items_with_ids', 'authors'),
994 ('$get_custom_items_with_ids', None, 7),
995 ),
996 )
997 for label in (
998 'tags',
999 'series',
1000 'authors',
1001 'comments',
1002 'rating',
1003 'date',
1004 'yesno',
1005 'isbn',
1006 'enum',
1007 'formats',
1008 'float',
1009 'comp_tags',
1010 ):
1011 for func in ('get_custom', 'get_custom_extra', 'get_custom_and_extra'):
1012 run_funcs(self, db, ndb, [(func, idx, label) for idx in range(3)])
1013
1014 # Test renaming/deleting
1015 t = {v: k for k, v in ndb.new_api.get_id_map('#tags').items()}['My Tag One']
1016 t2 = {v: k for k, v in ndb.new_api.get_id_map('#tags').items()}['My Tag Two']
1017 a = {v: k for k, v in ndb.new_api.get_id_map('#authors').items()}['My Author Two']
1018 a2 = {v: k for k, v in ndb.new_api.get_id_map('#authors').items()}['Custom One']
1019 s = {v: k for k, v in ndb.new_api.get_id_map('#series').items()}['My Series One']
1020 run_funcs(
1021 self,
1022 db,
1023 ndb,
1024 (
1025 ('delete_custom_item_using_id', t, 'tags'),
1026 ('delete_custom_item_using_id', a, 'authors'),
1027 ('rename_custom_item', t2, 't2', 'tags'),
1028 ('rename_custom_item', a2, 'custom one', 'authors'),
1029 ('rename_custom_item', s, 'My Series Two', 'series'),
1030 ('delete_item_from_multiple', 'custom two', 'authors'),
1031 (db.clean,),
1032 (db.refresh,),
1033 ('all_custom', 'series'),
1034 ('all_custom', 'tags'),
1035 ('all_custom', 'authors'),
1036 ),
1037 )
1038 for label in ('tags', 'authors', 'series'):
1039 run_funcs(self, db, ndb, [('get_custom_and_extra', idx, label) for idx in range(3)])
1040 db.close()
1041
1042 ndb = self.init_legacy(self.cloned_library)
1043 db = self.init_old(self.cloned_library)
1044 # Test setting
1045 run_funcs(
1046 self,
1047 db,
1048 ndb,
1049 (
1050 ('-set_custom', 1, 't1 & t2', 'authors'),
1051 ('-set_custom', 1, 't3 & t4', 'authors', None, True),
1052 ('-set_custom', 3, 'test one & test Two', 'authors'),
1053 ('-set_custom', 1, 'ijfkghkjdf', 'enum'),
1054 ('-set_custom', 3, 'One', 'enum'),
1055 ('-set_custom', 3, 'xxx', 'formats'),
1056 ('-set_custom', 1, 'my tag two', 'tags', None, False, False, None, True, True),
1057 (db.clean,),
1058 (db.refresh,),
1059 ('all_custom', 'series'),
1060 ('all_custom', 'tags'),
1061 ('all_custom', 'authors'),
1062 ),
1063 )
1064 for label in (
1065 'tags',
1066 'series',
1067 'authors',
1068 'comments',
1069 'rating',
1070 'date',
1071 'yesno',
1072 'isbn',
1073 'enum',
1074 'formats',
1075 'float',
1076 'comp_tags',
1077 ):
1078 for func in ('get_custom', 'get_custom_extra', 'get_custom_and_extra'):
1079 run_funcs(self, db, ndb, [(func, idx, label) for idx in range(3)])
1080 db.close()
1081
1082 ndb = self.init_legacy(self.cloned_library)
1083 db = self.init_old(self.cloned_library)
1084 # Test setting bulk
1085 run_funcs(
1086 self,
1087 db,
1088 ndb,
1089 (
1090 ('set_custom_bulk', (1, 2, 3), 't1 & t2', 'authors'),
1091 ('set_custom_bulk', (1, 2, 3), 'a series', 'series', None, False, False, (9, 10, 11)),
1092 ('set_custom_bulk', (1, 2, 3), 't1', 'tags', None, True),
1093 (db.clean,),
1094 (db.refresh,),
1095 ('all_custom', 'series'),
1096 ('all_custom', 'tags'),
1097 ('all_custom', 'authors'),
1098 ),
1099 )
1100 for label in (
1101 'tags',
1102 'series',
1103 'authors',
1104 'comments',
1105 'rating',
1106 'date',
1107 'yesno',
1108 'isbn',
1109 'enum',
1110 'formats',
1111 'float',
1112 'comp_tags',
1113 ):
1114 for func in ('get_custom', 'get_custom_extra', 'get_custom_and_extra'):
1115 run_funcs(self, db, ndb, [(func, idx, label) for idx in range(3)])
1116 db.close()
1117
1118 ndb = self.init_legacy(self.cloned_library)
1119 db = self.init_old(self.cloned_library)
1120 # Test bulk multiple
1121 run_funcs(
1122 self,
1123 db,
1124 ndb,
1125 (
1126 ('set_custom_bulk_multiple', (1, 2, 3), ['t1'], ['My Tag One'], 'tags'),
1127 (db.clean,),
1128 (db.refresh,),
1129 ('all_custom', 'tags'),
1130 ('get_custom', 0, 'tags'),
1131 ('get_custom', 1, 'tags'),
1132 ('get_custom', 2, 'tags'),
1133 ),
1134 )
1135 db.close()
1136
1137 o = self.cloned_library
1138 n = self.cloned_library
1139 ndb, db = self.init_legacy(n), self.init_old(o)
1140 ndb.create_custom_column('created', 'Created', 'text', True, True, {'moose': 'cat'})
1141 db.create_custom_column('created', 'Created', 'text', True, True, {'moose': 'cat'})
1142 db.close()
1143 ndb, db = self.init_legacy(n), self.init_old(o)
1144 self.assertEqual(db.custom_column_label_map['created'], ndb.backend.custom_field_metadata('created'))
1145 num = db.custom_column_label_map['created']['num']
1146 ndb.set_custom_column_metadata(num, is_editable=False, name='Crikey', display={})
1147 db.set_custom_column_metadata(num, is_editable=False, name='Crikey', display={})
1148 db.close()
1149 ndb, db = self.init_legacy(n), self.init_old(o)
1150 self.assertEqual(db.custom_column_label_map['created'], ndb.backend.custom_field_metadata('created'))
1151 db.close()
1152 ndb = self.init_legacy(n)
1153 ndb.delete_custom_column('created')
1154 ndb = self.init_legacy(n)
1155 self.assertRaises(KeyError, ndb.custom_field_name, num=num)
1156
1157 # Test setting custom series
1158 ndb = self.init_legacy(self.cloned_library)
1159 ndb.set_custom(1, 'TS [9]', label='series')
1160 self.assertEqual(ndb.new_api.field_for('#series', 1), 'TS')
1161 self.assertEqual(ndb.new_api.field_for('#series_index', 1), 9)
1162
1163 # }}}
1164
1165 def test_legacy_saved_search(self): # {{{
1166 "Test legacy saved search API"
1167 db, ndb = self.init_old(), self.init_legacy()
1168 run_funcs(
1169 self,
1170 db,
1171 ndb,
1172 (
1173 ('saved_search_set_all', {'one': 'a', 'two': 'b'}),
1174 ('saved_search_names',),
1175 ('saved_search_lookup', 'one'),
1176 ('saved_search_lookup', 'two'),
1177 ('saved_search_lookup', 'xxx'),
1178 ('saved_search_rename', 'one', '1'),
1179 ('saved_search_names',),
1180 ('saved_search_lookup', '1'),
1181 ('saved_search_delete', '1'),
1182 ('saved_search_names',),
1183 ('saved_search_add', 'n', 'm'),
1184 ('saved_search_names',),
1185 ('saved_search_lookup', 'n'),
1186 ),
1187 )
1188 db.close()
1189
1190 # }}}