From 9f66cfbd006f559509970416f0e44e3277a2b1ea Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 23 Aug 2016 10:50:15 -0500 Subject: [PATCH 1/4] fixed caplog.records call in test_print_bookmark --- tests/test_bukuDb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 0456af3..e4fbe6b 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -284,7 +284,7 @@ def test_print_bookmark(capsys, caplog, setup): bdb.print_bookmark(1) out, err = capsys.readouterr() - for record in caplog.records: + for record in caplog.records(): assert record.levelname == "ERROR" assert record.getMessage() == "No matching index" assert (out, err) == ('', '') From 4a43b3974380d839abd4f9f5c616c8c62e6f4c19 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 24 Aug 2016 01:51:27 -0500 Subject: [PATCH 2/4] adding 2 tests, refactors for improved testing --- buku | 53 +++++++++++++++++++++++++++++++------------- tests/test_bukuDb.py | 14 ++++++++++++ 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/buku b/buku index 1061fc4..0672b1b 100755 --- a/buku +++ b/buku @@ -446,7 +446,12 @@ class BukuDb: :tag_manual: string of comma-separated tags to add manually """ - self.select_tags_from_bookmarks(index) + if index == 0: + if self.select_all_tags_from_bookmarks(self.prompt_append_all_bookmarks) == None: + return + else: + self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,)) + resultset = self.cur.fetchall() for row in resultset: tags = '%s%s' % (row[1], tag_manual[1:]) @@ -455,20 +460,21 @@ class BukuDb: self.conn.commit() - def select_tags_from_bookmarks(self, index): - """ Select either the tags for one bookmark or all bookmarks + def select_all_tags_from_bookmarks(self, input_function): + """ Return all tags for all bookmarks along with id's - :param index: int position of record, 0 for all + :param input_function: function to pass in to confirm selection """ - if index == 0: - resp = input('Tags will be appended for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ') - if resp != 'y': - return + resp = input_function() + if resp != 'y': + return - self.cur.execute('SELECT id, tags FROM bookmarks ORDER BY id ASC') - else: - self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,)) + self.cur.execute('SELECT id, tags FROM bookmarks ORDER BY id ASC') + return True + + def prompt_append_all_bookmarks(self): + return input('Tags will be appended for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ') def delete_tag_at_index(self, index, tag_manual): """ Delete tags for bookmark at index @@ -479,12 +485,8 @@ class BukuDb: tags_to_delete = tag_manual.strip(DELIMITER).split(DELIMITER) if index == 0: - resp = input('Tags will be deleted for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ') - if resp != 'y': + if self.select_all_tags_like(tags_to_delete, self.prompt_delete_all_bookmarks) == None: return - - for tag in tags_to_delete: - self.cur.execute("SELECT id, tags FROM bookmarks WHERE tags LIKE '%' || ? || '%' ORDER BY id ASC", (tag,)) else: self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,)) @@ -500,6 +502,25 @@ class BukuDb: self.conn.commit() + def select_all_tags_like(self, tags, input_function): + """ Select all tags from a list of tags + + :param tags: list of tags to look for + :param input_function: function to pass in to confirm selection + """ + + resp = input_function() + if resp != 'y': + return + + for tag in tags: + self.cur.execute("SELECT id, tags FROM bookmarks WHERE tags LIKE '%' || ? || '%' ORDER BY id ASC", (tag,)) + + return True + + def prompt_delete_all_bookmarks(self): + return input('Tags will be deleted for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ') + def update_bookmark(self, index, url='', title_manual=None, tag_manual=None, desc=None, append_tag=False, delete_tag=False): """ Update an existing record at index diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index e4fbe6b..0cecb8d 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -257,6 +257,20 @@ class TestBukuDb(unittest.TestCase): elif title == "test": self.assertEqual(from_db[3], parse_tags(["test,tes,est,__04"])) + # @unittest.skip('skipping') + def test_select_all_tags_from_bookmarks(self): + bdb = BukuDb() + + # adding bookmarks + for bookmark in self.bookmarks: + bdb.add_bookmark(*bookmark) + + # checking if None returned for non-"y" response + self.assertIsNone(bdb.select_all_tags_from_bookmarks(lambda: "n")) + # checking if all tags are selected when response "y" + bdb.select_all_tags_from_bookmarks(lambda: "y") + self.assertEqual([(1, ',news,old,'), (2, ',gęślą,jaźń,zażółć,'), (3, ',es,est,tes,test,')], bdb.cur.fetchall()) + # def test_browse_by_index(self): # self.fail() From b294351436a0d540a97461e9e6e3525dd1a182a0 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 24 Aug 2016 13:31:45 -0500 Subject: [PATCH 3/4] added test_append_tag_at_all_indices --- buku | 6 ++++-- tests/test_bukuDb.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/buku b/buku index 0672b1b..5608d80 100755 --- a/buku +++ b/buku @@ -439,15 +439,17 @@ class BukuDb: _, _, linenumber, func, _, _ = inspect.stack()[0] logger.error('%s(), ln %d: %s', func, linenumber, e) - def append_tag_at_index(self, index, tag_manual): + def append_tag_at_index(self, index, tag_manual, input_func=None): """ Append tags for bookmark at index :param index: int position of record, 0 for all :tag_manual: string of comma-separated tags to add manually """ + input_func = input_func or self.select_all_tags_from_bookmarks + if index == 0: - if self.select_all_tags_from_bookmarks(self.prompt_append_all_bookmarks) == None: + if self.select_all_tags_from_bookmarks(input_func) == None: return else: self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,)) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 0cecb8d..cfab86c 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -186,6 +186,28 @@ class TestBukuDb(unittest.TestCase): self.assertTrue(all( x in from_db.split(',') for x in old_tags.split(',') )) # @unittest.skip('skipping') + def test_append_tag_at_all_indices(self): + bdb = BukuDb() + for bookmark in self.bookmarks: + bdb.add_bookmark(*bookmark) + + inclusive_range = lambda start, end: range(start, end + 1) + old_tags = [ bdb.get_bookmark_by_index(i)[3] for i in inclusive_range(1, len(self.bookmarks)) ] + # tag to add + new_tags = ",foo,bar,baz" + bdb.append_tag_at_index(0, new_tags, input_func=lambda: "y") + # updated list of all tags + # each element is a two-tuple containing the bookmark index in the db and the tags for that bookmark + from_db = [ bdb.get_bookmark_by_index(i)[:4:3] for i in inclusive_range(1, len(self.bookmarks)) ] + + for index, tags in from_db: + index = index - 1 + # checking if new tags were added + self.assertTrue(all( x in tags.split(',') for x in new_tags.split(',') )) + # checking if old togs still exist + self.assertTrue(all( x in tags.split(',') for x in old_tags[index].split(',') )) + + @unittest.skip('skipping') def test_refreshdb(self): bdb = BukuDb() From f6895e98ab444c13893a8e8a757e184029c31d30 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 24 Aug 2016 16:58:51 -0500 Subject: [PATCH 4/4] adding test_delete_tag_at_index, test_delete_tag_at_all_indices --- buku | 44 ++++++++++++++++++++++++++++-------------- tests/test_bukuDb.py | 46 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/buku b/buku index 5608d80..951fa34 100755 --- a/buku +++ b/buku @@ -446,9 +446,9 @@ class BukuDb: :tag_manual: string of comma-separated tags to add manually """ - input_func = input_func or self.select_all_tags_from_bookmarks if index == 0: + input_func = input_func or self.prompt_append_all_bookmarks if self.select_all_tags_from_bookmarks(input_func) == None: return else: @@ -465,7 +465,7 @@ class BukuDb: def select_all_tags_from_bookmarks(self, input_function): """ Return all tags for all bookmarks along with id's - :param input_function: function to pass in to confirm selection + :param input_func: function to pass in which collects input for user selection """ resp = input_function() @@ -478,16 +478,19 @@ class BukuDb: def prompt_append_all_bookmarks(self): return input('Tags will be appended for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ') - def delete_tag_at_index(self, index, tag_manual): + def delete_tag_at_index(self, index, tag_manual, input_func=None): """ Delete tags for bookmark at index :param index: int position of record, 0 for all :tag_manual: string of comma-separated tags to delete manually + :param input_func: function to pass in which collects input for user selection """ + tags_to_delete = tag_manual.strip(DELIMITER).split(DELIMITER) if index == 0: - if self.select_all_tags_like(tags_to_delete, self.prompt_delete_all_bookmarks) == None: + input_func = input_func or self.prompt_delete_tags_all_bookmarks + if self.select_all_tags_like(tags_to_delete, input_func) == None: return else: self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,)) @@ -504,14 +507,16 @@ class BukuDb: self.conn.commit() - def select_all_tags_like(self, tags, input_function): + return True + + def select_all_tags_like(self, tags, input_func): """ Select all tags from a list of tags :param tags: list of tags to look for - :param input_function: function to pass in to confirm selection + :param input_func: function to pass in which collects input for user selection """ - resp = input_function() + resp = input_func() if resp != 'y': return @@ -520,7 +525,7 @@ class BukuDb: return True - def prompt_delete_all_bookmarks(self): + def prompt_delete_tags_all_bookmarks(self): return input('Tags will be deleted for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ') def update_bookmark(self, index, url='', title_manual=None, @@ -731,12 +736,14 @@ class BukuDb: self.conn.commit() print('Index %d moved to %d' % (row[0], index)) - def delete_bookmark(self, index, low=0, high=0, is_range=False): + def delete_bookmark(self, index, low=0, high=0, is_range=False, input_func=None): """Delete a single record or remove the table if index is None - Params: index to delete + :param index: bookmark at index to delete, 0 for delete all bookmarks + :param input_func: function to pass in which collects input for user selection """ + if is_range: try: self.cur.execute('DELETE from bookmarks where id BETWEEN ? AND ?', (low, high)) @@ -748,9 +755,8 @@ class BukuDb: for index in range(low, high + 1): self.compactdb(index) elif index == 0: # Remove the table - resp = input('ALL bookmarks will be removed. Enter \x1b[1my\x1b[21m to confirm: ') - if resp != 'y': - print('No bookmarks deleted') + input_func = input_func or self.prompt_delete_all_bookmarks + if self.delete_all_bookmarks(input_func) == None: return self.delete_all_bookmarks() @@ -767,14 +773,24 @@ class BukuDb: except IndexError: logger.error('Index out of bound') - def delete_all_bookmarks(self): + def delete_all_bookmarks(self, input_func): """Should only be called inside of delete_bookmark Drops the bookmark table if it exists """ + #resp = input('ALL bookmarks will be removed. Enter \x1b[1my\x1b[21m to confirm: ') + resp = input_func() + if resp != 'y': + print('No bookmarks deleted') + return self.cur.execute('DROP TABLE if exists bookmarks') self.conn.commit() + return True + + def prompt_delete_all_bookmarks(self): + return input('ALL bookmarks will be removed. Enter \x1b[1my\x1b[21m to confirm: ') + def print_bookmark(self, index, empty=False): """Print bookmark details at index or all bookmarks if index is 0 Print only bookmarks with blank title or tag if empty is True diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index cfab86c..0bd3d50 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -207,7 +207,7 @@ class TestBukuDb(unittest.TestCase): # checking if old togs still exist self.assertTrue(all( x in tags.split(',') for x in old_tags[index].split(',') )) - @unittest.skip('skipping') + # @unittest.skip('skipping') def test_refreshdb(self): bdb = BukuDb() @@ -240,11 +240,53 @@ class TestBukuDb(unittest.TestCase): # adding bookmarks bdb.add_bookmark(*self.bookmarks[0]) # deleting all bookmarks - bdb.delete_all_bookmarks() + bdb.delete_all_bookmarks(lambda: "y") # assert table has been dropped with self.assertRaises(sqlite3.OperationalError): bdb.get_bookmark_by_index(0) + # @unittest.skip('skipping') + def test_delete_tag_at_index(self): + bdb = BukuDb() + # adding bookmark and getting index + bookmark_to_add = self.bookmarks[0] + tags = bookmark_to_add[3] + # adding bookmark + bdb.add_bookmark(*bookmark_to_add) + index = bdb.get_bookmark_index(bookmark_to_add[0]) + + # get tag to delete + target_tag = tags.split(',')[1] + # delete tag + bdb.delete_tag_at_index(index, target_tag) + from_db = bdb.get_bookmark_by_index(index)[3] + # checking tag no longer exists + self.assertFalse( target_tag in from_db.split(',') ) + # checking non-"y" response returns None + self.assertIsNone( bdb.delete_tag_at_index(0, target_tag, input_func=lambda: "n") ) + + @unittest.skip('skipping') + def test_delete_tag_at_all_indices(self): + # test currently fails + # only the last tag in the tags_to_delete string is removed from all bookmarks + + bdb = BukuDb() + # adding bookmarks + for bookmark in self.bookmarks: + bdb.add_bookmark(*bookmark) + + inclusive_range = lambda start, end: range(start, end + 1) + # string of tags to remove + tags_to_delete = ',old,jaźń,est,' + # deleting tags + bdb.delete_tag_at_index(0, tags_to_delete, input_func=lambda: "y") + # list of strings; each string is tagset for a bookmark + all_tags = [ bdb.get_bookmark_by_index(i)[3] for i in inclusive_range(1, len(self.bookmarks)) ] + + for tagset in all_tags: + # checking that no tags marked for deletion still exist + self.assertTrue(all( x not in tagset.split(',') for x in tags_to_delete.split(',') if x )) + # @unittest.skip('skipping') def test_replace_tag(self): bdb = BukuDb()