|
4 | 4 | # via env var for longer runs in travis.
|
5 | 5 | import os
|
6 | 6 | import sys
|
| 7 | +import numbers |
7 | 8 |
|
8 | 9 | from nose.plugins.skip import SkipTest
|
9 | 10 | from hypothesis import given, settings, assume, HealthCheck
|
|
12 | 13 | from jmespath import lexer
|
13 | 14 | from jmespath import parser
|
14 | 15 | from jmespath import exceptions
|
| 16 | +from jmespath.functions import Functions |
15 | 17 |
|
16 | 18 |
|
17 | 19 | if sys.version_info[:2] == (2, 6):
|
18 | 20 | raise RuntimeError("Hypothesis tests are not supported on python2.6. "
|
19 | 21 | "Use python2.7, or python3.3 and greater.")
|
20 | 22 |
|
21 | 23 |
|
| 24 | +JSON_NUMBERS = (st.integers() | st.floats(allow_nan=False, |
| 25 | + allow_infinity=False)) |
| 26 | + |
22 | 27 | RANDOM_JSON = st.recursive(
|
23 |
| - st.floats() | st.booleans() | st.text() | st.none(), |
| 28 | + JSON_NUMBERS | st.booleans() | st.text() | st.none(), |
24 | 29 | lambda children: st.lists(children) | st.dictionaries(st.text(), children)
|
25 | 30 | )
|
26 | 31 |
|
27 |
| - |
28 | 32 | MAX_EXAMPLES = int(os.environ.get('JP_MAX_EXAMPLES', 1000))
|
29 | 33 | BASE_SETTINGS = {
|
30 | 34 | 'max_examples': MAX_EXAMPLES,
|
@@ -105,3 +109,32 @@ def test_search_api(expr, data):
|
105 | 109 | return
|
106 | 110 | except Exception as e:
|
107 | 111 | 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) |
0 commit comments