diff --git a/src/calibre/db/tests/reading.py b/src/calibre/db/tests/reading.py index 590c1c2927..951cbae765 100644 --- a/src/calibre/db/tests/reading.py +++ b/src/calibre/db/tests/reading.py @@ -754,66 +754,55 @@ class ReadingTest(BaseTest): db = self.init_cache(self.library_path) 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) + mi = db.get_metadata(1) - class GetGuiAns(): - current_db = None - get_gui_ans = GetGuiAns() - get_gui_ans.current_db = db - from calibre.gui2.ui import get_gui - get_gui.ans = get_gui_ans + # test counting books matching the search + v = formatter.safe_format('program: book_count("series:true", 0)', {}, 'TEMPLATE ERROR', mi) + self.assertEqual(v, '2') - try: - # need an empty metadata object to pass to the formatter - mi = Metadata('A', 'B') + # test counting books when none match the search + v = formatter.safe_format('program: book_count("series:afafaf", 0)', {}, 'TEMPLATE ERROR', mi) + self.assertEqual(v, '0') - # test counting books matching the search - v = formatter.safe_format('program: book_count("series:true", 0)', {}, 'TEMPLATE ERROR', mi) - self.assertEqual(v, '2') + # test is_multiple values + v = formatter.safe_format('program: book_values("tags", "tags:true", ",", 0)', {}, 'TEMPLATE ERROR', mi) + self.assertEqual(set(v.split(',')), {'Tag One', 'News', 'Tag Two'}) - # test counting books when none match the search - v = formatter.safe_format('program: book_count("series:afafaf", 0)', {}, 'TEMPLATE ERROR', mi) - self.assertEqual(v, '0') + # test not is_multiple values + v = formatter.safe_format('program: book_values("series", "series:true", ",", 0)', {}, 'TEMPLATE ERROR', mi) + self.assertEqual(v, 'A Series One') - # test is_multiple values - v = formatter.safe_format('program: book_values("tags", "tags:true", ",", 0)', {}, 'TEMPLATE ERROR', mi) - self.assertEqual(set(v.split(',')), {'Tag One', 'News', 'Tag Two'}) + # test returning values for a column not searched for + 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'}) - # test not is_multiple values - v = formatter.safe_format('program: book_values("series", "series:true", ",", 0)', {}, 'TEMPLATE ERROR', mi) - self.assertEqual(v, 'A Series One') + # test getting a singleton value from books where the column is empty + v = formatter.safe_format('program: book_values("series", "series:false", ",", 0)', {}, 'TEMPLATE ERROR', mi) + self.assertEqual(v, '') - # test returning values for a column not searched for - 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'}) + # test getting a multiple value from books where the column is empty + v = formatter.safe_format('program: book_values("tags", "tags:false", ",", 0)', {}, 'TEMPLATE ERROR', mi) + self.assertEqual(v, '') - # test getting a singleton value from books where the column is empty - v = formatter.safe_format('program: book_values("series", "series:false", ",", 0)', {}, 'TEMPLATE ERROR', mi) - self.assertEqual(v, '') + # test fetching an unknown column + v = formatter.safe_format('program: book_values("taaags", "tags:false", ",", 0)', {}, 'TEMPLATE ERROR', mi) + self.assertEqual(v, "TEMPLATE ERROR The column taaags doesn't exist") - # test getting a multiple value from books where the column is empty - v = formatter.safe_format('program: book_values("tags", "tags:false", ",", 0)', {}, 'TEMPLATE ERROR', mi) - self.assertEqual(v, '') + # test finding all books + v = formatter.safe_format('program: book_values("id", "title:true", ",", 0)', {}, 'TEMPLATE ERROR', mi) + self.assertEqual(set(v.split(',')), {'1', '2', '3'}) - # test fetching an unknown column - v = formatter.safe_format('program: book_values("taaags", "tags:false", ",", 0)', {}, 'TEMPLATE ERROR', mi) - self.assertEqual(v, "TEMPLATE ERROR The column taaags doesn't exist") + # test getting value of a composite + v = formatter.safe_format('program: book_values("#mult", "id:1", ",", 0)', {}, 'TEMPLATE ERROR', mi) + self.assertEqual(set(v.split(',')), {'b', 'c', 'a'}) - # test finding all books - v = formatter.safe_format('program: book_values("id", "title:true", ",", 0)', {}, 'TEMPLATE ERROR', mi) - self.assertEqual(set(v.split(',')), {'1', '2', '3'}) + # 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 a composite - v = formatter.safe_format('program: book_values("#mult", "id:1", ",", 0)', {}, 'TEMPLATE ERROR', mi) - self.assertEqual(set(v.split(',')), {'b', 'c', 'a'}) - - # 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 + # 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'}) # }}}