forked from ron-rivest/audit-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_csv_readers.py
188 lines (165 loc) · 5.33 KB
/
test_csv_readers.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
import os
import sys
from io import StringIO
from contextlib import redirect_stdout
from csv_readers import read_csv_file
test_filename = "test_file.csv"
def test_csv_len():
# First sample given in csv_readers.py
contents = "A,B,C\n1,2,3\n4,5"
expected = [
{'A':'1', 'B':'2', 'C':'3'},
{'A':'4', 'B':'5', 'C':''}
]
with open(test_filename, "w") as f:
f.write(contents)
assert read_csv_file(test_filename) == expected
os.remove(test_filename)
def test_csv_varlen():
# Second sample given in csv_readers.py
contents = "A,B,C\n1,2,3\n4,5\n6,7,8,9"
with open(test_filename, "w") as f:
f.write(contents)
expected = [
{'A':'1', 'B':'2', 'C':('3',)},
{'A':'4', 'B':'5', 'C':()},
{'A':'6', 'B':'7', 'C':('8','9')}
]
assert read_csv_file(test_filename, varlen=True) == expected
os.remove(test_filename)
def test_csv_oneEntry_len():
# One header with one entry
contents = "A\n\n1"
expected = [
{'A':''},
{'A':'1'}
]
with open(test_filename, "w") as f:
f.write(contents)
assert read_csv_file(test_filename) == expected
os.remove(test_filename)
def test_csv_emptyEntries_len():
# 5 headers with 2 entries in 1 line
contents = "A,B,C,D,E\n1,2\n1,2,3,4,5"
expected = [
{'A':'1','B':'2','C':'','D':'','E':''},
{'A':'1','B': '2','C':'3','D':'4','E':'5'}
]
with open(test_filename, "w") as f:
f.write(contents)
assert read_csv_file(test_filename) == expected
os.remove(test_filename)
def test_csv_extraEntries_len():
# non-varlen error when more entries than fields
contents = "A,B\n1,2,3"
expected = [
{'A':'1','B':'2'}
]
# expected warning message - "Ignoring extra values in row:0"
with open(test_filename, "w") as f:
f.write(contents)
assert read_csv_file(test_filename) == expected
os.remove(test_filename)
def test_csv_empty_line_len():
# 5 headers with no entries
contents = "A,B,C,D,E\n\n1,2,3,4,5"
expected = [
{'A':'','B': '','C':'','D':'','E':''},
{'A':'1','B': '2','C':'3','D':'4','E':'5'}
]
with open(test_filename, "w") as f:
f.write(contents)
assert read_csv_file(test_filename) == expected
os.remove(test_filename)
def test_csv_tooShortRow_varlen():
contents = "A,B,C\n1\n1,2,3"
expected = [
{'A':'1','B':'2','C':'3'}
]
with open(test_filename, "w") as f:
f.write(contents)
f = StringIO()
with redirect_stdout(f):
try:
read_csv_file(test_filename, varlen=True)
except SystemExit:
pass
s = f.getvalue()
lines = [
"Reading CSV file: test_file.csv",
"WARNING: Ignoring too-short row:{}\n".format(['1'])
]
assert s == "\n".join(lines)
os.remove(test_filename)
def test_csv_longTuple_varlen():
# Testing the tupling of the last elements if longer than header
# Note: Documentation for varlen tuples, inside the tuples should be STRING, not INT
contents = "A,B,C\n1,2,3\n4,5\n6,7,8,9,10,11,12,13,14,15"
expected = [
{'A':'1', 'B':'2', 'C':('3',)},
{'A':'4', 'B':'5', 'C':()},
{'A': '6', 'B': '7', 'C': ('8', '9', '10', '11', '12', '13', '14', '15')}
]
with open(test_filename, "w") as f:
f.write(contents)
assert read_csv_file(test_filename, varlen=True) == expected
os.remove(test_filename)
def test_csv_required_fieldname_missing():
# required file name test
contents = "A,B,C\n1,2,3\n4,5"
fieldnames = ["A","B","C"]
missing_fieldnames = set(["D"])
required_fieldnames = ['A','B','C','D']
with open(test_filename, "w") as f:
f.write(contents)
f = StringIO()
with redirect_stdout(f):
try:
read_csv_file(test_filename, required_fieldnames=required_fieldnames)
except SystemExit:
pass
s = f.getvalue()
lines = [
"Reading CSV file: test_file.csv",
"FATAL ERROR: File {} has fieldnames {}, while {} are required. Missing {}.\n".format(
test_filename, fieldnames, required_fieldnames, missing_fieldnames)
]
assert s == "\n".join(lines)
os.remove(test_filename)
def test_csv_extra_required_fieldname():
# required file name test
contents = "A,B,C\n1,2,3\n4,5"
fieldnames = ["A","B","C"]
extra_fieldnames = set(["C"])
with open(test_filename, 'w') as f:
f.write(contents)
f = StringIO()
with redirect_stdout(f):
try:
read_csv_file(test_filename, required_fieldnames=['A','B'])
except SystemExit:
pass
s = f.getvalue()
lines = [
"Reading CSV file: test_file.csv",
"WARNING: File {} has extra fieldnames (ignored): {}\n".format(test_filename, extra_fieldnames)
]
assert s == "\n".join(lines)
os.remove(test_filename)
def test_csv_duplicate_field():
contents = "A,B,B\n1,2,3\n4,5"
with open(test_filename, 'w') as f:
f.write(contents)
f = StringIO()
with redirect_stdout(f):
try:
read_csv_file(test_filename)
except SystemExit:
pass
s = f.getvalue()
lines = [
"Reading CSV file: test_file.csv",
"FATAL ERROR: Duplicate field name:{}\n".format(['A', 'B', 'B'])
]
assert s == "\n".join(lines)
os.remove(test_filename)