Skip to content

Commit

Permalink
Add search_and_filter (#252)
Browse files Browse the repository at this point in the history
* Add search_and_filter

* Update search_any_keyword_and_filter_by_tags and tests.
  • Loading branch information
saltyCatfish authored and jarun committed Mar 22, 2018
1 parent 1c5695f commit 6db1944
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 8 deletions.
28 changes: 27 additions & 1 deletion buku.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,26 @@ def search_by_tag(self, tags):
self.cur.execute(query, tuple(tags, ))
return self.cur.fetchall()


def search_any_keyword_and_filter_by_tags(self, sany, deep, stag):
"""Search bookmarks for entries with any kewords while
filtering out entries with matching tags.
Parameters
----------
args : argParse.namespace
Contains the argument necessary to run searchdb
and search_by_tag
Returns
-------
list or None
List of search results, or None if no matches.
"""
sany_results = self.searchdb(sany, False, deep)
stag_results = self.search_by_tag(''.join(stag))
return list(set(sany_results) & set(stag_results))

def compactdb(self, index, delay_commit=False):
"""When an entry at index is deleted, move the
last entry in DB to index, if index is lesser.
Expand Down Expand Up @@ -4320,7 +4340,12 @@ def main():
search_opted = True
update_search_results = False

if args.sany is not None:
if args.stag is not None and args.sany is not None:
# Search URLs, title, tags for any keyword while
# filtering out matching tags
search_results = bdb.search_any_keyword_and_filter_by_tags(args.sany, args.deep, args.stag)

elif args.sany is not None:
if len(args.sany):
# Search URLs, titles, tags for any keyword
search_results = bdb.searchdb(args.sany, False, args.deep)
Expand All @@ -4338,6 +4363,7 @@ def main():
search_results = bdb.searchdb(args.sreg, regex=True)
else:
logerr('no expression')

elif args.stag is not None:
if len(args.stag):
# Search bookmarks by tag
Expand Down
86 changes: 79 additions & 7 deletions tests/test_bukuDb.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,74 @@ def test_refreshdb(self):
from_db = self.bdb.get_rec_by_id(1)
self.assertEqual(from_db[2], "Google")

# @unittest.skip('skipping')
def test_search_any_keyword_and_filter_by_tags(self):
# adding bookmark
for bookmark in self.bookmarks:
self.bdb.add_rec(*bookmark)

with mock.patch('buku.prompt'):
expected = [(3,
'https://test.com:8080',
'test',
',es,est,tes,test,',
'a case for replace_tag test')]
results = self.bdb.search_any_keyword_and_filter_by_tags(
['News', 'case'],
False,
['est'],
)
self.assertIn(expected[0], results)
expected = [(3,
'https://test.com:8080',
'test',
',es,est,tes,test,',
'a case for replace_tag test'),
(2,
'http://www.zażółćgęśląjaźń.pl/',
'ZAŻÓŁĆ',
',gęślą,jaźń,zażółć,',
'Testing UTF-8, zażółć gęślą jaźń.')]
results = self.bdb.search_any_keyword_and_filter_by_tags(
['UTF-8', 'case'],
False,
'jaźń, test',
)
self.assertIn(expected[0], results)
self.assertIn(expected[1], results)


def test_search_by_multiple_tags_search_any(self):
# adding bookmarks
for bookmark in self.bookmarks:
self.bdb.add_rec(*bookmark)

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 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'),
(1, 'http://slashdot.org', 'SLASHDOT',
parse_tags([',news,old,']),
"News for old nerds, stuff that doesn't matter"),
(3, 'https://test.com:8080', 'test',
parse_tags([',test,tes,est,es,']),
"a case for replace_tag test")
]
self.assertEqual(results, expected)

# @unittest.skip('skipping')
def test_searchdb(self):
# adding bookmarks
Expand Down Expand Up @@ -602,7 +670,7 @@ def test_tnyfy_url(self):
self.assertEqual(url, 'https://www.google.com')

# def test_browse_by_index(self):
# self.fail()
# self.fail()

# @unittest.skip('skipping')
def test_close_quit(self):
Expand All @@ -618,7 +686,8 @@ def test_close_quit(self):
self.assertEqual(err.args[0], 1)

# def test_import_bookmark(self):
# self.fail()
# self.fail()


@given(
index=st.integers(min_value=-10, max_value=10),
Expand Down Expand Up @@ -697,8 +766,10 @@ def test_compactdb(setup):
bdb.compactdb(2)

# asserting bookmarks have correct indices
assert bdb.get_rec_by_id(1) == (1, 'http://slashdot.org', 'SLASHDOT', ',news,old,', "News for old nerds, stuff that doesn't matter", 0)
assert bdb.get_rec_by_id(2) == (2, 'https://test.com:8080', 'test', ',es,est,tes,test,', 'a case for replace_tag test', 0)
assert bdb.get_rec_by_id(1) == (
1, 'http://slashdot.org', 'SLASHDOT', ',news,old,', "News for old nerds, stuff that doesn't matter", 0)
assert bdb.get_rec_by_id(2) == (
2, 'https://test.com:8080', 'test', ',es,est,tes,test,', 'a case for replace_tag test', 0)
assert bdb.get_rec_by_id(3) is None


Expand Down Expand Up @@ -1066,7 +1137,7 @@ def test_search_by_tag_query(caplog, tags_to_search, exp_query, exp_arguments):
assert caplog.records[-1].levelname == 'DEBUG'
except IndexError as e:
# TODO: fix test
if (sys.version_info.major, sys.version_info.minor) in [(3,4), (3, 5), (3, 6)]:
if (sys.version_info.major, sys.version_info.minor) in [(3, 4), (3, 5), (3, 6)]:
print('caplog records: {}'.format(caplog.records))
for idx, record in enumerate(caplog.records):
print('idx:{};{};message:{};levelname:{}'.format(
Expand Down Expand Up @@ -1101,7 +1172,7 @@ def test_update_rec_invalid_tag(caplog, invalid_tag):
assert caplog.records[0].getMessage() == 'Please specify a tag'
assert caplog.records[0].levelname == 'ERROR'
except IndexError as e:
if (sys.version_info.major, sys.version_info.minor) == (3,4):
if (sys.version_info.major, sys.version_info.minor) == (3, 4):
print('caplog records: {}'.format(caplog.records))
for idx, record in enumerate(caplog.records):
print('idx:{};{};message:{};levelname:{}'.format(
Expand All @@ -1123,7 +1194,7 @@ def test_update_rec_update_all_bookmark(caplog, read_in_retval):
assert res
try:
assert caplog.records[0].getMessage() == \
'query: "UPDATE bookmarks SET tags = ?", args: [\',tags1\']'
'query: "UPDATE bookmarks SET tags = ?", args: [\',tags1\']'
assert caplog.records[0].levelname == 'DEBUG'
except IndexError as e:
# TODO: fix test
Expand Down Expand Up @@ -1203,6 +1274,7 @@ def bookmark_folder(tmpdir):
extract_all_from_zip_url(zip_url, tmp_zip, tmpdir)
return tmpdir


@pytest.fixture()
def chrome_db(bookmark_folder):
# compatibility
Expand Down

0 comments on commit 6db1944

Please sign in to comment.