Add some tests for python templates

This commit is contained in:
Charles Haley 2022-10-10 21:25:06 +01:00
parent 294a28433c
commit 3f856408d7

View File

@ -811,3 +811,39 @@ class ReadingTest(BaseTest):
v = formatter.safe_format('program: book_values("rating", "title:true", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(set(v.split(',')), {'4', '6'})
# }}}
def test_python_templates(self): # {{{
from calibre.ebooks.metadata.book.formatter import SafeFormat
formatter = SafeFormat()
# need an empty metadata object to pass to the formatter
db = self.init_legacy(self.library_path)
mi = db.get_metadata(1)
# test counting books matching a search
template = '''python:
def evaluate(book, db, **kwargs):
ids = db.new_api.search("series:true")
return str(len(ids))
'''
v = formatter.safe_format(template, {}, 'TEMPLATE ERROR', mi)
self.assertEqual(v, '2')
# test counting books when none match the search
template = '''python:
def evaluate(book, db, **kwargs):
ids = db.new_api.search("series:afafaf")
return str(len(ids))
'''
v = formatter.safe_format(template, {}, 'TEMPLATE ERROR', mi)
self.assertEqual(v, '0')
# test is_multiple values
template = '''python:
def evaluate(book, db, **kwargs):
tags = db.new_api.all_field_names('tags')
return ','.join(list(tags))
'''
v = formatter.safe_format(template, {}, 'TEMPLATE ERROR', mi)
self.assertEqual(set(v.split(',')), {'Tag One', 'News', 'Tag Two'})
# }}}