Merge pull request #57 from wheresmyjetpack/master
Some more tests, refactors for supplying input in tests
This commit is contained in:
commit
dbff2e0ee7
87
buku
87
buku
@ -439,14 +439,21 @@ class BukuDb:
|
|||||||
_, _, linenumber, func, _, _ = inspect.stack()[0]
|
_, _, linenumber, func, _, _ = inspect.stack()[0]
|
||||||
logger.error('%s(), ln %d: %s', func, linenumber, e)
|
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
|
""" Append tags for bookmark at index
|
||||||
|
|
||||||
:param index: int position of record, 0 for all
|
:param index: int position of record, 0 for all
|
||||||
:tag_manual: string of comma-separated tags to add manually
|
:tag_manual: string of comma-separated tags to add manually
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.select_tags_from_bookmarks(index)
|
|
||||||
|
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,))
|
||||||
|
|
||||||
resultset = self.cur.fetchall()
|
resultset = self.cur.fetchall()
|
||||||
for row in resultset:
|
for row in resultset:
|
||||||
tags = '%s%s' % (row[1], tag_manual[1:])
|
tags = '%s%s' % (row[1], tag_manual[1:])
|
||||||
@ -455,36 +462,36 @@ class BukuDb:
|
|||||||
|
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
def select_tags_from_bookmarks(self, index):
|
def select_all_tags_from_bookmarks(self, input_function):
|
||||||
""" Select either the tags for one bookmark or all bookmarks
|
""" Return all tags for all bookmarks along with id's
|
||||||
|
|
||||||
:param index: int position of record, 0 for all
|
:param input_func: function to pass in which collects input for user selection
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if index == 0:
|
resp = input_function()
|
||||||
resp = input('Tags will be appended for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ')
|
if resp != 'y':
|
||||||
if resp != 'y':
|
return
|
||||||
return
|
|
||||||
|
|
||||||
self.cur.execute('SELECT id, tags FROM bookmarks ORDER BY id ASC')
|
self.cur.execute('SELECT id, tags FROM bookmarks ORDER BY id ASC')
|
||||||
else:
|
return True
|
||||||
self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,))
|
|
||||||
|
|
||||||
def delete_tag_at_index(self, index, tag_manual):
|
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):
|
||||||
""" Delete tags for bookmark at index
|
""" Delete tags for bookmark at index
|
||||||
|
|
||||||
:param index: int position of record, 0 for all
|
:param index: int position of record, 0 for all
|
||||||
:tag_manual: string of comma-separated tags to delete manually
|
: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)
|
tags_to_delete = tag_manual.strip(DELIMITER).split(DELIMITER)
|
||||||
|
|
||||||
if index == 0:
|
if index == 0:
|
||||||
resp = input('Tags will be deleted for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ')
|
input_func = input_func or self.prompt_delete_tags_all_bookmarks
|
||||||
if resp != 'y':
|
if self.select_all_tags_like(tags_to_delete, input_func) == None:
|
||||||
return
|
return
|
||||||
|
|
||||||
for tag in tags_to_delete:
|
|
||||||
self.cur.execute("SELECT id, tags FROM bookmarks WHERE tags LIKE '%' || ? || '%' ORDER BY id ASC", (tag,))
|
|
||||||
else:
|
else:
|
||||||
self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,))
|
self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,))
|
||||||
|
|
||||||
@ -500,6 +507,27 @@ class BukuDb:
|
|||||||
|
|
||||||
self.conn.commit()
|
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,
|
def update_bookmark(self, index, url='', title_manual=None,
|
||||||
tag_manual=None, desc=None, append_tag=False, delete_tag=False):
|
tag_manual=None, desc=None, append_tag=False, delete_tag=False):
|
||||||
""" Update an existing record at index
|
""" Update an existing record at index
|
||||||
@ -708,12 +736,14 @@ class BukuDb:
|
|||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
print('Index %d moved to %d' % (row[0], index))
|
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
|
"""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:
|
if is_range:
|
||||||
try:
|
try:
|
||||||
self.cur.execute('DELETE from bookmarks where id BETWEEN ? AND ?', (low, high))
|
self.cur.execute('DELETE from bookmarks where id BETWEEN ? AND ?', (low, high))
|
||||||
@ -725,9 +755,8 @@ class BukuDb:
|
|||||||
for index in range(low, high + 1):
|
for index in range(low, high + 1):
|
||||||
self.compactdb(index)
|
self.compactdb(index)
|
||||||
elif index == 0: # Remove the table
|
elif index == 0: # Remove the table
|
||||||
resp = input('ALL bookmarks will be removed. Enter \x1b[1my\x1b[21m to confirm: ')
|
input_func = input_func or self.prompt_delete_all_bookmarks
|
||||||
if resp != 'y':
|
if self.delete_all_bookmarks(input_func) == None:
|
||||||
print('No bookmarks deleted')
|
|
||||||
return
|
return
|
||||||
|
|
||||||
self.delete_all_bookmarks()
|
self.delete_all_bookmarks()
|
||||||
@ -744,14 +773,24 @@ class BukuDb:
|
|||||||
except IndexError:
|
except IndexError:
|
||||||
logger.error('Index out of bound')
|
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
|
"""Should only be called inside of delete_bookmark
|
||||||
Drops the bookmark table if it exists
|
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.cur.execute('DROP TABLE if exists bookmarks')
|
||||||
self.conn.commit()
|
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):
|
def print_bookmark(self, index, empty=False):
|
||||||
"""Print bookmark details at index or all bookmarks if index is 0
|
"""Print bookmark details at index or all bookmarks if index is 0
|
||||||
Print only bookmarks with blank title or tag if empty is True
|
Print only bookmarks with blank title or tag if empty is True
|
||||||
|
@ -185,6 +185,28 @@ class TestBukuDb(unittest.TestCase):
|
|||||||
# checking if old tags still exist
|
# checking if old tags still exist
|
||||||
self.assertTrue(all( x in from_db.split(',') for x in old_tags.split(',') ))
|
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')
|
# @unittest.skip('skipping')
|
||||||
def test_refreshdb(self):
|
def test_refreshdb(self):
|
||||||
bdb = BukuDb()
|
bdb = BukuDb()
|
||||||
@ -218,11 +240,53 @@ class TestBukuDb(unittest.TestCase):
|
|||||||
# adding bookmarks
|
# adding bookmarks
|
||||||
bdb.add_bookmark(*self.bookmarks[0])
|
bdb.add_bookmark(*self.bookmarks[0])
|
||||||
# deleting all bookmarks
|
# deleting all bookmarks
|
||||||
bdb.delete_all_bookmarks()
|
bdb.delete_all_bookmarks(lambda: "y")
|
||||||
# assert table has been dropped
|
# assert table has been dropped
|
||||||
with self.assertRaises(sqlite3.OperationalError):
|
with self.assertRaises(sqlite3.OperationalError):
|
||||||
bdb.get_bookmark_by_index(0)
|
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')
|
# @unittest.skip('skipping')
|
||||||
def test_replace_tag(self):
|
def test_replace_tag(self):
|
||||||
bdb = BukuDb()
|
bdb = BukuDb()
|
||||||
@ -257,6 +321,20 @@ class TestBukuDb(unittest.TestCase):
|
|||||||
elif title == "test":
|
elif title == "test":
|
||||||
self.assertEqual(from_db[3], parse_tags(["test,tes,est,__04"]))
|
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):
|
# def test_browse_by_index(self):
|
||||||
# self.fail()
|
# self.fail()
|
||||||
|
|
||||||
@ -284,7 +362,7 @@ def test_print_bookmark(capsys, caplog, setup):
|
|||||||
bdb.print_bookmark(1)
|
bdb.print_bookmark(1)
|
||||||
out, err = capsys.readouterr()
|
out, err = capsys.readouterr()
|
||||||
|
|
||||||
for record in caplog.records:
|
for record in caplog.records():
|
||||||
assert record.levelname == "ERROR"
|
assert record.levelname == "ERROR"
|
||||||
assert record.getMessage() == "No matching index"
|
assert record.getMessage() == "No matching index"
|
||||||
assert (out, err) == ('', '')
|
assert (out, err) == ('', '')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user