1 | n/a | # Copyright (C) 2001,2002 Python Software Foundation |
---|
2 | n/a | # csv package unit tests |
---|
3 | n/a | |
---|
4 | n/a | import copy |
---|
5 | n/a | import sys |
---|
6 | n/a | import unittest |
---|
7 | n/a | from io import StringIO |
---|
8 | n/a | from tempfile import TemporaryFile |
---|
9 | n/a | import csv |
---|
10 | n/a | import gc |
---|
11 | n/a | import pickle |
---|
12 | n/a | from test import support |
---|
13 | n/a | from itertools import permutations |
---|
14 | n/a | from textwrap import dedent |
---|
15 | n/a | from collections import OrderedDict |
---|
16 | n/a | |
---|
17 | n/a | class Test_Csv(unittest.TestCase): |
---|
18 | n/a | """ |
---|
19 | n/a | Test the underlying C csv parser in ways that are not appropriate |
---|
20 | n/a | from the high level interface. Further tests of this nature are done |
---|
21 | n/a | in TestDialectRegistry. |
---|
22 | n/a | """ |
---|
23 | n/a | def _test_arg_valid(self, ctor, arg): |
---|
24 | n/a | self.assertRaises(TypeError, ctor) |
---|
25 | n/a | self.assertRaises(TypeError, ctor, None) |
---|
26 | n/a | self.assertRaises(TypeError, ctor, arg, bad_attr = 0) |
---|
27 | n/a | self.assertRaises(TypeError, ctor, arg, delimiter = 0) |
---|
28 | n/a | self.assertRaises(TypeError, ctor, arg, delimiter = 'XX') |
---|
29 | n/a | self.assertRaises(csv.Error, ctor, arg, 'foo') |
---|
30 | n/a | self.assertRaises(TypeError, ctor, arg, delimiter=None) |
---|
31 | n/a | self.assertRaises(TypeError, ctor, arg, delimiter=1) |
---|
32 | n/a | self.assertRaises(TypeError, ctor, arg, quotechar=1) |
---|
33 | n/a | self.assertRaises(TypeError, ctor, arg, lineterminator=None) |
---|
34 | n/a | self.assertRaises(TypeError, ctor, arg, lineterminator=1) |
---|
35 | n/a | self.assertRaises(TypeError, ctor, arg, quoting=None) |
---|
36 | n/a | self.assertRaises(TypeError, ctor, arg, |
---|
37 | n/a | quoting=csv.QUOTE_ALL, quotechar='') |
---|
38 | n/a | self.assertRaises(TypeError, ctor, arg, |
---|
39 | n/a | quoting=csv.QUOTE_ALL, quotechar=None) |
---|
40 | n/a | |
---|
41 | n/a | def test_reader_arg_valid(self): |
---|
42 | n/a | self._test_arg_valid(csv.reader, []) |
---|
43 | n/a | |
---|
44 | n/a | def test_writer_arg_valid(self): |
---|
45 | n/a | self._test_arg_valid(csv.writer, StringIO()) |
---|
46 | n/a | |
---|
47 | n/a | def _test_default_attrs(self, ctor, *args): |
---|
48 | n/a | obj = ctor(*args) |
---|
49 | n/a | # Check defaults |
---|
50 | n/a | self.assertEqual(obj.dialect.delimiter, ',') |
---|
51 | n/a | self.assertEqual(obj.dialect.doublequote, True) |
---|
52 | n/a | self.assertEqual(obj.dialect.escapechar, None) |
---|
53 | n/a | self.assertEqual(obj.dialect.lineterminator, "\r\n") |
---|
54 | n/a | self.assertEqual(obj.dialect.quotechar, '"') |
---|
55 | n/a | self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL) |
---|
56 | n/a | self.assertEqual(obj.dialect.skipinitialspace, False) |
---|
57 | n/a | self.assertEqual(obj.dialect.strict, False) |
---|
58 | n/a | # Try deleting or changing attributes (they are read-only) |
---|
59 | n/a | self.assertRaises(AttributeError, delattr, obj.dialect, 'delimiter') |
---|
60 | n/a | self.assertRaises(AttributeError, setattr, obj.dialect, 'delimiter', ':') |
---|
61 | n/a | self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting') |
---|
62 | n/a | self.assertRaises(AttributeError, setattr, obj.dialect, |
---|
63 | n/a | 'quoting', None) |
---|
64 | n/a | |
---|
65 | n/a | def test_reader_attrs(self): |
---|
66 | n/a | self._test_default_attrs(csv.reader, []) |
---|
67 | n/a | |
---|
68 | n/a | def test_writer_attrs(self): |
---|
69 | n/a | self._test_default_attrs(csv.writer, StringIO()) |
---|
70 | n/a | |
---|
71 | n/a | def _test_kw_attrs(self, ctor, *args): |
---|
72 | n/a | # Now try with alternate options |
---|
73 | n/a | kwargs = dict(delimiter=':', doublequote=False, escapechar='\\', |
---|
74 | n/a | lineterminator='\r', quotechar='*', |
---|
75 | n/a | quoting=csv.QUOTE_NONE, skipinitialspace=True, |
---|
76 | n/a | strict=True) |
---|
77 | n/a | obj = ctor(*args, **kwargs) |
---|
78 | n/a | self.assertEqual(obj.dialect.delimiter, ':') |
---|
79 | n/a | self.assertEqual(obj.dialect.doublequote, False) |
---|
80 | n/a | self.assertEqual(obj.dialect.escapechar, '\\') |
---|
81 | n/a | self.assertEqual(obj.dialect.lineterminator, "\r") |
---|
82 | n/a | self.assertEqual(obj.dialect.quotechar, '*') |
---|
83 | n/a | self.assertEqual(obj.dialect.quoting, csv.QUOTE_NONE) |
---|
84 | n/a | self.assertEqual(obj.dialect.skipinitialspace, True) |
---|
85 | n/a | self.assertEqual(obj.dialect.strict, True) |
---|
86 | n/a | |
---|
87 | n/a | def test_reader_kw_attrs(self): |
---|
88 | n/a | self._test_kw_attrs(csv.reader, []) |
---|
89 | n/a | |
---|
90 | n/a | def test_writer_kw_attrs(self): |
---|
91 | n/a | self._test_kw_attrs(csv.writer, StringIO()) |
---|
92 | n/a | |
---|
93 | n/a | def _test_dialect_attrs(self, ctor, *args): |
---|
94 | n/a | # Now try with dialect-derived options |
---|
95 | n/a | class dialect: |
---|
96 | n/a | delimiter='-' |
---|
97 | n/a | doublequote=False |
---|
98 | n/a | escapechar='^' |
---|
99 | n/a | lineterminator='$' |
---|
100 | n/a | quotechar='#' |
---|
101 | n/a | quoting=csv.QUOTE_ALL |
---|
102 | n/a | skipinitialspace=True |
---|
103 | n/a | strict=False |
---|
104 | n/a | args = args + (dialect,) |
---|
105 | n/a | obj = ctor(*args) |
---|
106 | n/a | self.assertEqual(obj.dialect.delimiter, '-') |
---|
107 | n/a | self.assertEqual(obj.dialect.doublequote, False) |
---|
108 | n/a | self.assertEqual(obj.dialect.escapechar, '^') |
---|
109 | n/a | self.assertEqual(obj.dialect.lineterminator, "$") |
---|
110 | n/a | self.assertEqual(obj.dialect.quotechar, '#') |
---|
111 | n/a | self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL) |
---|
112 | n/a | self.assertEqual(obj.dialect.skipinitialspace, True) |
---|
113 | n/a | self.assertEqual(obj.dialect.strict, False) |
---|
114 | n/a | |
---|
115 | n/a | def test_reader_dialect_attrs(self): |
---|
116 | n/a | self._test_dialect_attrs(csv.reader, []) |
---|
117 | n/a | |
---|
118 | n/a | def test_writer_dialect_attrs(self): |
---|
119 | n/a | self._test_dialect_attrs(csv.writer, StringIO()) |
---|
120 | n/a | |
---|
121 | n/a | |
---|
122 | n/a | def _write_test(self, fields, expect, **kwargs): |
---|
123 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
124 | n/a | writer = csv.writer(fileobj, **kwargs) |
---|
125 | n/a | writer.writerow(fields) |
---|
126 | n/a | fileobj.seek(0) |
---|
127 | n/a | self.assertEqual(fileobj.read(), |
---|
128 | n/a | expect + writer.dialect.lineterminator) |
---|
129 | n/a | |
---|
130 | n/a | def _write_error_test(self, exc, fields, **kwargs): |
---|
131 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
132 | n/a | writer = csv.writer(fileobj, **kwargs) |
---|
133 | n/a | with self.assertRaises(exc): |
---|
134 | n/a | writer.writerow(fields) |
---|
135 | n/a | fileobj.seek(0) |
---|
136 | n/a | self.assertEqual(fileobj.read(), '') |
---|
137 | n/a | |
---|
138 | n/a | def test_write_arg_valid(self): |
---|
139 | n/a | self._write_error_test(csv.Error, None) |
---|
140 | n/a | self._write_test((), '') |
---|
141 | n/a | self._write_test([None], '""') |
---|
142 | n/a | self._write_error_test(csv.Error, [None], quoting = csv.QUOTE_NONE) |
---|
143 | n/a | # Check that exceptions are passed up the chain |
---|
144 | n/a | class BadList: |
---|
145 | n/a | def __len__(self): |
---|
146 | n/a | return 10; |
---|
147 | n/a | def __getitem__(self, i): |
---|
148 | n/a | if i > 2: |
---|
149 | n/a | raise OSError |
---|
150 | n/a | self._write_error_test(OSError, BadList()) |
---|
151 | n/a | class BadItem: |
---|
152 | n/a | def __str__(self): |
---|
153 | n/a | raise OSError |
---|
154 | n/a | self._write_error_test(OSError, [BadItem()]) |
---|
155 | n/a | |
---|
156 | n/a | def test_write_bigfield(self): |
---|
157 | n/a | # This exercises the buffer realloc functionality |
---|
158 | n/a | bigstring = 'X' * 50000 |
---|
159 | n/a | self._write_test([bigstring,bigstring], '%s,%s' % \ |
---|
160 | n/a | (bigstring, bigstring)) |
---|
161 | n/a | |
---|
162 | n/a | def test_write_quoting(self): |
---|
163 | n/a | self._write_test(['a',1,'p,q'], 'a,1,"p,q"') |
---|
164 | n/a | self._write_error_test(csv.Error, ['a',1,'p,q'], |
---|
165 | n/a | quoting = csv.QUOTE_NONE) |
---|
166 | n/a | self._write_test(['a',1,'p,q'], 'a,1,"p,q"', |
---|
167 | n/a | quoting = csv.QUOTE_MINIMAL) |
---|
168 | n/a | self._write_test(['a',1,'p,q'], '"a",1,"p,q"', |
---|
169 | n/a | quoting = csv.QUOTE_NONNUMERIC) |
---|
170 | n/a | self._write_test(['a',1,'p,q'], '"a","1","p,q"', |
---|
171 | n/a | quoting = csv.QUOTE_ALL) |
---|
172 | n/a | self._write_test(['a\nb',1], '"a\nb","1"', |
---|
173 | n/a | quoting = csv.QUOTE_ALL) |
---|
174 | n/a | |
---|
175 | n/a | def test_write_escape(self): |
---|
176 | n/a | self._write_test(['a',1,'p,q'], 'a,1,"p,q"', |
---|
177 | n/a | escapechar='\\') |
---|
178 | n/a | self._write_error_test(csv.Error, ['a',1,'p,"q"'], |
---|
179 | n/a | escapechar=None, doublequote=False) |
---|
180 | n/a | self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""', |
---|
181 | n/a | escapechar='\\', doublequote = False) |
---|
182 | n/a | self._write_test(['"'], '""""', |
---|
183 | n/a | escapechar='\\', quoting = csv.QUOTE_MINIMAL) |
---|
184 | n/a | self._write_test(['"'], '\\"', |
---|
185 | n/a | escapechar='\\', quoting = csv.QUOTE_MINIMAL, |
---|
186 | n/a | doublequote = False) |
---|
187 | n/a | self._write_test(['"'], '\\"', |
---|
188 | n/a | escapechar='\\', quoting = csv.QUOTE_NONE) |
---|
189 | n/a | self._write_test(['a',1,'p,q'], 'a,1,p\\,q', |
---|
190 | n/a | escapechar='\\', quoting = csv.QUOTE_NONE) |
---|
191 | n/a | |
---|
192 | n/a | def test_write_iterable(self): |
---|
193 | n/a | self._write_test(iter(['a', 1, 'p,q']), 'a,1,"p,q"') |
---|
194 | n/a | self._write_test(iter(['a', 1, None]), 'a,1,') |
---|
195 | n/a | self._write_test(iter([]), '') |
---|
196 | n/a | self._write_test(iter([None]), '""') |
---|
197 | n/a | self._write_error_test(csv.Error, iter([None]), quoting=csv.QUOTE_NONE) |
---|
198 | n/a | self._write_test(iter([None, None]), ',') |
---|
199 | n/a | |
---|
200 | n/a | def test_writerows(self): |
---|
201 | n/a | class BrokenFile: |
---|
202 | n/a | def write(self, buf): |
---|
203 | n/a | raise OSError |
---|
204 | n/a | writer = csv.writer(BrokenFile()) |
---|
205 | n/a | self.assertRaises(OSError, writer.writerows, [['a']]) |
---|
206 | n/a | |
---|
207 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
208 | n/a | writer = csv.writer(fileobj) |
---|
209 | n/a | self.assertRaises(TypeError, writer.writerows, None) |
---|
210 | n/a | writer.writerows([['a','b'],['c','d']]) |
---|
211 | n/a | fileobj.seek(0) |
---|
212 | n/a | self.assertEqual(fileobj.read(), "a,b\r\nc,d\r\n") |
---|
213 | n/a | |
---|
214 | n/a | @support.cpython_only |
---|
215 | n/a | def test_writerows_legacy_strings(self): |
---|
216 | n/a | import _testcapi |
---|
217 | n/a | |
---|
218 | n/a | c = _testcapi.unicode_legacy_string('a') |
---|
219 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
220 | n/a | writer = csv.writer(fileobj) |
---|
221 | n/a | writer.writerows([[c]]) |
---|
222 | n/a | fileobj.seek(0) |
---|
223 | n/a | self.assertEqual(fileobj.read(), "a\r\n") |
---|
224 | n/a | |
---|
225 | n/a | def _read_test(self, input, expect, **kwargs): |
---|
226 | n/a | reader = csv.reader(input, **kwargs) |
---|
227 | n/a | result = list(reader) |
---|
228 | n/a | self.assertEqual(result, expect) |
---|
229 | n/a | |
---|
230 | n/a | def test_read_oddinputs(self): |
---|
231 | n/a | self._read_test([], []) |
---|
232 | n/a | self._read_test([''], [[]]) |
---|
233 | n/a | self.assertRaises(csv.Error, self._read_test, |
---|
234 | n/a | ['"ab"c'], None, strict = 1) |
---|
235 | n/a | # cannot handle null bytes for the moment |
---|
236 | n/a | self.assertRaises(csv.Error, self._read_test, |
---|
237 | n/a | ['ab\0c'], None, strict = 1) |
---|
238 | n/a | self._read_test(['"ab"c'], [['abc']], doublequote = 0) |
---|
239 | n/a | |
---|
240 | n/a | self.assertRaises(csv.Error, self._read_test, |
---|
241 | n/a | [b'ab\0c'], None) |
---|
242 | n/a | |
---|
243 | n/a | |
---|
244 | n/a | def test_read_eol(self): |
---|
245 | n/a | self._read_test(['a,b'], [['a','b']]) |
---|
246 | n/a | self._read_test(['a,b\n'], [['a','b']]) |
---|
247 | n/a | self._read_test(['a,b\r\n'], [['a','b']]) |
---|
248 | n/a | self._read_test(['a,b\r'], [['a','b']]) |
---|
249 | n/a | self.assertRaises(csv.Error, self._read_test, ['a,b\rc,d'], []) |
---|
250 | n/a | self.assertRaises(csv.Error, self._read_test, ['a,b\nc,d'], []) |
---|
251 | n/a | self.assertRaises(csv.Error, self._read_test, ['a,b\r\nc,d'], []) |
---|
252 | n/a | |
---|
253 | n/a | def test_read_eof(self): |
---|
254 | n/a | self._read_test(['a,"'], [['a', '']]) |
---|
255 | n/a | self._read_test(['"a'], [['a']]) |
---|
256 | n/a | self._read_test(['^'], [['\n']], escapechar='^') |
---|
257 | n/a | self.assertRaises(csv.Error, self._read_test, ['a,"'], [], strict=True) |
---|
258 | n/a | self.assertRaises(csv.Error, self._read_test, ['"a'], [], strict=True) |
---|
259 | n/a | self.assertRaises(csv.Error, self._read_test, |
---|
260 | n/a | ['^'], [], escapechar='^', strict=True) |
---|
261 | n/a | |
---|
262 | n/a | def test_read_escape(self): |
---|
263 | n/a | self._read_test(['a,\\b,c'], [['a', 'b', 'c']], escapechar='\\') |
---|
264 | n/a | self._read_test(['a,b\\,c'], [['a', 'b,c']], escapechar='\\') |
---|
265 | n/a | self._read_test(['a,"b\\,c"'], [['a', 'b,c']], escapechar='\\') |
---|
266 | n/a | self._read_test(['a,"b,\\c"'], [['a', 'b,c']], escapechar='\\') |
---|
267 | n/a | self._read_test(['a,"b,c\\""'], [['a', 'b,c"']], escapechar='\\') |
---|
268 | n/a | self._read_test(['a,"b,c"\\'], [['a', 'b,c\\']], escapechar='\\') |
---|
269 | n/a | |
---|
270 | n/a | def test_read_quoting(self): |
---|
271 | n/a | self._read_test(['1,",3,",5'], [['1', ',3,', '5']]) |
---|
272 | n/a | self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']], |
---|
273 | n/a | quotechar=None, escapechar='\\') |
---|
274 | n/a | self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']], |
---|
275 | n/a | quoting=csv.QUOTE_NONE, escapechar='\\') |
---|
276 | n/a | # will this fail where locale uses comma for decimals? |
---|
277 | n/a | self._read_test([',3,"5",7.3, 9'], [['', 3, '5', 7.3, 9]], |
---|
278 | n/a | quoting=csv.QUOTE_NONNUMERIC) |
---|
279 | n/a | self._read_test(['"a\nb", 7'], [['a\nb', ' 7']]) |
---|
280 | n/a | self.assertRaises(ValueError, self._read_test, |
---|
281 | n/a | ['abc,3'], [[]], |
---|
282 | n/a | quoting=csv.QUOTE_NONNUMERIC) |
---|
283 | n/a | |
---|
284 | n/a | def test_read_bigfield(self): |
---|
285 | n/a | # This exercises the buffer realloc functionality and field size |
---|
286 | n/a | # limits. |
---|
287 | n/a | limit = csv.field_size_limit() |
---|
288 | n/a | try: |
---|
289 | n/a | size = 50000 |
---|
290 | n/a | bigstring = 'X' * size |
---|
291 | n/a | bigline = '%s,%s' % (bigstring, bigstring) |
---|
292 | n/a | self._read_test([bigline], [[bigstring, bigstring]]) |
---|
293 | n/a | csv.field_size_limit(size) |
---|
294 | n/a | self._read_test([bigline], [[bigstring, bigstring]]) |
---|
295 | n/a | self.assertEqual(csv.field_size_limit(), size) |
---|
296 | n/a | csv.field_size_limit(size-1) |
---|
297 | n/a | self.assertRaises(csv.Error, self._read_test, [bigline], []) |
---|
298 | n/a | self.assertRaises(TypeError, csv.field_size_limit, None) |
---|
299 | n/a | self.assertRaises(TypeError, csv.field_size_limit, 1, None) |
---|
300 | n/a | finally: |
---|
301 | n/a | csv.field_size_limit(limit) |
---|
302 | n/a | |
---|
303 | n/a | def test_read_linenum(self): |
---|
304 | n/a | r = csv.reader(['line,1', 'line,2', 'line,3']) |
---|
305 | n/a | self.assertEqual(r.line_num, 0) |
---|
306 | n/a | next(r) |
---|
307 | n/a | self.assertEqual(r.line_num, 1) |
---|
308 | n/a | next(r) |
---|
309 | n/a | self.assertEqual(r.line_num, 2) |
---|
310 | n/a | next(r) |
---|
311 | n/a | self.assertEqual(r.line_num, 3) |
---|
312 | n/a | self.assertRaises(StopIteration, next, r) |
---|
313 | n/a | self.assertEqual(r.line_num, 3) |
---|
314 | n/a | |
---|
315 | n/a | def test_roundtrip_quoteed_newlines(self): |
---|
316 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
317 | n/a | writer = csv.writer(fileobj) |
---|
318 | n/a | self.assertRaises(TypeError, writer.writerows, None) |
---|
319 | n/a | rows = [['a\nb','b'],['c','x\r\nd']] |
---|
320 | n/a | writer.writerows(rows) |
---|
321 | n/a | fileobj.seek(0) |
---|
322 | n/a | for i, row in enumerate(csv.reader(fileobj)): |
---|
323 | n/a | self.assertEqual(row, rows[i]) |
---|
324 | n/a | |
---|
325 | n/a | def test_roundtrip_escaped_unquoted_newlines(self): |
---|
326 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
327 | n/a | writer = csv.writer(fileobj,quoting=csv.QUOTE_NONE,escapechar="\\") |
---|
328 | n/a | rows = [['a\nb','b'],['c','x\r\nd']] |
---|
329 | n/a | writer.writerows(rows) |
---|
330 | n/a | fileobj.seek(0) |
---|
331 | n/a | for i, row in enumerate(csv.reader(fileobj,quoting=csv.QUOTE_NONE,escapechar="\\")): |
---|
332 | n/a | self.assertEqual(row,rows[i]) |
---|
333 | n/a | |
---|
334 | n/a | class TestDialectRegistry(unittest.TestCase): |
---|
335 | n/a | def test_registry_badargs(self): |
---|
336 | n/a | self.assertRaises(TypeError, csv.list_dialects, None) |
---|
337 | n/a | self.assertRaises(TypeError, csv.get_dialect) |
---|
338 | n/a | self.assertRaises(csv.Error, csv.get_dialect, None) |
---|
339 | n/a | self.assertRaises(csv.Error, csv.get_dialect, "nonesuch") |
---|
340 | n/a | self.assertRaises(TypeError, csv.unregister_dialect) |
---|
341 | n/a | self.assertRaises(csv.Error, csv.unregister_dialect, None) |
---|
342 | n/a | self.assertRaises(csv.Error, csv.unregister_dialect, "nonesuch") |
---|
343 | n/a | self.assertRaises(TypeError, csv.register_dialect, None) |
---|
344 | n/a | self.assertRaises(TypeError, csv.register_dialect, None, None) |
---|
345 | n/a | self.assertRaises(TypeError, csv.register_dialect, "nonesuch", 0, 0) |
---|
346 | n/a | self.assertRaises(TypeError, csv.register_dialect, "nonesuch", |
---|
347 | n/a | badargument=None) |
---|
348 | n/a | self.assertRaises(TypeError, csv.register_dialect, "nonesuch", |
---|
349 | n/a | quoting=None) |
---|
350 | n/a | self.assertRaises(TypeError, csv.register_dialect, []) |
---|
351 | n/a | |
---|
352 | n/a | def test_registry(self): |
---|
353 | n/a | class myexceltsv(csv.excel): |
---|
354 | n/a | delimiter = "\t" |
---|
355 | n/a | name = "myexceltsv" |
---|
356 | n/a | expected_dialects = csv.list_dialects() + [name] |
---|
357 | n/a | expected_dialects.sort() |
---|
358 | n/a | csv.register_dialect(name, myexceltsv) |
---|
359 | n/a | self.addCleanup(csv.unregister_dialect, name) |
---|
360 | n/a | self.assertEqual(csv.get_dialect(name).delimiter, '\t') |
---|
361 | n/a | got_dialects = sorted(csv.list_dialects()) |
---|
362 | n/a | self.assertEqual(expected_dialects, got_dialects) |
---|
363 | n/a | |
---|
364 | n/a | def test_register_kwargs(self): |
---|
365 | n/a | name = 'fedcba' |
---|
366 | n/a | csv.register_dialect(name, delimiter=';') |
---|
367 | n/a | self.addCleanup(csv.unregister_dialect, name) |
---|
368 | n/a | self.assertEqual(csv.get_dialect(name).delimiter, ';') |
---|
369 | n/a | self.assertEqual([['X', 'Y', 'Z']], list(csv.reader(['X;Y;Z'], name))) |
---|
370 | n/a | |
---|
371 | n/a | def test_incomplete_dialect(self): |
---|
372 | n/a | class myexceltsv(csv.Dialect): |
---|
373 | n/a | delimiter = "\t" |
---|
374 | n/a | self.assertRaises(csv.Error, myexceltsv) |
---|
375 | n/a | |
---|
376 | n/a | def test_space_dialect(self): |
---|
377 | n/a | class space(csv.excel): |
---|
378 | n/a | delimiter = " " |
---|
379 | n/a | quoting = csv.QUOTE_NONE |
---|
380 | n/a | escapechar = "\\" |
---|
381 | n/a | |
---|
382 | n/a | with TemporaryFile("w+") as fileobj: |
---|
383 | n/a | fileobj.write("abc def\nc1ccccc1 benzene\n") |
---|
384 | n/a | fileobj.seek(0) |
---|
385 | n/a | reader = csv.reader(fileobj, dialect=space()) |
---|
386 | n/a | self.assertEqual(next(reader), ["abc", "def"]) |
---|
387 | n/a | self.assertEqual(next(reader), ["c1ccccc1", "benzene"]) |
---|
388 | n/a | |
---|
389 | n/a | def compare_dialect_123(self, expected, *writeargs, **kwwriteargs): |
---|
390 | n/a | |
---|
391 | n/a | with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
---|
392 | n/a | |
---|
393 | n/a | writer = csv.writer(fileobj, *writeargs, **kwwriteargs) |
---|
394 | n/a | writer.writerow([1,2,3]) |
---|
395 | n/a | fileobj.seek(0) |
---|
396 | n/a | self.assertEqual(fileobj.read(), expected) |
---|
397 | n/a | |
---|
398 | n/a | def test_dialect_apply(self): |
---|
399 | n/a | class testA(csv.excel): |
---|
400 | n/a | delimiter = "\t" |
---|
401 | n/a | class testB(csv.excel): |
---|
402 | n/a | delimiter = ":" |
---|
403 | n/a | class testC(csv.excel): |
---|
404 | n/a | delimiter = "|" |
---|
405 | n/a | class testUni(csv.excel): |
---|
406 | n/a | delimiter = "\u039B" |
---|
407 | n/a | |
---|
408 | n/a | csv.register_dialect('testC', testC) |
---|
409 | n/a | try: |
---|
410 | n/a | self.compare_dialect_123("1,2,3\r\n") |
---|
411 | n/a | self.compare_dialect_123("1\t2\t3\r\n", testA) |
---|
412 | n/a | self.compare_dialect_123("1:2:3\r\n", dialect=testB()) |
---|
413 | n/a | self.compare_dialect_123("1|2|3\r\n", dialect='testC') |
---|
414 | n/a | self.compare_dialect_123("1;2;3\r\n", dialect=testA, |
---|
415 | n/a | delimiter=';') |
---|
416 | n/a | self.compare_dialect_123("1\u039B2\u039B3\r\n", |
---|
417 | n/a | dialect=testUni) |
---|
418 | n/a | |
---|
419 | n/a | finally: |
---|
420 | n/a | csv.unregister_dialect('testC') |
---|
421 | n/a | |
---|
422 | n/a | def test_bad_dialect(self): |
---|
423 | n/a | # Unknown parameter |
---|
424 | n/a | self.assertRaises(TypeError, csv.reader, [], bad_attr = 0) |
---|
425 | n/a | # Bad values |
---|
426 | n/a | self.assertRaises(TypeError, csv.reader, [], delimiter = None) |
---|
427 | n/a | self.assertRaises(TypeError, csv.reader, [], quoting = -1) |
---|
428 | n/a | self.assertRaises(TypeError, csv.reader, [], quoting = 100) |
---|
429 | n/a | |
---|
430 | n/a | def test_copy(self): |
---|
431 | n/a | for name in csv.list_dialects(): |
---|
432 | n/a | dialect = csv.get_dialect(name) |
---|
433 | n/a | self.assertRaises(TypeError, copy.copy, dialect) |
---|
434 | n/a | |
---|
435 | n/a | def test_pickle(self): |
---|
436 | n/a | for name in csv.list_dialects(): |
---|
437 | n/a | dialect = csv.get_dialect(name) |
---|
438 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
439 | n/a | self.assertRaises(TypeError, pickle.dumps, dialect, proto) |
---|
440 | n/a | |
---|
441 | n/a | class TestCsvBase(unittest.TestCase): |
---|
442 | n/a | def readerAssertEqual(self, input, expected_result): |
---|
443 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
444 | n/a | fileobj.write(input) |
---|
445 | n/a | fileobj.seek(0) |
---|
446 | n/a | reader = csv.reader(fileobj, dialect = self.dialect) |
---|
447 | n/a | fields = list(reader) |
---|
448 | n/a | self.assertEqual(fields, expected_result) |
---|
449 | n/a | |
---|
450 | n/a | def writerAssertEqual(self, input, expected_result): |
---|
451 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
452 | n/a | writer = csv.writer(fileobj, dialect = self.dialect) |
---|
453 | n/a | writer.writerows(input) |
---|
454 | n/a | fileobj.seek(0) |
---|
455 | n/a | self.assertEqual(fileobj.read(), expected_result) |
---|
456 | n/a | |
---|
457 | n/a | class TestDialectExcel(TestCsvBase): |
---|
458 | n/a | dialect = 'excel' |
---|
459 | n/a | |
---|
460 | n/a | def test_single(self): |
---|
461 | n/a | self.readerAssertEqual('abc', [['abc']]) |
---|
462 | n/a | |
---|
463 | n/a | def test_simple(self): |
---|
464 | n/a | self.readerAssertEqual('1,2,3,4,5', [['1','2','3','4','5']]) |
---|
465 | n/a | |
---|
466 | n/a | def test_blankline(self): |
---|
467 | n/a | self.readerAssertEqual('', []) |
---|
468 | n/a | |
---|
469 | n/a | def test_empty_fields(self): |
---|
470 | n/a | self.readerAssertEqual(',', [['', '']]) |
---|
471 | n/a | |
---|
472 | n/a | def test_singlequoted(self): |
---|
473 | n/a | self.readerAssertEqual('""', [['']]) |
---|
474 | n/a | |
---|
475 | n/a | def test_singlequoted_left_empty(self): |
---|
476 | n/a | self.readerAssertEqual('"",', [['','']]) |
---|
477 | n/a | |
---|
478 | n/a | def test_singlequoted_right_empty(self): |
---|
479 | n/a | self.readerAssertEqual(',""', [['','']]) |
---|
480 | n/a | |
---|
481 | n/a | def test_single_quoted_quote(self): |
---|
482 | n/a | self.readerAssertEqual('""""', [['"']]) |
---|
483 | n/a | |
---|
484 | n/a | def test_quoted_quotes(self): |
---|
485 | n/a | self.readerAssertEqual('""""""', [['""']]) |
---|
486 | n/a | |
---|
487 | n/a | def test_inline_quote(self): |
---|
488 | n/a | self.readerAssertEqual('a""b', [['a""b']]) |
---|
489 | n/a | |
---|
490 | n/a | def test_inline_quotes(self): |
---|
491 | n/a | self.readerAssertEqual('a"b"c', [['a"b"c']]) |
---|
492 | n/a | |
---|
493 | n/a | def test_quotes_and_more(self): |
---|
494 | n/a | # Excel would never write a field containing '"a"b', but when |
---|
495 | n/a | # reading one, it will return 'ab'. |
---|
496 | n/a | self.readerAssertEqual('"a"b', [['ab']]) |
---|
497 | n/a | |
---|
498 | n/a | def test_lone_quote(self): |
---|
499 | n/a | self.readerAssertEqual('a"b', [['a"b']]) |
---|
500 | n/a | |
---|
501 | n/a | def test_quote_and_quote(self): |
---|
502 | n/a | # Excel would never write a field containing '"a" "b"', but when |
---|
503 | n/a | # reading one, it will return 'a "b"'. |
---|
504 | n/a | self.readerAssertEqual('"a" "b"', [['a "b"']]) |
---|
505 | n/a | |
---|
506 | n/a | def test_space_and_quote(self): |
---|
507 | n/a | self.readerAssertEqual(' "a"', [[' "a"']]) |
---|
508 | n/a | |
---|
509 | n/a | def test_quoted(self): |
---|
510 | n/a | self.readerAssertEqual('1,2,3,"I think, therefore I am",5,6', |
---|
511 | n/a | [['1', '2', '3', |
---|
512 | n/a | 'I think, therefore I am', |
---|
513 | n/a | '5', '6']]) |
---|
514 | n/a | |
---|
515 | n/a | def test_quoted_quote(self): |
---|
516 | n/a | self.readerAssertEqual('1,2,3,"""I see,"" said the blind man","as he picked up his hammer and saw"', |
---|
517 | n/a | [['1', '2', '3', |
---|
518 | n/a | '"I see," said the blind man', |
---|
519 | n/a | 'as he picked up his hammer and saw']]) |
---|
520 | n/a | |
---|
521 | n/a | def test_quoted_nl(self): |
---|
522 | n/a | input = '''\ |
---|
523 | n/a | 1,2,3,"""I see,"" |
---|
524 | n/a | said the blind man","as he picked up his |
---|
525 | n/a | hammer and saw" |
---|
526 | n/a | 9,8,7,6''' |
---|
527 | n/a | self.readerAssertEqual(input, |
---|
528 | n/a | [['1', '2', '3', |
---|
529 | n/a | '"I see,"\nsaid the blind man', |
---|
530 | n/a | 'as he picked up his\nhammer and saw'], |
---|
531 | n/a | ['9','8','7','6']]) |
---|
532 | n/a | |
---|
533 | n/a | def test_dubious_quote(self): |
---|
534 | n/a | self.readerAssertEqual('12,12,1",', [['12', '12', '1"', '']]) |
---|
535 | n/a | |
---|
536 | n/a | def test_null(self): |
---|
537 | n/a | self.writerAssertEqual([], '') |
---|
538 | n/a | |
---|
539 | n/a | def test_single_writer(self): |
---|
540 | n/a | self.writerAssertEqual([['abc']], 'abc\r\n') |
---|
541 | n/a | |
---|
542 | n/a | def test_simple_writer(self): |
---|
543 | n/a | self.writerAssertEqual([[1, 2, 'abc', 3, 4]], '1,2,abc,3,4\r\n') |
---|
544 | n/a | |
---|
545 | n/a | def test_quotes(self): |
---|
546 | n/a | self.writerAssertEqual([[1, 2, 'a"bc"', 3, 4]], '1,2,"a""bc""",3,4\r\n') |
---|
547 | n/a | |
---|
548 | n/a | def test_quote_fieldsep(self): |
---|
549 | n/a | self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') |
---|
550 | n/a | |
---|
551 | n/a | def test_newlines(self): |
---|
552 | n/a | self.writerAssertEqual([[1, 2, 'a\nbc', 3, 4]], '1,2,"a\nbc",3,4\r\n') |
---|
553 | n/a | |
---|
554 | n/a | class EscapedExcel(csv.excel): |
---|
555 | n/a | quoting = csv.QUOTE_NONE |
---|
556 | n/a | escapechar = '\\' |
---|
557 | n/a | |
---|
558 | n/a | class TestEscapedExcel(TestCsvBase): |
---|
559 | n/a | dialect = EscapedExcel() |
---|
560 | n/a | |
---|
561 | n/a | def test_escape_fieldsep(self): |
---|
562 | n/a | self.writerAssertEqual([['abc,def']], 'abc\\,def\r\n') |
---|
563 | n/a | |
---|
564 | n/a | def test_read_escape_fieldsep(self): |
---|
565 | n/a | self.readerAssertEqual('abc\\,def\r\n', [['abc,def']]) |
---|
566 | n/a | |
---|
567 | n/a | class TestDialectUnix(TestCsvBase): |
---|
568 | n/a | dialect = 'unix' |
---|
569 | n/a | |
---|
570 | n/a | def test_simple_writer(self): |
---|
571 | n/a | self.writerAssertEqual([[1, 'abc def', 'abc']], '"1","abc def","abc"\n') |
---|
572 | n/a | |
---|
573 | n/a | def test_simple_reader(self): |
---|
574 | n/a | self.readerAssertEqual('"1","abc def","abc"\n', [['1', 'abc def', 'abc']]) |
---|
575 | n/a | |
---|
576 | n/a | class QuotedEscapedExcel(csv.excel): |
---|
577 | n/a | quoting = csv.QUOTE_NONNUMERIC |
---|
578 | n/a | escapechar = '\\' |
---|
579 | n/a | |
---|
580 | n/a | class TestQuotedEscapedExcel(TestCsvBase): |
---|
581 | n/a | dialect = QuotedEscapedExcel() |
---|
582 | n/a | |
---|
583 | n/a | def test_write_escape_fieldsep(self): |
---|
584 | n/a | self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') |
---|
585 | n/a | |
---|
586 | n/a | def test_read_escape_fieldsep(self): |
---|
587 | n/a | self.readerAssertEqual('"abc\\,def"\r\n', [['abc,def']]) |
---|
588 | n/a | |
---|
589 | n/a | class TestDictFields(unittest.TestCase): |
---|
590 | n/a | ### "long" means the row is longer than the number of fieldnames |
---|
591 | n/a | ### "short" means there are fewer elements in the row than fieldnames |
---|
592 | n/a | def test_write_simple_dict(self): |
---|
593 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
594 | n/a | writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) |
---|
595 | n/a | writer.writeheader() |
---|
596 | n/a | fileobj.seek(0) |
---|
597 | n/a | self.assertEqual(fileobj.readline(), "f1,f2,f3\r\n") |
---|
598 | n/a | writer.writerow({"f1": 10, "f3": "abc"}) |
---|
599 | n/a | fileobj.seek(0) |
---|
600 | n/a | fileobj.readline() # header |
---|
601 | n/a | self.assertEqual(fileobj.read(), "10,,abc\r\n") |
---|
602 | n/a | |
---|
603 | n/a | def test_write_multiple_dict_rows(self): |
---|
604 | n/a | fileobj = StringIO() |
---|
605 | n/a | writer = csv.DictWriter(fileobj, fieldnames=["f1", "f2", "f3"]) |
---|
606 | n/a | writer.writeheader() |
---|
607 | n/a | self.assertEqual(fileobj.getvalue(), "f1,f2,f3\r\n") |
---|
608 | n/a | writer.writerows([{"f1": 1, "f2": "abc", "f3": "f"}, |
---|
609 | n/a | {"f1": 2, "f2": 5, "f3": "xyz"}]) |
---|
610 | n/a | self.assertEqual(fileobj.getvalue(), |
---|
611 | n/a | "f1,f2,f3\r\n1,abc,f\r\n2,5,xyz\r\n") |
---|
612 | n/a | |
---|
613 | n/a | def test_write_no_fields(self): |
---|
614 | n/a | fileobj = StringIO() |
---|
615 | n/a | self.assertRaises(TypeError, csv.DictWriter, fileobj) |
---|
616 | n/a | |
---|
617 | n/a | def test_write_fields_not_in_fieldnames(self): |
---|
618 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
619 | n/a | writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) |
---|
620 | n/a | # Of special note is the non-string key (issue 19449) |
---|
621 | n/a | with self.assertRaises(ValueError) as cx: |
---|
622 | n/a | writer.writerow({"f4": 10, "f2": "spam", 1: "abc"}) |
---|
623 | n/a | exception = str(cx.exception) |
---|
624 | n/a | self.assertIn("fieldnames", exception) |
---|
625 | n/a | self.assertIn("'f4'", exception) |
---|
626 | n/a | self.assertNotIn("'f2'", exception) |
---|
627 | n/a | self.assertIn("1", exception) |
---|
628 | n/a | |
---|
629 | n/a | def test_typo_in_extrasaction_raises_error(self): |
---|
630 | n/a | fileobj = StringIO() |
---|
631 | n/a | self.assertRaises(ValueError, csv.DictWriter, fileobj, ['f1', 'f2'], |
---|
632 | n/a | extrasaction="raised") |
---|
633 | n/a | |
---|
634 | n/a | def test_write_field_not_in_field_names_raise(self): |
---|
635 | n/a | fileobj = StringIO() |
---|
636 | n/a | writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="raise") |
---|
637 | n/a | dictrow = {'f0': 0, 'f1': 1, 'f2': 2, 'f3': 3} |
---|
638 | n/a | self.assertRaises(ValueError, csv.DictWriter.writerow, writer, dictrow) |
---|
639 | n/a | |
---|
640 | n/a | def test_write_field_not_in_field_names_ignore(self): |
---|
641 | n/a | fileobj = StringIO() |
---|
642 | n/a | writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="ignore") |
---|
643 | n/a | dictrow = {'f0': 0, 'f1': 1, 'f2': 2, 'f3': 3} |
---|
644 | n/a | csv.DictWriter.writerow(writer, dictrow) |
---|
645 | n/a | self.assertEqual(fileobj.getvalue(), "1,2\r\n") |
---|
646 | n/a | |
---|
647 | n/a | def test_read_dict_fields(self): |
---|
648 | n/a | with TemporaryFile("w+") as fileobj: |
---|
649 | n/a | fileobj.write("1,2,abc\r\n") |
---|
650 | n/a | fileobj.seek(0) |
---|
651 | n/a | reader = csv.DictReader(fileobj, |
---|
652 | n/a | fieldnames=["f1", "f2", "f3"]) |
---|
653 | n/a | self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) |
---|
654 | n/a | |
---|
655 | n/a | def test_read_dict_no_fieldnames(self): |
---|
656 | n/a | with TemporaryFile("w+") as fileobj: |
---|
657 | n/a | fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") |
---|
658 | n/a | fileobj.seek(0) |
---|
659 | n/a | reader = csv.DictReader(fileobj) |
---|
660 | n/a | self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) |
---|
661 | n/a | self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) |
---|
662 | n/a | |
---|
663 | n/a | # Two test cases to make sure existing ways of implicitly setting |
---|
664 | n/a | # fieldnames continue to work. Both arise from discussion in issue3436. |
---|
665 | n/a | def test_read_dict_fieldnames_from_file(self): |
---|
666 | n/a | with TemporaryFile("w+") as fileobj: |
---|
667 | n/a | fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") |
---|
668 | n/a | fileobj.seek(0) |
---|
669 | n/a | reader = csv.DictReader(fileobj, |
---|
670 | n/a | fieldnames=next(csv.reader(fileobj))) |
---|
671 | n/a | self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) |
---|
672 | n/a | self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) |
---|
673 | n/a | |
---|
674 | n/a | def test_read_dict_fieldnames_chain(self): |
---|
675 | n/a | import itertools |
---|
676 | n/a | with TemporaryFile("w+") as fileobj: |
---|
677 | n/a | fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") |
---|
678 | n/a | fileobj.seek(0) |
---|
679 | n/a | reader = csv.DictReader(fileobj) |
---|
680 | n/a | first = next(reader) |
---|
681 | n/a | for row in itertools.chain([first], reader): |
---|
682 | n/a | self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) |
---|
683 | n/a | self.assertEqual(row, {"f1": '1', "f2": '2', "f3": 'abc'}) |
---|
684 | n/a | |
---|
685 | n/a | def test_read_long(self): |
---|
686 | n/a | with TemporaryFile("w+") as fileobj: |
---|
687 | n/a | fileobj.write("1,2,abc,4,5,6\r\n") |
---|
688 | n/a | fileobj.seek(0) |
---|
689 | n/a | reader = csv.DictReader(fileobj, |
---|
690 | n/a | fieldnames=["f1", "f2"]) |
---|
691 | n/a | self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
---|
692 | n/a | None: ["abc", "4", "5", "6"]}) |
---|
693 | n/a | |
---|
694 | n/a | def test_read_long_with_rest(self): |
---|
695 | n/a | with TemporaryFile("w+") as fileobj: |
---|
696 | n/a | fileobj.write("1,2,abc,4,5,6\r\n") |
---|
697 | n/a | fileobj.seek(0) |
---|
698 | n/a | reader = csv.DictReader(fileobj, |
---|
699 | n/a | fieldnames=["f1", "f2"], restkey="_rest") |
---|
700 | n/a | self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
---|
701 | n/a | "_rest": ["abc", "4", "5", "6"]}) |
---|
702 | n/a | |
---|
703 | n/a | def test_read_long_with_rest_no_fieldnames(self): |
---|
704 | n/a | with TemporaryFile("w+") as fileobj: |
---|
705 | n/a | fileobj.write("f1,f2\r\n1,2,abc,4,5,6\r\n") |
---|
706 | n/a | fileobj.seek(0) |
---|
707 | n/a | reader = csv.DictReader(fileobj, restkey="_rest") |
---|
708 | n/a | self.assertEqual(reader.fieldnames, ["f1", "f2"]) |
---|
709 | n/a | self.assertEqual(next(reader), {"f1": '1', "f2": '2', |
---|
710 | n/a | "_rest": ["abc", "4", "5", "6"]}) |
---|
711 | n/a | |
---|
712 | n/a | def test_read_short(self): |
---|
713 | n/a | with TemporaryFile("w+") as fileobj: |
---|
714 | n/a | fileobj.write("1,2,abc,4,5,6\r\n1,2,abc\r\n") |
---|
715 | n/a | fileobj.seek(0) |
---|
716 | n/a | reader = csv.DictReader(fileobj, |
---|
717 | n/a | fieldnames="1 2 3 4 5 6".split(), |
---|
718 | n/a | restval="DEFAULT") |
---|
719 | n/a | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
---|
720 | n/a | "4": '4', "5": '5', "6": '6'}) |
---|
721 | n/a | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
---|
722 | n/a | "4": 'DEFAULT', "5": 'DEFAULT', |
---|
723 | n/a | "6": 'DEFAULT'}) |
---|
724 | n/a | |
---|
725 | n/a | def test_read_multi(self): |
---|
726 | n/a | sample = [ |
---|
727 | n/a | '2147483648,43.0e12,17,abc,def\r\n', |
---|
728 | n/a | '147483648,43.0e2,17,abc,def\r\n', |
---|
729 | n/a | '47483648,43.0,170,abc,def\r\n' |
---|
730 | n/a | ] |
---|
731 | n/a | |
---|
732 | n/a | reader = csv.DictReader(sample, |
---|
733 | n/a | fieldnames="i1 float i2 s1 s2".split()) |
---|
734 | n/a | self.assertEqual(next(reader), {"i1": '2147483648', |
---|
735 | n/a | "float": '43.0e12', |
---|
736 | n/a | "i2": '17', |
---|
737 | n/a | "s1": 'abc', |
---|
738 | n/a | "s2": 'def'}) |
---|
739 | n/a | |
---|
740 | n/a | def test_read_with_blanks(self): |
---|
741 | n/a | reader = csv.DictReader(["1,2,abc,4,5,6\r\n","\r\n", |
---|
742 | n/a | "1,2,abc,4,5,6\r\n"], |
---|
743 | n/a | fieldnames="1 2 3 4 5 6".split()) |
---|
744 | n/a | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
---|
745 | n/a | "4": '4', "5": '5', "6": '6'}) |
---|
746 | n/a | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
---|
747 | n/a | "4": '4', "5": '5', "6": '6'}) |
---|
748 | n/a | |
---|
749 | n/a | def test_read_semi_sep(self): |
---|
750 | n/a | reader = csv.DictReader(["1;2;abc;4;5;6\r\n"], |
---|
751 | n/a | fieldnames="1 2 3 4 5 6".split(), |
---|
752 | n/a | delimiter=';') |
---|
753 | n/a | self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', |
---|
754 | n/a | "4": '4', "5": '5', "6": '6'}) |
---|
755 | n/a | |
---|
756 | n/a | class TestArrayWrites(unittest.TestCase): |
---|
757 | n/a | def test_int_write(self): |
---|
758 | n/a | import array |
---|
759 | n/a | contents = [(20-i) for i in range(20)] |
---|
760 | n/a | a = array.array('i', contents) |
---|
761 | n/a | |
---|
762 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
763 | n/a | writer = csv.writer(fileobj, dialect="excel") |
---|
764 | n/a | writer.writerow(a) |
---|
765 | n/a | expected = ",".join([str(i) for i in a])+"\r\n" |
---|
766 | n/a | fileobj.seek(0) |
---|
767 | n/a | self.assertEqual(fileobj.read(), expected) |
---|
768 | n/a | |
---|
769 | n/a | def test_double_write(self): |
---|
770 | n/a | import array |
---|
771 | n/a | contents = [(20-i)*0.1 for i in range(20)] |
---|
772 | n/a | a = array.array('d', contents) |
---|
773 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
774 | n/a | writer = csv.writer(fileobj, dialect="excel") |
---|
775 | n/a | writer.writerow(a) |
---|
776 | n/a | expected = ",".join([str(i) for i in a])+"\r\n" |
---|
777 | n/a | fileobj.seek(0) |
---|
778 | n/a | self.assertEqual(fileobj.read(), expected) |
---|
779 | n/a | |
---|
780 | n/a | def test_float_write(self): |
---|
781 | n/a | import array |
---|
782 | n/a | contents = [(20-i)*0.1 for i in range(20)] |
---|
783 | n/a | a = array.array('f', contents) |
---|
784 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
785 | n/a | writer = csv.writer(fileobj, dialect="excel") |
---|
786 | n/a | writer.writerow(a) |
---|
787 | n/a | expected = ",".join([str(i) for i in a])+"\r\n" |
---|
788 | n/a | fileobj.seek(0) |
---|
789 | n/a | self.assertEqual(fileobj.read(), expected) |
---|
790 | n/a | |
---|
791 | n/a | def test_char_write(self): |
---|
792 | n/a | import array, string |
---|
793 | n/a | a = array.array('u', string.ascii_letters) |
---|
794 | n/a | |
---|
795 | n/a | with TemporaryFile("w+", newline='') as fileobj: |
---|
796 | n/a | writer = csv.writer(fileobj, dialect="excel") |
---|
797 | n/a | writer.writerow(a) |
---|
798 | n/a | expected = ",".join(a)+"\r\n" |
---|
799 | n/a | fileobj.seek(0) |
---|
800 | n/a | self.assertEqual(fileobj.read(), expected) |
---|
801 | n/a | |
---|
802 | n/a | class TestDialectValidity(unittest.TestCase): |
---|
803 | n/a | def test_quoting(self): |
---|
804 | n/a | class mydialect(csv.Dialect): |
---|
805 | n/a | delimiter = ";" |
---|
806 | n/a | escapechar = '\\' |
---|
807 | n/a | doublequote = False |
---|
808 | n/a | skipinitialspace = True |
---|
809 | n/a | lineterminator = '\r\n' |
---|
810 | n/a | quoting = csv.QUOTE_NONE |
---|
811 | n/a | d = mydialect() |
---|
812 | n/a | self.assertEqual(d.quoting, csv.QUOTE_NONE) |
---|
813 | n/a | |
---|
814 | n/a | mydialect.quoting = None |
---|
815 | n/a | self.assertRaises(csv.Error, mydialect) |
---|
816 | n/a | |
---|
817 | n/a | mydialect.doublequote = True |
---|
818 | n/a | mydialect.quoting = csv.QUOTE_ALL |
---|
819 | n/a | mydialect.quotechar = '"' |
---|
820 | n/a | d = mydialect() |
---|
821 | n/a | self.assertEqual(d.quoting, csv.QUOTE_ALL) |
---|
822 | n/a | self.assertEqual(d.quotechar, '"') |
---|
823 | n/a | self.assertTrue(d.doublequote) |
---|
824 | n/a | |
---|
825 | n/a | mydialect.quotechar = "''" |
---|
826 | n/a | with self.assertRaises(csv.Error) as cm: |
---|
827 | n/a | mydialect() |
---|
828 | n/a | self.assertEqual(str(cm.exception), |
---|
829 | n/a | '"quotechar" must be a 1-character string') |
---|
830 | n/a | |
---|
831 | n/a | mydialect.quotechar = 4 |
---|
832 | n/a | with self.assertRaises(csv.Error) as cm: |
---|
833 | n/a | mydialect() |
---|
834 | n/a | self.assertEqual(str(cm.exception), |
---|
835 | n/a | '"quotechar" must be string, not int') |
---|
836 | n/a | |
---|
837 | n/a | def test_delimiter(self): |
---|
838 | n/a | class mydialect(csv.Dialect): |
---|
839 | n/a | delimiter = ";" |
---|
840 | n/a | escapechar = '\\' |
---|
841 | n/a | doublequote = False |
---|
842 | n/a | skipinitialspace = True |
---|
843 | n/a | lineterminator = '\r\n' |
---|
844 | n/a | quoting = csv.QUOTE_NONE |
---|
845 | n/a | d = mydialect() |
---|
846 | n/a | self.assertEqual(d.delimiter, ";") |
---|
847 | n/a | |
---|
848 | n/a | mydialect.delimiter = ":::" |
---|
849 | n/a | with self.assertRaises(csv.Error) as cm: |
---|
850 | n/a | mydialect() |
---|
851 | n/a | self.assertEqual(str(cm.exception), |
---|
852 | n/a | '"delimiter" must be a 1-character string') |
---|
853 | n/a | |
---|
854 | n/a | mydialect.delimiter = "" |
---|
855 | n/a | with self.assertRaises(csv.Error) as cm: |
---|
856 | n/a | mydialect() |
---|
857 | n/a | self.assertEqual(str(cm.exception), |
---|
858 | n/a | '"delimiter" must be a 1-character string') |
---|
859 | n/a | |
---|
860 | n/a | mydialect.delimiter = b"," |
---|
861 | n/a | with self.assertRaises(csv.Error) as cm: |
---|
862 | n/a | mydialect() |
---|
863 | n/a | self.assertEqual(str(cm.exception), |
---|
864 | n/a | '"delimiter" must be string, not bytes') |
---|
865 | n/a | |
---|
866 | n/a | mydialect.delimiter = 4 |
---|
867 | n/a | with self.assertRaises(csv.Error) as cm: |
---|
868 | n/a | mydialect() |
---|
869 | n/a | self.assertEqual(str(cm.exception), |
---|
870 | n/a | '"delimiter" must be string, not int') |
---|
871 | n/a | |
---|
872 | n/a | def test_lineterminator(self): |
---|
873 | n/a | class mydialect(csv.Dialect): |
---|
874 | n/a | delimiter = ";" |
---|
875 | n/a | escapechar = '\\' |
---|
876 | n/a | doublequote = False |
---|
877 | n/a | skipinitialspace = True |
---|
878 | n/a | lineterminator = '\r\n' |
---|
879 | n/a | quoting = csv.QUOTE_NONE |
---|
880 | n/a | d = mydialect() |
---|
881 | n/a | self.assertEqual(d.lineterminator, '\r\n') |
---|
882 | n/a | |
---|
883 | n/a | mydialect.lineterminator = ":::" |
---|
884 | n/a | d = mydialect() |
---|
885 | n/a | self.assertEqual(d.lineterminator, ":::") |
---|
886 | n/a | |
---|
887 | n/a | mydialect.lineterminator = 4 |
---|
888 | n/a | with self.assertRaises(csv.Error) as cm: |
---|
889 | n/a | mydialect() |
---|
890 | n/a | self.assertEqual(str(cm.exception), |
---|
891 | n/a | '"lineterminator" must be a string') |
---|
892 | n/a | |
---|
893 | n/a | def test_invalid_chars(self): |
---|
894 | n/a | def create_invalid(field_name, value): |
---|
895 | n/a | class mydialect(csv.Dialect): |
---|
896 | n/a | pass |
---|
897 | n/a | setattr(mydialect, field_name, value) |
---|
898 | n/a | d = mydialect() |
---|
899 | n/a | |
---|
900 | n/a | for field_name in ("delimiter", "escapechar", "quotechar"): |
---|
901 | n/a | with self.subTest(field_name=field_name): |
---|
902 | n/a | self.assertRaises(csv.Error, create_invalid, field_name, "") |
---|
903 | n/a | self.assertRaises(csv.Error, create_invalid, field_name, "abc") |
---|
904 | n/a | self.assertRaises(csv.Error, create_invalid, field_name, b'x') |
---|
905 | n/a | self.assertRaises(csv.Error, create_invalid, field_name, 5) |
---|
906 | n/a | |
---|
907 | n/a | |
---|
908 | n/a | class TestSniffer(unittest.TestCase): |
---|
909 | n/a | sample1 = """\ |
---|
910 | n/a | Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes |
---|
911 | n/a | Shark City, Glendale Heights, IL, 12/28/02, Prezence |
---|
912 | n/a | Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow |
---|
913 | n/a | Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back |
---|
914 | n/a | """ |
---|
915 | n/a | sample2 = """\ |
---|
916 | n/a | 'Harry''s':'Arlington Heights':'IL':'2/1/03':'Kimi Hayes' |
---|
917 | n/a | 'Shark City':'Glendale Heights':'IL':'12/28/02':'Prezence' |
---|
918 | n/a | 'Tommy''s Place':'Blue Island':'IL':'12/28/02':'Blue Sunday/White Crow' |
---|
919 | n/a | 'Stonecutters ''Seafood'' and Chop House':'Lemont':'IL':'12/19/02':'Week Back' |
---|
920 | n/a | """ |
---|
921 | n/a | header1 = '''\ |
---|
922 | n/a | "venue","city","state","date","performers" |
---|
923 | n/a | ''' |
---|
924 | n/a | sample3 = '''\ |
---|
925 | n/a | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
---|
926 | n/a | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
---|
927 | n/a | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
---|
928 | n/a | ''' |
---|
929 | n/a | |
---|
930 | n/a | sample4 = '''\ |
---|
931 | n/a | 2147483648;43.0e12;17;abc;def |
---|
932 | n/a | 147483648;43.0e2;17;abc;def |
---|
933 | n/a | 47483648;43.0;170;abc;def |
---|
934 | n/a | ''' |
---|
935 | n/a | |
---|
936 | n/a | sample5 = "aaa\tbbb\r\nAAA\t\r\nBBB\t\r\n" |
---|
937 | n/a | sample6 = "a|b|c\r\nd|e|f\r\n" |
---|
938 | n/a | sample7 = "'a'|'b'|'c'\r\n'd'|e|f\r\n" |
---|
939 | n/a | |
---|
940 | n/a | # Issue 18155: Use a delimiter that is a special char to regex: |
---|
941 | n/a | |
---|
942 | n/a | header2 = '''\ |
---|
943 | n/a | "venue"+"city"+"state"+"date"+"performers" |
---|
944 | n/a | ''' |
---|
945 | n/a | sample8 = """\ |
---|
946 | n/a | Harry's+ Arlington Heights+ IL+ 2/1/03+ Kimi Hayes |
---|
947 | n/a | Shark City+ Glendale Heights+ IL+ 12/28/02+ Prezence |
---|
948 | n/a | Tommy's Place+ Blue Island+ IL+ 12/28/02+ Blue Sunday/White Crow |
---|
949 | n/a | Stonecutters Seafood and Chop House+ Lemont+ IL+ 12/19/02+ Week Back |
---|
950 | n/a | """ |
---|
951 | n/a | sample9 = """\ |
---|
952 | n/a | 'Harry''s'+ Arlington Heights'+ 'IL'+ '2/1/03'+ 'Kimi Hayes' |
---|
953 | n/a | 'Shark City'+ Glendale Heights'+' IL'+ '12/28/02'+ 'Prezence' |
---|
954 | n/a | 'Tommy''s Place'+ Blue Island'+ 'IL'+ '12/28/02'+ 'Blue Sunday/White Crow' |
---|
955 | n/a | 'Stonecutters ''Seafood'' and Chop House'+ 'Lemont'+ 'IL'+ '12/19/02'+ 'Week Back' |
---|
956 | n/a | """ |
---|
957 | n/a | |
---|
958 | n/a | def test_has_header(self): |
---|
959 | n/a | sniffer = csv.Sniffer() |
---|
960 | n/a | self.assertEqual(sniffer.has_header(self.sample1), False) |
---|
961 | n/a | self.assertEqual(sniffer.has_header(self.header1 + self.sample1), |
---|
962 | n/a | True) |
---|
963 | n/a | |
---|
964 | n/a | def test_has_header_regex_special_delimiter(self): |
---|
965 | n/a | sniffer = csv.Sniffer() |
---|
966 | n/a | self.assertEqual(sniffer.has_header(self.sample8), False) |
---|
967 | n/a | self.assertEqual(sniffer.has_header(self.header2 + self.sample8), |
---|
968 | n/a | True) |
---|
969 | n/a | |
---|
970 | n/a | def test_sniff(self): |
---|
971 | n/a | sniffer = csv.Sniffer() |
---|
972 | n/a | dialect = sniffer.sniff(self.sample1) |
---|
973 | n/a | self.assertEqual(dialect.delimiter, ",") |
---|
974 | n/a | self.assertEqual(dialect.quotechar, '"') |
---|
975 | n/a | self.assertEqual(dialect.skipinitialspace, True) |
---|
976 | n/a | |
---|
977 | n/a | dialect = sniffer.sniff(self.sample2) |
---|
978 | n/a | self.assertEqual(dialect.delimiter, ":") |
---|
979 | n/a | self.assertEqual(dialect.quotechar, "'") |
---|
980 | n/a | self.assertEqual(dialect.skipinitialspace, False) |
---|
981 | n/a | |
---|
982 | n/a | def test_delimiters(self): |
---|
983 | n/a | sniffer = csv.Sniffer() |
---|
984 | n/a | dialect = sniffer.sniff(self.sample3) |
---|
985 | n/a | # given that all three lines in sample3 are equal, |
---|
986 | n/a | # I think that any character could have been 'guessed' as the |
---|
987 | n/a | # delimiter, depending on dictionary order |
---|
988 | n/a | self.assertIn(dialect.delimiter, self.sample3) |
---|
989 | n/a | dialect = sniffer.sniff(self.sample3, delimiters="?,") |
---|
990 | n/a | self.assertEqual(dialect.delimiter, "?") |
---|
991 | n/a | dialect = sniffer.sniff(self.sample3, delimiters="/,") |
---|
992 | n/a | self.assertEqual(dialect.delimiter, "/") |
---|
993 | n/a | dialect = sniffer.sniff(self.sample4) |
---|
994 | n/a | self.assertEqual(dialect.delimiter, ";") |
---|
995 | n/a | dialect = sniffer.sniff(self.sample5) |
---|
996 | n/a | self.assertEqual(dialect.delimiter, "\t") |
---|
997 | n/a | dialect = sniffer.sniff(self.sample6) |
---|
998 | n/a | self.assertEqual(dialect.delimiter, "|") |
---|
999 | n/a | dialect = sniffer.sniff(self.sample7) |
---|
1000 | n/a | self.assertEqual(dialect.delimiter, "|") |
---|
1001 | n/a | self.assertEqual(dialect.quotechar, "'") |
---|
1002 | n/a | dialect = sniffer.sniff(self.sample8) |
---|
1003 | n/a | self.assertEqual(dialect.delimiter, '+') |
---|
1004 | n/a | dialect = sniffer.sniff(self.sample9) |
---|
1005 | n/a | self.assertEqual(dialect.delimiter, '+') |
---|
1006 | n/a | self.assertEqual(dialect.quotechar, "'") |
---|
1007 | n/a | |
---|
1008 | n/a | def test_doublequote(self): |
---|
1009 | n/a | sniffer = csv.Sniffer() |
---|
1010 | n/a | dialect = sniffer.sniff(self.header1) |
---|
1011 | n/a | self.assertFalse(dialect.doublequote) |
---|
1012 | n/a | dialect = sniffer.sniff(self.header2) |
---|
1013 | n/a | self.assertFalse(dialect.doublequote) |
---|
1014 | n/a | dialect = sniffer.sniff(self.sample2) |
---|
1015 | n/a | self.assertTrue(dialect.doublequote) |
---|
1016 | n/a | dialect = sniffer.sniff(self.sample8) |
---|
1017 | n/a | self.assertFalse(dialect.doublequote) |
---|
1018 | n/a | dialect = sniffer.sniff(self.sample9) |
---|
1019 | n/a | self.assertTrue(dialect.doublequote) |
---|
1020 | n/a | |
---|
1021 | n/a | class NUL: |
---|
1022 | n/a | def write(s, *args): |
---|
1023 | n/a | pass |
---|
1024 | n/a | writelines = write |
---|
1025 | n/a | |
---|
1026 | n/a | @unittest.skipUnless(hasattr(sys, "gettotalrefcount"), |
---|
1027 | n/a | 'requires sys.gettotalrefcount()') |
---|
1028 | n/a | class TestLeaks(unittest.TestCase): |
---|
1029 | n/a | def test_create_read(self): |
---|
1030 | n/a | delta = 0 |
---|
1031 | n/a | lastrc = sys.gettotalrefcount() |
---|
1032 | n/a | for i in range(20): |
---|
1033 | n/a | gc.collect() |
---|
1034 | n/a | self.assertEqual(gc.garbage, []) |
---|
1035 | n/a | rc = sys.gettotalrefcount() |
---|
1036 | n/a | csv.reader(["a,b,c\r\n"]) |
---|
1037 | n/a | csv.reader(["a,b,c\r\n"]) |
---|
1038 | n/a | csv.reader(["a,b,c\r\n"]) |
---|
1039 | n/a | delta = rc-lastrc |
---|
1040 | n/a | lastrc = rc |
---|
1041 | n/a | # if csv.reader() leaks, last delta should be 3 or more |
---|
1042 | n/a | self.assertEqual(delta < 3, True) |
---|
1043 | n/a | |
---|
1044 | n/a | def test_create_write(self): |
---|
1045 | n/a | delta = 0 |
---|
1046 | n/a | lastrc = sys.gettotalrefcount() |
---|
1047 | n/a | s = NUL() |
---|
1048 | n/a | for i in range(20): |
---|
1049 | n/a | gc.collect() |
---|
1050 | n/a | self.assertEqual(gc.garbage, []) |
---|
1051 | n/a | rc = sys.gettotalrefcount() |
---|
1052 | n/a | csv.writer(s) |
---|
1053 | n/a | csv.writer(s) |
---|
1054 | n/a | csv.writer(s) |
---|
1055 | n/a | delta = rc-lastrc |
---|
1056 | n/a | lastrc = rc |
---|
1057 | n/a | # if csv.writer() leaks, last delta should be 3 or more |
---|
1058 | n/a | self.assertEqual(delta < 3, True) |
---|
1059 | n/a | |
---|
1060 | n/a | def test_read(self): |
---|
1061 | n/a | delta = 0 |
---|
1062 | n/a | rows = ["a,b,c\r\n"]*5 |
---|
1063 | n/a | lastrc = sys.gettotalrefcount() |
---|
1064 | n/a | for i in range(20): |
---|
1065 | n/a | gc.collect() |
---|
1066 | n/a | self.assertEqual(gc.garbage, []) |
---|
1067 | n/a | rc = sys.gettotalrefcount() |
---|
1068 | n/a | rdr = csv.reader(rows) |
---|
1069 | n/a | for row in rdr: |
---|
1070 | n/a | pass |
---|
1071 | n/a | delta = rc-lastrc |
---|
1072 | n/a | lastrc = rc |
---|
1073 | n/a | # if reader leaks during read, delta should be 5 or more |
---|
1074 | n/a | self.assertEqual(delta < 5, True) |
---|
1075 | n/a | |
---|
1076 | n/a | def test_write(self): |
---|
1077 | n/a | delta = 0 |
---|
1078 | n/a | rows = [[1,2,3]]*5 |
---|
1079 | n/a | s = NUL() |
---|
1080 | n/a | lastrc = sys.gettotalrefcount() |
---|
1081 | n/a | for i in range(20): |
---|
1082 | n/a | gc.collect() |
---|
1083 | n/a | self.assertEqual(gc.garbage, []) |
---|
1084 | n/a | rc = sys.gettotalrefcount() |
---|
1085 | n/a | writer = csv.writer(s) |
---|
1086 | n/a | for row in rows: |
---|
1087 | n/a | writer.writerow(row) |
---|
1088 | n/a | delta = rc-lastrc |
---|
1089 | n/a | lastrc = rc |
---|
1090 | n/a | # if writer leaks during write, last delta should be 5 or more |
---|
1091 | n/a | self.assertEqual(delta < 5, True) |
---|
1092 | n/a | |
---|
1093 | n/a | class TestUnicode(unittest.TestCase): |
---|
1094 | n/a | |
---|
1095 | n/a | names = ["Martin von Löwis", |
---|
1096 | n/a | "Marc André Lemburg", |
---|
1097 | n/a | "Guido van Rossum", |
---|
1098 | n/a | "François Pinard"] |
---|
1099 | n/a | |
---|
1100 | n/a | def test_unicode_read(self): |
---|
1101 | n/a | with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
---|
1102 | n/a | fileobj.write(",".join(self.names) + "\r\n") |
---|
1103 | n/a | fileobj.seek(0) |
---|
1104 | n/a | reader = csv.reader(fileobj) |
---|
1105 | n/a | self.assertEqual(list(reader), [self.names]) |
---|
1106 | n/a | |
---|
1107 | n/a | |
---|
1108 | n/a | def test_unicode_write(self): |
---|
1109 | n/a | with TemporaryFile("w+", newline='', encoding="utf-8") as fileobj: |
---|
1110 | n/a | writer = csv.writer(fileobj) |
---|
1111 | n/a | writer.writerow(self.names) |
---|
1112 | n/a | expected = ",".join(self.names)+"\r\n" |
---|
1113 | n/a | fileobj.seek(0) |
---|
1114 | n/a | self.assertEqual(fileobj.read(), expected) |
---|
1115 | n/a | |
---|
1116 | n/a | class KeyOrderingTest(unittest.TestCase): |
---|
1117 | n/a | |
---|
1118 | n/a | def test_ordering_for_the_dict_reader_and_writer(self): |
---|
1119 | n/a | resultset = set() |
---|
1120 | n/a | for keys in permutations("abcde"): |
---|
1121 | n/a | with TemporaryFile('w+', newline='', encoding="utf-8") as fileobject: |
---|
1122 | n/a | dw = csv.DictWriter(fileobject, keys) |
---|
1123 | n/a | dw.writeheader() |
---|
1124 | n/a | fileobject.seek(0) |
---|
1125 | n/a | dr = csv.DictReader(fileobject) |
---|
1126 | n/a | kt = tuple(dr.fieldnames) |
---|
1127 | n/a | self.assertEqual(keys, kt) |
---|
1128 | n/a | resultset.add(kt) |
---|
1129 | n/a | # Final sanity check: were all permutations unique? |
---|
1130 | n/a | self.assertEqual(len(resultset), 120, "Key ordering: some key permutations not collected (expected 120)") |
---|
1131 | n/a | |
---|
1132 | n/a | def test_ordered_dict_reader(self): |
---|
1133 | n/a | data = dedent('''\ |
---|
1134 | n/a | FirstName,LastName |
---|
1135 | n/a | Eric,Idle |
---|
1136 | n/a | Graham,Chapman,Over1,Over2 |
---|
1137 | n/a | |
---|
1138 | n/a | Under1 |
---|
1139 | n/a | John,Cleese |
---|
1140 | n/a | ''').splitlines() |
---|
1141 | n/a | |
---|
1142 | n/a | self.assertEqual(list(csv.DictReader(data)), |
---|
1143 | n/a | [OrderedDict([('FirstName', 'Eric'), ('LastName', 'Idle')]), |
---|
1144 | n/a | OrderedDict([('FirstName', 'Graham'), ('LastName', 'Chapman'), |
---|
1145 | n/a | (None, ['Over1', 'Over2'])]), |
---|
1146 | n/a | OrderedDict([('FirstName', 'Under1'), ('LastName', None)]), |
---|
1147 | n/a | OrderedDict([('FirstName', 'John'), ('LastName', 'Cleese')]), |
---|
1148 | n/a | ]) |
---|
1149 | n/a | |
---|
1150 | n/a | self.assertEqual(list(csv.DictReader(data, restkey='OtherInfo')), |
---|
1151 | n/a | [OrderedDict([('FirstName', 'Eric'), ('LastName', 'Idle')]), |
---|
1152 | n/a | OrderedDict([('FirstName', 'Graham'), ('LastName', 'Chapman'), |
---|
1153 | n/a | ('OtherInfo', ['Over1', 'Over2'])]), |
---|
1154 | n/a | OrderedDict([('FirstName', 'Under1'), ('LastName', None)]), |
---|
1155 | n/a | OrderedDict([('FirstName', 'John'), ('LastName', 'Cleese')]), |
---|
1156 | n/a | ]) |
---|
1157 | n/a | |
---|
1158 | n/a | del data[0] # Remove the header row |
---|
1159 | n/a | self.assertEqual(list(csv.DictReader(data, fieldnames=['fname', 'lname'])), |
---|
1160 | n/a | [OrderedDict([('fname', 'Eric'), ('lname', 'Idle')]), |
---|
1161 | n/a | OrderedDict([('fname', 'Graham'), ('lname', 'Chapman'), |
---|
1162 | n/a | (None, ['Over1', 'Over2'])]), |
---|
1163 | n/a | OrderedDict([('fname', 'Under1'), ('lname', None)]), |
---|
1164 | n/a | OrderedDict([('fname', 'John'), ('lname', 'Cleese')]), |
---|
1165 | n/a | ]) |
---|
1166 | n/a | |
---|
1167 | n/a | |
---|
1168 | n/a | class MiscTestCase(unittest.TestCase): |
---|
1169 | n/a | def test__all__(self): |
---|
1170 | n/a | extra = {'__doc__', '__version__'} |
---|
1171 | n/a | support.check__all__(self, csv, ('csv', '_csv'), extra=extra) |
---|
1172 | n/a | |
---|
1173 | n/a | |
---|
1174 | n/a | if __name__ == '__main__': |
---|
1175 | n/a | unittest.main() |
---|