Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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, listtags=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 == ''
# removing nonexistent tag which is also a substring of other tag
with mock.patch('builtins.input', return_value='y'):
self.bdb.replace_tag("e")
for url, title, _, _ in self.bookmarks:
# retrieving from db
index = self.bdb.get_rec_id(url)
from_db = self.bdb.get_rec_by_id(index)
# asserting tags were replaced
if title == "SLASHDOT":
self.assertEqual(from_db[3], parse_tags(["__01"]))
elif title == "ZAŻÓŁĆ":
self.assertEqual(from_db[3], parse_tags(["__02,__03,jaźń"]))
elif title == "test":
self.assertEqual(from_db[3], parse_tags(["test,tes,est,__04"]))
def test_parse_tags(self):
# call with None
parsed = parse_tags(None)
self.assertIsNone(parsed)
# call with empty list
parsed = parse_tags([])
self.assertEqual(parsed, ",")
# empty tags
parsed = parse_tags([",,,,,"])
self.assertEqual(parsed, ",")
# sorting tags
parsed = parse_tags(["z_tag,a_tag,n_tag"])
self.assertEqual(parsed, ",a_tag,n_tag,z_tag,")
# whitespaces
parsed = parse_tags([" a tag , , , ,\t,\n,\r,\x0b,\x0c"])
self.assertEqual(parsed, ",a tag,")
# duplicates, excessive spaces
parsed = parse_tags(["tag,tag, tag, tag,tag , tag "])
self.assertEqual(parsed, ",tag,")
'additional bookmark to test multiple tag search', 0]
self.bdb.add_rec(*new_bookmark)
with mock.patch('buku.prompt'):
# search for bookmarks matching ANY of the supplied tags
results = self.bdb.search_by_tag('test, old')
# Expect a list of five-element tuples containing all bookmark data
# db index, URL, title, tags, description, ordered by records with
# the most number of matches.
expected = [
(4, 'https://newbookmark.com', 'New Bookmark',
parse_tags([',test,old,new,']),
'additional bookmark to test multiple tag search', 0),
(1, 'http://slashdot.org', 'SLASHDOT',
parse_tags([',news,old,']),
"News for old nerds, stuff that doesn't matter", 0),
(3, 'http://example.com/', 'test', ',es,est,tes,test,', 'a case for replace_tag test', 0)
]
self.assertEqual(results, expected)
def test_search_by_tags_enforces_space_seprations_search_all(self):
bookmark1 = ['https://bookmark1.com',
'Bookmark One',
parse_tags(['tag, two,tag+two']),
"test case for bookmark with '+' in tag"]
bookmark2 = ['https://bookmark2.com',
'Bookmark Two',
parse_tags(['tag,two, tag-two']),
"test case for bookmark with hyphenated tag"]
self.bdb.add_rec(*bookmark1)
self.bdb.add_rec(*bookmark2)
with mock.patch('buku.prompt'):
# check that space separation for ' + ' operator is enforced
results = self.bdb.search_by_tag('tag+two')
# Expect a list of five-element tuples containing all bookmark data
# db index, URL, title, tags, description
expected = [
new_bookmark = ['https://newbookmark.com',
'New Bookmark',
parse_tags(['test,old,new']),
'additional bookmark to test multiple tag search']
self.bdb.add_rec(*new_bookmark)
with mock.patch('buku.prompt'):
# search for bookmarks matching ALL of the supplied tags
results = self.bdb.search_by_tag('test + old')
# Expect a list of five-element tuples containing all bookmark data
# db index, URL, title, tags, description
expected = [
(4, 'https://newbookmark.com', 'New Bookmark',
parse_tags([',test,old,new,']),
'additional bookmark to test multiple tag search', 0)
]
self.assertEqual(results, expected)
'keywords, exp_res',
[
(None, None),
([], None),
(['tag1', 'tag2'], ',tag1 tag2,'),
(['tag1,tag2', 'tag3'], ',tag1,tag2 tag3,'),
]
)
def test_parse_tags(keywords, exp_res):
"""test func."""
import buku
if keywords is None:
pass
elif not keywords:
exp_res = buku.DELIM
res = buku.parse_tags(keywords)
assert res == exp_res
def test_search_by_tags_enforces_space_seprations_exclusion(self):
bookmark1 = ['https://bookmark1.com',
'Bookmark One',
parse_tags(['tag, two,tag+two']),
"test case for bookmark with '+' in tag"]
bookmark2 = ['https://bookmark2.com',
'Bookmark Two',
parse_tags(['tag,two, tag-two']),
"test case for bookmark with hyphenated tag"]
bookmark3 = ['https://bookmark3.com',
'Bookmark Three',
parse_tags(['tag, tag three']),
"second test case for bookmark with hyphenated tag"]
self.bdb.add_rec(*bookmark1)
self.bdb.add_rec(*bookmark2)
self.bdb.add_rec(*bookmark3)
with mock.patch('buku.prompt'):
# check that space separation for ' - ' operator is enforced
results = self.bdb.search_by_tag('tag-two')
# Expect a list of five-element tuples containing all bookmark data
# db index, URL, title, tags, description
expected = [
(2, 'https://bookmark2.com', 'Bookmark Two',
parse_tags([',tag,two,tag-two,']),
"test case for bookmark with hyphenated tag", 0),
]
'additional bookmark to test multiple tag search']
self.bdb.add_rec(*new_bookmark)
with mock.patch('buku.prompt'):
# search for bookmarks matching ANY of the supplied tags
# while excluding bookmarks from results that match a given tag
results = self.bdb.search_by_tag('test, old - est')
# Expect a list of five-element tuples containing all bookmark data
# db index, URL, title, tags, description
expected = [
(4, 'https://newbookmark.com', 'New Bookmark',
parse_tags([',test,old,new,']),
'additional bookmark to test multiple tag search', 0),
(1, 'http://slashdot.org', 'SLASHDOT',
parse_tags([',news,old,']),
"News for old nerds, stuff that doesn't matter", 0),
]
self.assertEqual(results, expected)