File: test_direct_strategies.py

package info (click to toggle)
python-hypothesis 3.6.1-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,788 kB
  • sloc: python: 15,048; sh: 226; makefile: 160
file content (264 lines) | stat: -rw-r--r-- 8,178 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# coding=utf-8
#
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis-python
#
# Most of this work is copyright (C) 2013-2016 David R. MacIver
# (david@drmaciver.com), but it contains contributions by others. See
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
# consult the git log if you need to determine who owns an individual
# contribution.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.
#
# END HEADER

from __future__ import division, print_function, absolute_import

import math
import decimal
import fractions

import pytest

import hypothesis.strategies as ds
from hypothesis import find, given, settings
from hypothesis.errors import InvalidArgument
from hypothesis.internal.reflection import nicerepr


def fn_test(*fnkwargs):
    fnkwargs = list(fnkwargs)
    return pytest.mark.parametrize(
        ('fn', 'args'), fnkwargs,
        ids=[
            '%s(%s)' % (fn.__name__, ', '.join(map(nicerepr, args)))
            for fn, args in fnkwargs
        ]
    )


def fn_ktest(*fnkwargs):
    fnkwargs = list(fnkwargs)
    return pytest.mark.parametrize(
        ('fn', 'kwargs'), fnkwargs,
        ids=[
            '%s(%s)' % (fn.__name__, ', '.join(sorted(
                '%s=%r' % (k, v)
                for k, v in kwargs.items()
            )),)
            for fn, kwargs in fnkwargs
        ]
    )


@fn_ktest(
    (ds.integers, {'min_value': float('nan')}),
    (ds.integers, {'min_value': 2, 'max_value': 1}),
    (ds.decimals, {'min_value': float('nan')}),
    (ds.decimals, {'min_value': 2, 'max_value': 1}),
    (ds.fractions, {'min_value': float('nan')}),
    (ds.fractions, {'min_value': 2, 'max_value': 1}),
    (ds.fractions, {'max_denominator': 0}),
    (ds.lists, {}),
    (ds.lists, {'average_size': '5'}),
    (ds.lists, {'average_size': float('nan')}),
    (ds.lists, {'min_size': 10, 'max_size': 9}),
    (ds.lists, {'min_size': -10, 'max_size': -9}),
    (ds.lists, {'max_size': -9}),
    (ds.lists, {'max_size': 10}),
    (ds.lists, {'min_size': -10}),
    (ds.lists, {'max_size': 10, 'average_size': 20}),
    (ds.lists, {'min_size': 1.0, 'average_size': 0.5}),
    (ds.lists, {'elements': 'hi'}),
    (ds.text, {'min_size': 10, 'max_size': 9}),
    (ds.text, {'max_size': 10, 'average_size': 20}),
    (ds.binary, {'min_size': 10, 'max_size': 9}),
    (ds.binary, {'max_size': 10, 'average_size': 20}),
    (ds.floats, {'min_value': float('nan')}),
    (ds.floats, {'max_value': 0.0, 'min_value': 1.0}),
    (ds.fixed_dictionaries, {'mapping': 'fish'}),
    (ds.fixed_dictionaries, {'mapping': {1: 'fish'}}),
    (ds.dictionaries, {'keys': ds.integers(), 'values': 1}),
    (ds.dictionaries, {'keys': 1, 'values': ds.integers()}),
    (ds.text, {'alphabet': '', 'min_size': 1}),
)
def test_validates_keyword_arguments(fn, kwargs):
    with pytest.raises(InvalidArgument):
        fn(**kwargs).example()


@fn_ktest(
    (ds.integers, {'min_value': 0}),
    (ds.integers, {'min_value': 11}),
    (ds.integers, {'min_value': 11, 'max_value': 100}),
    (ds.integers, {'max_value': 0}),
    (ds.integers, {'min_value': decimal.Decimal('1.5')}),
    (ds.integers, {'min_value': fractions.Fraction(1, 2)}),
    (ds.decimals, {'min_value': 1.0, 'max_value': 1.5}),
    (ds.decimals, {'min_value': decimal.Decimal('1.5')}),
    (ds.decimals, {'min_value': fractions.Fraction(1, 3)}),
    (ds.fractions, {
        'min_value': -1, 'max_value': 1, 'max_denominator': 1000}),
    (ds.fractions, {'min_value': 1.0}),
    (ds.fractions, {'min_value': decimal.Decimal('1.0')}),
    (ds.fractions, {'min_value': fractions.Fraction(1, 2)}),
    (ds.lists, {'max_size': 0}),
    (ds.lists, {'elements': ds.integers()}),
    (ds.lists, {'elements': ds.integers(), 'max_size': 5}),
    (ds.lists, {'elements': ds.booleans(), 'min_size': 5}),
    (ds.lists, {'elements': ds.booleans(), 'min_size': 5, 'max_size': 10}),
    (ds.lists, {
        'average_size': 20, 'elements': ds.booleans(), 'max_size': 25}),
    (ds.sets, {
        'min_size': 10, 'max_size': 10, 'elements': ds.integers(),
    }),
    (ds.booleans, {}),
    (ds.just, {'value': 'hi'}),
    (ds.integers, {'min_value': 12, 'max_value': 12}),
    (ds.floats, {}),
    (ds.floats, {'min_value': 1.0}),
    (ds.floats, {'max_value': 1.0}),
    (ds.floats, {'max_value': 1.0, 'min_value': -1.0}),
    (ds.sampled_from, {'elements': [1]}),
    (ds.sampled_from, {'elements': [1, 2, 3]}),
    (ds.fixed_dictionaries, {'mapping': {1: ds.integers()}}),
    (ds.dictionaries, {'keys': ds.booleans(), 'values': ds.integers()}),
    (ds.text, {'alphabet': 'abc'}),
    (ds.text, {'alphabet': ''}),
    (ds.text, {'alphabet': ds.sampled_from('abc')}),
)
def test_produces_valid_examples_from_keyword(fn, kwargs):
    fn(**kwargs).example()


@fn_test(
    (ds.one_of, (1,))
)
def test_validates_args(fn, args):
    with pytest.raises(InvalidArgument):
        fn(*args).example()


@fn_test(
    (ds.one_of, (ds.booleans(), ds.tuples(ds.booleans()))),
    (ds.one_of, (ds.booleans(),)),
    (ds.text, ()),
    (ds.binary, ()),
    (ds.builds, (lambda x, y: x + y, ds.integers(), ds.integers())),
)
def test_produces_valid_examples_from_args(fn, args):
    fn(*args).example()


def test_tuples_raise_error_on_bad_kwargs():
    with pytest.raises(TypeError):
        ds.tuples(stuff='things')


@given(ds.lists(ds.booleans(), min_size=10, max_size=10))
def test_has_specified_length(xs):
    assert len(xs) == 10


@given(ds.integers(max_value=100))
@settings(max_examples=100)
def test_has_upper_bound(x):
    assert x <= 100


@given(ds.integers(min_value=100))
def test_has_lower_bound(x):
    assert x >= 100


@given(ds.integers(min_value=1, max_value=2))
def test_is_in_bounds(x):
    assert 1 <= x <= 2


@given(ds.fractions(min_value=-1, max_value=1, max_denominator=1000))
def test_fraction_is_in_bounds(x):
    assert -1 <= x <= 1 and abs(x.denominator) <= 1000


@given(ds.fractions(min_value=fractions.Fraction(1, 2)))
def test_fraction_gt_positive(x):
    assert fractions.Fraction(1, 2) <= x


@given(ds.fractions(max_value=fractions.Fraction(-1, 2)))
def test_fraction_lt_negative(x):
    assert x <= fractions.Fraction(-1, 2)


@given(ds.decimals(min_value=-1.5, max_value=1.5))
def test_decimal_is_in_bounds(x):
    # decimal.Decimal("-1.5") == -1.5 (not explicitly testable in py2.6)
    assert decimal.Decimal('-1.5') <= x <= decimal.Decimal('1.5')


def test_float_can_find_max_value_inf():
    assert find(
        ds.floats(max_value=float('inf')), lambda x: math.isinf(x)
    ) == float('inf')
    assert find(
        ds.floats(min_value=0.0), lambda x: math.isinf(x)) == float('inf')


def test_float_can_find_min_value_inf():
    find(ds.floats(), lambda x: x < 0 and math.isinf(x))
    find(
        ds.floats(min_value=float('-inf'), max_value=0.0),
        lambda x: math.isinf(x))


def test_can_find_none_list():
    assert find(ds.lists(ds.none()), lambda x: len(x) >= 3) == [None] * 3


def test_fractions():
    assert find(ds.fractions(), lambda f: f >= 1) == 1


def test_decimals():
    assert find(ds.decimals(), lambda f: f.is_finite() and f >= 1) == 1


def test_non_float_decimal():
    find(ds.decimals(), lambda d: ds.float_to_decimal(float(d)) != d)


def test_produces_dictionaries_of_at_least_minimum_size():
    t = find(
        ds.dictionaries(ds.booleans(), ds.integers(), min_size=2),
        lambda x: True)
    assert t == {False: 0, True: 0}


@given(ds.dictionaries(ds.integers(), ds.integers(), max_size=5))
@settings(max_examples=50)
def test_dictionaries_respect_size(d):
    assert len(d) <= 5


@given(ds.dictionaries(ds.integers(), ds.integers(), max_size=0))
@settings(max_examples=50)
def test_dictionaries_respect_zero_size(d):
    assert len(d) <= 5


@given(
    ds.lists(ds.none(), max_size=5)
)
def test_none_lists_respect_max_size(ls):
    assert len(ls) <= 5


@given(
    ds.lists(ds.none(), max_size=5, min_size=1)
)
def test_none_lists_respect_max_and_min_size(ls):
    assert 1 <= len(ls) <= 5