Skip to content

Commit 491501b

Browse files
committed
Add additional hypothesis tests for functions
Fixed a type check bug for long() types in python2.
1 parent e32adb9 commit 491501b

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

extra/test_hypothesis.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# via env var for longer runs in travis.
55
import os
66
import sys
7+
import numbers
78

89
from nose.plugins.skip import SkipTest
910
from hypothesis import given, settings, assume, HealthCheck
@@ -12,19 +13,22 @@
1213
from jmespath import lexer
1314
from jmespath import parser
1415
from jmespath import exceptions
16+
from jmespath.functions import Functions
1517

1618

1719
if sys.version_info[:2] == (2, 6):
1820
raise RuntimeError("Hypothesis tests are not supported on python2.6. "
1921
"Use python2.7, or python3.3 and greater.")
2022

2123

24+
JSON_NUMBERS = (st.integers() | st.floats(allow_nan=False,
25+
allow_infinity=False))
26+
2227
RANDOM_JSON = st.recursive(
23-
st.floats() | st.booleans() | st.text() | st.none(),
28+
JSON_NUMBERS | st.booleans() | st.text() | st.none(),
2429
lambda children: st.lists(children) | st.dictionaries(st.text(), children)
2530
)
2631

27-
2832
MAX_EXAMPLES = int(os.environ.get('JP_MAX_EXAMPLES', 1000))
2933
BASE_SETTINGS = {
3034
'max_examples': MAX_EXAMPLES,
@@ -105,3 +109,32 @@ def test_search_api(expr, data):
105109
return
106110
except Exception as e:
107111
raise AssertionError("Non JMESPathError raised: %s" % e)
112+
113+
114+
# Additional property tests for functions.
115+
116+
@given(arg=JSON_NUMBERS)
117+
def test_abs(arg):
118+
assert Functions().call_function('abs', [arg]) >= 0
119+
120+
121+
@given(arg=st.lists(JSON_NUMBERS))
122+
def test_avg(arg):
123+
result = Functions().call_function('avg', [arg])
124+
if result is not None:
125+
assert isinstance(result, numbers.Number)
126+
127+
128+
@given(arg=st.lists(st.floats() | st.booleans() | st.text() | st.none(),
129+
min_size=1))
130+
def test_not_null(arg):
131+
result = Functions().call_function('not_null', arg)
132+
if result is not None:
133+
assert result in arg
134+
135+
136+
@given(arg=RANDOM_JSON)
137+
def test_to_number(arg):
138+
result = Functions().call_function('to_number', [arg])
139+
if result is not None:
140+
assert isinstance(result, numbers.Number)

jmespath/functions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'str': 'string',
1717
'float': 'number',
1818
'int': 'number',
19+
'long': 'number',
1920
'OrderedDict': 'object',
2021
'_Projection': 'array',
2122
'_Expression': 'expref',
@@ -29,7 +30,7 @@
2930
'object': ('dict', 'OrderedDict',),
3031
'null': ('None',),
3132
'string': ('unicode', 'str'),
32-
'number': ('float', 'int'),
33+
'number': ('float', 'int', 'long'),
3334
'expref': ('_Expression',),
3435
}
3536

0 commit comments

Comments
 (0)