1
|
|
|
# tests/test_api.py |
2
|
|
|
"""Test core API.""" |
3
|
|
|
import pytest |
4
|
|
|
from pandas import Series |
5
|
|
|
|
6
|
|
|
import ittools |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
# -------------- ittools.depth ------------------ |
10
|
|
|
class EndlessIterator: |
11
|
|
|
"""Mock an endless iterator.""" |
12
|
|
|
|
13
|
|
|
def __iter__(self): |
14
|
|
|
"""Retun instance of self, when iterated.""" |
15
|
|
|
return self |
16
|
|
|
|
17
|
|
|
def __next__(self): |
18
|
|
|
"""Increase the integer value by one when nexted on.""" |
19
|
|
|
return self |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
@pytest.mark.parametrize( |
23
|
|
|
("arg", "exclude", "expected_result"), |
24
|
|
|
[ |
25
|
|
|
([[2, 2], [2, [3, 3]], 1], (str,), 3), |
26
|
|
|
([[2, 2], [2, [1, (3, 3)]], 1], (tuple,), 3), |
27
|
|
|
("hallo", None, 0), # default exclude test |
28
|
|
|
# ([2, 2], None, 1), # test infinite loop |
29
|
|
|
(list, None, 0), # test infinite type error |
30
|
|
|
(EndlessIterator(), None, 1), # test infinite |
31
|
|
|
([[[[[]]]]], None, 4), # test max() value error |
32
|
|
|
], |
33
|
|
|
) |
34
|
|
|
def test_depth(arg, exclude, expected_result): |
35
|
|
|
"""Test correct ittools.depth functionaility.""" |
36
|
|
|
assert ittools.depth(arg, exclude) == expected_result |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
# -------------- ittools.nestify ------------------ |
40
|
|
|
@pytest.mark.parametrize( |
41
|
|
|
("arguments", "expected_result"), |
42
|
|
|
[ |
43
|
|
|
(([1, 2, 3], 3), [[[1, 2, 3]]]), |
44
|
|
|
(([1, 2, 3], 3, list), [[[1, 2, 3]]]), |
45
|
|
|
(([1, 2, 3], 3, tuple), (([1, 2, 3],),)), |
46
|
|
|
], |
47
|
|
|
) |
48
|
|
|
def test_nestify(arguments, expected_result): |
49
|
|
|
"""Test correct ittools.nestify functionaility.""" |
50
|
|
|
assert ittools.nestify(*arguments) == expected_result |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def test_nestify_fail_on_unhashable(): |
54
|
|
|
"""Test ittools.nestify fail on unhashables.""" |
55
|
|
|
try: |
56
|
|
|
ittools.nestify([1, 2, 3], 3, set) |
57
|
|
|
|
58
|
|
|
except TypeError as terror: |
59
|
|
|
assert "unhashable" in str(terror) |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
# -------------- ittools.itrify ------------------ |
63
|
|
|
@pytest.mark.parametrize( |
64
|
|
|
("arguments", "expected_result"), |
65
|
|
|
[ |
66
|
|
|
(("String", list), ["String"]), |
67
|
|
|
(("String", set), {"String"}), |
68
|
|
|
(([1, 2, 3], tuple), [1, 2, 3]), |
69
|
|
|
((Series([1, 2, 3]), set), {1, 2, 3}), |
70
|
|
|
], |
71
|
|
|
) |
72
|
|
|
def test_itrify(arguments, expected_result): |
73
|
|
|
"""Test correct ittools.itrify functionaility.""" |
74
|
|
|
assert ittools.itrify(*arguments) == expected_result |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
def test_default_itrify(): |
78
|
|
|
"""Test correct default ittools.itrify functionaility.""" |
79
|
|
|
assert ittools.itrify("String") == ["String"] |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
# -------------- ittools.is_empty ------------------ |
83
|
|
|
@pytest.mark.parametrize( |
84
|
|
|
("argument", "expected_result"), |
85
|
|
|
[ |
86
|
|
|
([], True), |
87
|
|
|
([[], [1, 2, 3]], False), |
88
|
|
|
], |
89
|
|
|
) |
90
|
|
|
def test_is_empty(argument, expected_result): |
91
|
|
|
"""Test correct ittools.is_empty functionaility.""" |
92
|
|
|
assert ittools.is_empty(argument) == expected_result |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
# -------------- ittools.Stringcrementor ------------------ |
96
|
|
|
def test_stringcrmentor_next(): |
97
|
|
|
"""Test correct ittools.Stringcrementor __next__ functionaility.""" |
98
|
|
|
strementor = ittools.Stringcrementor("Number") |
99
|
|
|
strementor_results = [] |
100
|
|
|
expected_result = [ |
101
|
|
|
"Number0", |
102
|
|
|
"Number1", |
103
|
|
|
"Number2", |
104
|
|
|
] |
105
|
|
|
for _i in range(3): |
106
|
|
|
strementor_results.append(next(strementor)) |
107
|
|
|
|
108
|
|
|
assert strementor_results == expected_result |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
def test_stringcrmentor_iter(): |
112
|
|
|
"""Test correct ittools.Stringcrementor __iter__ functionaility.""" |
113
|
|
|
assert isinstance( |
114
|
|
|
iter(ittools.Stringcrementor()), |
115
|
|
|
ittools.Stringcrementor, |
116
|
|
|
) |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
# -------------- ittools.enum_to_2dix ------------------ |
120
|
|
|
@pytest.mark.parametrize( |
121
|
|
|
("number", "shape", "expected_result"), |
122
|
|
|
[ |
123
|
|
|
(0, (3, 2), (0, 0)), |
124
|
|
|
(1, (3, 2), (0, 1)), |
125
|
|
|
(2, (3, 2), (1, 0)), |
126
|
|
|
(3, (3, 2), (1, 1)), |
127
|
|
|
(4, (3, 2), (2, 0)), |
128
|
|
|
(5, (3, 2), (2, 1)), |
129
|
|
|
# change gears to rows dont matter |
130
|
|
|
(0, (1, 6), (0, 0)), |
131
|
|
|
(1, (1, 6), (0, 1)), |
132
|
|
|
(2, (1, 6), (0, 2)), |
133
|
|
|
(3, (1, 6), (0, 3)), |
134
|
|
|
(4, (1, 6), (0, 4)), |
135
|
|
|
(5, (1, 6), (0, 5)), |
136
|
|
|
(6, (1, 6), (1, 0)), |
137
|
|
|
(7, (1, 6), (1, 1)), |
138
|
|
|
# change gears to nonsensically use negatives |
139
|
|
|
(0, (2, 2), (0, 0)), |
140
|
|
|
(-1, (2, 2), (-1, 1)), |
141
|
|
|
(-2, (2, 2), (-1, 0)), |
142
|
|
|
], |
143
|
|
|
) |
144
|
|
|
def test_enum_to_2dix(number, shape, expected_result): |
145
|
|
|
"""Test correct ittools.enum_to_2dix functionaility.""" |
146
|
|
|
assert ittools.enum_to_2dix(number, shape) == expected_result |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
# -------------- ittools.Index2D ------------------ |
150
|
|
|
@pytest.mark.parametrize( |
151
|
|
|
("number", "shape", "expected_result"), |
152
|
|
|
[ |
153
|
|
|
(0, (3, 2), (0, 0)), |
154
|
|
|
(1, (3, 2), (0, 1)), |
155
|
|
|
(2, (3, 2), (1, 0)), |
156
|
|
|
(3, (3, 2), (1, 1)), |
157
|
|
|
(5, (3, 2), (2, 1)), |
158
|
|
|
# change gears to rows dont matter |
159
|
|
|
(0, (1, 6), (0, 0)), |
160
|
|
|
(1, (1, 6), (0, 1)), |
161
|
|
|
(2, (1, 6), (0, 2)), |
162
|
|
|
(3, (1, 6), (0, 3)), |
163
|
|
|
(4, (1, 6), (0, 4)), |
164
|
|
|
(5, (1, 6), (0, 5)), |
165
|
|
|
(7, (1, 6), (1, 1)), |
166
|
|
|
# change gears to nonsensically use negatives |
167
|
|
|
(0, (2, 2), (0, 0)), |
168
|
|
|
(-2, (2, 2), (-1, 0)), |
169
|
|
|
], |
170
|
|
|
) |
171
|
|
|
def test_index2d(number, shape, expected_result): |
172
|
|
|
"""Test correct ittools.Index2D functionaility.""" |
173
|
|
|
idx2d = ittools.Index2D(shape) |
174
|
|
|
assert idx2d(number) == expected_result |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
# -------------- ittools.zip_split ------------------ |
178
|
|
|
def test_zip_split(): |
179
|
|
|
"""Test correct ittools.zip_split functionaility.""" |
180
|
|
|
result = [["hi", "hi", "hi", "hi"], ["hi", "hi", "hi"], ["hi", "hi", "hi"]] |
181
|
|
|
assert list(ittools.zip_split(10 * ["hi"], 3)) == result |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
# -------------- ittools.group ------------------ |
185
|
|
|
def test_group(): |
186
|
|
|
"""Test correct ittools.group functionaility.""" |
187
|
|
|
result = [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, None, None)] |
188
|
|
|
assert list(ittools.group(range(10), chunks=3)) == result |
189
|
|
|
|