forked from HypothesisWorks/hypothesis
/
test_argument_validation.py
159 lines (144 loc) · 5.82 KB
/
test_argument_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
# 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 numpy
import pytest
import hypothesis.extra.numpy as nps
import hypothesis.strategies as st
from hypothesis import given
from hypothesis.errors import InvalidArgument
from tests.common.utils import checks_deprecated_behaviour
def e(a, **kwargs):
return (a, kwargs)
@pytest.mark.parametrize(
("function", "kwargs"),
[
e(nps.array_dtypes, min_size=2, max_size=1),
e(nps.array_dtypes, min_size=-1),
e(nps.array_shapes, min_side=2, max_side=1),
e(nps.array_shapes, min_dims=3, max_dims=2),
e(nps.array_shapes, min_dims=-1),
e(nps.array_shapes, min_side=-1),
e(nps.array_shapes, min_side="not an int"),
e(nps.array_shapes, max_side="not an int"),
e(nps.array_shapes, min_dims="not an int"),
e(nps.array_shapes, max_dims="not an int"),
e(nps.array_shapes, min_dims=33),
e(nps.array_shapes, max_dims=33),
e(nps.arrays, dtype=float, shape=(0.5,)),
e(nps.arrays, dtype=object, shape=1),
e(nps.arrays, dtype=float, shape=1, fill=3),
e(nps.byte_string_dtypes, min_len=-1),
e(nps.byte_string_dtypes, min_len=2, max_len=1),
e(nps.datetime64_dtypes, max_period=11),
e(nps.datetime64_dtypes, min_period=11),
e(nps.datetime64_dtypes, min_period="Y", max_period="M"),
e(nps.timedelta64_dtypes, max_period=11),
e(nps.timedelta64_dtypes, min_period=11),
e(nps.timedelta64_dtypes, min_period="Y", max_period="M"),
e(nps.unicode_string_dtypes, min_len=-1),
e(nps.unicode_string_dtypes, min_len=2, max_len=1),
e(nps.unsigned_integer_dtypes, endianness=3),
e(nps.unsigned_integer_dtypes, sizes=()),
e(nps.unsigned_integer_dtypes, sizes=(3,)),
e(nps.from_dtype, dtype="float64"),
e(nps.from_dtype, dtype=float),
e(nps.from_dtype, dtype=numpy.int8),
e(nps.from_dtype, dtype=1),
e(nps.valid_tuple_axes, ndim=-1),
e(nps.valid_tuple_axes, ndim=2, min_size=-1),
e(nps.valid_tuple_axes, ndim=2, min_size=3, max_size=10),
e(nps.valid_tuple_axes, ndim=2, min_size=2, max_size=1),
e(nps.valid_tuple_axes, ndim=2.0, min_size=2, max_size=1),
e(nps.valid_tuple_axes, ndim=2, min_size=1.0, max_size=2),
e(nps.valid_tuple_axes, ndim=2, min_size=1, max_size=2.0),
e(nps.valid_tuple_axes, ndim=2, min_size=1, max_size=3),
e(nps.broadcastable_shapes, shape="a"),
e(nps.broadcastable_shapes, shape=(2, 2), min_side="a"),
e(nps.broadcastable_shapes, shape=(2, 2), min_dims="a"),
e(nps.broadcastable_shapes, shape=(2, 2), max_side="a"),
e(nps.broadcastable_shapes, shape=(2, 2), max_dims="a"),
e(nps.broadcastable_shapes, shape=(2, 2), min_side=-1),
e(nps.broadcastable_shapes, shape=(2, 2), min_dims=-1),
e(nps.broadcastable_shapes, shape=(2, 2), min_dims=33, max_dims=None),
e(nps.broadcastable_shapes, shape=(2, 2), min_dims=1, max_dims=33),
e(nps.broadcastable_shapes, shape=(2, 2), min_side=1, max_side=0),
e(nps.broadcastable_shapes, shape=(2, 2), min_dims=1, max_dims=0),
e(
nps.broadcastable_shapes, # max_side too small
shape=(5, 1),
min_dims=2,
max_dims=4,
min_side=2,
max_side=3,
),
e(
nps.broadcastable_shapes, # min_side too large
shape=(0, 1),
min_dims=2,
max_dims=4,
min_side=2,
max_side=3,
),
e(
nps.broadcastable_shapes, # default max_dims unsatisfiable
shape=(5, 3, 2, 1),
min_dims=3,
max_dims=None,
min_side=2,
max_side=3,
),
e(
nps.broadcastable_shapes, # default max_dims unsatisfiable
shape=(0, 3, 2, 1),
min_dims=3,
max_dims=None,
min_side=2,
max_side=3,
),
e(nps.integer_array_indices, shape=()),
e(nps.integer_array_indices, shape=(2, 0)),
e(nps.integer_array_indices, shape="a"),
e(nps.integer_array_indices, shape=(2,), result_shape=(2, 2)),
e(nps.integer_array_indices, shape=(2,), dtype=float),
],
)
def test_raise_invalid_argument(function, kwargs):
with pytest.raises(InvalidArgument):
function(**kwargs).example()
@nps.defines_dtype_strategy
def bad_dtype_strategy():
return st.just([("f1", "int32"), ("f1", "int32")])
@given(st.data())
def test_bad_dtype_strategy(capsys, data):
s = bad_dtype_strategy()
with pytest.raises(ValueError):
data.draw(s)
val = s.wrapped_strategy.mapped_strategy.value
assert capsys.readouterr().out.startswith(
"Got invalid dtype value=%r from strategy=just(%r), function=" % (val, val)
)
@checks_deprecated_behaviour
@given(st.data())
def test_byte_string_dtype_len_0(data):
s = nps.byte_string_dtypes(min_len=0, max_len=0)
assert data.draw(s).itemsize == 1
@checks_deprecated_behaviour
@given(st.data())
def test_unicode_string_dtype_len_0(data):
s = nps.unicode_string_dtypes(min_len=0, max_len=0)
assert data.draw(s).itemsize == 4