-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_csv2json.py
164 lines (127 loc) · 4.56 KB
/
test_csv2json.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
import time
import datetime
try:
from tqdm import tqdm
except ImportError:
tqdm = list
from csv2json import headers2template, json2csv_headers
fromisoformat = lambda a: datetime.datetime.strptime(a, "%Y-%m-%dT%H:%M:%S")
# samples (default options)
def test_basic():
headers = "abc.0,abc.1,status,item1.subitem1".split(",")
assert headers2template(headers).render_as_dict([1, 2, 3, 0]) == {
"abc": [1, 2],
"item1": {"subitem1": 0},
"status": 3,
}
def test_basic_list_with_dict():
headers = "abc.0.def,abc.1,status".split(",")
assert headers2template(headers).render_as_dict([3, 1, 2]) == {
"abc": [{"def": 3}, 1],
"status": 2,
}
def test_basic_non_uniform_list():
headers = "abc.10,abc.1,status".split(",")
assert headers2template(headers).render_as_dict([3, 1, 2]) == {
"abc": [None, 1, None, None, None, None, None, None, None, None, 3],
"status": 2,
}
def test_basic_sub_list_with_dict():
headers = "abc.5.3.def,abc.1,status".split(",")
assert headers2template(headers).render_as_dict([3, 1, 2]) == {
"abc": [None, 1, None, None, None, [None, None, None, {"def": 3}]],
"status": 2,
}
def test_basic_types_conservation():
headers = "abc.5.3.def,abc.1,status,foo.bar".split(",")
assert headers2template(headers).render_as_dict(
["3", "1", datetime.datetime(2019, 11, 1), True]
) == {
"abc": [None, "1", None, None, None, [None, None, None, {"def": "3"}]],
"status": datetime.datetime(2019, 11, 1),
"foo": {"bar": True},
}
def test_basic_perf():
headers = "abc.5.3.def,abc.1,status,foo.bar".split(",")
n = 10_000
start = time.time()
t = headers2template(headers)
for _ in tqdm(range(n)):
t.render_as_dict(["3", "1", datetime.datetime(2019, 11, 1), True])
print(f"completed {n} basic resolution in {time.time() - start}")
def test_multiple_rows():
headers = "a,b,c".split(",")
t = headers2template(headers)
for a, b, c in ["abc", "def", ("1", "345345", "abc")]:
assert t.render_as_dict([a, b, c]) == {
"a": a,
"b": b,
"c": c,
}
# sample with options
def test_with_options_types():
headers = "abc.3,status,def".split(",")
options = {
"abc.3": {"infer_type": True},
"def": {"infer_type": True},
"status": {"render": fromisoformat},
}
assert headers2template(headers, options=options).render_as_dict(
["1", "2019-02-01T01:01:01", "d,e,f"]
) == {
"abc": [None, None, None, 1],
"status": datetime.datetime(2019, 2, 1, 1, 1, 1),
"def": ["d", "e", "f"],
}
def test_with_options_on_value():
headers = "abc.0,abc.3,status,item1.subitem1,foo".split(",")
options = {
"abc.3": {"infer_type": True},
"abc": {"fill_value": 0},
"status": {"render": fromisoformat},
"foo": {"optional": len},
}
assert headers2template(headers, options=options).render_as_dict(
["1", "2", "2019-02-01T01:01:01", "0", ""]
) == {
"abc": ["1", 0, 0, 2],
"item1": {"subitem1": "0"},
"status": datetime.datetime(2019, 2, 1, 1, 1, 1),
}
def test_with_options_perf():
n = 10_000
headers = "abc.0,abc.3,status,item1.subitem1,foo".split(",")
options = {
"abc.3": {"infer_type": True},
"abc": {"fill_value": 0},
"status": {"render": fromisoformat},
"foo": {"optional": len},
}
start = time.time()
t = headers2template(headers, options=options)
for _ in tqdm(range(n)):
t.render_as_dict(["1", "2", "2019-02-01T01:01:01", "0", ""])
print(f"completed {n} complex resolution in {time.time() - start}")
def test_optional_multi_level():
headers = "abc.0,foo.0,foo.1".split(",")
options = {
"abc.0": {"optional": len},
"foo": {"optional": len},
"foo.0": {"optional": len},
"foo.1": {"optional": len},
}
assert headers2template(headers, options=options).render_as_dict(["", "", ""]) == {
"abc": []
}
# test json to csv
def test_empty_json_2_csv():
assert json2csv_headers("{}") == ([], [])
assert json2csv_headers({}) == ([], [])
def test_json_with_null_2_csv():
assert json2csv_headers('{"a": "true","b": null}') == (["a", "b"], ["true", None])
assert json2csv_headers({"a": "true", "b": None}) == (["a", "b"], ["true", None])
def test_json_2_csv():
assert json2csv_headers('{"a": "true","b": [1, {"f": 2}, 234]}') == (
["a", "b.0", "b.1.f", "b.2"],
["true", 1, 2, 234],
)