From b28a069e187cd33fb854bde8b090c2e96768709b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 23 Apr 2013 15:45:13 +0530 Subject: [PATCH] Add tests for legacy getters --- src/calibre/db/legacy.py | 19 +++++++++++++++++++ src/calibre/db/tests/legacy.py | 18 +++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/calibre/db/legacy.py b/src/calibre/db/legacy.py index 3c06f21fed..0c07814ab1 100644 --- a/src/calibre/db/legacy.py +++ b/src/calibre/db/legacy.py @@ -74,6 +74,14 @@ class LibraryDatabase(object): def library_id(self): return self.backend.library_id + @property + def library_path(self): + return self.backend.library_path + + @property + def dbpath(self): + return self.backend.dbpath + def last_modified(self): return self.backend.last_modified() @@ -111,4 +119,15 @@ class LibraryDatabase(object): # }}} + def path(self, index, index_is_id=False): + 'Return the relative path to the directory containing this books files as a unicode string.' + book_id = index if index_is_id else self.data.index_to_id(index) + return self.data.cache.field_for('path', book_id).replace('/', os.sep) + + def abspath(self, index, index_is_id=False, create_dirs=True): + 'Return the absolute path to the directory containing this books files as a unicode string.' + path = os.path.join(self.library_path, self.path(index, index_is_id=index_is_id)) + if create_dirs and not os.path.exists(path): + os.makedirs(path) + return path diff --git a/src/calibre/db/tests/legacy.py b/src/calibre/db/tests/legacy.py index 2eba15c375..0f378c566c 100644 --- a/src/calibre/db/tests/legacy.py +++ b/src/calibre/db/tests/legacy.py @@ -16,7 +16,7 @@ class LegacyTest(BaseTest): 'Test library wide properties' def get_props(db): props = ('user_version', 'is_second_db', 'library_id', 'field_metadata', - 'custom_column_label_map', 'custom_column_num_map') + 'custom_column_label_map', 'custom_column_num_map', 'library_path', 'dbpath') fprops = ('last_modified', ) ans = {x:getattr(db, x) for x in props} ans.update({x:getattr(db, x)() for x in fprops}) @@ -76,3 +76,19 @@ class LegacyTest(BaseTest): self.assertEqual(db.title(1, index_is_id=True), 'xxx') # }}} + def test_legacy_getters(self): # {{{ + old = self.init_old() + getters = ('path', 'abspath', 'title', 'authors', 'series', + 'publisher', 'author_sort', 'authors', 'comments', + 'comment', 'publisher', 'rating', 'series_index', 'tags', + 'timestamp', 'uuid', 'pubdate', 'ondevice', + 'metadata_last_modified', 'languages') + oldvals = {g:tuple(getattr(old, g)(x) for x in xrange(3)) for g in getters} + old.close() + db = self.init_legacy() + newvals = {g:tuple(getattr(db, g)(x) for x in xrange(3)) for g in getters} + for x in (oldvals, newvals): + x['tags'] = tuple(set(y.split(',')) if y else y for y in x['tags']) + self.assertEqual(oldvals, newvals) + # }}} +