diff --git a/src/calibre/db/fields.py b/src/calibre/db/fields.py index 0a497555c4..5c0dffd383 100644 --- a/src/calibre/db/fields.py +++ b/src/calibre/db/fields.py @@ -164,7 +164,7 @@ class ManyToOneField(Field): def for_book(self, book_id, default_value=None): ids = self.table.book_col_map.get(book_id, None) if ids is not None: - ans = self.id_map[ids] + ans = self.table.id_map[ids] else: ans = default_value return ans @@ -182,7 +182,7 @@ class ManyToOneField(Field): return self.table.id_map.iterkeys() def sort_keys_for_books(self, get_metadata, all_book_ids): - keys = {id_ : self._sort_key(self.id_map.get(id_, '')) for id_ in + keys = {id_ : self._sort_key(self.table.id_map.get(id_, '')) for id_ in all_book_ids} return {id_ : keys.get( self.book_col_map.get(id_, None), '') for id_ in all_book_ids} @@ -196,7 +196,7 @@ class ManyToManyField(Field): def for_book(self, book_id, default_value=None): ids = self.table.book_col_map.get(book_id, ()) if ids: - ans = tuple(self.id_map[i] for i in ids) + ans = tuple(self.table.id_map[i] for i in ids) else: ans = default_value return ans @@ -211,7 +211,7 @@ class ManyToManyField(Field): return self.table.id_map.iterkeys() def sort_keys_for_books(self, get_metadata, all_book_ids): - keys = {id_ : self._sort_key(self.id_map.get(id_, '')) for id_ in + keys = {id_ : self._sort_key(self.table.id_map.get(id_, '')) for id_ in all_book_ids} def sort_key_for_book(book_id): @@ -222,6 +222,13 @@ class ManyToManyField(Field): return {id_ : sort_key_for_book(id_) for id_ in all_book_ids} +class IdentifiersField(ManyToManyField): + + def for_book(self, book_id, default_value=None): + ids = self.table.book_col_map.get(book_id, ()) + if not ids: + ids = default_value + return ids class AuthorsField(ManyToManyField): @@ -249,6 +256,8 @@ def create_field(name, table): cls = OnDeviceField elif name == 'formats': cls = FormatsField + elif name == 'identifiers': + cls = IdentifiersField elif table.metadata['datatype'] == 'composite': cls = CompositeField return cls(name, table) diff --git a/src/calibre/db/tables.py b/src/calibre/db/tables.py index 185d15d86b..6a6730c2b4 100644 --- a/src/calibre/db/tables.py +++ b/src/calibre/db/tables.py @@ -66,8 +66,6 @@ class VirtualTable(Table): self.table_type = table_type Table.__init__(self, name, metadata) - - class OneToOneTable(Table): ''' diff --git a/src/calibre/db/tests/metadata.db b/src/calibre/db/tests/metadata.db index 8fb89cbb8e..812bf296ba 100644 Binary files a/src/calibre/db/tests/metadata.db and b/src/calibre/db/tests/metadata.db differ diff --git a/src/calibre/db/tests/reading.py b/src/calibre/db/tests/reading.py index 6149839a88..91ec2a92f6 100644 --- a/src/calibre/db/tests/reading.py +++ b/src/calibre/db/tests/reading.py @@ -8,7 +8,9 @@ __copyright__ = '2011, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import os, shutil, unittest, tempfile +import os, shutil, unittest, tempfile, datetime + +from calibre.utils.date import local_tz def create_db(library_path): from calibre.library.database2 import LibraryDatabase2 @@ -19,6 +21,14 @@ def create_db(library_path): shutil.copyfile(src, db) return db +def init_cache(library_path): + from calibre.db.backend import DB + from calibre.db.cache import Cache + backend = DB(library_path) + cache = Cache(backend) + cache.init() + return cache + class ReadingTest(unittest.TestCase): def setUp(self): @@ -29,5 +39,78 @@ class ReadingTest(unittest.TestCase): shutil.rmtree(self.library_path) def test_read(self): - pass + cache = init_cache(self.library_path) + tests = { + 2 : { + 'title': 'Title One', + 'sort': 'One', + 'authors': ('Author One',), + 'author_sort': 'One, Author', + 'series' : 'Series One', + 'series_index': 1.0, + 'tags':('Tag One',), + 'rating': 4.0, + 'identifiers': {'test':'one'}, + 'timestamp': datetime.datetime(2011, 9, 5, 15, 6, + tzinfo=local_tz), + 'pubdate': datetime.datetime(2011, 9, 5, 15, 6, + tzinfo=local_tz), + 'publisher': 'Publisher One', + 'languages': ('eng',), + 'comments': '

Comments One

', + '#enum':'One', + '#authors':('Custom One', 'Custom Two'), + '#date':datetime.datetime(2011, 9, 5, 0, 0, + tzinfo=local_tz), + '#rating':2.0, + '#series':'My Series One', + '#series_index': 1.0, + '#tags':('My Tag One', 'My Tag Two'), + '#yesno':True, + '#comments': '
My Comments One

', + }, + 1 : { + 'title': 'Title Two', + 'sort': 'Title Two', + 'authors': ('Author Two', 'Author One'), + 'author_sort': 'Two, Author & One, Author', + 'series' : 'Series Two', + 'series_index': 2.0, + 'rating': 6.0, + 'tags': ('Tag Two',), + 'identifiers': {'test':'two'}, + 'timestamp': datetime.datetime(2011, 9, 6, 0, 0, + tzinfo=local_tz), + 'pubdate': datetime.datetime(2011, 8, 5, 0, 0, + tzinfo=local_tz), + 'publisher': 'Publisher Two', + 'languages': ('deu',), + 'comments': '

Comments Two

', + '#enum':'Two', + '#authors':('My Author Two',), + '#date':datetime.datetime(2011, 9, 1, 0, 0, + tzinfo=local_tz), + '#rating':4.0, + '#series':'My Series Two', + '#series_index': 3.0, + '#tags':('My Tag Two',), + '#yesno':False, + '#comments': '
My Comments Two

', + + }, + } + for book_id, test in tests.iteritems(): + for field, expected_val in test.iteritems(): + self.assertEqual(expected_val, + cache.field_for(field, book_id)) + break + +def tests(): + return unittest.TestLoader().loadTestsFromTestCase(ReadingTest) + +def run(): + unittest.TextTestRunner(verbosity=2).run(tests()) + +if __name__ == '__main__': + run() diff --git a/src/calibre/debug.py b/src/calibre/debug.py index 79110d9585..20f8617396 100644 --- a/src/calibre/debug.py +++ b/src/calibre/debug.py @@ -180,6 +180,7 @@ def main(args=sys.argv): sys.path.insert(0, base) g = globals() g['__name__'] = '__main__' + g['__file__'] = ef execfile(ef, g) return