Skip Montanaro | 1a56665 | 2003-05-06 15:56:05 +0000 | [diff] [blame^] | 1 | # -*- coding: iso-8859-1 -*- |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 2 | # Copyright (C) 2001,2002 Python Software Foundation |
| 3 | # csv package unit tests |
| 4 | |
| 5 | import sys |
| 6 | import unittest |
| 7 | from StringIO import StringIO |
Skip Montanaro | 594adac | 2003-04-10 17:16:15 +0000 | [diff] [blame] | 8 | import csv |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 9 | import gc |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 10 | from test import test_support |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 11 | |
| 12 | class Test_Csv(unittest.TestCase): |
| 13 | """ |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 14 | Test the underlying C csv parser in ways that are not appropriate |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 15 | from the high level interface. Further tests of this nature are done |
| 16 | in TestDialectRegistry. |
| 17 | """ |
| 18 | def test_reader_arg_valid(self): |
| 19 | self.assertRaises(TypeError, csv.reader) |
| 20 | self.assertRaises(TypeError, csv.reader, None) |
| 21 | self.assertRaises(AttributeError, csv.reader, [], bad_attr = 0) |
| 22 | self.assertRaises(csv.Error, csv.reader, [], 'foo') |
| 23 | class BadClass: |
| 24 | def __init__(self): |
| 25 | raise IOError |
| 26 | self.assertRaises(IOError, csv.reader, [], BadClass) |
| 27 | self.assertRaises(TypeError, csv.reader, [], None) |
| 28 | class BadDialect: |
| 29 | bad_attr = 0 |
| 30 | self.assertRaises(AttributeError, csv.reader, [], BadDialect) |
| 31 | |
| 32 | def test_writer_arg_valid(self): |
| 33 | self.assertRaises(TypeError, csv.writer) |
| 34 | self.assertRaises(TypeError, csv.writer, None) |
| 35 | self.assertRaises(AttributeError, csv.writer, StringIO(), bad_attr = 0) |
| 36 | |
| 37 | def _test_attrs(self, obj): |
| 38 | self.assertEqual(obj.dialect.delimiter, ',') |
| 39 | obj.dialect.delimiter = '\t' |
| 40 | self.assertEqual(obj.dialect.delimiter, '\t') |
| 41 | self.assertRaises(TypeError, delattr, obj.dialect, 'delimiter') |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 42 | self.assertRaises(TypeError, setattr, obj.dialect, |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 43 | 'lineterminator', None) |
| 44 | obj.dialect.escapechar = None |
| 45 | self.assertEqual(obj.dialect.escapechar, None) |
| 46 | self.assertRaises(TypeError, delattr, obj.dialect, 'quoting') |
| 47 | self.assertRaises(TypeError, setattr, obj.dialect, 'quoting', None) |
| 48 | obj.dialect.quoting = csv.QUOTE_MINIMAL |
| 49 | self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL) |
| 50 | |
| 51 | def test_reader_attrs(self): |
| 52 | self._test_attrs(csv.reader([])) |
| 53 | |
| 54 | def test_writer_attrs(self): |
| 55 | self._test_attrs(csv.writer(StringIO())) |
| 56 | |
| 57 | def _write_test(self, fields, expect, **kwargs): |
| 58 | fileobj = StringIO() |
| 59 | writer = csv.writer(fileobj, **kwargs) |
| 60 | writer.writerow(fields) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 61 | self.assertEqual(fileobj.getvalue(), |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 62 | expect + writer.dialect.lineterminator) |
| 63 | |
| 64 | def test_write_arg_valid(self): |
| 65 | self.assertRaises(csv.Error, self._write_test, None, '') |
| 66 | self._write_test((), '') |
| 67 | self._write_test([None], '""') |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 68 | self.assertRaises(csv.Error, self._write_test, |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 69 | [None], None, quoting = csv.QUOTE_NONE) |
| 70 | # Check that exceptions are passed up the chain |
| 71 | class BadList: |
| 72 | def __len__(self): |
| 73 | return 10; |
| 74 | def __getitem__(self, i): |
| 75 | if i > 2: |
| 76 | raise IOError |
| 77 | self.assertRaises(IOError, self._write_test, BadList(), '') |
| 78 | class BadItem: |
| 79 | def __str__(self): |
| 80 | raise IOError |
| 81 | self.assertRaises(IOError, self._write_test, [BadItem()], '') |
| 82 | |
| 83 | def test_write_bigfield(self): |
| 84 | # This exercises the buffer realloc functionality |
| 85 | bigstring = 'X' * 50000 |
| 86 | self._write_test([bigstring,bigstring], '%s,%s' % \ |
| 87 | (bigstring, bigstring)) |
| 88 | |
| 89 | def test_write_quoting(self): |
| 90 | self._write_test(['a','1','p,q'], 'a,1,"p,q"') |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 91 | self.assertRaises(csv.Error, |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 92 | self._write_test, |
| 93 | ['a','1','p,q'], 'a,1,"p,q"', |
| 94 | quoting = csv.QUOTE_NONE) |
| 95 | self._write_test(['a','1','p,q'], 'a,1,"p,q"', |
| 96 | quoting = csv.QUOTE_MINIMAL) |
| 97 | self._write_test(['a','1','p,q'], '"a",1,"p,q"', |
| 98 | quoting = csv.QUOTE_NONNUMERIC) |
| 99 | self._write_test(['a','1','p,q'], '"a","1","p,q"', |
| 100 | quoting = csv.QUOTE_ALL) |
| 101 | |
| 102 | def test_write_escape(self): |
| 103 | self._write_test(['a','1','p,q'], 'a,1,"p,q"', |
| 104 | escapechar='\\') |
| 105 | # FAILED - needs to be fixed [am]: |
| 106 | # self._write_test(['a','1','p,"q"'], 'a,1,"p,\\"q\\"', |
| 107 | # escapechar='\\', doublequote = 0) |
| 108 | self._write_test(['a','1','p,q'], 'a,1,p\\,q', |
| 109 | escapechar='\\', quoting = csv.QUOTE_NONE) |
| 110 | |
| 111 | def test_writerows(self): |
| 112 | class BrokenFile: |
| 113 | def write(self, buf): |
| 114 | raise IOError |
| 115 | writer = csv.writer(BrokenFile()) |
| 116 | self.assertRaises(IOError, writer.writerows, [['a']]) |
| 117 | fileobj = StringIO() |
| 118 | writer = csv.writer(fileobj) |
| 119 | self.assertRaises(TypeError, writer.writerows, None) |
| 120 | writer.writerows([['a','b'],['c','d']]) |
| 121 | self.assertEqual(fileobj.getvalue(), "a,b\r\nc,d\r\n") |
| 122 | |
| 123 | def _read_test(self, input, expect, **kwargs): |
| 124 | reader = csv.reader(input, **kwargs) |
| 125 | result = list(reader) |
| 126 | self.assertEqual(result, expect) |
| 127 | |
| 128 | def test_read_oddinputs(self): |
| 129 | self._read_test([], []) |
| 130 | self._read_test([''], [[]]) |
| 131 | self.assertRaises(csv.Error, self._read_test, |
| 132 | ['"ab"c'], None, strict = 1) |
| 133 | # cannot handle null bytes for the moment |
| 134 | self.assertRaises(csv.Error, self._read_test, |
| 135 | ['ab\0c'], None, strict = 1) |
| 136 | self._read_test(['"ab"c'], [['abc']], doublequote = 0) |
| 137 | |
| 138 | def test_read_eol(self): |
| 139 | self._read_test(['a,b'], [['a','b']]) |
| 140 | self._read_test(['a,b\n'], [['a','b']]) |
| 141 | self._read_test(['a,b\r\n'], [['a','b']]) |
| 142 | self._read_test(['a,b\r'], [['a','b']]) |
| 143 | self.assertRaises(csv.Error, self._read_test, ['a,b\rc,d'], []) |
| 144 | self.assertRaises(csv.Error, self._read_test, ['a,b\nc,d'], []) |
| 145 | self.assertRaises(csv.Error, self._read_test, ['a,b\r\nc,d'], []) |
| 146 | |
| 147 | def test_read_escape(self): |
| 148 | self._read_test(['a,\\b,c'], [['a', '\\b', 'c']], escapechar='\\') |
| 149 | self._read_test(['a,b\\,c'], [['a', 'b,c']], escapechar='\\') |
| 150 | self._read_test(['a,"b\\,c"'], [['a', 'b,c']], escapechar='\\') |
| 151 | self._read_test(['a,"b,\\c"'], [['a', 'b,\\c']], escapechar='\\') |
| 152 | self._read_test(['a,"b,c\\""'], [['a', 'b,c"']], escapechar='\\') |
| 153 | self._read_test(['a,"b,c"\\'], [['a', 'b,c\\']], escapechar='\\') |
| 154 | |
| 155 | def test_read_bigfield(self): |
| 156 | # This exercises the buffer realloc functionality |
| 157 | bigstring = 'X' * 50000 |
| 158 | bigline = '%s,%s' % (bigstring, bigstring) |
| 159 | self._read_test([bigline], [[bigstring, bigstring]]) |
| 160 | |
| 161 | class TestDialectRegistry(unittest.TestCase): |
| 162 | def test_registry_badargs(self): |
| 163 | self.assertRaises(TypeError, csv.list_dialects, None) |
| 164 | self.assertRaises(TypeError, csv.get_dialect) |
| 165 | self.assertRaises(csv.Error, csv.get_dialect, None) |
| 166 | self.assertRaises(csv.Error, csv.get_dialect, "nonesuch") |
| 167 | self.assertRaises(TypeError, csv.unregister_dialect) |
| 168 | self.assertRaises(csv.Error, csv.unregister_dialect, None) |
| 169 | self.assertRaises(csv.Error, csv.unregister_dialect, "nonesuch") |
| 170 | self.assertRaises(TypeError, csv.register_dialect, None) |
| 171 | self.assertRaises(TypeError, csv.register_dialect, None, None) |
| 172 | self.assertRaises(TypeError, csv.register_dialect, "nonesuch", None) |
| 173 | class bogus: |
| 174 | def __init__(self): |
| 175 | raise KeyError |
| 176 | self.assertRaises(KeyError, csv.register_dialect, "nonesuch", bogus) |
| 177 | |
| 178 | def test_registry(self): |
| 179 | class myexceltsv(csv.excel): |
| 180 | delimiter = "\t" |
| 181 | name = "myexceltsv" |
| 182 | expected_dialects = csv.list_dialects() + [name] |
| 183 | expected_dialects.sort() |
| 184 | csv.register_dialect(name, myexceltsv) |
| 185 | try: |
| 186 | self.failUnless(isinstance(csv.get_dialect(name), myexceltsv)) |
| 187 | got_dialects = csv.list_dialects() |
| 188 | got_dialects.sort() |
| 189 | self.assertEqual(expected_dialects, got_dialects) |
| 190 | finally: |
| 191 | csv.unregister_dialect(name) |
| 192 | |
| 193 | def test_incomplete_dialect(self): |
| 194 | class myexceltsv(csv.Dialect): |
| 195 | delimiter = "\t" |
| 196 | self.assertRaises(csv.Error, myexceltsv) |
| 197 | |
| 198 | def test_space_dialect(self): |
| 199 | class space(csv.excel): |
| 200 | delimiter = " " |
| 201 | quoting = csv.QUOTE_NONE |
| 202 | escapechar = "\\" |
| 203 | |
| 204 | s = StringIO("abc def\nc1ccccc1 benzene\n") |
| 205 | rdr = csv.reader(s, dialect=space()) |
| 206 | self.assertEqual(rdr.next(), ["abc", "def"]) |
| 207 | self.assertEqual(rdr.next(), ["c1ccccc1", "benzene"]) |
| 208 | |
| 209 | def test_dialect_apply(self): |
| 210 | class testA(csv.excel): |
| 211 | delimiter = "\t" |
| 212 | class testB(csv.excel): |
| 213 | delimiter = ":" |
| 214 | class testC(csv.excel): |
| 215 | delimiter = "|" |
| 216 | |
| 217 | csv.register_dialect('testC', testC) |
| 218 | try: |
| 219 | fileobj = StringIO() |
| 220 | writer = csv.writer(fileobj) |
| 221 | writer.writerow([1,2,3]) |
| 222 | self.assertEqual(fileobj.getvalue(), "1,2,3\r\n") |
| 223 | |
| 224 | fileobj = StringIO() |
| 225 | writer = csv.writer(fileobj, testA) |
| 226 | writer.writerow([1,2,3]) |
| 227 | self.assertEqual(fileobj.getvalue(), "1\t2\t3\r\n") |
| 228 | |
| 229 | fileobj = StringIO() |
| 230 | writer = csv.writer(fileobj, dialect=testB()) |
| 231 | writer.writerow([1,2,3]) |
| 232 | self.assertEqual(fileobj.getvalue(), "1:2:3\r\n") |
| 233 | |
| 234 | fileobj = StringIO() |
| 235 | writer = csv.writer(fileobj, dialect='testC') |
| 236 | writer.writerow([1,2,3]) |
| 237 | self.assertEqual(fileobj.getvalue(), "1|2|3\r\n") |
| 238 | |
| 239 | fileobj = StringIO() |
| 240 | writer = csv.writer(fileobj, dialect=testA, delimiter=';') |
| 241 | writer.writerow([1,2,3]) |
| 242 | self.assertEqual(fileobj.getvalue(), "1;2;3\r\n") |
| 243 | finally: |
| 244 | csv.unregister_dialect('testC') |
| 245 | |
| 246 | def test_bad_dialect(self): |
| 247 | # Unknown parameter |
| 248 | self.assertRaises(AttributeError, csv.reader, [], bad_attr = 0) |
| 249 | # Bad values |
| 250 | self.assertRaises(TypeError, csv.reader, [], delimiter = None) |
| 251 | self.assertRaises(TypeError, csv.reader, [], quoting = -1) |
| 252 | self.assertRaises(TypeError, csv.reader, [], quoting = 100) |
| 253 | |
| 254 | class TestCsvBase(unittest.TestCase): |
| 255 | def readerAssertEqual(self, input, expected_result): |
| 256 | reader = csv.reader(StringIO(input), dialect = self.dialect) |
| 257 | fields = list(reader) |
| 258 | self.assertEqual(fields, expected_result) |
| 259 | |
| 260 | def writerAssertEqual(self, input, expected_result): |
| 261 | fileobj = StringIO() |
| 262 | writer = csv.writer(fileobj, dialect = self.dialect) |
| 263 | writer.writerows(input) |
| 264 | self.assertEqual(fileobj.getvalue(), expected_result) |
| 265 | |
| 266 | class TestDialectExcel(TestCsvBase): |
| 267 | dialect = 'excel' |
| 268 | |
| 269 | def test_single(self): |
| 270 | self.readerAssertEqual('abc', [['abc']]) |
| 271 | |
| 272 | def test_simple(self): |
| 273 | self.readerAssertEqual('1,2,3,4,5', [['1','2','3','4','5']]) |
| 274 | |
| 275 | def test_blankline(self): |
| 276 | self.readerAssertEqual('', []) |
| 277 | |
| 278 | def test_empty_fields(self): |
| 279 | self.readerAssertEqual(',', [['', '']]) |
| 280 | |
| 281 | def test_singlequoted(self): |
| 282 | self.readerAssertEqual('""', [['']]) |
| 283 | |
| 284 | def test_singlequoted_left_empty(self): |
| 285 | self.readerAssertEqual('"",', [['','']]) |
| 286 | |
| 287 | def test_singlequoted_right_empty(self): |
| 288 | self.readerAssertEqual(',""', [['','']]) |
| 289 | |
| 290 | def test_single_quoted_quote(self): |
| 291 | self.readerAssertEqual('""""', [['"']]) |
| 292 | |
| 293 | def test_quoted_quotes(self): |
| 294 | self.readerAssertEqual('""""""', [['""']]) |
| 295 | |
| 296 | def test_inline_quote(self): |
| 297 | self.readerAssertEqual('a""b', [['a""b']]) |
| 298 | |
| 299 | def test_inline_quotes(self): |
| 300 | self.readerAssertEqual('a"b"c', [['a"b"c']]) |
| 301 | |
| 302 | def test_quotes_and_more(self): |
| 303 | self.readerAssertEqual('"a"b', [['ab']]) |
| 304 | |
| 305 | def test_lone_quote(self): |
| 306 | self.readerAssertEqual('a"b', [['a"b']]) |
| 307 | |
| 308 | def test_quote_and_quote(self): |
| 309 | self.readerAssertEqual('"a" "b"', [['a "b"']]) |
| 310 | |
| 311 | def test_space_and_quote(self): |
| 312 | self.readerAssertEqual(' "a"', [[' "a"']]) |
| 313 | |
| 314 | def test_quoted(self): |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 315 | self.readerAssertEqual('1,2,3,"I think, therefore I am",5,6', |
| 316 | [['1', '2', '3', |
| 317 | 'I think, therefore I am', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 318 | '5', '6']]) |
| 319 | |
| 320 | def test_quoted_quote(self): |
| 321 | self.readerAssertEqual('1,2,3,"""I see,"" said the blind man","as he picked up his hammer and saw"', |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 322 | [['1', '2', '3', |
| 323 | '"I see," said the blind man', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 324 | 'as he picked up his hammer and saw']]) |
| 325 | |
| 326 | def test_quoted_nl(self): |
| 327 | input = '''\ |
| 328 | 1,2,3,"""I see,"" |
| 329 | said the blind man","as he picked up his |
| 330 | hammer and saw" |
| 331 | 9,8,7,6''' |
| 332 | self.readerAssertEqual(input, |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 333 | [['1', '2', '3', |
| 334 | '"I see,"\nsaid the blind man', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 335 | 'as he picked up his\nhammer and saw'], |
| 336 | ['9','8','7','6']]) |
| 337 | |
| 338 | def test_dubious_quote(self): |
| 339 | self.readerAssertEqual('12,12,1",', [['12', '12', '1"', '']]) |
| 340 | |
| 341 | def test_null(self): |
| 342 | self.writerAssertEqual([], '') |
| 343 | |
| 344 | def test_single(self): |
| 345 | self.writerAssertEqual([['abc']], 'abc\r\n') |
| 346 | |
| 347 | def test_simple(self): |
| 348 | self.writerAssertEqual([[1, 2, 'abc', 3, 4]], '1,2,abc,3,4\r\n') |
| 349 | |
| 350 | def test_quotes(self): |
| 351 | self.writerAssertEqual([[1, 2, 'a"bc"', 3, 4]], '1,2,"a""bc""",3,4\r\n') |
| 352 | |
| 353 | def test_quote_fieldsep(self): |
| 354 | self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') |
| 355 | |
| 356 | def test_newlines(self): |
| 357 | self.writerAssertEqual([[1, 2, 'a\nbc', 3, 4]], '1,2,"a\nbc",3,4\r\n') |
| 358 | |
| 359 | class EscapedExcel(csv.excel): |
| 360 | quoting = csv.QUOTE_NONE |
| 361 | escapechar = '\\' |
| 362 | |
| 363 | class TestEscapedExcel(TestCsvBase): |
| 364 | dialect = EscapedExcel() |
| 365 | |
| 366 | def test_escape_fieldsep(self): |
| 367 | self.writerAssertEqual([['abc,def']], 'abc\\,def\r\n') |
| 368 | |
| 369 | def test_read_escape_fieldsep(self): |
| 370 | self.readerAssertEqual('abc\\,def\r\n', [['abc,def']]) |
| 371 | |
| 372 | class QuotedEscapedExcel(csv.excel): |
| 373 | quoting = csv.QUOTE_NONNUMERIC |
| 374 | escapechar = '\\' |
| 375 | |
| 376 | class TestQuotedEscapedExcel(TestCsvBase): |
| 377 | dialect = QuotedEscapedExcel() |
| 378 | |
| 379 | def test_write_escape_fieldsep(self): |
| 380 | self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') |
| 381 | |
| 382 | def test_read_escape_fieldsep(self): |
| 383 | self.readerAssertEqual('"abc\\,def"\r\n', [['abc,def']]) |
| 384 | |
| 385 | # Disabled, pending support in csv.utils module |
| 386 | class TestDictFields(unittest.TestCase): |
| 387 | ### "long" means the row is longer than the number of fieldnames |
| 388 | ### "short" means there are fewer elements in the row than fieldnames |
| 389 | def test_write_simple_dict(self): |
| 390 | fileobj = StringIO() |
| 391 | writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) |
| 392 | writer.writerow({"f1": 10, "f3": "abc"}) |
| 393 | self.assertEqual(fileobj.getvalue(), "10,,abc\r\n") |
| 394 | |
| 395 | def test_write_no_fields(self): |
| 396 | fileobj = StringIO() |
| 397 | self.assertRaises(TypeError, csv.DictWriter, fileobj) |
| 398 | |
| 399 | def test_read_dict_fields(self): |
| 400 | reader = csv.DictReader(StringIO("1,2,abc\r\n"), |
| 401 | fieldnames=["f1", "f2", "f3"]) |
| 402 | self.assertEqual(reader.next(), {"f1": '1', "f2": '2', "f3": 'abc'}) |
| 403 | |
| 404 | def test_read_long(self): |
| 405 | reader = csv.DictReader(StringIO("1,2,abc,4,5,6\r\n"), |
| 406 | fieldnames=["f1", "f2"]) |
| 407 | self.assertEqual(reader.next(), {"f1": '1', "f2": '2', |
| 408 | None: ["abc", "4", "5", "6"]}) |
| 409 | |
| 410 | def test_read_long_with_rest(self): |
| 411 | reader = csv.DictReader(StringIO("1,2,abc,4,5,6\r\n"), |
| 412 | fieldnames=["f1", "f2"], restkey="_rest") |
| 413 | self.assertEqual(reader.next(), {"f1": '1', "f2": '2', |
| 414 | "_rest": ["abc", "4", "5", "6"]}) |
| 415 | |
| 416 | def test_read_short(self): |
| 417 | reader = csv.DictReader(["1,2,abc,4,5,6\r\n","1,2,abc\r\n"], |
| 418 | fieldnames="1 2 3 4 5 6".split(), |
| 419 | restval="DEFAULT") |
| 420 | self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', |
| 421 | "4": '4', "5": '5', "6": '6'}) |
| 422 | self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', |
| 423 | "4": 'DEFAULT', "5": 'DEFAULT', |
| 424 | "6": 'DEFAULT'}) |
| 425 | |
| 426 | def test_read_with_blanks(self): |
| 427 | reader = csv.DictReader(["1,2,abc,4,5,6\r\n","\r\n", |
| 428 | "1,2,abc,4,5,6\r\n"], |
| 429 | fieldnames="1 2 3 4 5 6".split()) |
| 430 | self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', |
| 431 | "4": '4', "5": '5', "6": '6'}) |
| 432 | self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', |
| 433 | "4": '4', "5": '5', "6": '6'}) |
| 434 | |
| 435 | class TestArrayWrites(unittest.TestCase): |
| 436 | def test_int_write(self): |
| 437 | import array |
| 438 | contents = [(20-i) for i in range(20)] |
| 439 | a = array.array('i', contents) |
| 440 | fileobj = StringIO() |
| 441 | writer = csv.writer(fileobj, dialect="excel") |
| 442 | writer.writerow(a) |
| 443 | expected = ",".join([str(i) for i in a])+"\r\n" |
| 444 | self.assertEqual(fileobj.getvalue(), expected) |
| 445 | |
| 446 | def test_double_write(self): |
| 447 | import array |
| 448 | contents = [(20-i)*0.1 for i in range(20)] |
| 449 | a = array.array('d', contents) |
| 450 | fileobj = StringIO() |
| 451 | writer = csv.writer(fileobj, dialect="excel") |
| 452 | writer.writerow(a) |
| 453 | expected = ",".join([str(i) for i in a])+"\r\n" |
| 454 | self.assertEqual(fileobj.getvalue(), expected) |
| 455 | |
| 456 | def test_float_write(self): |
| 457 | import array |
| 458 | contents = [(20-i)*0.1 for i in range(20)] |
| 459 | a = array.array('f', contents) |
| 460 | fileobj = StringIO() |
| 461 | writer = csv.writer(fileobj, dialect="excel") |
| 462 | writer.writerow(a) |
| 463 | expected = ",".join([str(i) for i in a])+"\r\n" |
| 464 | self.assertEqual(fileobj.getvalue(), expected) |
| 465 | |
| 466 | def test_char_write(self): |
| 467 | import array, string |
| 468 | a = array.array('c', string.letters) |
| 469 | fileobj = StringIO() |
| 470 | writer = csv.writer(fileobj, dialect="excel") |
| 471 | writer.writerow(a) |
| 472 | expected = ",".join(a)+"\r\n" |
| 473 | self.assertEqual(fileobj.getvalue(), expected) |
| 474 | |
| 475 | class TestDialectValidity(unittest.TestCase): |
| 476 | def test_quoting(self): |
| 477 | class mydialect(csv.Dialect): |
| 478 | delimiter = ";" |
| 479 | escapechar = '\\' |
| 480 | doublequote = False |
| 481 | skipinitialspace = True |
| 482 | lineterminator = '\r\n' |
| 483 | quoting = csv.QUOTE_NONE |
| 484 | d = mydialect() |
| 485 | |
| 486 | mydialect.quoting = None |
| 487 | self.assertRaises(csv.Error, mydialect) |
| 488 | |
| 489 | mydialect.quoting = csv.QUOTE_NONE |
| 490 | mydialect.escapechar = None |
| 491 | self.assertRaises(csv.Error, mydialect) |
| 492 | |
| 493 | mydialect.doublequote = True |
| 494 | mydialect.quoting = csv.QUOTE_ALL |
| 495 | mydialect.quotechar = '"' |
| 496 | d = mydialect() |
| 497 | |
| 498 | mydialect.quotechar = "''" |
| 499 | self.assertRaises(csv.Error, mydialect) |
| 500 | |
| 501 | mydialect.quotechar = 4 |
| 502 | self.assertRaises(csv.Error, mydialect) |
| 503 | |
| 504 | def test_delimiter(self): |
| 505 | class mydialect(csv.Dialect): |
| 506 | delimiter = ";" |
| 507 | escapechar = '\\' |
| 508 | doublequote = False |
| 509 | skipinitialspace = True |
| 510 | lineterminator = '\r\n' |
| 511 | quoting = csv.QUOTE_NONE |
| 512 | d = mydialect() |
| 513 | |
| 514 | mydialect.delimiter = ":::" |
| 515 | self.assertRaises(csv.Error, mydialect) |
| 516 | |
| 517 | mydialect.delimiter = 4 |
| 518 | self.assertRaises(csv.Error, mydialect) |
| 519 | |
| 520 | def test_lineterminator(self): |
| 521 | class mydialect(csv.Dialect): |
| 522 | delimiter = ";" |
| 523 | escapechar = '\\' |
| 524 | doublequote = False |
| 525 | skipinitialspace = True |
| 526 | lineterminator = '\r\n' |
| 527 | quoting = csv.QUOTE_NONE |
| 528 | d = mydialect() |
| 529 | |
| 530 | mydialect.lineterminator = ":::" |
| 531 | d = mydialect() |
| 532 | |
| 533 | mydialect.lineterminator = 4 |
| 534 | self.assertRaises(csv.Error, mydialect) |
| 535 | |
| 536 | |
Skip Montanaro | 48816c6 | 2003-04-25 14:43:14 +0000 | [diff] [blame] | 537 | class TestSniffer(unittest.TestCase): |
| 538 | sample1 = """\ |
| 539 | Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes |
| 540 | Shark City, Glendale Heights, IL, 12/28/02, Prezence |
| 541 | Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow |
| 542 | Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back |
| 543 | """ |
| 544 | sample2 = """\ |
| 545 | 'Harry''s':'Arlington Heights':'IL':'2/1/03':'Kimi Hayes' |
| 546 | 'Shark City':'Glendale Heights':'IL':'12/28/02':'Prezence' |
| 547 | 'Tommy''s Place':'Blue Island':'IL':'12/28/02':'Blue Sunday/White Crow' |
| 548 | 'Stonecutters Seafood and Chop House':'Lemont':'IL':'12/19/02':'Week Back' |
| 549 | """ |
| 550 | |
| 551 | header = '''\ |
| 552 | "venue","city","state","date","performers" |
| 553 | ''' |
| 554 | def test_has_header(self): |
| 555 | sniffer = csv.Sniffer() |
| 556 | self.assertEqual(sniffer.has_header(self.sample1), False) |
| 557 | self.assertEqual(sniffer.has_header(self.header+self.sample1), True) |
| 558 | |
| 559 | def test_sniff(self): |
| 560 | sniffer = csv.Sniffer() |
| 561 | dialect = sniffer.sniff(self.sample1) |
| 562 | self.assertEqual(dialect.delimiter, ",") |
| 563 | self.assertEqual(dialect.quotechar, '"') |
| 564 | self.assertEqual(dialect.skipinitialspace, True) |
| 565 | |
| 566 | dialect = sniffer.sniff(self.sample2) |
| 567 | self.assertEqual(dialect.delimiter, ":") |
| 568 | self.assertEqual(dialect.quotechar, "'") |
| 569 | self.assertEqual(dialect.skipinitialspace, False) |
| 570 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 571 | if not hasattr(sys, "gettotalrefcount"): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 572 | if test_support.verbose: print "*** skipping leakage tests ***" |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 573 | else: |
| 574 | class NUL: |
| 575 | def write(s, *args): |
| 576 | pass |
| 577 | writelines = write |
| 578 | |
| 579 | class TestLeaks(unittest.TestCase): |
| 580 | def test_create_read(self): |
| 581 | delta = 0 |
| 582 | lastrc = sys.gettotalrefcount() |
| 583 | for i in xrange(20): |
| 584 | gc.collect() |
| 585 | self.assertEqual(gc.garbage, []) |
| 586 | rc = sys.gettotalrefcount() |
| 587 | csv.reader(["a,b,c\r\n"]) |
| 588 | csv.reader(["a,b,c\r\n"]) |
| 589 | csv.reader(["a,b,c\r\n"]) |
| 590 | delta = rc-lastrc |
| 591 | lastrc = rc |
| 592 | # if csv.reader() leaks, last delta should be 3 or more |
| 593 | self.assertEqual(delta < 3, True) |
| 594 | |
| 595 | def test_create_write(self): |
| 596 | delta = 0 |
| 597 | lastrc = sys.gettotalrefcount() |
| 598 | s = NUL() |
| 599 | for i in xrange(20): |
| 600 | gc.collect() |
| 601 | self.assertEqual(gc.garbage, []) |
| 602 | rc = sys.gettotalrefcount() |
| 603 | csv.writer(s) |
| 604 | csv.writer(s) |
| 605 | csv.writer(s) |
| 606 | delta = rc-lastrc |
| 607 | lastrc = rc |
| 608 | # if csv.writer() leaks, last delta should be 3 or more |
| 609 | self.assertEqual(delta < 3, True) |
| 610 | |
| 611 | def test_read(self): |
| 612 | delta = 0 |
| 613 | rows = ["a,b,c\r\n"]*5 |
| 614 | lastrc = sys.gettotalrefcount() |
| 615 | for i in xrange(20): |
| 616 | gc.collect() |
| 617 | self.assertEqual(gc.garbage, []) |
| 618 | rc = sys.gettotalrefcount() |
| 619 | rdr = csv.reader(rows) |
| 620 | for row in rdr: |
| 621 | pass |
| 622 | delta = rc-lastrc |
| 623 | lastrc = rc |
| 624 | # if reader leaks during read, delta should be 5 or more |
| 625 | self.assertEqual(delta < 5, True) |
| 626 | |
| 627 | def test_write(self): |
| 628 | delta = 0 |
| 629 | rows = [[1,2,3]]*5 |
| 630 | s = NUL() |
| 631 | lastrc = sys.gettotalrefcount() |
| 632 | for i in xrange(20): |
| 633 | gc.collect() |
| 634 | self.assertEqual(gc.garbage, []) |
| 635 | rc = sys.gettotalrefcount() |
| 636 | writer = csv.writer(s) |
| 637 | for row in rows: |
| 638 | writer.writerow(row) |
| 639 | delta = rc-lastrc |
| 640 | lastrc = rc |
| 641 | # if writer leaks during write, last delta should be 5 or more |
| 642 | self.assertEqual(delta < 5, True) |
| 643 | |
Skip Montanaro | 1a56665 | 2003-05-06 15:56:05 +0000 | [diff] [blame^] | 644 | # commented out for now - csv module doesn't yet support Unicode |
| 645 | if 0: |
| 646 | from StringIO import StringIO |
| 647 | import csv |
| 648 | |
| 649 | class TestUnicode(unittest.TestCase): |
| 650 | def test_unicode_read(self): |
| 651 | import codecs |
| 652 | f = codecs.EncodedFile(StringIO("Martin von Löwis," |
| 653 | "Marc André Lemburg," |
| 654 | "Guido van Rossum," |
| 655 | "François Pinard\r\n"), |
| 656 | data_encoding='iso-8859-1') |
| 657 | reader = csv.reader(f) |
| 658 | self.assertEqual(list(reader), [[u"Martin von Löwis", |
| 659 | u"Marc André Lemburg", |
| 660 | u"Guido van Rossum", |
| 661 | u"François Pinardn"]]) |
| 662 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 663 | def test_main(): |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 664 | mod = sys.modules[__name__] |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 665 | test_support.run_unittest( |
| 666 | *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')] |
| 667 | ) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 668 | |
| 669 | if __name__ == '__main__': |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 670 | test_main() |