From a5324474d85c4ac90ae5c73cf8d9521777d729c2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 14 Apr 2019 14:29:57 +0530 Subject: [PATCH] py3: port legacy conversion options test --- src/calibre/db/backend.py | 4 +++- src/calibre/db/tests/legacy.py | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index ba4056e31e..72a61a6fe4 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -13,7 +13,7 @@ from functools import partial import apsw from polyglot.builtins import (iteritems, itervalues, - unicode_type, reraise, string_or_bytes, cmp) + unicode_type, reraise, string_or_bytes, cmp, native_string_type) from calibre import isbytestring, force_unicode, prints, as_unicode from calibre.constants import (iswindows, filesystem_encoding, @@ -1745,6 +1745,8 @@ class DB(object): def set_conversion_options(self, options, fmt): def map_data(x): + if not isinstance(x, string_or_bytes): + x = native_string_type(x) x = x.encode('utf-8') if isinstance(x, unicode_type) else x x = pickle_binary_string(x) if not ispy3: diff --git a/src/calibre/db/tests/legacy.py b/src/calibre/db/tests/legacy.py index 2e31b09bac..41f1af4e91 100644 --- a/src/calibre/db/tests/legacy.py +++ b/src/calibre/db/tests/legacy.py @@ -298,9 +298,15 @@ class LegacyTest(BaseTest): def test_legacy_conversion_options(self): # {{{ 'Test conversion options API' ndb = self.init_legacy() - db = self.init_old() + db = self.init_old() all_ids = ndb.new_api.all_book_ids() - op1 = {'xx':'yy'} + op1 = {'xx': 'yy'} + + def decode(x): + if isinstance(x, bytes): + x = x.decode('utf-8') + return x + for x in ( ('has_conversion_options', all_ids), ('conversion_options', 1, 'PIPE'), @@ -311,8 +317,10 @@ class LegacyTest(BaseTest): ('has_conversion_options', all_ids), ): meth, args = x[0], x[1:] - self.assertEqual((getattr(db, meth)(*args)), (getattr(ndb, meth)(*args)), - 'The method: %s() returned different results for argument %s' % (meth, args)) + self.assertEqual( + decode(getattr(db, meth)(*args)), decode(getattr(ndb, meth)(*args)), + 'The method: %s() returned different results for argument %s' % (meth, args) + ) db.close() # }}}