Merge branch 'kovidgoyal/master'

This commit is contained in:
Charles Haley 2013-07-13 11:38:41 +02:00
commit 45664e5ddf
5 changed files with 50 additions and 11 deletions

View File

@ -114,4 +114,7 @@ Various things that require other things before they can be migrated:
3. From refresh in the legacy interface: Rember to flush the composite
column template cache.
4. Replace the metadatabackup thread with the new implementation when using the new backend.
5. In the new API refresh() does not re-read from disk. That might break a
few things, for example content server reloading on db change as well as
dump/restore of db?
'''

View File

@ -402,6 +402,13 @@ class LibraryDatabase(object):
def delete_conversion_options(self, book_id, fmt, commit=True):
self.new_api.delete_conversion_options((book_id,), fmt=fmt)
def set(self, index, field, val, allow_case_change=False):
book_id = self.data.index_to_id(index)
try:
return self.new_api.set_field(field, {book_id:val}, allow_case_change=allow_case_change)
finally:
self.notify('metadata', [book_id])
# Private interface {{{
def __iter__(self):
for row in self.data.iterall():

View File

@ -40,6 +40,19 @@ def compare_argspecs(old, new, attr):
if not ok:
raise AssertionError('The argspec for %s does not match. %r != %r' % (attr, old, new))
def run_funcs(self, db, ndb, funcs):
for func in funcs:
meth, args = func[0], func[1:]
if callable(meth):
meth(*args)
else:
fmt = lambda x:x
if meth[0] in {'!', '@', '#'}:
fmt = {'!':dict, '@':frozenset, '#':lambda x:set((x or '').split(','))}[meth[0]]
meth = meth[1:]
self.assertEqual(fmt(getattr(db, meth)(*args)), fmt(getattr(ndb, meth)(*args)),
'The method: %s() returned different results for argument %s' % (meth, args))
class LegacyTest(BaseTest):
''' Test the emulation of the legacy interface. '''
@ -137,8 +150,9 @@ class LegacyTest(BaseTest):
def test_legacy_direct(self): # {{{
'Test methods that are directly equivalent in the old and new interface'
from calibre.ebooks.metadata.book.base import Metadata
ndb = self.init_legacy()
ndb = self.init_legacy(self.cloned_library)
db = self.init_old()
for meth, args in {
'get_next_series_num_for': [('A Series One',)],
'author_sort_from_authors': [(['Author One', 'Author Two', 'Unknown'],)],
@ -221,19 +235,13 @@ class LegacyTest(BaseTest):
t = next(tmap.iterkeys())
pmap = cache.get_id_map('publisher')
p = next(pmap.iterkeys())
for x in (
run_funcs(self, db, ndb, (
('delete_tag_using_id', t),
('delete_publisher_using_id', p),
(db.refresh,),
('all_tag_names',), ('tags', 0), ('tags', 1), ('tags', 2),
('all_publisher_names',), ('publisher', 0), ('publisher', 1), ('publisher', 2),
):
meth, args = x[0], x[1:]
if callable(meth):
meth(*args)
else:
self.assertEqual((getattr(db, meth)(*args)), (getattr(ndb, meth)(*args)),
'The method: %s() returned different results for argument %s' % (meth, args))
))
db.close()
# }}}
@ -387,3 +395,24 @@ class LegacyTest(BaseTest):
old.close()
# }}}
def test_legacy_setters(self): # {{{
'Test methods that are directly equivalent in the old and new interface'
ndb = self.init_legacy(self.cloned_library)
db = self.init_old(self.cloned_library)
run_funcs(self, db, ndb, (
('set', 0, 'title', 'newtitle'),
('set', 0, 'tags', 't1,t2,tag one', True),
('set', 0, 'authors', 'author one & Author Two', True),
('set', 0, 'rating', 3.2),
('set', 0, 'publisher', 'publisher one', True),
(db.refresh,),
('title', 0),
('rating', 0),
('#tags', 0), ('#tags', 1), ('#tags', 2),
('authors', 0), ('authors', 1), ('authors', 2),
('publisher', 0), ('publisher', 1), ('publisher', 2),
))
db.close()
# }}}

View File

@ -142,7 +142,7 @@ def get_adapter(name, metadata):
elif dt == 'comments':
ans = single_text
elif dt == 'rating':
ans = lambda x: None if x in {None, 0} else min(10., max(0., adapt_number(float, x)))
ans = lambda x: None if x in {None, 0} else min(10, max(0, adapt_number(int, x)))
elif dt == 'enumeration':
ans = single_text
elif dt == 'composite':

View File

@ -2212,7 +2212,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
id = self.data[row][0]
col = self.FIELD_MAP[column]
books_to_refresh = set()
books_to_refresh = {id}
set_args = (row, col, val)
if column == 'authors':
val = string_to_authors(val)