-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_core.py
199 lines (133 loc) · 7.66 KB
/
test_core.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import datetime
import numpy as np #type: ignore
import pytest #type: ignore
from frame_fixtures.core import Fixture
from frame_fixtures.core import SourceValues
from frame_fixtures.core import iter_shift
from frame_fixtures.core import COUNT_INIT
from frame_fixtures.core import Grammar
from frame_fixtures.core import GrammarDoc
# from frame_fixtures.core import parse
from frame_fixtures.core import FrameFixtureSyntaxError
from frame_fixtures.core import repeat_count
def test_iter_shift_a() -> None:
assert list(iter_shift(range(5), 3, wrap=True)) == [3, 4, 0, 1, 2]
assert list(iter_shift(range(5), 1, wrap=True)) == [1, 2, 3, 4, 0]
assert list(iter_shift(range(5), 10, wrap=True)) == list(range(5))
def test_repeat_count_a() -> None:
post = list(repeat_count(range(4), count=3))
assert post == [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3]
def test_repeat_count_b() -> None:
with pytest.raises(ValueError):
post = list(repeat_count(range(4), count=-3))
#-------------------------------------------------------------------------------
def test_source_values_a() -> None:
SourceValues.update_primitives()
post = SourceValues._INTS
assert len(post) == SourceValues._COUNT
# assert post[:3].tolist() == [845545, 563150, 468891]
size = COUNT_INIT * 2 + 1
SourceValues.update_primitives(size)
assert len(SourceValues._INTS) == size * 2
assert len(SourceValues._CHARS) == size * 2
assert SourceValues._INTS[:4].tolist() == post[:4].tolist()
def test_source_values_b() -> None:
SourceValues.update_primitives()
post = SourceValues._CHARS
assert len(post) == SourceValues._COUNT
# assert post[:3].tolist() == ['fa14b27e5f09', 'ebbc39aaf008', '04e42a6ee7a9']
# import ipdb; ipdb.set_trace()
def test_source_values_dtype_to_element_iter_a() -> None:
for dtype in (
np.dtype('i'),
np.dtype('u8'),
np.dtype('f8'),
np.dtype('c16'),
np.dtype('?'),
np.dtype('U'),
np.dtype('datetime64[D]'),
np.dtype(object),
):
gen = SourceValues.dtype_to_element_iter(dtype)
[next(gen) for x in range(10)]
def test_source_values_dtype_to_element_iter_b() -> None:
a = list(x for x, _ in zip(
SourceValues.dtype_to_element_iter(np.dtype('i')),
range(8),
))
b = list(x for x, _ in zip(
SourceValues.dtype_to_element_iter(np.dtype('i'), shift=3),
range(5),
))
assert a[3:] == b
def test_source_values_dtype_to_element_iter_c() -> None:
a = list(x for x, _ in zip(
SourceValues.dtype_to_element_iter(np.dtype('i')),
range(8),
))
def test_source_values_dtype_spec_to_array_c() -> None:
a = SourceValues.dtype_spec_to_array(np.dtype('timedelta64[Y]'),
shift=101, count=3)
assert len(a) == 3
assert a.tolist() == [188510, 61878, 194249]
def test_source_values_dtype_spec_to_array_d() -> None:
with pytest.raises(NotImplementedError):
a = SourceValues.dtype_spec_to_array(np.dtype('V'), shift=101, count=3)
def test_source_values_dtype_spec_to_array_e() -> None:
a = SourceValues.dtype_spec_to_array((int, str), shift=101, count=3)
assert a.tolist() == [(188510, 'zlm0'), (-61878, 'zDIP'), (194249, 'zOgj')]
#-------------------------------------------------------------------------------
def test_grammer_a() -> None:
with pytest.raises(FrameFixtureSyntaxError):
Grammar.dsl_to_str_constructors('')
with pytest.raises(FrameFixtureSyntaxError):
Grammar.dsl_to_str_constructors('i(I,str)')
with pytest.raises(FrameFixtureSyntaxError):
Grammar.dsl_to_str_constructors('f(a,b)|s(3,3)')
with pytest.raises(FrameFixtureSyntaxError):
Grammar.dsl_to_str_constructors('i(a)|s(3,3)')
with pytest.raises(FrameFixtureSyntaxError):
Grammar.dsl_to_str_constructors('i(a,b,c)|s(3,3)')
with pytest.raises(FrameFixtureSyntaxError):
Grammar.dsl_to_str_constructors('c(a)|s(3,3)')
with pytest.raises(FrameFixtureSyntaxError):
Grammar.dsl_to_str_constructors('c(a,b,c)|s(3,3)')
with pytest.raises(FrameFixtureSyntaxError):
Grammar.dsl_to_str_constructors('x(a,b,c)|s(3,3)')
with pytest.raises(FrameFixtureSyntaxError):
Grammar.dsl_to_str_constructors('lambda x: 3')
with pytest.raises(FrameFixtureSyntaxError):
Grammar.dsl_to_str_constructors('s(3,3)|i(I,lambda x: 3)')
#-------------------------------------------------------------------------------
def test_fixture_to_frame_a() -> None:
# msg1 = 'f(Fg)|i(I,str)|c(IDg,dtD)|v(float)|s(4,6)'
msg = 'f(F)|i((I,I),(str,int))|c((IS,I),(dts,int))|v(str,bool,object)|s(4,6)'
f1 = Fixture.parse(msg)
assert f1.to_pairs(0) == (((datetime.datetime(1970, 1, 1, 9, 38, 35), 105269), ((('zZbu', 105269), 'zjZQ'), (('zZbu', 119909), 'zO5l'), (('ztsv', 194224), 'zEdH'), (('ztsv', 172133), 'zB7E'))), ((datetime.datetime(1970, 1, 1, 9, 38, 35), 119909), ((('zZbu', 105269), False), (('zZbu', 119909), False), (('ztsv', 194224), False), (('ztsv', 172133), False))), ((datetime.datetime(1970, 1, 1, 1, 0, 48), 194224), ((('zZbu', 105269), True), (('zZbu', 119909), False), (('ztsv', 194224), 105269), (('ztsv', 172133), 119909))), ((datetime.datetime(1970, 1, 1, 1, 0, 48), 172133), ((('zZbu', 105269), 'z2Oo'), (('zZbu', 119909), 'z5l6'), (('ztsv', 194224), 'zCE3'), (('ztsv', 172133), 'zr4u'))), ((datetime.datetime(1970, 1, 2, 1, 21, 41), 96520), ((('zZbu', 105269), True), (('zZbu', 119909), True), (('ztsv', 194224), True), (('ztsv', 172133), False))), ((datetime.datetime(1970, 1, 2, 1, 21, 41), -88017), ((('zZbu', 105269), 92867), (('zZbu', 119909), 3884.98), (('ztsv', 194224), -646.86), (('ztsv', 172133), -314.34))))
def test_fixture_to_frame_b() -> None:
f1 = Fixture.parse('s(2,2)')
assert f1.to_pairs(0) == ((0, ((0, 1930.4), (1, -1760.34))), (1, ((0, -610.8), (1, 3243.94))))
def test_fixture_to_frame_c() -> None:
f1 = Fixture.parse('s(2,6)|c(IH,(str,dtD,int,int))')
assert f1.to_pairs(0) == ((('zZbu', datetime.date(2258, 3, 21), 58768, -97851), ((0, 1930.4), (1, -1760.34))), (('zZbu', datetime.date(2258, 3, 21), 58768, 168362), ((0, -610.8), (1, 3243.94))), (('zZbu', datetime.date(2258, 3, 21), 146284, 130010), ((0, 694.3), (1, -72.96))), (('zZbu', datetime.date(2258, 3, 21), 146284, -150573), ((0, 1080.4), (1, 2580.34))), (('zZbu', datetime.date(2298, 4, 20), 170440, -157437), ((0, 3511.58), (1, 1175.36))), (('zZbu', datetime.date(2298, 4, 20), 170440, 35684), ((0, 1857.34), (1, 1699.34))))
def test_fixture_to_frame_d() -> None:
f1 = Fixture.parse('s(2,2)|v(tdD,tds)')
assert f1.to_pairs(0) == ((0, ((0, np.timedelta64(88017,'D')), (1, np.timedelta64(92867,'D')))), (1, ((0, np.timedelta64(162197,'s')), (1, np.timedelta64(41157,'s')))))
def test_fixture_to_frame_e() -> None:
with pytest.raises(RuntimeError):
f1 = Fixture.parse('s(2,2)|c(IH,tds)')
with pytest.raises(RuntimeError):
f1 = Fixture.parse('s(2,2)|c((I,I),(int,str,float))')
def test_large_a() -> None:
f1 = Fixture.parse('s(200000,4)|i(I,int)|c(I,str)|v(str)')
assert f1.shape == (200000, 4)
#-------------------------------------------------------------------------------
def test_import() -> None:
import frame_fixtures as ff
f1 = ff.parse('s(2,2)|i(I,str)')
assert f1.to_pairs(0) == ((0, (('zZbu', 1930.4), ('ztsv', -1760.34))), (1, (('zZbu', -610.8), ('ztsv', 3243.94))))
#-------------------------------------------------------------------------------
def test_grammar_definition() -> None:
cc = GrammarDoc.container_components()
cc = GrammarDoc.specifiers_constructor()
cc = GrammarDoc.specifiers_dtype()