From 893c02747f1b6559450074a171c6fdb291121869 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Wed, 22 Mar 2017 17:52:25 +0800 Subject: [PATCH 01/27] new: test: additional data test set. --- tests/test_bukuDb.py | 75 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 10 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index c992e6eb..af08adba 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -493,9 +493,9 @@ def test_compactdb(setup): @pytest.mark.parametrize( 'index, low, high, is_range', product( - [-1, 0], - [-1, 0], - [-1, 0], + [-1, 0, 'max'], + [-1, 0, 'max'], + [-1, 0, 'max'], [True, False] ) ) @@ -508,15 +508,28 @@ def test_delete_rec_negative(setup, index, low, high, is_range): bdb.add_rec(*bookmark) db_len = len(TEST_BOOKMARKS) + # normalize vars + n_index, n_high, n_low = normalize_index_and_range( + db_len=db_len, index=index, low=low, high=high) + 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]): + if is_range and any([n_low < 0, n_high < 0]): assert not res assert db_len == len(bdb.get_rec_all()) - elif not is_range and index < 0: + elif not is_range and n_index < 0: assert not res assert db_len == len(bdb.get_rec_all()) - else: + elif is_range and n_low != 0 and n_low == n_high: + assert res + assert db_len - 1 == len(bdb.get_rec_all) + elif is_range and n_low != 0: + assert res + assert db_len - (n_high - n_low) == len(bdb.get_rec_all) + elif not is_range and n_index != 0: + assert res + assert db_len - 1 == len(bdb.get_rec_all) + else: # (index == 0 and not is_range) or (low == 0 and is_range) assert res with pytest.raises(sqlite3.OperationalError): assert len(bdb.get_rec_all()) == 0 @@ -530,12 +543,16 @@ def test_delete_rec_negative(setup, index, low, high, is_range): product( [True, False], ['y', 'n'], - [0, 1], - [0, 1], + [0, 1, 'max'], + [0, 1, "max"], ) ) def test_delete_rec_cleardb(setup, is_range, input_retval, high, low): """test scenario when meet cleardb function.""" + # skip (non-zero, max) pair for is_range + if is_range and ((low != 0 and high == 'max') or (low == 0 and high != 'max')): + return + bdb = BukuDb() index = 0 @@ -544,9 +561,11 @@ def test_delete_rec_cleardb(setup, is_range, input_retval, high, low): bdb.add_rec(*bookmark) db_len = len(TEST_BOOKMARKS) + _, n_low, n_high = normalize_index_and_range(db_len=db_len, low=low, high=high) + 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: + if is_range and n_high == 1 and n_low == 1: assert res assert len(bdb.get_rec_all()) == db_len - 1 elif is_range and input_retval != 'y': @@ -621,7 +640,7 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit): @pytest.mark.parametrize( 'index, delay_commit', product( - [1, 1000], + [1, 1000, "max"], [True, False], ) ) @@ -635,6 +654,8 @@ def test_delete_rec_index_and_delay_commit(index, delay_commit): bdb.add_rec(*bookmark) db_len = len(TEST_BOOKMARKS) + n_index, _, _ = normalize_index_and_range(db_len=db_len, index=index) + res = bdb.delete_rec(index=index, delay_commit=delay_commit) if index > db_len: @@ -693,5 +714,39 @@ def split_and_test_membership(a, b): def inclusive_range(start, end): return range(start, end + 1) + +def normalize_index_and_range(db_len, index=0, low=0, high=0): + """normalize index and range. + + Args: + db_len (int): database length. + index (int): index. + low (int): low limit. + high (int): high limit. + + Returns: + Tuple contain following normalized variables (index, low, high) + """ + if index == 'max': + n_index = db_len + if low == 'max' and high == 'max': + n_low = db_len + n_high = db_len + 1 + elif low == 'max' and high != 'max': + n_low = high + n_high = db_len + 1 + elif low != 'max' and high == 'max': + n_low = low + n_high = db_len + 1 + else: + n_low = low + n_high = high + + if n_high < n_low: + n_high, n_low = n_low, n_high + + return (n_index, n_low, n_high) + + if __name__ == "__main__": unittest.main() From 951ca270c40609a2877d912c7db215f9fa35a8db Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Wed, 22 Mar 2017 18:15:25 +0800 Subject: [PATCH 02/27] new: test: test on non integer. --- tests/test_bukuDb.py | 50 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index af08adba..8ed808d4 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -702,6 +702,40 @@ def test_get_delete_rec_on_empty_database(setup, index, is_range, low, high): # teardown os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH + +@pytest.mark.parametrize( + 'index, low, high, is_range', + product( + ['a', "max"], + ['a', "max"], + ['a', "max"], + [True, False], + ) +) +def test_delete_rec_on_non_interger(index, low, high, is_range): + """test delete rec on non integer arg.""" + bdb = BukuDb() + + def is_non_integer_and_not_max(arg): + return not isinstance(arg, int) and arg != 'max' + + for bookmark in TEST_BOOKMARKS: + bdb.add_rec(*bookmark) + db_len = len(TEST_BOOKMARKS) + + res = bdb.delete_rec(index, is_range, low, high) + + if is_range and any([is_non_integer_and_not_max(low), is_non_integer_and_not_max(high)]): + assert not res + assert len(bdb.get_rec_all()) == db_len + elif not is_range and is_non_integer_and_not_max(index): + assert not res + assert len(bdb.get_rec_all()) == db_len + else: + assert res + assert len(bdb.get_rec_all()) == db_len - 1 + + # Helper functions for testcases @@ -727,6 +761,17 @@ def normalize_index_and_range(db_len, index=0, low=0, high=0): Returns: Tuple contain following normalized variables (index, low, high) """ + range_require_comparison = True + # don't deal with non instance of the variable. + if not isinstance(index, int) and index != 'max': + n_index = index + if not isinstance(low, int) and low != 'max': + n_low = low + range_require_comparison = False + if not isinstance(high, int) and high != 'max': + n_high = high + range_require_comparison = False + if index == 'max': n_index = db_len if low == 'max' and high == 'max': @@ -742,8 +787,9 @@ def normalize_index_and_range(db_len, index=0, low=0, high=0): n_low = low n_high = high - if n_high < n_low: - n_high, n_low = n_low, n_high + if range_require_comparison: + if n_high < n_low: + n_high, n_low = n_low, n_high return (n_index, n_low, n_high) From 335503ecdf885b0ede8470c7f733a392be2855cf Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Wed, 22 Mar 2017 18:16:14 +0800 Subject: [PATCH 03/27] chg: test: rename function for consistency. --- tests/test_bukuDb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 8ed808d4..5192752f 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -684,7 +684,7 @@ def test_delete_rec_index_and_delay_commit(index, delay_commit): (0, False, 0, 0), ] ) -def test_get_delete_rec_on_empty_database(setup, index, is_range, low, high): +def test_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'): From f109e482c853cdab54ab652ac478880479e7bcb3 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Thu, 23 Mar 2017 15:08:52 +0800 Subject: [PATCH 04/27] chg: test: change normalize function. --- tests/test_bukuDb.py | 57 ++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 5192752f..f21113e8 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -509,8 +509,8 @@ def test_delete_rec_negative(setup, index, low, high, is_range): db_len = len(TEST_BOOKMARKS) # normalize vars - n_index, n_high, n_low = normalize_index_and_range( - db_len=db_len, index=index, low=low, high=high) + n_index = normalize_index(db_len, index) + n_low, n_high = normalize_range(db_len=db_len, low=low, high=high) with mock.patch('builtins.input', return_value='y'): res = bdb.delete_rec(index=index, low=low, high=high, is_range=is_range) @@ -561,7 +561,7 @@ def test_delete_rec_cleardb(setup, is_range, input_retval, high, low): bdb.add_rec(*bookmark) db_len = len(TEST_BOOKMARKS) - _, n_low, n_high = normalize_index_and_range(db_len=db_len, low=low, high=high) + n_low, n_high = normalize_range(db_len=db_len, low=low, high=high) with mock.patch('builtins.input', return_value=input_retval): res = bdb.delete_rec(index=index, low=low, high=high, is_range=is_range) @@ -608,10 +608,7 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit): 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 + n_low, n_high = normalize_range(db_len=db_len, low=low, high=high) exp_res = True if n_high > db_len and n_low <= db_len: @@ -654,7 +651,7 @@ def test_delete_rec_index_and_delay_commit(index, delay_commit): bdb.add_rec(*bookmark) db_len = len(TEST_BOOKMARKS) - n_index, _, _ = normalize_index_and_range(db_len=db_len, index=index) + n_index = normalize_index(db_len=db_len, index=index) res = bdb.delete_rec(index=index, delay_commit=delay_commit) @@ -749,49 +746,63 @@ def inclusive_range(start, end): return range(start, end + 1) -def normalize_index_and_range(db_len, index=0, low=0, high=0): - """normalize index and range. +def normalize_index(db_len, index): + """normalize index. Args: db_len (int): database length. index (int): index. + + Returns: + Normalized index. + """ + if not isinstance(index, int) and index != 'max': + n_index = index + return n_index + if index == 'max': + return db_len + return index + + +def normalize_range(db_len, low, high): + """normalize index and range. + + Args: + db_len (int): database length. low (int): low limit. high (int): high limit. Returns: - Tuple contain following normalized variables (index, low, high) + Tuple contain following normalized variables (low, high) """ - range_require_comparison = True + require_comparison = True # don't deal with non instance of the variable. - if not isinstance(index, int) and index != 'max': - n_index = index if not isinstance(low, int) and low != 'max': n_low = low - range_require_comparison = False + require_comparison = False if not isinstance(high, int) and high != 'max': n_high = high - range_require_comparison = False + require_comparison = False - if index == 'max': - n_index = db_len + max_value = db_len + 1 if low == 'max' and high == 'max': n_low = db_len - n_high = db_len + 1 + n_high = max_value elif low == 'max' and high != 'max': n_low = high - n_high = db_len + 1 + n_high = max_value elif low != 'max' and high == 'max': n_low = low - n_high = db_len + 1 + n_high = max_value else: n_low = low n_high = high - if range_require_comparison: + if require_comparison: if n_high < n_low: n_high, n_low = n_low, n_high - return (n_index, n_low, n_high) + return (n_low, n_high) if __name__ == "__main__": From f7ea8c17eeabd0767794d5cd54e54af4a767139d Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Fri, 24 Mar 2017 10:11:34 +0800 Subject: [PATCH 05/27] chg: test: change max value for high var --- tests/test_bukuDb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index f21113e8..9eb52352 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -784,7 +784,7 @@ def normalize_range(db_len, low, high): n_high = high require_comparison = False - max_value = db_len + 1 + max_value = db_len if low == 'max' and high == 'max': n_low = db_len n_high = max_value From 7f0655ff01f8d63c23cbff2334dcf4391eaf610e Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Fri, 24 Mar 2017 10:15:01 +0800 Subject: [PATCH 06/27] fix: test: use normalized index --- tests/test_bukuDb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 9eb52352..166e7eaf 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -655,7 +655,7 @@ def test_delete_rec_index_and_delay_commit(index, delay_commit): res = bdb.delete_rec(index=index, delay_commit=delay_commit) - if index > db_len: + if n_index > db_len: assert not res assert len(bdb.get_rec_all()) == db_len else: From a42e59c02432841fdebbc89360c4a93b8ded4d22 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sat, 25 Mar 2017 21:45:26 +0800 Subject: [PATCH 07/27] fix: test: remove 'max' as valid value --- tests/test_bukuDb.py | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 166e7eaf..0c09db33 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -509,7 +509,7 @@ def test_delete_rec_negative(setup, index, low, high, is_range): db_len = len(TEST_BOOKMARKS) # normalize vars - n_index = normalize_index(db_len, index) + n_index = index n_low, n_high = normalize_range(db_len=db_len, low=low, high=high) with mock.patch('builtins.input', return_value='y'): @@ -651,7 +651,7 @@ def test_delete_rec_index_and_delay_commit(index, delay_commit): bdb.add_rec(*bookmark) db_len = len(TEST_BOOKMARKS) - n_index = normalize_index(db_len=db_len, index=index) + n_index = index res = bdb.delete_rec(index=index, delay_commit=delay_commit) @@ -746,24 +746,6 @@ def inclusive_range(start, end): return range(start, end + 1) -def normalize_index(db_len, index): - """normalize index. - - Args: - db_len (int): database length. - index (int): index. - - Returns: - Normalized index. - """ - if not isinstance(index, int) and index != 'max': - n_index = index - return n_index - if index == 'max': - return db_len - return index - - def normalize_range(db_len, low, high): """normalize index and range. @@ -777,10 +759,10 @@ def normalize_range(db_len, low, high): """ require_comparison = True # don't deal with non instance of the variable. - if not isinstance(low, int) and low != 'max': + if not isinstance(low, int): n_low = low require_comparison = False - if not isinstance(high, int) and high != 'max': + if not isinstance(high, int): n_high = high require_comparison = False From dd184944d32213547355cdf7a828af97b0de40d2 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 14:30:08 +0800 Subject: [PATCH 08/27] chg: test: use hypothesis to test delete_rec index --- tests/test_bukuDb.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 0c09db33..8b736bcf 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -5,13 +5,16 @@ import os import re import sqlite3 +import sys from genericpath import exists from itertools import product from tempfile import TemporaryDirectory +from hypothesis import given +from hypothesis import strategies as st +from unittest import mock as mock import pytest import unittest -from unittest import mock as mock from buku import BukuDb, parse_tags, prompt @@ -634,14 +637,8 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit): os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH -@pytest.mark.parametrize( - 'index, delay_commit', - product( - [1, 1000, "max"], - [True, False], - ) -) -def test_delete_rec_index_and_delay_commit(index, delay_commit): +@given(index=st.integers(), delay_commit=st.booleans(), input_retval=st.booleans()) +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. @@ -653,9 +650,20 @@ def test_delete_rec_index_and_delay_commit(index, delay_commit): n_index = index - res = bdb.delete_rec(index=index, delay_commit=delay_commit) + if index.bit_length() > 63: + with pytest.raises(OverflowError): + bdb.delete_rec(index=index, delay_commit=delay_commit) + return + + with mock.patch('builtins.input', return_value=input_retval): + res = bdb.delete_rec(index=index, delay_commit=delay_commit) - if n_index > db_len: + if n_index < 0: + assert not res + elif n_index > db_len: + assert not res + assert len(bdb.get_rec_all()) == db_len + elif index == 0 and input_retval != 'y': assert not res assert len(bdb.get_rec_all()) == db_len else: From 5c10f988bab9e640113c136db9836c87558d3940 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 14:31:20 +0800 Subject: [PATCH 09/27] new: test: add hypothesis package --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 2290a23b..77a4a3be 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ 'REQUESTS': ['requests'] }, test_suite='tests', - tests_require=['pytest-cov', 'pytest-catchlog'], + tests_require=['pytest-cov', 'pytest-catchlog', 'hypothesis==3.7.0'], keywords='cli bookmarks tag utility', classifiers=[ 'Development Status :: 5 - Production/Stable', From 2135aa3eb45ded50657f57b0a17e86421b32919c Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 15:51:00 +0800 Subject: [PATCH 10/27] chg: test: use hypothesis to test delete_rec index --- tests/test_bukuDb.py | 50 +++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 8b736bcf..a822aed7 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -5,12 +5,11 @@ import os import re import sqlite3 -import sys from genericpath import exists from itertools import product from tempfile import TemporaryDirectory -from hypothesis import given +from hypothesis import given, example, settings, Verbosity from hypothesis import strategies as st from unittest import mock as mock import pytest @@ -590,15 +589,15 @@ def test_delete_rec_cleardb(setup, is_range, input_retval, high, low): os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH -@pytest.mark.parametrize( - 'low, high, delay_commit', - product( - [1, 1000], - [1, 1000], - [True, False], - ) +@given( + low=st.integers(), + high=st.integers(), + delay_commit=st.booleans(), + input_retval=st.characters() ) -def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit): +@example(low=0, high=0, delay_commit=False, input_retval='y') +@settings(verbosity=Verbosity.verbose) +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. @@ -624,8 +623,35 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit): 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) + with mock.patch('builtins.input', return_value=input_retval): + res = bdb.delete_rec( + index=index, low=low, high=high, is_range=is_range, delay_commit=delay_commit) + + if (low == 0 or high == 0) and input_retval != 'y': + assert not res + assert len(bdb_dc.get_rec_all()) == db_len + # teardown + os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH + return + elif (low == 0 or high == 0) and input_retval == 'y': + assert res == exp_res + with pytest.raises(sqlite3.OperationalError): + bdb.get_rec_all() + # teardown + os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH + return + elif n_low > db_len and n_low > 0: + assert not res + assert len(bdb_dc.get_rec_all()) == db_len + # teardown + os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH + return + elif n_low < 0: + assert not res + assert len(bdb_dc.get_rec_all()) == db_len + # teardown + os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH + return assert res == exp_res assert len(bdb.get_rec_all()) == exp_db_len if delay_commit: From b8ec8154bb4147201a5ab6acda797b7cb5c094b6 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 15:57:59 +0800 Subject: [PATCH 11/27] chg: test: add hypothesis to travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8ffcbb8e..c9e64e81 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ python: - "3.4" - "3.5" before_install: - - pip install pytest pytest-cov pytest-catchlog + - pip install pytest pytest-cov pytest-catchlog hypothesis install: "pip install -r requirements.txt" addons: apt: From fa51f37950ff28023dc3e6af601b43c42c403157 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 16:25:34 +0800 Subject: [PATCH 12/27] chg: test: limit integer test. --- tests/test_bukuDb.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index a822aed7..98dfd3c1 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -590,8 +590,8 @@ def test_delete_rec_cleardb(setup, is_range, input_retval, high, low): @given( - low=st.integers(), - high=st.integers(), + low=st.integers(min_value=-10, max_value=10), + high=st.integers(min_value=-10, max_value=10), delay_commit=st.booleans(), input_retval=st.characters() ) From d75a472844ad6b1351741cad340d22b977c5cf8f Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 16:27:57 +0800 Subject: [PATCH 13/27] chg: dev: remove unused test --- tests/test_bukuDb.py | 97 -------------------------------------------- 1 file changed, 97 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 98dfd3c1..d9a48d1c 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -492,103 +492,6 @@ def test_compactdb(setup): assert bdb.get_rec_by_id(3) is None -@pytest.mark.parametrize( - 'index, low, high, is_range', - product( - [-1, 0, 'max'], - [-1, 0, 'max'], - [-1, 0, 'max'], - [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) - - # normalize vars - n_index = index - n_low, n_high = normalize_range(db_len=db_len, low=low, high=high) - - 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([n_low < 0, n_high < 0]): - assert not res - assert db_len == len(bdb.get_rec_all()) - elif not is_range and n_index < 0: - assert not res - assert db_len == len(bdb.get_rec_all()) - elif is_range and n_low != 0 and n_low == n_high: - assert res - assert db_len - 1 == len(bdb.get_rec_all) - elif is_range and n_low != 0: - assert res - assert db_len - (n_high - n_low) == len(bdb.get_rec_all) - elif not is_range and n_index != 0: - assert res - assert db_len - 1 == len(bdb.get_rec_all) - else: # (index == 0 and not is_range) or (low == 0 and is_range) - 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, 'max'], - [0, 1, "max"], - ) -) -def test_delete_rec_cleardb(setup, is_range, input_retval, high, low): - """test scenario when meet cleardb function.""" - # skip (non-zero, max) pair for is_range - if is_range and ((low != 0 and high == 'max') or (low == 0 and high != 'max')): - return - - bdb = BukuDb() - index = 0 - - # Fill bookmark - for bookmark in TEST_BOOKMARKS: - bdb.add_rec(*bookmark) - db_len = len(TEST_BOOKMARKS) - - n_low, n_high = normalize_range(db_len=db_len, low=low, high=high) - - 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 n_high == 1 and n_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 - - @given( low=st.integers(min_value=-10, max_value=10), high=st.integers(min_value=-10, max_value=10), From d05561aac11440252b863064bc901be22df2c7c5 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 16:54:45 +0800 Subject: [PATCH 14/27] fix: test: fix test on non integer. --- tests/test_bukuDb.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index d9a48d1c..e8dd2fae 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -640,9 +640,9 @@ def test_delete_rec_on_empty_database(setup, index, is_range, low, high): @pytest.mark.parametrize( 'index, low, high, is_range', product( - ['a', "max"], - ['a', "max"], - ['a', "max"], + ['a', 1], + ['a', 1], + ['a', 1], [True, False], ) ) @@ -650,24 +650,20 @@ def test_delete_rec_on_non_interger(index, low, high, is_range): """test delete rec on non integer arg.""" bdb = BukuDb() - def is_non_integer_and_not_max(arg): - return not isinstance(arg, int) and arg != 'max' - for bookmark in TEST_BOOKMARKS: bdb.add_rec(*bookmark) db_len = len(TEST_BOOKMARKS) - res = bdb.delete_rec(index, is_range, low, high) - - if is_range and any([is_non_integer_and_not_max(low), is_non_integer_and_not_max(high)]): - assert not res - assert len(bdb.get_rec_all()) == db_len - elif not is_range and is_non_integer_and_not_max(index): + if is_range and not (isinstance(low, int) and isinstance(high, int)): + with pytest.raises(TypeError): + bdb.delete_rec(index=index, low=low, high=high, is_range=is_range) + return + elif not is_range and not isinstance(index, int): + res = bdb.delete_rec(index=index, low=low, high=high, is_range=is_range) assert not res assert len(bdb.get_rec_all()) == db_len else: - assert res - assert len(bdb.get_rec_all()) == db_len - 1 + assert bdb.delete_rec(index=index, low=low, high=high, is_range=is_range) # Helper functions for testcases From 609690469fa5f424e6e949ebe57a3632b5c4b9c8 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 17:39:48 +0800 Subject: [PATCH 15/27] new: test: add big integer test on range in delete_rec method. --- tests/test_bukuDb.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index e8dd2fae..1065f80c 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -2,6 +2,7 @@ # # Unit test cases for buku # +import sys import os import re import sqlite3 @@ -566,6 +567,29 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit, input os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH +@pytest.mark.skip(reason='Test freeze') +@pytest.mark.parametrize( + 'low, high', + product( + [1, sys.maxsize], + [1, sys.maxsize], + ) +) +def test_delete_rec_range_and_big_int(setup, low, high): + """test delete rec, range and big integer.""" + bdb = BukuDb() + index = 0 + is_range = True + + # Fill bookmark + for bookmark in TEST_BOOKMARKS: + bdb.add_rec(*bookmark) + # db_len = len(TEST_BOOKMARKS) + + res = bdb.delete_rec(index=index, low=low, high=high, is_range=is_range) + assert res + + @given(index=st.integers(), delay_commit=st.booleans(), input_retval=st.booleans()) def test_delete_rec_index_and_delay_commit(index, delay_commit, input_retval): """test delete rec, index and delay commit.""" From d82a66021eb2b1e56fddf55c80c0d3999d72297e Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 18:10:57 +0800 Subject: [PATCH 16/27] fix: test: fix high low diff --- tests/test_bukuDb.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 1065f80c..b18b58e5 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -500,7 +500,6 @@ def test_compactdb(setup): input_retval=st.characters() ) @example(low=0, high=0, delay_commit=False, input_retval='y') -@settings(verbosity=Verbosity.verbose) def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit, input_retval): """test delete rec, range and delay commit.""" bdb = BukuDb() @@ -525,7 +524,7 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit, input 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) + exp_db_len = db_len - (n_high + 1 - n_low) with mock.patch('builtins.input', return_value=input_retval): res = bdb.delete_rec( From 46c5e4ecac9c2c859b62d171446aa21cef29f7f3 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 18:25:44 +0800 Subject: [PATCH 17/27] fix: test: skip only for python<3.5 --- tests/test_bukuDb.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index b18b58e5..e3e0a854 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -38,6 +38,8 @@ "a case for replace_tag test"], ] +only_python_3_5 = pytest.mark.skipif(sys.version_info < (3, 5), reason="requires python3.5") + @pytest.fixture() def setup(): @@ -566,7 +568,7 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit, input os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH -@pytest.mark.skip(reason='Test freeze') +@only_python_3_5 @pytest.mark.parametrize( 'low, high', product( From 4b2e183d6a2e23c2d156648dbefad95473faf820 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 19:41:35 +0800 Subject: [PATCH 18/27] chg: test: change test_delete_rec_range_and_big_int - remove skip - use constant value instead sys.maxsize - fix assert --- tests/test_bukuDb.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index e3e0a854..94e19280 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -2,15 +2,15 @@ # # Unit test cases for buku # -import sys import os import re import sqlite3 +import math from genericpath import exists from itertools import product from tempfile import TemporaryDirectory -from hypothesis import given, example, settings, Verbosity +from hypothesis import given, example from hypothesis import strategies as st from unittest import mock as mock import pytest @@ -22,6 +22,7 @@ TEST_TEMP_DIR_PATH = TEST_TEMP_DIR_OBJ.name TEST_TEMP_DBDIR_PATH = os.path.join(TEST_TEMP_DIR_PATH, 'buku') TEST_TEMP_DBFILE_PATH = os.path.join(TEST_TEMP_DBDIR_PATH, 'bookmarks.db') +MAX_SQLITE_INT = math.pow(2, 47) TEST_BOOKMARKS = [ ['http://slashdot.org', @@ -38,8 +39,6 @@ "a case for replace_tag test"], ] -only_python_3_5 = pytest.mark.skipif(sys.version_info < (3, 5), reason="requires python3.5") - @pytest.fixture() def setup(): @@ -568,12 +567,11 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit, input os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH -@only_python_3_5 @pytest.mark.parametrize( 'low, high', product( - [1, sys.maxsize], - [1, sys.maxsize], + [1, int(MAX_SQLITE_INT + 1)], + [1, int(MAX_SQLITE_INT + 1)], ) ) def test_delete_rec_range_and_big_int(setup, low, high): @@ -585,9 +583,11 @@ def test_delete_rec_range_and_big_int(setup, low, high): # Fill bookmark for bookmark in TEST_BOOKMARKS: bdb.add_rec(*bookmark) - # db_len = len(TEST_BOOKMARKS) - + db_len = len(TEST_BOOKMARKS) res = bdb.delete_rec(index=index, low=low, high=high, is_range=is_range) + if high > db_len and low > db_len: + assert not res + return assert res From 6544a5a9707c60fc106c9449e6c1620b580ca4b4 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 20:40:48 +0800 Subject: [PATCH 19/27] chg: test: use setup.py to manage test package instead travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c9e64e81..cb95c9d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ python: - "3.4" - "3.5" before_install: - - pip install pytest pytest-cov pytest-catchlog hypothesis + - python setup.py develop install: "pip install -r requirements.txt" addons: apt: From c69f461467041d1905958ac0774b86d6dd7c29b2 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 21:05:29 +0800 Subject: [PATCH 20/27] chg: test: add tests extras on setup.py --- setup.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 77a4a3be..3ef33346 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,8 @@ with open('README.md', encoding='utf-8') as f: long_description = f.read() +tests_require = ['pytest-cov', 'pytest-catchlog', 'hypothesis==3.7.0'], + setup( name='buku', version=version, @@ -33,10 +35,11 @@ 'HTTP': ['urllib3'], 'CRYPTO': ['cryptography'], 'HTML': ['beautifulsoup4'], - 'REQUESTS': ['requests'] + 'REQUESTS': ['requests'], + 'tests': tests_require, }, test_suite='tests', - tests_require=['pytest-cov', 'pytest-catchlog', 'hypothesis==3.7.0'], + tests_require=tests_require, keywords='cli bookmarks tag utility', classifiers=[ 'Development Status :: 5 - Production/Stable', From 809867d02a908aa7ade4a23eae4d29c1f8f8dc9b Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 21:05:43 +0800 Subject: [PATCH 21/27] chg: test: change install test package. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cb95c9d6..3199ef53 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ python: - "3.4" - "3.5" before_install: - - python setup.py develop + - "pip install -e . [tests]" install: "pip install -r requirements.txt" addons: apt: From df23c62e0b6537840254eda84dc883c38f5de24f Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sun, 26 Mar 2017 21:12:54 +0800 Subject: [PATCH 22/27] fix: test: fix whitespace --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3199ef53..1c5964c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ python: - "3.4" - "3.5" before_install: - - "pip install -e . [tests]" + - "pip install -e .[tests]" install: "pip install -r requirements.txt" addons: apt: From 1eba948c13ec954603b89afc1c8a1e0618aa649a Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Mon, 27 Mar 2017 22:17:19 +0800 Subject: [PATCH 23/27] fix: test: MAX_SQLITE_INT value --- tests/test_bukuDb.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 94e19280..ab33902b 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -22,7 +22,7 @@ TEST_TEMP_DIR_PATH = TEST_TEMP_DIR_OBJ.name TEST_TEMP_DBDIR_PATH = os.path.join(TEST_TEMP_DIR_PATH, 'buku') TEST_TEMP_DBFILE_PATH = os.path.join(TEST_TEMP_DBDIR_PATH, 'bookmarks.db') -MAX_SQLITE_INT = math.pow(2, 47) +MAX_SQLITE_INT = int(math.pow(2, 63) - 1) TEST_BOOKMARKS = [ ['http://slashdot.org', @@ -570,8 +570,8 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit, input @pytest.mark.parametrize( 'low, high', product( - [1, int(MAX_SQLITE_INT + 1)], - [1, int(MAX_SQLITE_INT + 1)], + [1, MAX_SQLITE_INT + 1], + [1, MAX_SQLITE_INT + 1], ) ) def test_delete_rec_range_and_big_int(setup, low, high): From 1eeac89ab490d2c2a315e673494f956088f35aa1 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Mon, 27 Mar 2017 23:06:08 +0800 Subject: [PATCH 24/27] chg: test: skip test for python<3.5 --- tests/test_bukuDb.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index ab33902b..d4001e9c 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -39,6 +39,8 @@ "a case for replace_tag test"], ] +only_python_3_5 = pytest.mark.skipif(sys.version_info < (3, 5), reason="requires python3.5") + @pytest.fixture() def setup(): @@ -567,6 +569,7 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit, input os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH +@only_python_3_5 @pytest.mark.parametrize( 'low, high', product( From 0b2e25a8cd016db361b01ac57512f0ea32d51232 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Mon, 27 Mar 2017 23:10:34 +0800 Subject: [PATCH 25/27] fix: test: fix import --- tests/test_bukuDb.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index d4001e9c..4420677c 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -2,10 +2,11 @@ # # Unit test cases for buku # +import math import os import re import sqlite3 -import math +import sys from genericpath import exists from itertools import product from tempfile import TemporaryDirectory From 3613d7a1a5936e7fd1d3ac5dfa43628852dda173 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Mon, 27 Mar 2017 23:16:23 +0800 Subject: [PATCH 26/27] chg: test: skip Impossible test --- tests/test_bukuDb.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 4420677c..507f69e5 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -571,6 +571,7 @@ def test_delete_rec_range_and_delay_commit(setup, low, high, delay_commit, input @only_python_3_5 +@pytest.mark.skip(reason='Impossible case.') @pytest.mark.parametrize( 'low, high', product( From 2cb8f4d3e4c4c37f704f88cd63222432741bcb8f Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Mon, 27 Mar 2017 23:48:59 +0800 Subject: [PATCH 27/27] chg: test: simplify test_delete_rec_on_non_interger --- tests/test_bukuDb.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 507f69e5..2fdd2423 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -669,12 +669,11 @@ def test_delete_rec_on_empty_database(setup, index, is_range, low, high): @pytest.mark.parametrize( 'index, low, high, is_range', - product( - ['a', 1], - ['a', 1], - ['a', 1], - [True, False], - ) + [ + ['a', 'a', 1, True], + ['a', 'a', 1, False], + ['a', 1, 'a', True], + ] ) def test_delete_rec_on_non_interger(index, low, high, is_range): """test delete rec on non integer arg."""