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_delete_rec_index_and_delay_commit(index, delay_commit, input_retval):
"""test delete rec, index and delay commit."""
bdb = BukuDb()
bdb_dc = BukuDb() # instance for delay_commit check.
# Fill bookmark
for bookmark in TEST_BOOKMARKS:
bdb.add_rec(*bookmark)
db_len = len(TEST_BOOKMARKS)
n_index = index
with mock.patch('builtins.input', return_value=input_retval):
res = bdb.delete_rec(index=index, delay_commit=delay_commit)
if n_index < 0:
assert not res
elif n_index > db_len:
assert not res
assert len(bdb.get_rec_all()) == db_len
def test_delete_rec_index_and_delay_commit(index, delay_commit, input_retval):
"""test delete rec, index and delay commit."""
bdb = BukuDb()
bdb_dc = BukuDb() # instance for delay_commit check.
# Fill bookmark
for bookmark in TEST_BOOKMARKS:
bdb.add_rec(*bookmark)
db_len = len(TEST_BOOKMARKS)
n_index = index
with mock.patch('builtins.input', return_value=input_retval):
res = bdb.delete_rec(index=index, delay_commit=delay_commit)
if n_index < 0:
assert not res
elif n_index > db_len:
assert not res
def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit, input_retval):
"""test delete rec, range and delay commit."""
bdb = BukuDb()
bdb_dc = BukuDb() # instance for delay_commit check.
index = 0
is_range = True
# Fill bookmark
for bookmark in TEST_BOOKMARKS:
bdb.add_rec(*bookmark)
db_len = len(TEST_BOOKMARKS)
# use normalized high and low variable
n_low, n_high = normalize_range(db_len=db_len, low=low, high=high)
exp_res = True
if n_high > db_len >= n_low:
exp_db_len = db_len - (db_len + 1 - n_low)
elif n_high == n_low > db_len:
exp_db_len = db_len
'http://www.vim.org/scripts/script.php?script_id=4641',
(
'mlessnau_case - "in-case" selection, deletion and substitution '
'for underscore, camel, mixed case : vim online',
None, None, 0, 0
)
],
]
)
def test_network_handler_with_url(url, exp_res):
"""test func."""
import buku
import urllib3
buku.urllib3 = urllib3
buku.myproxy = None
res = buku.network_handler(url)
if urlparse(url).netloc == 'www.google.ru':
temp_res = [res[0].split(" - ")[0], ]
temp_res.extend(res[1:])
res = tuple(temp_res)
assert res == exp_res
def test_search_and_open_in_broswer_by_range(self):
# adding bookmarks
for bookmark in self.bookmarks:
self.bdb.add_rec(*bookmark)
# simulate user input, select range of indices 1-3
index_range = '1-%s' % len(self.bookmarks)
with mock.patch('builtins.input', side_effect=[index_range]):
with mock.patch('buku.browse') as mock_browse:
try:
# search the db with keywords from each bookmark
# searching using the first tag from bookmarks
get_first_tag = lambda x: x[2].split(',')[1]
results = self.bdb.searchdb([get_first_tag(bm) for bm in self.bookmarks])
prompt(self.bdb, results)
except StopIteration:
# catch exception thrown by reaching the end of the side effect iterable
pass
# collect arguments passed to browse
arg_list = [args[0] for args, _ in mock_browse.call_args_list]
# expect a list of one-tuples that are bookmark URLs
expected = [x[0] for x in self.bookmarks]
# checking if browse called with expected arguments
self.assertEqual(arg_list, expected)
def test_search_and_open_all_in_browser(self):
# adding bookmarks
for bookmark in self.bookmarks:
self.bdb.add_rec(*bookmark)
# simulate user input, select 'a' to open all bookmarks in results
with mock.patch('builtins.input', side_effect=['a']):
with mock.patch('buku.browse') as mock_browse:
try:
# search the db with keywords from each bookmark
# searching using the first tag from bookmarks
get_first_tag = lambda x: x[2].split(',')[1]
results = self.bdb.searchdb([get_first_tag(bm) for bm in self.bookmarks[:2]])
prompt(self.bdb, results)
except StopIteration:
# catch exception thrown by reaching the end of the side effect iterable
pass
# collect arguments passed to browse
arg_list = [args[0] for args, _ in mock_browse.call_args_list]
# expect a list of one-tuples that are bookmark URLs
expected = [x[0] for x in self.bookmarks][:2]
# checking if browse called with expected arguments
self.assertEqual(arg_list, expected)
desc_text = '\n'
elif desc == '':
desc_text = '-'
else:
desc_text = desc
if title_in is None:
title_text = ''
elif title_in == '':
title_text = '-'
else:
title_text = title_in
if tags_in is None:
with pytest.raises(AttributeError):
res = buku.to_temp_file_content(url, title_in, tags_in, desc)
return
res = buku.to_temp_file_content(url, title_in, tags_in, desc)
lines = """# Lines beginning with "#" will be stripped.
# Add URL in next line (single line).{}
# Add TITLE in next line (single line). Leave blank to web fetch, "-" for no title.{}
# Add comma-separated TAGS in next line (single line).{}
# Add COMMENTS in next line(s). Leave blank to web fetch, "-" for no comments.{}""".format(
''.join(['\n', url]) if url is not None else '',
''.join(['\n', title_text]),
''.join(['\n', ','.join([x for x in tags_in.split(',') if x])]) if tags_in else '\n',
''.join(['\n', desc_text])
)
assert res == lines
import buku
if desc is None:
desc_text = '\n'
elif desc == '':
desc_text = '-'
else:
desc_text = desc
if title_in is None:
title_text = ''
elif title_in == '':
title_text = '-'
else:
title_text = title_in
if tags_in is None:
with pytest.raises(AttributeError):
res = buku.to_temp_file_content(url, title_in, tags_in, desc)
return
res = buku.to_temp_file_content(url, title_in, tags_in, desc)
lines = """# Lines beginning with "#" will be stripped.
# Add URL in next line (single line).{}
# Add TITLE in next line (single line). Leave blank to web fetch, "-" for no title.{}
# Add comma-separated TAGS in next line (single line).{}
# Add COMMENTS in next line(s). Leave blank to web fetch, "-" for no comments.{}""".format(
''.join(['\n', url]) if url is not None else '',
''.join(['\n', title_text]),
''.join(['\n', ','.join([x for x in tags_in.split(',') if x])]) if tags_in else '\n',
''.join(['\n', desc_text])
)
assert res == lines
def test_update_rec_update_all_bookmark(caplog, read_in_retval):
"""test method."""
if (sys.version_info.major, sys.version_info.minor) == (3, 8):
caplog.set_level(logging.DEBUG)
with mock.patch('buku.read_in', return_value=read_in_retval):
import buku
bdb = buku.BukuDb()
res = bdb.update_rec(index=0, tags_in='tags1')
if read_in_retval != 'y':
assert not res
return
assert res
try:
if (sys.version_info.major, sys.version_info.minor) == (3, 8):
assert caplog.records[0].getMessage() == \
'update_rec query: "UPDATE bookmarks SET tags = ?", args: [\',tags1,\']'
else:
assert caplog.records[0].getMessage() == \
'query: "UPDATE bookmarks SET tags = ?", args: [\',tags1\']'
assert caplog.records[0].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), (3, 7)]:
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 == ''