Make the db and server tests locale independent

This commit is contained in:
Kovid Goyal 2019-05-01 16:31:02 +05:30
parent d1f94b510c
commit 2ed54d1782
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
6 changed files with 35 additions and 29 deletions

View File

@ -218,14 +218,14 @@ class AddRemoveTest(BaseTest):
authors = cache.fields['authors'].table
# Delete a single book, with no formats and check cleaning
self.assertIn(_('Unknown'), set(itervalues(authors.id_map)))
self.assertIn('Unknown', set(itervalues(authors.id_map)))
olen = len(authors.id_map)
item_id = {v:k for k, v in iteritems(authors.id_map)}[_('Unknown')]
item_id = {v:k for k, v in iteritems(authors.id_map)}['Unknown']
cache.remove_books((3,))
for c in (cache, self.init_cache()):
table = c.fields['authors'].table
self.assertNotIn(3, c.all_book_ids())
self.assertNotIn(_('Unknown'), set(itervalues(table.id_map)))
self.assertNotIn('Unknown', set(itervalues(table.id_map)))
self.assertNotIn(item_id, table.asort_map)
self.assertNotIn(item_id, table.alink_map)
ae(len(table.id_map), olen-1)

View File

@ -130,18 +130,18 @@ class LegacyTest(BaseTest):
ans[label] = tuple(set(x.split(',')) if x else x for x in ans[label])
if label == 'series_sort':
# The old db code did not take book language into account
# when generating series_sort values (the first book has
# lang=deu)
ans[label] = ans[label][1:]
# when generating series_sort values
ans[label] = None
return ans
db = self.init_legacy()
new_vals = get_values(db)
db.close()
old = self.init_old()
old_vals = get_values(old)
old.close()
old = None
db = self.init_legacy()
new_vals = get_values(db)
db.close()
self.assertEqual(old_vals, new_vals)
# }}}

View File

@ -12,10 +12,15 @@ from io import BytesIO
from time import time
from calibre.utils.date import utc_tz
from calibre.utils.localization import calibre_langcode_to_name
from calibre.db.tests.base import BaseTest
from polyglot.builtins import iteritems, itervalues, range
def p(x):
return datetime.datetime.strptime(x, '%Y-%m-%d').replace(tzinfo=utc_tz)
class ReadingTest(BaseTest):
def test_read(self): # {{{
@ -130,6 +135,9 @@ class ReadingTest(BaseTest):
'Test sorting'
cache = self.init_cache()
ae = self.assertEqual
lmap = {x:cache.field_for('languages', x) for x in (1, 2, 3)}
lq = sorted(lmap, key=lambda x: calibre_langcode_to_name((lmap[x] or ('',))[0]))
for field, order in iteritems({
'title' : [2, 1, 3],
'authors': [2, 1, 3],
@ -143,7 +151,7 @@ class ReadingTest(BaseTest):
'timestamp': [2, 1, 3],
'pubdate' : [1, 2, 3],
'publisher': [3, 2, 1],
'languages': [3, 2, 1],
'languages': lq,
'comments': [3, 2, 1],
'#enum' : [3, 2, 1],
'#authors' : [3, 2, 1],
@ -182,7 +190,6 @@ class ReadingTest(BaseTest):
ae([2, 3, 1], cache.multisort([(field, False)], ids_to_sort=(1, 2, 3)))
# Test tweak to sort dates by visible format
from calibre.utils.date import parse_only_date as p
from calibre.utils.config_base import Tweak
ae(cache.set_field('pubdate', {1:p('2001-3-3'), 2:p('2002-2-3'), 3:p('2003-1-3')}), {1, 2, 3})
ae([1, 2, 3], cache.multisort([('pubdate', True)]))
@ -285,7 +292,7 @@ class ReadingTest(BaseTest):
oldvals = {query:set(old.search_getting_ids(query, '')) for query in (
# Date tests
'date:9/6/2011', 'date:true', 'date:false', 'pubdate:1/9/2011',
'#date:true', 'date:<100daysago', 'date:>9/6/2011',
'#date:true', 'date:<100_daysago', 'date:>9/6/2011',
'#date:>9/1/2011', '#date:=2011',
# Number tests
@ -296,8 +303,8 @@ class ReadingTest(BaseTest):
'series_index:<3',
# Bool tests
'#yesno:true', '#yesno:false', '#yesno:yes', '#yesno:no',
'#yesno:empty',
'#yesno:true', '#yesno:false', '#yesno:_yes', '#yesno:_no',
'#yesno:_empty',
# Keypair tests
'identifiers:true', 'identifiers:false', 'identifiers:test',
@ -632,14 +639,13 @@ class ReadingTest(BaseTest):
def test_composites(self): # {{{
' Test sorting and searching in composite columns '
from calibre.utils.date import parse_only_date as p
cache = self.init_cache()
cache.create_custom_column('mult', 'CC1', 'composite', True, display={'composite_template': 'b,a,c'})
cache.create_custom_column('single', 'CC2', 'composite', False, display={'composite_template': 'b,a,c'})
cache.create_custom_column('number', 'CC3', 'composite', False, display={'composite_template': '{#float}', 'composite_sort':'number'})
cache.create_custom_column('size', 'CC4', 'composite', False, display={'composite_template': '{#float:human_readable()}', 'composite_sort':'number'})
cache.create_custom_column('ccdate', 'CC5', 'composite', False,
display={'composite_template': '{pubdate:format_date(d-M-yy)}', 'composite_sort':'date'})
display={'composite_template': "{:'format_date(raw_field('pubdate'), 'dd-MM-yy')'}", 'composite_sort':'date'})
cache.create_custom_column('bool', 'CC6', 'composite', False, display={'composite_template': '{#yesno}', 'composite_sort':'bool'})
cache.create_custom_column('ccm', 'CC7', 'composite', True, display={'composite_template': '{#tags}'})
cache.create_custom_column('ccp', 'CC8', 'composite', True, display={'composite_template': '{publisher}'})
@ -659,7 +665,7 @@ class ReadingTest(BaseTest):
self.assertEqual([1, 2, 3], cache.multisort([('#size', True)]))
# Test date sorting
cache.set_field('pubdate', {1:p('2001-2-6'), 2:p('2001-10-6'), 3:p('2001-6-6')})
cache.set_field('pubdate', {1:p('2001-02-06'), 2:p('2001-10-06'), 3:p('2001-06-06')})
self.assertEqual([1, 3, 2], cache.multisort([('#ccdate', True)]))
# Test bool sorting
@ -692,7 +698,7 @@ class ReadingTest(BaseTest):
lm2.languages = ['eng']
for mi, books in (
(Metadata('title one', ['author one']), {2}),
(Metadata(_('Unknown')), {3}),
(Metadata('Unknown', ['Unknown']), {3}),
(Metadata('title two', ['author one']), {1}),
(lm, {1}),
(lm2, set()),

View File

@ -11,7 +11,7 @@ from collections import namedtuple
from functools import partial
from io import BytesIO
from calibre.ebooks.metadata import author_to_author_sort
from calibre.ebooks.metadata import author_to_author_sort, title_sort
from calibre.utils.date import UNDEFINED_DATE
from calibre.db.tests.base import BaseTest, IMG
from polyglot.builtins import iteritems, itervalues
@ -237,7 +237,7 @@ class WritingTest(BaseTest):
for name in ('authors', '#authors'):
f = cache.fields[name]
ae(len(f.table.id_map), 3)
af(cache.set_field(name, {3:None if name == 'authors' else 'Unknown'}))
af(cache.set_field(name, {3:'Unknown'}))
ae(cache.set_field(name, {3:'Kovid Goyal & Divok Layog'}), {3})
ae(cache.set_field(name, {1:'', 2:'An, Author'}), {1,2})
cache2 = self.init_cache(cl)
@ -245,9 +245,9 @@ class WritingTest(BaseTest):
ae(len(c.fields[name].table.id_map), 4 if name =='authors' else 3)
ae(c.field_for(name, 3), ('Kovid Goyal', 'Divok Layog'))
ae(c.field_for(name, 2), ('An, Author',))
ae(c.field_for(name, 1), ('Unknown',) if name=='authors' else ())
ae(c.field_for(name, 1), (_('Unknown'),) if name=='authors' else ())
if name == 'authors':
ae(c.field_for('author_sort', 1), author_to_author_sort('Unknown'))
ae(c.field_for('author_sort', 1), author_to_author_sort(_('Unknown')))
ae(c.field_for('author_sort', 2), author_to_author_sort('An, Author'))
ae(c.field_for('author_sort', 3), author_to_author_sort('Kovid Goyal') + ' & ' + author_to_author_sort('Divok Layog'))
del cache2
@ -292,8 +292,8 @@ class WritingTest(BaseTest):
ae(sf('title', {1:'The Moose', 2:'Cat'}), {1, 2})
cache2 = self.init_cache(cl)
for c in (cache, cache2):
ae(c.field_for('sort', 1), 'Moose, The')
ae(c.field_for('sort', 2), 'Cat')
ae(c.field_for('sort', 1), title_sort('The Moose'))
ae(c.field_for('sort', 2), title_sort('Cat'))
# Test setting with the same value repeated
ae(sf('tags', {3: ('a', 'b', 'a')}), {3})

View File

@ -346,7 +346,7 @@ class ResultCache(SearchQueryParser): # {{{
untrans_daysago_len = len('_daysago')
def get_dates_matches(self, location, query, candidates):
matches = set([])
matches = set()
if len(query) < 2:
return matches
@ -395,7 +395,7 @@ class ResultCache(SearchQueryParser): # {{{
qd = now()
field_count = 2
elif query.endswith(self.local_daysago) or query.endswith(self.untrans_daysago):
num = query[0:-(self.local_daysago_len if query.endswith(self.local_daysago) else self.untrans_daysago_len)]
num = query[0:-(self.untrans_daysago_len if query.endswith(self.untrans_daysago) else self.local_daysago_len)]
try:
qd = now() - timedelta(int(num))
except:

View File

@ -69,9 +69,9 @@ class ContentTest(LibraryBaseTest):
self.ae(r.status, OK)
self.ae(data, xdata)
names = {x['name']:x['url'] for x in data}
for q in ('Newest', 'All books', 'Tags', 'Series', 'Authors', 'Enum', 'Composite Tags'):
for q in (_('Newest'), _('All books'), _('Tags'), _('Authors'), _('Enum'), _('Composite Tags')):
self.assertIn(q, names)
r, data = request(names['Tags'], prefix='')
r, data = request(names[_('Tags')], prefix='')
self.ae(r.status, OK)
names = {x['name']:x['url'] for x in data['items']}
self.ae(set(names), set('Tag One,Tag Two,News'.split(',')))
@ -128,7 +128,7 @@ class ContentTest(LibraryBaseTest):
ae(set(r(url_for('/ajax/search?query=id:2'))['book_ids']), {2})
ae(set(r(url_for('/ajax/search?vl=1'))['book_ids']), {1})
data = make_request(conn, '/ajax/search', username='inv', password='test', prefix='', method='GET')[1]
ae(data['bad_restriction'], 'Invalid syntax. Expected a lookup name or a word')
ae(data['bad_restriction'], _('Invalid syntax. Expected a lookup name or a word'))
# books.py
nf(url_for('/book-manifest', book_id=3, fmt='TXT'))