Last active
March 26, 2016 20:17
-
-
Save kirsn/abda630d8752fd8734ce to your computer and use it in GitHub Desktop.
Experiment with Hypothesis(https://hypothesis.readthedocs.org/en/master/index.html) and testing WEB2PY (http://web2py.com). These tests are only for a few web2py validators.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from decimal import Decimal | |
import json | |
import unittest | |
from hypothesis import given, strategies as st | |
from gluon import validators as vld | |
# JSON Test related code: http://jerith.github.io/pyconza2015-property-based-testing-with-Hypothesis/#/4/3 | |
def nest_data(st_values): | |
return st.lists(st_values) | st.dictionaries(st.text(), st_values) | |
def nested_data(): | |
return st.none() | st.integers() | st.floats(-1e308, 1e308) | st.text() | |
class TestValidators(unittest.TestCase): | |
@given(st.text()) | |
def test_IS_ALPHANUMERIC(self, value): | |
result = vld.IS_ALPHANUMERIC()(value) | |
assert (result == (value, None)) or (result == (value, 'Enter only letters, numbers, and underscore')) | |
@given(maxsize=st.integers(), minsize=st.integers(), value=st.text()) | |
def test_IS_LENGTH(self, maxsize, minsize, value): | |
result = vld.IS_LENGTH(maxsize=maxsize, minsize=minsize)(value) | |
assert (result == (value, None)) or (result == (value, 'Enter from %s to %s characters' % (minsize, maxsize))) | |
@given(maxsize=st.integers(), minsize=st.integers(), value=st.floats(), dot=st.characters()) | |
def test_IS_DECIMAL_IN_RANGE(self, maxsize, minsize, value, dot): | |
result = vld.IS_DECIMAL_IN_RANGE(maximum=maxsize, minimum=minsize, dot=dot)(value) | |
assert (result == (Decimal(value), None)) or (result == (Decimal(value), 'Enter from %s to %s characters' % (minsize, maxsize))) | |
@given( | |
theset=st.lists(st.text()), | |
labels=st.none() | st.lists(st.text()), | |
error_message=st.just('Value not allowed'), | |
multiple=st.booleans(), | |
zero=st.just(''), | |
sort=st.booleans(), | |
value=st.text() | |
) | |
def test_IS_IN_SET(self, theset, labels, error_message, multiple, zero, sort, value): | |
result = vld.IS_IN_SET(theset, labels, error_message, multiple, zero, sort)(value) | |
assert (result == (value, None)) or (result == (value, 'Value not allowed')) | |
@given( | |
data=st.recursive(nested_data(), nest_data) | st.text(), | |
native_json=st.booleans(), | |
error_message=st.just('Invalid json') | |
) | |
def test_IS_JSON(self, data, native_json, error_message): | |
result = vld.IS_JSON(error_message=error_message, native_json=native_json)(json.dumps(data)) | |
js = json.dumps(data) if native_json else data | |
assert result == (js, None) or result == (js, 'Invalid json') | |
if __name__ == '__main__': | |
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Finding files... done. | |
Importing test modules ... done. | |
Falsifying example: test_IS_DECIMAL_IN_RANGE(self=<test_validators.TestValidators testMethod=test_IS_DECIMAL_IN_RANGE>, maxsize=0, minsize=0, value=0.0, dot=u'0') | |
Falsifying example: test_IS_IN_SET(self=<test_validators.TestValidators testMethod=test_IS_IN_SET>, theset=[], labels=None, error_message='Value not allowed', multiple=True, zero='', sort=False, value=u'') | |
Falsifying example: test_IS_LENGTH(self=<test_validators.TestValidators testMethod=test_IS_LENGTH>, maxsize=1000000, minsize=1, value=u'') | |
====================================================================== | |
FAIL: test_IS_DECIMAL_IN_RANGE (test_validators.TestValidators) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File "D:\code\git\web2py_hypothesis\tests\test_validators.py", line 32, in test_IS_DECIMAL_IN_RANGE | |
def test_IS_DECIMAL_IN_RANGE(self, maxsize, minsize, value, dot): | |
File "D:\programs\open\python27\lib\site-packages\hypothesis\core.py", line 540, in wrapped_test | |
print_example=True, is_final=True | |
File "D:\programs\open\python27\lib\site-packages\hypothesis\executors.py", line 57, in default_new_style_executor | |
return function(data) | |
File "D:\programs\open\python27\lib\site-packages\hypothesis\core.py", line 103, in run | |
return test(*args, **kwargs) | |
File "D:\code\git\web2py_hypothesis\tests\test_validators.py", line 34, in test_IS_DECIMAL_IN_RANGE | |
assert (result == (Decimal(value), None)) or (result == (Decimal(value), 'Enter from %s to %s characters' % (minsize, maxsize))) | |
AssertionError | |
====================================================================== | |
FAIL: test_IS_IN_SET (test_validators.TestValidators) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File "D:\code\git\web2py_hypothesis\tests\test_validators.py", line 37, in test_IS_IN_SET | |
theset=st.lists(st.text()), | |
File "D:\programs\open\python27\lib\site-packages\hypothesis\core.py", line 540, in wrapped_test | |
print_example=True, is_final=True | |
File "D:\programs\open\python27\lib\site-packages\hypothesis\executors.py", line 57, in default_new_style_executor | |
return function(data) | |
File "D:\programs\open\python27\lib\site-packages\hypothesis\core.py", line 103, in run | |
return test(*args, **kwargs) | |
File "D:\code\git\web2py_hypothesis\tests\test_validators.py", line 47, in test_IS_IN_SET | |
assert (result == (value, None)) or (result == (value, 'Value not allowed')) | |
AssertionError | |
====================================================================== | |
FAIL: test_IS_LENGTH (test_validators.TestValidators) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File "D:\code\git\web2py_hypothesis\tests\test_validators.py", line 27, in test_IS_LENGTH | |
def test_IS_LENGTH(self, maxsize, minsize, value): | |
File "D:\programs\open\python27\lib\site-packages\hypothesis\core.py", line 540, in wrapped_test | |
print_example=True, is_final=True | |
File "D:\programs\open\python27\lib\site-packages\hypothesis\executors.py", line 57, in default_new_style_executor | |
return function(data) | |
File "D:\programs\open\python27\lib\site-packages\hypothesis\core.py", line 103, in run | |
return test(*args, **kwargs) | |
File "D:\code\git\web2py_hypothesis\tests\test_validators.py", line 29, in test_IS_LENGTH | |
assert (result == (value, None)) or (result == (value, 'Enter from %s to %s characters' % (minsize, maxsize))) | |
AssertionError | |
---------------------------------------------------------------------- | |
Ran 5 tests in 4.252s | |
FAILED (failures=3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment