Revert "Some more tests, refactors for supplying input in tests"

This commit is contained in:
Arun Prakash Jana 2016-08-26 00:02:33 +05:30 committed by GitHub
parent dbff2e0ee7
commit 33072b2edf
2 changed files with 26 additions and 143 deletions

87
buku
View File

@ -439,21 +439,14 @@ 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, input_func=None):
def append_tag_at_index(self, index, tag_manual):
""" 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
"""
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:
self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,))
self.select_tags_from_bookmarks(index)
resultset = self.cur.fetchall()
for row in resultset:
tags = '%s%s' % (row[1], tag_manual[1:])
@ -462,36 +455,36 @@ class BukuDb:
self.conn.commit()
def select_all_tags_from_bookmarks(self, input_function):
""" Return all tags for all bookmarks along with id's
def select_tags_from_bookmarks(self, index):
""" Select either the tags for one bookmark or all bookmarks
:param input_func: function to pass in which collects input for user selection
:param index: int position of record, 0 for all
"""
resp = input_function()
if resp != 'y':
return
if index == 0:
resp = input('Tags will be appended for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ')
if resp != 'y':
return
self.cur.execute('SELECT id, tags FROM bookmarks ORDER BY id ASC')
return True
self.cur.execute('SELECT id, tags FROM bookmarks ORDER BY id ASC')
else:
self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,))
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, input_func=None):
def delete_tag_at_index(self, index, tag_manual):
""" 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:
input_func = input_func or self.prompt_delete_tags_all_bookmarks
if self.select_all_tags_like(tags_to_delete, input_func) == None:
resp = input('Tags will be deleted for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ')
if resp != 'y':
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,))
@ -507,27 +500,6 @@ class BukuDb:
self.conn.commit()
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_func: function to pass in which collects input for user selection
"""
resp = input_func()
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_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,
tag_manual=None, desc=None, append_tag=False, delete_tag=False):
""" Update an existing record at index
@ -736,14 +708,12 @@ 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, input_func=None):
def delete_bookmark(self, index, low=0, high=0, is_range=False):
"""Delete a single record or remove the table if index is None
: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
Params: index to delete
"""
if is_range:
try:
self.cur.execute('DELETE from bookmarks where id BETWEEN ? AND ?', (low, high))
@ -755,8 +725,9 @@ class BukuDb:
for index in range(low, high + 1):
self.compactdb(index)
elif index == 0: # Remove the table
input_func = input_func or self.prompt_delete_all_bookmarks
if self.delete_all_bookmarks(input_func) == None:
resp = input('ALL bookmarks will be removed. Enter \x1b[1my\x1b[21m to confirm: ')
if resp != 'y':
print('No bookmarks deleted')
return
self.delete_all_bookmarks()
@ -773,24 +744,14 @@ class BukuDb:
except IndexError:
logger.error('Index out of bound')
def delete_all_bookmarks(self, input_func):
def delete_all_bookmarks(self):
"""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

View File

@ -185,28 +185,6 @@ class TestBukuDb(unittest.TestCase):
# checking if old tags still exist
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()
@ -240,53 +218,11 @@ class TestBukuDb(unittest.TestCase):
# adding bookmarks
bdb.add_bookmark(*self.bookmarks[0])
# deleting all bookmarks
bdb.delete_all_bookmarks(lambda: "y")
bdb.delete_all_bookmarks()
# 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()
@ -321,20 +257,6 @@ 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()
@ -362,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) == ('', '')