def test_search_and_open_all_in_browser(self): # adding bookmarks for bookmark in self.bookmarks: self.bdb.add_rec(*bookmark) # simulate user input, select 'a' to open all bookmarks in results with mock.patch('builtins.input', side_effect=['a']): with mock.patch('buku.browse') as mock_browse: try: # search the db with keywords from each bookmark # searching using the first tag from bookmarks get_first_tag = lambda x: x[2].split(',')[1] results = self.bdb.searchdb( [get_first_tag(bm) for bm in self.bookmarks[:2]]) prompt(self.bdb, results) except StopIteration: # catch exception thrown by reaching the end of the side effect iterable pass # collect arguments passed to browse arg_list = [args[0] for args, _ in mock_browse.call_args_list] # expect a list of one-tuples that are bookmark URLs expected = [x[0] for x in self.bookmarks][:2] # checking if browse called with expected arguments self.assertEqual(arg_list, expected)
def test_search_and_open_in_broswer_by_range(self): # adding bookmarks for bookmark in self.bookmarks: self.bdb.add_rec(*bookmark) # simulate user input, select range of indices 1-3 index_range = '1-%s' % len(self.bookmarks) with mock.patch('builtins.input', side_effect=[index_range]): with mock.patch('buku.open_in_browser') as mock_open_in_browser: try: # search the db with keywords from each bookmark # searching using the first tag from bookmarks get_first_tag = lambda x: x[2].split(',')[1] results = self.bdb.searchdb([get_first_tag(bm) for bm in self.bookmarks]) prompt(self.bdb, results) except StopIteration: # catch exception thrown by reaching the end of the side effect iterable pass # collect arguments passed to open_in_browser arg_list = [args[0] for args, _ in mock_open_in_browser.call_args_list] # expect a list of one-tuples that are bookmark URLs expected = [x[0] for x in self.bookmarks] # checking if open_in_browser called with expected arguments self.assertEqual(arg_list, expected)
def test_search_and_open_in_broswer_by_range(self): # adding bookmarks for bookmark in self.bookmarks: self.bdb.add_rec(*bookmark) # simulate user input, select range of indices 1-3 index_range = '1-%s' % len(self.bookmarks) with mock.patch('builtins.input', side_effect=[index_range]): with mock.patch('buku.browse') as mock_browse: try: # search the db with keywords from each bookmark # searching using the first tag from bookmarks get_first_tag = lambda x: x[2].split(',')[1] results = self.bdb.searchdb([get_first_tag(bm) for bm in self.bookmarks]) prompt(self.bdb, results) except StopIteration: # catch exception thrown by reaching the end of the side effect iterable pass # collect arguments passed to browse arg_list = [args[0] for args, _ in mock_browse.call_args_list] # expect a list of one-tuples that are bookmark URLs expected = [x[0] for x in self.bookmarks] # checking if browse called with expected arguments self.assertEqual(arg_list, expected)
def test_list_tags(capsys, setup): bdb = BukuDb() # adding bookmarks bdb.add_rec("http://one.com", "", parse_tags(['cat,ant,bee,1']), "") bdb.add_rec("http://two.com", "", parse_tags(['Cat,Ant,bee,1']), "") bdb.add_rec("http://three.com", "", parse_tags(['Cat,Ant,3,Bee,2']), "") # listing tags, asserting output out, err = capsys.readouterr() prompt(bdb, None, True, subprompt=True) out, err = capsys.readouterr() assert out == " 1. 1 (2)\n 2. 2 (1)\n 3. 3 (1)\n 4. ant (3)\n 5. bee (3)\n 6. cat (3)\n\n" assert err == ''
def test_print_rec(capsys, caplog, setup): bdb = BukuDb() out, err = capsys.readouterr() # calling with nonexistent index bdb.print_rec(1) out, err = capsys.readouterr() for record in caplog.records(): assert record.levelname == "ERROR" assert record.getMessage() == "No matching index 1" assert (out, err) == ('', '') # adding bookmarks bdb.add_rec("http://full-bookmark.com", "full", parse_tags(['full,bookmark']), "full bookmark") bdb.add_rec("http://blank-title.com", "", parse_tags(['blank,title']), "blank title") bdb.add_rec("http://empty-tags.com", "empty tags", parse_tags(['']), "empty tags") bdb.add_rec("http://all-empty.com", "", parse_tags(['']), "all empty") out, err = capsys.readouterr() # printing first bookmark bdb.print_rec(1) out, err = capsys.readouterr() assert out == "\x1b[93;1m1. \x1b[0;92mhttp://full-bookmark.com\x1b[0m\n \x1b[91m>\x1b[0m full\n \x1b[91m+\x1b[0m full bookmark\n \x1b[91m#\x1b[0m bookmark,full\n\n" assert err == '' # printing all bookmarks bdb.print_rec(0) out, err = capsys.readouterr() assert out == "\x1b[93;1m1. \x1b[0;92mhttp://full-bookmark.com\x1b[0m\n \x1b[91m>\x1b[0m full\n \x1b[91m+\x1b[0m full bookmark\n \x1b[91m#\x1b[0m bookmark,full\n\n\x1b[93;1m2. \x1b[0;92mhttp://blank-title.com\x1b[0m\n \x1b[91m+\x1b[0m blank title\n \x1b[91m#\x1b[0m blank,title\n\n\x1b[93;1m3. \x1b[0;92mhttp://empty-tags.com\x1b[0m\n \x1b[91m>\x1b[0m empty tags\n \x1b[91m+\x1b[0m empty tags\n\n\x1b[93;1m4. \x1b[0;92mhttp://all-empty.com\x1b[0m\n \x1b[91m+\x1b[0m all empty\n\n" assert err == '' # printing all bookmarks with empty fields results = bdb.searchdb(['blank'], True) prompt(bdb, results, True) out, err = capsys.readouterr() assert out == "\x1b[93;1m1. \x1b[0;92mhttp://blank-title.com\x1b[0;1m [2]\x1b[0m\n \x1b[91m+\x1b[0m blank title\n \x1b[91m#\x1b[0m blank,title\n\n\x1b[93;1m2. \x1b[0;92mhttp://empty-tags.com\x1b[0;1m [3]\x1b[0m\n \x1b[91m>\x1b[0m empty tags\n \x1b[91m+\x1b[0m empty tags\n\n\x1b[93;1m3. \x1b[0;92mhttp://all-empty.com\x1b[0;1m [4]\x1b[0m\n \x1b[91m+\x1b[0m all empty\n\n" assert err == ''
def test_search_and_open_all_in_browser(self): # adding bookmarks for bookmark in self.bookmarks: self.bdb.add_bm(*bookmark) # simulate user input, select 'a' to open all bookmarks in results with mock.patch('builtins.input', side_effect=['a']): with mock.patch('buku.open_in_browser') as mock_open_in_browser: try: # search the db with keywords from each bookmark # searching using the first tag from bookmarks get_first_tag = lambda x: x[2].split(',')[1] results = self.bdb.searchdb([get_first_tag(bm) for bm in self.bookmarks[:2]]) prompt(results) except StopIteration: # catch exception thrown by reaching the end of the side effect iterable pass # collect arguments passed to open_in_browser arg_list = [args[0] for args, _ in mock_open_in_browser.call_args_list] # expect a list of one-tuples that are bookmark URLs expected = [x[0] for x in self.bookmarks][:2] # checking if open_in_browser called with expected arguments self.assertEqual(arg_list, expected)