Fix the db formatter functions test

This commit is contained in:
Charles Haley 2022-07-12 15:07:26 +01:00
parent f0677655f8
commit d86c664e69

View File

@ -754,66 +754,55 @@ class ReadingTest(BaseTest):
db = self.init_cache(self.library_path) db = self.init_cache(self.library_path)
db.create_custom_column('mult', 'CC1', 'composite', True, display={'composite_template': 'b,a,c'}) db.create_custom_column('mult', 'CC1', 'composite', True, display={'composite_template': 'b,a,c'})
# need an empty metadata object to pass to the formatter
db = self.init_legacy(self.library_path) db = self.init_legacy(self.library_path)
mi = db.get_metadata(1)
class GetGuiAns(): # test counting books matching the search
current_db = None v = formatter.safe_format('program: book_count("series:true", 0)', {}, 'TEMPLATE ERROR', mi)
get_gui_ans = GetGuiAns() self.assertEqual(v, '2')
get_gui_ans.current_db = db
from calibre.gui2.ui import get_gui
get_gui.ans = get_gui_ans
try: # test counting books when none match the search
# need an empty metadata object to pass to the formatter v = formatter.safe_format('program: book_count("series:afafaf", 0)', {}, 'TEMPLATE ERROR', mi)
mi = Metadata('A', 'B') self.assertEqual(v, '0')
# test counting books matching the search # test is_multiple values
v = formatter.safe_format('program: book_count("series:true", 0)', {}, 'TEMPLATE ERROR', mi) v = formatter.safe_format('program: book_values("tags", "tags:true", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(v, '2') self.assertEqual(set(v.split(',')), {'Tag One', 'News', 'Tag Two'})
# test counting books when none match the search # test not is_multiple values
v = formatter.safe_format('program: book_count("series:afafaf", 0)', {}, 'TEMPLATE ERROR', mi) v = formatter.safe_format('program: book_values("series", "series:true", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(v, '0') self.assertEqual(v, 'A Series One')
# test is_multiple values # test returning values for a column not searched for
v = formatter.safe_format('program: book_values("tags", "tags:true", ",", 0)', {}, 'TEMPLATE ERROR', mi) v = formatter.safe_format('program: book_values("tags", "series:\\"A Series One\\"", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(set(v.split(',')), {'Tag One', 'News', 'Tag Two'}) self.assertEqual(set(v.split(',')), {'Tag One', 'News', 'Tag Two'})
# test not is_multiple values # test getting a singleton value from books where the column is empty
v = formatter.safe_format('program: book_values("series", "series:true", ",", 0)', {}, 'TEMPLATE ERROR', mi) v = formatter.safe_format('program: book_values("series", "series:false", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(v, 'A Series One') self.assertEqual(v, '')
# test returning values for a column not searched for # test getting a multiple value from books where the column is empty
v = formatter.safe_format('program: book_values("tags", "series:\\"A Series One\\"", ",", 0)', {}, 'TEMPLATE ERROR', mi) v = formatter.safe_format('program: book_values("tags", "tags:false", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(set(v.split(',')), {'Tag One', 'News', 'Tag Two'}) self.assertEqual(v, '')
# test getting a singleton value from books where the column is empty # test fetching an unknown column
v = formatter.safe_format('program: book_values("series", "series:false", ",", 0)', {}, 'TEMPLATE ERROR', mi) v = formatter.safe_format('program: book_values("taaags", "tags:false", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(v, '') self.assertEqual(v, "TEMPLATE ERROR The column taaags doesn't exist")
# test getting a multiple value from books where the column is empty # test finding all books
v = formatter.safe_format('program: book_values("tags", "tags:false", ",", 0)', {}, 'TEMPLATE ERROR', mi) v = formatter.safe_format('program: book_values("id", "title:true", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(v, '') self.assertEqual(set(v.split(',')), {'1', '2', '3'})
# test fetching an unknown column # test getting value of a composite
v = formatter.safe_format('program: book_values("taaags", "tags:false", ",", 0)', {}, 'TEMPLATE ERROR', mi) v = formatter.safe_format('program: book_values("#mult", "id:1", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(v, "TEMPLATE ERROR The column taaags doesn't exist") self.assertEqual(set(v.split(',')), {'b', 'c', 'a'})
# test finding all books # test getting value of a custom float
v = formatter.safe_format('program: book_values("id", "title:true", ",", 0)', {}, 'TEMPLATE ERROR', mi) v = formatter.safe_format('program: book_values("#float", "title:true", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(set(v.split(',')), {'1', '2', '3'}) self.assertEqual(set(v.split(',')), {'20.02', '10.01'})
# test getting value of a composite # test getting value of an int (rating)
v = formatter.safe_format('program: book_values("#mult", "id:1", ",", 0)', {}, 'TEMPLATE ERROR', mi) v = formatter.safe_format('program: book_values("rating", "title:true", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(set(v.split(',')), {'b', 'c', 'a'}) self.assertEqual(set(v.split(',')), {'4', '6'})
# test getting value of a custom float
v = formatter.safe_format('program: book_values("#float", "title:true", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(set(v.split(',')), {'20.02', '10.01'})
# test getting value of an int (rating)
v = formatter.safe_format('program: book_values("rating", "title:true", ",", 0)', {}, 'TEMPLATE ERROR', mi)
self.assertEqual(set(v.split(',')), {'4', '6'})
finally:
get_gui.ans = None
# }}} # }}}