-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
146 lines (124 loc) · 3.84 KB
/
test.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
from io import StringIO, open
import pytest
import pwk
@pytest.mark.parametrize("input_format", ["csv", "tsv"])
@pytest.mark.parametrize("output_format", ["csv", "tsv", "plain"])
def test_file_formats(input_format, output_format):
input_filename = f"test_files/test.{input_format}"
output_filename = f"test_files/test_output.{output_format}"
output = StringIO()
pwk.main(
["_1 * 2, _2.upper(), _3 + '?'", input_filename, "-o", output_format], output
)
with open(output_filename, newline="") as output_file:
expected_output = output_file.read()
assert output.getvalue() == expected_output
def test_aggregate():
output = StringIO()
pwk.main(["-a", "sum(_2)", "test_files/aggregate.csv", "-o", "csv"], output)
with open("test_files/aggregate_output.csv", newline="") as output_file:
expected_output = output_file.read()
assert output.getvalue() == expected_output
def test_header():
output = StringIO()
pwk.main(
[
"_['first name'] + ' ' + _['last_name'], _['phone-number']",
"test_files/header.csv",
"-H",
"-s",
"-o",
"csv",
],
output,
)
with open("test_files/header_output.csv", newline="") as output_file:
expected_output = output_file.read()
assert output.getvalue() == expected_output
# Aggregation
output = StringIO()
pwk.main(
["sum(_['phone-number'])", "test_files/header.csv", "-H", "-a", "-o", "csv"],
output,
)
assert output.getvalue() == "1111111110\r\n"
def test_regex():
# Using capture groups
output = StringIO()
pwk.main(
[
"fullmatch(r'(.+?)://(?:.+?@)?(.+?)(?::.+)?(?:/.+)?', _1)",
"test_files/regex.txt",
"-o",
"csv",
],
output,
)
with open("test_files/regex_output.csv", newline="") as output_file:
expected_output = output_file.read()
assert output.getvalue() == expected_output
# Without using capture groups
output = StringIO()
pwk.main(
[
"match(r'[^:]+', _1), search(r'\\w+\\.\\w+', _1)",
"test_files/regex.txt",
"-o",
"csv",
],
output,
)
with open("test_files/regex_output.csv", newline="") as output_file:
expected_output = output_file.read()
assert output.getvalue() == expected_output
def test_iterable():
output = StringIO()
pwk.main(
[
"tuple(_1), list(enumerate(_2))",
"test_files/iterable.csv",
"-s",
"-o",
"csv",
],
output,
)
with open("test_files/iterable_output.csv", newline="") as output_file:
expected_output = output_file.read()
assert output.getvalue() == expected_output
def test_filter():
output = StringIO()
pwk.main(
[
"_0 if _n % 2 == 1 else None",
"test_files/filter.csv",
"-o",
"csv",
],
output,
)
with open("test_files/filter_output.csv", newline="") as output_file:
expected_output = output_file.read()
assert output.getvalue() == expected_output
def test_import():
output = StringIO()
pwk.main(
[
"html.escape(_1), html.unescape(_2)",
"test_files/import.csv",
"-o",
"csv",
"-I",
"html",
],
output,
)
with open("test_files/import_output.csv", newline="") as output_file:
expected_output = output_file.read()
assert output.getvalue() == expected_output
def test_syntax_error():
with pytest.raises(SyntaxError):
pwk.main(["_1 ? _2"])
def test_runtime_error():
with pytest.raises(ZeroDivisionError):
pwk.main(["-i", "csv", "_1 / _2", "test_files/error_zerodiv.csv"])