mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
...
This commit is contained in:
parent
b3a0671a46
commit
442d0ab267
@ -164,7 +164,7 @@ class ManyToOneField(Field):
|
|||||||
def for_book(self, book_id, default_value=None):
|
def for_book(self, book_id, default_value=None):
|
||||||
ids = self.table.book_col_map.get(book_id, None)
|
ids = self.table.book_col_map.get(book_id, None)
|
||||||
if ids is not None:
|
if ids is not None:
|
||||||
ans = self.id_map[ids]
|
ans = self.table.id_map[ids]
|
||||||
else:
|
else:
|
||||||
ans = default_value
|
ans = default_value
|
||||||
return ans
|
return ans
|
||||||
@ -182,7 +182,7 @@ class ManyToOneField(Field):
|
|||||||
return self.table.id_map.iterkeys()
|
return self.table.id_map.iterkeys()
|
||||||
|
|
||||||
def sort_keys_for_books(self, get_metadata, all_book_ids):
|
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}
|
all_book_ids}
|
||||||
return {id_ : keys.get(
|
return {id_ : keys.get(
|
||||||
self.book_col_map.get(id_, None), '') for id_ in all_book_ids}
|
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):
|
def for_book(self, book_id, default_value=None):
|
||||||
ids = self.table.book_col_map.get(book_id, ())
|
ids = self.table.book_col_map.get(book_id, ())
|
||||||
if ids:
|
if ids:
|
||||||
ans = tuple(self.id_map[i] for i in ids)
|
ans = tuple(self.table.id_map[i] for i in ids)
|
||||||
else:
|
else:
|
||||||
ans = default_value
|
ans = default_value
|
||||||
return ans
|
return ans
|
||||||
@ -211,7 +211,7 @@ class ManyToManyField(Field):
|
|||||||
return self.table.id_map.iterkeys()
|
return self.table.id_map.iterkeys()
|
||||||
|
|
||||||
def sort_keys_for_books(self, get_metadata, all_book_ids):
|
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}
|
all_book_ids}
|
||||||
|
|
||||||
def sort_key_for_book(book_id):
|
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}
|
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):
|
class AuthorsField(ManyToManyField):
|
||||||
|
|
||||||
@ -249,6 +256,8 @@ def create_field(name, table):
|
|||||||
cls = OnDeviceField
|
cls = OnDeviceField
|
||||||
elif name == 'formats':
|
elif name == 'formats':
|
||||||
cls = FormatsField
|
cls = FormatsField
|
||||||
|
elif name == 'identifiers':
|
||||||
|
cls = IdentifiersField
|
||||||
elif table.metadata['datatype'] == 'composite':
|
elif table.metadata['datatype'] == 'composite':
|
||||||
cls = CompositeField
|
cls = CompositeField
|
||||||
return cls(name, table)
|
return cls(name, table)
|
||||||
|
@ -66,8 +66,6 @@ class VirtualTable(Table):
|
|||||||
self.table_type = table_type
|
self.table_type = table_type
|
||||||
Table.__init__(self, name, metadata)
|
Table.__init__(self, name, metadata)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class OneToOneTable(Table):
|
class OneToOneTable(Table):
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
Binary file not shown.
@ -8,7 +8,9 @@ __copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
|
|||||||
__docformat__ = 'restructuredtext en'
|
__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):
|
def create_db(library_path):
|
||||||
from calibre.library.database2 import LibraryDatabase2
|
from calibre.library.database2 import LibraryDatabase2
|
||||||
@ -19,6 +21,14 @@ def create_db(library_path):
|
|||||||
shutil.copyfile(src, db)
|
shutil.copyfile(src, db)
|
||||||
return 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):
|
class ReadingTest(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -29,5 +39,78 @@ class ReadingTest(unittest.TestCase):
|
|||||||
shutil.rmtree(self.library_path)
|
shutil.rmtree(self.library_path)
|
||||||
|
|
||||||
def test_read(self):
|
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': '<p>Comments One</p>',
|
||||||
|
'#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': '<div>My Comments One<p></p></div>',
|
||||||
|
},
|
||||||
|
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': '<p>Comments Two</p>',
|
||||||
|
'#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': '<div>My Comments Two<p></p></div>',
|
||||||
|
|
||||||
|
},
|
||||||
|
}
|
||||||
|
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()
|
||||||
|
|
||||||
|
@ -180,6 +180,7 @@ def main(args=sys.argv):
|
|||||||
sys.path.insert(0, base)
|
sys.path.insert(0, base)
|
||||||
g = globals()
|
g = globals()
|
||||||
g['__name__'] = '__main__'
|
g['__name__'] = '__main__'
|
||||||
|
g['__file__'] = ef
|
||||||
execfile(ef, g)
|
execfile(ef, g)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user