forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_util.py
128 lines (105 loc) · 5.18 KB
/
test_util.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
import numpy as np
from pandas import date_range, Index
import pandas.util.testing as tm
from pandas.core.reshape.util import cartesian_product
from hypothesis import strategies as st
from hypothesis import given, settings, assume
from datetime import date
from dateutil import relativedelta
import string
NO_OF_EXAMPLES_PER_TEST_CASE = 20
def get_elements(elem_type):
strategy = st.nothing()
if elem_type == bool:
strategy = st.booleans()
elif elem_type == int:
strategy = st.integers()
elif elem_type == float:
strategy = st.floats()
elif elem_type == str:
strategy = st.text(string.ascii_letters, max_size=10)
return strategy
@st.composite
def get_seq(draw, types, mixed=False, min_size=None, max_size=None, transform_func=None):
"""helper function to generate strategy for creating lists. parameters define the nature of to be generated list.
:param types: what type of elements constitute the list
:param mixed: if True, list will contains elements from all types listed in arg, oterwise it will have elements only from types[0].
:param min_size: minimum size of the list.
:param max_size: maximum size of the list.
:param transform_func: a callable which can be applied to whole list after it has been generated.
"""
strategy = st.nothing()
if min_size is None:
min_size = draw(st.integers(min_value=0, max_value=100))
if max_size is None:
max_size = draw(st.integers(min_value=min_size, max_value=100))
assert min_size <= max_size, 'max_size must be greater than equal to min_size'
elem_strategies = []
for elem_type in types:
elem_strategies.append(get_elements(elem_type))
if not mixed:
break
if transform_func:
strategy = draw(st.lists(st.one_of(elem_strategies),
min_size=min_size, max_size=max_size).map(transform_func))
else:
strategy = draw(st.lists(st.one_of(elem_strategies),
min_size=min_size, max_size=max_size))
return strategy
class TestCartesianProduct(object):
@settings(max_examples=NO_OF_EXAMPLES_PER_TEST_CASE)
@given(get_seq((str,), False, 1, 1),
get_seq((int,), False, 1, 2))
def test_simple(self, x, y):
x = list(x[0])
# non-empty test case is handled in test_empty, therefore ignore it here
assume(len(x) != 0)
result1, result2 = cartesian_product([x, y])
expected1 = np.array([item1 for item1 in x for item2 in y])
expected2 = np.array([item2 for item1 in x for item2 in y])
tm.assert_numpy_array_equal(result1, expected1)
tm.assert_numpy_array_equal(result2, expected2)
@settings(max_examples=NO_OF_EXAMPLES_PER_TEST_CASE)
def test_datetimeindex(self):
# regression test for GitHub issue #6439
# make sure that the ordering on datetimeindex is consistent
d = st.dates(min_value=date(1900, 1, 1), max_value=date(2100, 1, 1)).example()
n = d + relativedelta.relativedelta(days=1)
x = date_range(d, periods=2)
result1, result2 = [Index(y).day for y in cartesian_product([x, x])]
expected1 = Index([d.day, d.day, n.day, n.day])
expected2 = Index([d.day, n.day, d.day, n.day])
tm.assert_index_equal(result1, expected1)
tm.assert_index_equal(result2, expected2)
@settings(max_examples=NO_OF_EXAMPLES_PER_TEST_CASE)
@given(st.lists(st.nothing()),
get_seq((int,), False),
get_seq((str,), False))
def test_empty(self, empty_list, list_of_int, list_of_str):
# product of empty factors
X = [empty_list, list_of_int, empty_list]
Y = [empty_list, empty_list, list_of_str]
for x, y in zip(X, Y):
expected1 = np.array([], dtype=np.asarray(x).dtype)
expected2 = np.array([], dtype=np.asarray(y).dtype)
result1, result2 = cartesian_product([x, y])
tm.assert_numpy_array_equal(result1, expected1)
tm.assert_numpy_array_equal(result2, expected2)
# empty product (empty input):
result = cartesian_product(empty_list)
expected = []
assert result == expected
@settings(max_examples=NO_OF_EXAMPLES_PER_TEST_CASE)
def test_invalid_input(self):
invalid_inputs = [st.integers().example(),
st.tuples(st.integers()).example(),
st.tuples(st.integers(), st.integers()).example(),
st.text(string.ascii_letters, min_size=1, max_size=1).example(),
st.tuples(st.text(string.ascii_letters, min_size=1, max_size=1)).example(),
st.tuples(st.text(string.ascii_letters, min_size=1, max_size=1),
st.text(string.ascii_letters, min_size=1, max_size=1)).example(),
st.tuples(st.tuples(st.text(string.ascii_letters, min_size=1, max_size=1)),
st.text(string.ascii_letters, min_size=1, max_size=1)).example()]
msg = "Input must be a list-like of list-likes"
for X in invalid_inputs:
tm.assert_raises_regex(TypeError, msg, cartesian_product, X=X)