-
Notifications
You must be signed in to change notification settings - Fork 575
/
test_validation.py
262 lines (188 loc) · 6.29 KB
/
test_validation.py
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
# coding=utf-8
#
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Most of this work is copyright (C) 2013-2019 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 https://mozilla.org/MPL/2.0/.
#
# END HEADER
from __future__ import absolute_import, division, print_function
import functools
import pytest
from hypothesis import find, given
from hypothesis.errors import InvalidArgument
from hypothesis.internal.validation import check_type
from hypothesis.strategies import (
binary,
booleans,
dictionaries,
floats,
frozensets,
integers,
lists,
recursive,
sets,
text,
)
from tests.common.utils import checks_deprecated_behaviour, fails_with
@checks_deprecated_behaviour
def test_min_size_none_behavior():
lists(integers(), min_size=None).example()
@checks_deprecated_behaviour
@given(lists(integers(), min_size=5.0))
def test_min_size_float_behaviour(arr):
assert len(arr) >= 5
@checks_deprecated_behaviour
@given(lists(integers(), max_size=5.0))
def test_max_size_float_behaviour(arr):
assert len(arr) <= 5
def test_errors_when_given_varargs():
@given(integers())
def has_varargs(*args):
pass
with pytest.raises(InvalidArgument) as e:
has_varargs()
assert u"varargs" in e.value.args[0]
def test_varargs_without_positional_arguments_allowed():
@given(somearg=integers())
def has_varargs(somearg, *args):
pass
def test_errors_when_given_varargs_and_kwargs_with_positional_arguments():
@given(integers())
def has_varargs(*args, **kw):
pass
with pytest.raises(InvalidArgument) as e:
has_varargs()
assert u"varargs" in e.value.args[0]
def test_varargs_and_kwargs_without_positional_arguments_allowed():
@given(somearg=integers())
def has_varargs(*args, **kw):
pass
def test_bare_given_errors():
@given()
def test():
pass
with pytest.raises(InvalidArgument):
test()
def test_errors_on_unwanted_kwargs():
@given(hello=int, world=int)
def greet(world):
pass
with pytest.raises(InvalidArgument):
greet()
def test_errors_on_too_many_positional_args():
@given(integers(), int, int)
def foo(x, y):
pass
with pytest.raises(InvalidArgument):
foo()
def test_errors_on_any_varargs():
@given(integers())
def oops(*args):
pass
with pytest.raises(InvalidArgument):
oops()
def test_can_put_arguments_in_the_middle():
@given(y=integers())
def foo(x, y, z):
pass
foo(1, 2)
def test_float_ranges():
with pytest.raises(InvalidArgument):
floats(float(u"nan"), 0).example()
with pytest.raises(InvalidArgument):
floats(1, -1).example()
def test_float_range_and_allow_nan_cannot_both_be_enabled():
with pytest.raises(InvalidArgument):
floats(min_value=1, allow_nan=True).example()
with pytest.raises(InvalidArgument):
floats(max_value=1, allow_nan=True).example()
def test_float_finite_range_and_allow_infinity_cannot_both_be_enabled():
with pytest.raises(InvalidArgument):
floats(0, 1, allow_infinity=True).example()
def test_does_not_error_if_min_size_is_bigger_than_default_size():
lists(integers(), min_size=50).example()
sets(integers(), min_size=50).example()
frozensets(integers(), min_size=50).example()
lists(integers(), min_size=50, unique=True).example()
def test_list_unique_and_unique_by_cannot_both_be_enabled():
@given(lists(integers(), unique=True, unique_by=lambda x: x))
def boom(t):
pass
with pytest.raises(InvalidArgument) as e:
boom()
assert "unique " in e.value.args[0]
assert "unique_by" in e.value.args[0]
def test_min_before_max():
with pytest.raises(InvalidArgument):
integers(min_value=1, max_value=0).validate()
def test_filter_validates():
with pytest.raises(InvalidArgument):
integers(min_value=1, max_value=0).filter(bool).validate()
def test_recursion_validates_base_case():
with pytest.raises(InvalidArgument):
recursive(integers(min_value=1, max_value=0), lists).validate()
def test_recursion_validates_recursive_step():
with pytest.raises(InvalidArgument):
recursive(integers(), lambda x: lists(x, min_size=3, max_size=1)).validate()
@fails_with(InvalidArgument)
@given(x=integers())
def test_stuff_keyword(x=1):
pass
@fails_with(InvalidArgument)
@given(integers())
def test_stuff_positional(x=1):
pass
@fails_with(InvalidArgument)
@given(integers(), integers())
def test_too_many_positional(x):
pass
def test_given_warns_on_use_of_non_strategies():
@given(bool)
def test(x):
pass
with pytest.raises(InvalidArgument):
test()
def test_given_warns_when_mixing_positional_with_keyword():
@given(booleans(), y=booleans())
def test(x, y):
pass
with pytest.raises(InvalidArgument):
test()
@checks_deprecated_behaviour
def test_cannot_find_non_strategies():
with pytest.raises(InvalidArgument):
find(bool, bool)
@pytest.mark.parametrize(
"strategy",
[
functools.partial(lists, elements=integers()),
functools.partial(dictionaries, keys=integers(), values=integers()),
text,
binary,
],
)
@pytest.mark.parametrize("min_size,max_size", [(0, "10"), ("0", 10)])
def test_valid_sizes(strategy, min_size, max_size):
@given(strategy(min_size=min_size, max_size=max_size))
def test(x):
pass
with pytest.raises(InvalidArgument):
test()
def test_check_type_with_tuple_of_length_two():
# This test covers logic for length-two tuples that is essential on PY2,
# e.g. string_types (str, unicode) which are all length-one on Python 3.
def type_checker(x):
check_type((int, str), x, "x")
type_checker(1)
type_checker("1")
with pytest.raises(InvalidArgument, match="Expected one of int, str but got "):
type_checker(1.0)