|
4 | 4 | import pandas.util.testing as tm
|
5 | 5 | from pandas.core.reshape.util import cartesian_product
|
6 | 6 |
|
7 |
| -from hypothesis import strategies as st |
8 |
| -from hypothesis import given, settings, assume |
| 7 | +import string |
9 | 8 | from datetime import date
|
10 | 9 | from dateutil import relativedelta
|
11 |
| -import string |
12 | 10 |
|
| 11 | +from pandas.util._hypothesis import (st, |
| 12 | + given, |
| 13 | + settings, |
| 14 | + get_seq, |
| 15 | + assume) |
13 | 16 |
|
14 |
| -NO_OF_EXAMPLES_PER_TEST_CASE = 20 |
15 | 17 |
|
16 |
| - |
17 |
| -def get_elements(elem_type): |
18 |
| - strategy = st.nothing() |
19 |
| - if elem_type == bool: |
20 |
| - strategy = st.booleans() |
21 |
| - elif elem_type == int: |
22 |
| - strategy = st.integers() |
23 |
| - elif elem_type == float: |
24 |
| - strategy = st.floats() |
25 |
| - elif elem_type == str: |
26 |
| - strategy = st.text(string.ascii_letters, max_size=10) |
27 |
| - return strategy |
28 |
| - |
29 |
| - |
30 |
| -@st.composite |
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'] |
80 |
| - """ |
81 |
| - strategy = st.nothing() |
82 |
| - if min_size is None: |
83 |
| - min_size = draw(st.integers(min_value=0, max_value=100)) |
84 |
| - |
85 |
| - if max_size is None: |
86 |
| - max_size = draw(st.integers(min_value=min_size, max_value=100)) |
87 |
| - |
88 |
| - assert min_size <= max_size, \ |
89 |
| - 'max_size must be greater than equal to min_size' |
90 |
| - |
91 |
| - elem_strategies = [] |
92 |
| - for elem_type in types: |
93 |
| - elem_strategies.append(get_elements(elem_type)) |
94 |
| - if not mixed: |
95 |
| - break |
96 |
| - |
97 |
| - if transform_func: |
98 |
| - strategy = draw(st.lists(st.one_of(elem_strategies), |
99 |
| - min_size=min_size, |
100 |
| - max_size=max_size).map(transform_func)) |
101 |
| - else: |
102 |
| - strategy = draw(st.lists(st.one_of(elem_strategies), |
103 |
| - min_size=min_size, |
104 |
| - max_size=max_size)) |
105 |
| - return strategy |
| 18 | +NO_OF_EXAMPLES_PER_TEST_CASE = 20 |
106 | 19 |
|
107 | 20 |
|
108 | 21 | class TestCartesianProduct(object):
|
|
0 commit comments