Passed
Pull Request — main (#1)
by Mathias
01:30 queued 14s
created

tests.test_api.test_nestify()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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