mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add some tests for python templates
This commit is contained in:
parent
294a28433c
commit
3f856408d7
@ -811,3 +811,39 @@ class ReadingTest(BaseTest):
|
|||||||
v = formatter.safe_format('program: book_values("rating", "title:true", ",", 0)', {}, 'TEMPLATE ERROR', mi)
|
v = formatter.safe_format('program: book_values("rating", "title:true", ",", 0)', {}, 'TEMPLATE ERROR', mi)
|
||||||
self.assertEqual(set(v.split(',')), {'4', '6'})
|
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'})
|
||||||
|
# }}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user