@@ -28,13 +28,55 @@ def get_elements(elem_type):
28
28
29
29
30
30
@st .composite
31
- def get_seq (draw , types , mixed = False , min_size = None , max_size = None , transform_func = None ):
32
- """helper function to generate strategy for creating lists. parameters define the nature of to be generated list.
33
- :param types: what type of elements constitute the list
34
- :param mixed: if True, list will contains elements from all types listed in arg, oterwise it will have elements only from types[0].
35
- :param min_size: minimum size of the list.
36
- :param max_size: maximum size of the list.
37
- :param transform_func: a callable which can be applied to whole list after it has been generated.
31
+ def get_seq (draw , types , mixed = False , min_size = None , max_size = None ,
32
+ transform_func = None ):
33
+ """
34
+ Helper function to generate strategy for creating lists.
35
+ What constitute in the generated list is driven by the different
36
+ parameters.
37
+
38
+ Parameters
39
+ ----------
40
+ types: iterable sequence like tuple or list
41
+ types which can be in the generated list.
42
+ mixed: bool
43
+ if True, list will contains elements from all types listed in arg,
44
+ otherwise it will have elements only from types[0].
45
+ min_size: int
46
+ minimum size of the list.
47
+ max_size: int
48
+ maximum size of the list.
49
+ transform_func: callable
50
+ a callable which can be applied to whole list after it has been
51
+ generated. It can think of as providing functionality of filter
52
+ and map function.
53
+
54
+ Returns
55
+ -------
56
+ hypothesis lists strategy.
57
+
58
+ Examples
59
+ --------
60
+ seq_strategy = get_seq((int, str, bool),
61
+ mixed=True, min_size=1, max_size=5)
62
+ seq_strategy.example()
63
+ Out[12]: ['lkYMSn', -2501, 35, 'J']
64
+ seq_strategy.example()
65
+ Out[13]: [True]
66
+ seq_strategy.example()
67
+ Out[14]: ['dRWgQYrBrW', True, False, 'gmsujJVDBM', 'Z']
68
+
69
+ seq_strategy = get_seq((int, bool),
70
+ mixed=False,
71
+ min_size=1,
72
+ max_size=5,
73
+ transform_func=lambda seq: [str(x) for x in seq])
74
+ seq_strategy.example()
75
+ Out[19]: ['-1892']
76
+ seq_strategy.example()
77
+ Out[20]: ['22', '66', '14785', '-26312', '32']
78
+ seq_strategy.example()
79
+ Out[21]: ['22890', '-15537', '96']
38
80
"""
39
81
strategy = st .nothing ()
40
82
if min_size is None :
@@ -43,7 +85,8 @@ def get_seq(draw, types, mixed=False, min_size=None, max_size=None, transform_fu
43
85
if max_size is None :
44
86
max_size = draw (st .integers (min_value = min_size , max_value = 100 ))
45
87
46
- assert min_size <= max_size , 'max_size must be greater than equal to min_size'
88
+ assert min_size <= max_size , \
89
+ 'max_size must be greater than equal to min_size'
47
90
48
91
elem_strategies = []
49
92
for elem_type in types :
@@ -53,10 +96,12 @@ def get_seq(draw, types, mixed=False, min_size=None, max_size=None, transform_fu
53
96
54
97
if transform_func :
55
98
strategy = draw (st .lists (st .one_of (elem_strategies ),
56
- min_size = min_size , max_size = max_size ).map (transform_func ))
99
+ min_size = min_size ,
100
+ max_size = max_size ).map (transform_func ))
57
101
else :
58
102
strategy = draw (st .lists (st .one_of (elem_strategies ),
59
- min_size = min_size , max_size = max_size ))
103
+ min_size = min_size ,
104
+ max_size = max_size ))
60
105
return strategy
61
106
62
107
@@ -67,7 +112,8 @@ class TestCartesianProduct(object):
67
112
get_seq ((int ,), False , 1 , 2 ))
68
113
def test_simple (self , x , y ):
69
114
x = list (x [0 ])
70
- # non-empty test case is handled in test_empty, therefore ignore it here
115
+ # non-empty test case is handled in test_empty,
116
+ # therefore ignore it here.
71
117
assume (len (x ) != 0 )
72
118
result1 , result2 = cartesian_product ([x , y ])
73
119
expected1 = np .array ([item1 for item1 in x for item2 in y ])
@@ -77,10 +123,10 @@ def test_simple(self, x, y):
77
123
tm .assert_numpy_array_equal (result2 , expected2 )
78
124
79
125
@settings (max_examples = NO_OF_EXAMPLES_PER_TEST_CASE )
80
- def test_datetimeindex (self ):
126
+ @given (st .dates (min_value = date (1900 , 1 , 1 ), max_value = date (2100 , 1 , 1 )))
127
+ def test_datetimeindex (self , d ):
81
128
# regression test for GitHub issue #6439
82
129
# make sure that the ordering on datetimeindex is consistent
83
- d = st .dates (min_value = date (1900 , 1 , 1 ), max_value = date (2100 , 1 , 1 )).example ()
84
130
n = d + relativedelta .relativedelta (days = 1 )
85
131
x = date_range (d , periods = 2 )
86
132
result1 , result2 = [Index (y ).day for y in cartesian_product ([x , x ])]
@@ -112,16 +158,22 @@ def test_empty(self, empty_list, list_of_int, list_of_str):
112
158
assert result == expected
113
159
114
160
@settings (max_examples = NO_OF_EXAMPLES_PER_TEST_CASE )
115
- def test_invalid_input (self ):
116
- invalid_inputs = [st .integers ().example (),
117
- st .tuples (st .integers ()).example (),
118
- st .tuples (st .integers (), st .integers ()).example (),
119
- st .text (string .ascii_letters , min_size = 1 , max_size = 1 ).example (),
120
- st .tuples (st .text (string .ascii_letters , min_size = 1 , max_size = 1 )).example (),
121
- st .tuples (st .text (string .ascii_letters , min_size = 1 , max_size = 1 ),
122
- st .text (string .ascii_letters , min_size = 1 , max_size = 1 )).example (),
123
- st .tuples (st .tuples (st .text (string .ascii_letters , min_size = 1 , max_size = 1 )),
124
- st .text (string .ascii_letters , min_size = 1 , max_size = 1 )).example ()]
161
+ @given (st .integers (),
162
+ st .text (string .ascii_letters , min_size = 1 ),
163
+ get_seq ((int , str ), True , min_size = 1 ),
164
+ st .lists (st .one_of (st .integers (),
165
+ st .text (string .ascii_letters , min_size = 1 ),
166
+ get_seq ((int ,), min_size = 1 )
167
+ ),
168
+ min_size = 1 ).filter (
169
+ lambda x : len (x ) == 1 and type (x [0 ]) != list )
170
+ )
171
+ def test_invalid_input (self , number , text , seq , mixed_seq ):
172
+
173
+ invalid_inputs = [number ,
174
+ text ,
175
+ seq ,
176
+ mixed_seq ]
125
177
126
178
msg = "Input must be a list-like of list-likes"
127
179
for X in invalid_inputs :
0 commit comments