Add the tag mapper tests to the main test runner

This commit is contained in:
Kovid Goyal 2016-07-04 09:37:15 +05:30
parent 7ff81c84b3
commit 08d56e2eee
2 changed files with 44 additions and 36 deletions

View File

@ -8,7 +8,7 @@ import unittest
from setup import Command
TEST_MODULES = frozenset('srv db polish opf css docx cfi matcher icu smartypants build'.split())
TEST_MODULES = frozenset('srv db polish opf css docx cfi matcher icu smartypants build misc'.split())
def find_tests(which_tests=None):
ans = []
@ -59,6 +59,9 @@ def find_tests(which_tests=None):
if ok('smartypants'):
from calibre.utils.smartypants import run_tests
a(run_tests(return_tests=True))
if ok('misc'):
from calibre.ebooks.metadata.tag_mapper import find_tests
a(find_tests())
tests = unittest.TestSuite(ans)
return tests
@ -68,7 +71,7 @@ class Test(Command):
description = 'Run the calibre test suite'
def add_options(self, parser):
parser.add_option('--test-module', default=[], action='append', type='choice', choices=sorted(map(str, TEST_MODULES)),
parser.add_option('--test-module', '--test-group', default=[], action='append', type='choice', choices=sorted(map(str, TEST_MODULES)),
help='The test module to run (can be specified more than once for multiple modules). Choices: %s' % ', '.join(sorted(TEST_MODULES)))
parser.add_option('--test-verbosity', type=int, default=4, help='Test verbosity (0-4)')
parser.add_option('--test-name', default=[], action='append',

View File

@ -122,42 +122,47 @@ def map_tags(tags, rules=()):
ans.extend(apply_rules(t, rules))
return uniq(filter(None, ans))
def test():
def find_tests():
import unittest
class TestTagMapper(unittest.TestCase):
def rule(action, query, replace=None, match_type='one_of'):
ans = {'action':action, 'query': query, 'match_type':match_type}
if replace is not None:
ans['replace'] = replace
return ans
def test_tag_mapper(self):
def run(rules, tags, expected):
if isinstance(rules, dict):
rules = [rules]
if isinstance(tags, type('')):
tags = [x.strip() for x in tags.split(',')]
if isinstance(expected, type('')):
expected = [x.strip() for x in expected.split(',')]
ans = map_tags(tags, rules)
if ans != expected:
raise AssertionError('Expected: %r != %r' % (expected, ans))
def rule(action, query, replace=None, match_type='one_of'):
ans = {'action':action, 'query': query, 'match_type':match_type}
if replace is not None:
ans['replace'] = replace
return ans
run(rule('capitalize', 't1,t2'), 't1,x1', 'T1,x1')
run(rule('upper', 'ta,t2'), 'ta,x1', 'TA,x1')
run(rule('lower', 'ta,x1'), 'TA,X1', 'ta,x1')
run(rule('replace', 't1', 't2'), 't1,x1', 't2,x1')
run(rule('replace', '(.)1', r'\g<1>2', 'matches'), 't1,x1', 't2,x2')
run(rule('replace', '(.)1', r'\g<1>2,3', 'matches'), 't1,x1', 't2,3,x2')
run(rule('replace', 't1', 't2, t3'), 't1,x1', 't2,t3,x1')
run([rule('replace', 't1', 't2,t3'), rule('remove', 't2')], 't1,x1', 't3,x1')
run(rule('replace', 't1', 't1'), 't1,x1', 't1,x1')
run([rule('replace', 't1', 't2'), rule('replace', 't2', 't1')], 't1,t2', 't1,t2')
run(rule('replace', 'a', 'A'), 'a,b', 'A,b')
run(rule('replace', 'a,b', 'A,B'), 'a,b', 'A,B')
run(rule('replace', 'L', 'T', 'has'), 'L', 'T')
run(rule('split', '/', '/', 'has'), 'a/b/c,d', 'a,b,c,d')
run(rule('split', '/', '/', 'has'), '/,d', 'd')
run(rule('split', '/', '/', 'has'), '/a/', 'a')
run(rule('split', 'a,b', '/'), 'a,b', 'a,b')
def run(rules, tags, expected):
if isinstance(rules, dict):
rules = [rules]
if isinstance(tags, type('')):
tags = [x.strip() for x in tags.split(',')]
if isinstance(expected, type('')):
expected = [x.strip() for x in expected.split(',')]
ans = map_tags(tags, rules)
self.assertEqual(ans, expected)
run(rule('capitalize', 't1,t2'), 't1,x1', 'T1,x1')
run(rule('upper', 'ta,t2'), 'ta,x1', 'TA,x1')
run(rule('lower', 'ta,x1'), 'TA,X1', 'ta,x1')
run(rule('replace', 't1', 't2'), 't1,x1', 't2,x1')
run(rule('replace', '(.)1', r'\g<1>2', 'matches'), 't1,x1', 't2,x2')
run(rule('replace', '(.)1', r'\g<1>2,3', 'matches'), 't1,x1', 't2,3,x2')
run(rule('replace', 't1', 't2, t3'), 't1,x1', 't2,t3,x1')
run([rule('replace', 't1', 't2,t3'), rule('remove', 't2')], 't1,x1', 't3,x1')
run(rule('replace', 't1', 't1'), 't1,x1', 't1,x1')
run([rule('replace', 't1', 't2'), rule('replace', 't2', 't1')], 't1,t2', 't1,t2')
run(rule('replace', 'a', 'A'), 'a,b', 'A,b')
run(rule('replace', 'a,b', 'A,B'), 'a,b', 'A,B')
run(rule('replace', 'L', 'T', 'has'), 'L', 'T')
run(rule('split', '/', '/', 'has'), 'a/b/c,d', 'a,b,c,d')
run(rule('split', '/', '/', 'has'), '/,d', 'd')
run(rule('split', '/', '/', 'has'), '/a/', 'a')
run(rule('split', 'a,b', '/'), 'a,b', 'a,b')
return unittest.defaultTestLoader.loadTestsFromTestCase(TestTagMapper)
if __name__ == '__main__':
test()
from calibre.utils.run_tests import run_cli
run_cli(find_tests())