new: test: test for delete_rec (#132)
* chg: dev: add db instance for delay_commit check * chg: dev: remove unused delete_rec test * chg: test: not check delay commit on empty db test. * chg: test: use simpler precise test for delete_rec * fix: test: change pytest parametrize arg * fix: test: fix instance of BukuDb * fix: test: fix test. * fix: test: logic on expected db len * new: test: test for delete_rec * new: test: test for delete_rec on empty database
This commit is contained in:
parent
4153298a42
commit
aab928c04f
@ -1,19 +0,0 @@
|
||||
appdirs==1.4.1
|
||||
beautifulsoup4==4.5.3
|
||||
buku==2.9
|
||||
cffi==1.9.1
|
||||
click==6.7
|
||||
cryptography==1.7.2
|
||||
Flask==0.12
|
||||
idna==2.2
|
||||
itsdangerous==0.24
|
||||
Jinja2==2.9.5
|
||||
MarkupSafe==0.23
|
||||
packaging==16.8
|
||||
pyasn1==0.2.2
|
||||
pycparser==2.17
|
||||
pyparsing==2.1.10
|
||||
requests==2.13.0
|
||||
six==1.10.0
|
||||
urllib3==1.20
|
||||
Werkzeug==0.11.15
|
@ -1,78 +0,0 @@
|
||||
from buku import BukuDb
|
||||
from flask import Flask, jsonify, request
|
||||
|
||||
|
||||
bukudb = BukuDb()
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route('/api/tags', methods=['GET'])
|
||||
def get_tags():
|
||||
tags = bukudb.get_tag_all()
|
||||
result = {
|
||||
'tags': tags[0]
|
||||
}
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@app.route('/api/bookmarks', methods=['GET', 'POST'])
|
||||
def bookmarks():
|
||||
if request.method == 'GET':
|
||||
bookmarks = bukudb.get_rec_all()
|
||||
result = {
|
||||
'bookmarks': []
|
||||
}
|
||||
for bookmark in bookmarks:
|
||||
result_bookmark = {
|
||||
'url': bookmark[1],
|
||||
'title': bookmark[2],
|
||||
'tags': list(filter(None, bookmark[3].split(','))),
|
||||
'description': bookmark[4]
|
||||
}
|
||||
result['bookmarks'].append(result_bookmark)
|
||||
return jsonify(result)
|
||||
elif request.method == 'POST':
|
||||
result_flag = bukudb.add_rec(request.form['url'], request.form['title'],
|
||||
request.form['tags'], request.form['description'])
|
||||
if result_flag:
|
||||
return jsonify({'status': 0, 'message': 'success'}), 200, {'ContentType': 'application/json'}
|
||||
else:
|
||||
return jsonify({'status': 1, 'message': 'failure'}), 400, {'ContentType': 'application/json'}
|
||||
|
||||
@app.route('/api/bookmarks/<id>', methods=['GET', 'PUT', 'DELETE'])
|
||||
def bookmark_api(id):
|
||||
try:
|
||||
id = int(id)
|
||||
except ValueError:
|
||||
return jsonify({'status': 1, 'message': 'failure'}), 400, {'ContentType': 'application/json'}
|
||||
if request.method == 'GET':
|
||||
bookmark = bukudb.get_rec_by_id(id)
|
||||
if bookmark is not None:
|
||||
result = {
|
||||
'url': bookmark[1],
|
||||
'title': bookmark[2],
|
||||
'tags': list(filter(None, bookmark[3].split(','))),
|
||||
'description': bookmark[4]
|
||||
}
|
||||
return jsonify(result)
|
||||
else:
|
||||
return jsonify({'status': 1, 'message': 'failure'}), 400, {'ContentType': 'application/json'}
|
||||
elif request.method == 'PUT':
|
||||
result_flag = bukudb.update_rec(id, request.form['url'], request.form.get('title'),
|
||||
request.form['tags'], request.form['description'])
|
||||
if result_flag:
|
||||
return jsonify({'status': 0, 'message': 'success'}), 200, {'ContentType': 'application/json'}
|
||||
else:
|
||||
return jsonify({'status': 1, 'message': 'failure'}), 400, {'ContentType': 'application/json'}
|
||||
else:
|
||||
result_flag = bukudb.delete_rec(id)
|
||||
if result_flag:
|
||||
return jsonify({'status': 0, 'message': 'success'}), 200, {'ContentType': 'application/json'}
|
||||
else:
|
||||
return jsonify({'status': 1, 'message': 'failure'}), 400, {'ContentType': 'application/json'}
|
||||
|
||||
def run():
|
||||
app.run(debug=True)
|
||||
|
||||
run()
|
@ -6,6 +6,7 @@ import os
|
||||
import re
|
||||
import sqlite3
|
||||
from genericpath import exists
|
||||
from itertools import product
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
import pytest
|
||||
@ -488,6 +489,198 @@ def test_compactdb(setup):
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'index, low, high, is_range',
|
||||
product(
|
||||
[-1, 0],
|
||||
[-1, 0],
|
||||
[-1, 0],
|
||||
[True, False]
|
||||
)
|
||||
)
|
||||
def test_delete_rec_negative(setup, index, low, high, is_range):
|
||||
"""test when index, low or high is less than 0."""
|
||||
bdb = BukuDb()
|
||||
|
||||
# Fill bookmark
|
||||
for bookmark in TEST_BOOKMARKS:
|
||||
bdb.add_rec(*bookmark)
|
||||
db_len = len(TEST_BOOKMARKS)
|
||||
|
||||
with mock.patch('builtins.input', return_value='y'):
|
||||
res = bdb.delete_rec(index=index, low=low, high=high, is_range=is_range)
|
||||
if is_range and any([low < 0, high < 0]):
|
||||
assert not res
|
||||
assert db_len == len(bdb.get_rec_all())
|
||||
elif not is_range and index < 0:
|
||||
assert not res
|
||||
assert db_len == len(bdb.get_rec_all())
|
||||
else:
|
||||
assert res
|
||||
with pytest.raises(sqlite3.OperationalError):
|
||||
assert len(bdb.get_rec_all()) == 0
|
||||
|
||||
# teardown
|
||||
os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'is_range, input_retval, high, low',
|
||||
product(
|
||||
[True, False],
|
||||
['y', 'n'],
|
||||
[0, 1],
|
||||
[0, 1],
|
||||
)
|
||||
)
|
||||
def test_delete_rec_cleardb(setup, is_range, input_retval, high, low):
|
||||
"""test scenario when meet cleardb function."""
|
||||
bdb = BukuDb()
|
||||
index = 0
|
||||
|
||||
# Fill bookmark
|
||||
for bookmark in TEST_BOOKMARKS:
|
||||
bdb.add_rec(*bookmark)
|
||||
db_len = len(TEST_BOOKMARKS)
|
||||
|
||||
with mock.patch('builtins.input', return_value=input_retval):
|
||||
res = bdb.delete_rec(index=index, low=low, high=high, is_range=is_range)
|
||||
if is_range and high == 1 and low == 1:
|
||||
assert res
|
||||
assert len(bdb.get_rec_all()) == db_len - 1
|
||||
elif is_range and input_retval != 'y':
|
||||
assert not res
|
||||
assert len(bdb.get_rec_all()) == db_len
|
||||
elif is_range:
|
||||
assert res
|
||||
with pytest.raises(sqlite3.OperationalError):
|
||||
bdb.get_rec_all()
|
||||
elif input_retval != 'y':
|
||||
assert not res
|
||||
assert len(bdb.get_rec_all()) == db_len
|
||||
else:
|
||||
assert res
|
||||
with pytest.raises(sqlite3.OperationalError):
|
||||
bdb.get_rec_all()
|
||||
|
||||
# teardown
|
||||
os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'low, high, delay_commit',
|
||||
product(
|
||||
[1, 1000],
|
||||
[1, 1000],
|
||||
[True, False],
|
||||
)
|
||||
)
|
||||
def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit):
|
||||
"""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
|
||||
if low > high:
|
||||
n_low, n_high = high, low
|
||||
else:
|
||||
n_low, n_high = low, high
|
||||
|
||||
exp_res = True
|
||||
if n_high > db_len and n_low <= db_len:
|
||||
exp_db_len = db_len - (db_len + 1 - n_low)
|
||||
elif n_high == n_low and n_low > db_len:
|
||||
exp_db_len = db_len
|
||||
exp_res = False
|
||||
elif n_high == n_low and n_low <= db_len:
|
||||
exp_db_len = db_len - 1
|
||||
else:
|
||||
exp_db_len = db_len - (n_high - n_low)
|
||||
|
||||
res = bdb.delete_rec(
|
||||
index=index, low=low, high=high, is_range=is_range, delay_commit=delay_commit)
|
||||
assert res == exp_res
|
||||
assert len(bdb.get_rec_all()) == exp_db_len
|
||||
if delay_commit:
|
||||
assert len(bdb_dc.get_rec_all()) == db_len
|
||||
else:
|
||||
assert len(bdb_dc.get_rec_all()) == exp_db_len
|
||||
|
||||
# teardown
|
||||
os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'index, delay_commit',
|
||||
product(
|
||||
[1, 1000],
|
||||
[True, False],
|
||||
)
|
||||
)
|
||||
def test_delete_rec_index_and_delay_commit(index, delay_commit):
|
||||
"""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)
|
||||
|
||||
res = bdb.delete_rec(index=index, delay_commit=delay_commit)
|
||||
|
||||
if index > db_len:
|
||||
assert not res
|
||||
assert len(bdb.get_rec_all()) == db_len
|
||||
else:
|
||||
assert res
|
||||
assert len(bdb.get_rec_all()) == db_len - 1
|
||||
if delay_commit:
|
||||
assert len(bdb_dc.get_rec_all()) == db_len
|
||||
else:
|
||||
assert len(bdb_dc.get_rec_all()) == db_len - 1
|
||||
|
||||
# teardown
|
||||
os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'index, is_range, low, high',
|
||||
[
|
||||
# range on non zero index
|
||||
(0, True, 1, 1),
|
||||
# range on zero index
|
||||
(0, True, 0, 0),
|
||||
# zero index only
|
||||
(0, False, 0, 0),
|
||||
]
|
||||
)
|
||||
def test_get_delete_rec_on_empty_database(setup, index, is_range, low, high):
|
||||
"""test delete rec, on empty database."""
|
||||
bdb = BukuDb()
|
||||
with mock.patch('builtins.input', return_value='y'):
|
||||
res = bdb.delete_rec(index, is_range, low, high)
|
||||
|
||||
if (is_range and any([low == 0, high == 0])) or (not is_range and index == 0):
|
||||
assert res
|
||||
# teardown
|
||||
os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
|
||||
return
|
||||
|
||||
if is_range and low > 1 and high > 1:
|
||||
assert not res
|
||||
|
||||
# teardown
|
||||
os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
|
||||
|
||||
# Helper functions for testcases
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user