0001 # -*- coding: iso-8859-1 -*- 0002 # Copyright (C) 2001,2002 Python Software Foundation 0003 # csv package unit tests 0004 0005 import sys 0006 import os 0007 import unittest 0008 from StringIO import StringIO 0009 import tempfile 0010 import csv 0011 import gc 0012 from test import test_support 0013 0014 class Test_Csv(unittest.TestCase): 0015 """ 0016 Test the underlying C csv parser in ways that are not appropriate 0017 from the high level interface. Further tests of this nature are done 0018 in TestDialectRegistry. 0019 """ 0020 def test_reader_arg_valid(self): 0021 self.assertRaises(TypeError, csv.reader) 0022 self.assertRaises(TypeError, csv.reader, None) 0023 self.assertRaises(AttributeError, csv.reader, [], bad_attr = 0) 0024 self.assertRaises(csv.Error, csv.reader, [], 'foo') 0025 class BadClass: 0026 def __init__(self): 0027 raise IOError 0028 self.assertRaises(IOError, csv.reader, [], BadClass) 0029 self.assertRaises(TypeError, csv.reader, [], None) 0030 class BadDialect: 0031 bad_attr = 0 0032 self.assertRaises(AttributeError, csv.reader, [], BadDialect) 0033 0034 def test_writer_arg_valid(self): 0035 self.assertRaises(TypeError, csv.writer) 0036 self.assertRaises(TypeError, csv.writer, None) 0037 self.assertRaises(AttributeError, csv.writer, StringIO(), bad_attr = 0) 0038 0039 def _test_attrs(self, obj): 0040 self.assertEqual(obj.dialect.delimiter, ',') 0041 obj.dialect.delimiter = '\t' 0042 self.assertEqual(obj.dialect.delimiter, '\t') 0043 self.assertRaises(TypeError, delattr, obj.dialect, 'delimiter') 0044 self.assertRaises(TypeError, setattr, obj.dialect, 0045 'lineterminator', None) 0046 obj.dialect.escapechar = None 0047 self.assertEqual(obj.dialect.escapechar, None) 0048 self.assertRaises(TypeError, delattr, obj.dialect, 'quoting') 0049 self.assertRaises(TypeError, setattr, obj.dialect, 'quoting', None) 0050 obj.dialect.quoting = csv.QUOTE_MINIMAL 0051 self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL) 0052 0053 def test_reader_attrs(self): 0054 self._test_attrs(csv.reader([])) 0055 0056 def test_writer_attrs(self): 0057 self._test_attrs(csv.writer(StringIO())) 0058 0059 def _write_test(self, fields, expect, **kwargs): 0060 fd, name = tempfile.mkstemp() 0061 fileobj = os.fdopen(fd, "w+b") 0062 try: 0063 writer = csv.writer(fileobj, **kwargs) 0064 writer.writerow(fields) 0065 fileobj.seek(0) 0066 self.assertEqual(fileobj.read(), 0067 expect + writer.dialect.lineterminator) 0068 finally: 0069 fileobj.close() 0070 os.unlink(name) 0071 0072 def test_write_arg_valid(self): 0073 self.assertRaises(csv.Error, self._write_test, None, '') 0074 self._write_test((), '') 0075 self._write_test([None], '""') 0076 self.assertRaises(csv.Error, self._write_test, 0077 [None], None, quoting = csv.QUOTE_NONE) 0078 # Check that exceptions are passed up the chain 0079 class BadList: 0080 def __len__(self): 0081 return 10; 0082 def __getitem__(self, i): 0083 if i > 2: 0084 raise IOError 0085 self.assertRaises(IOError, self._write_test, BadList(), '') 0086 class BadItem: 0087 def __str__(self): 0088 raise IOError 0089 self.assertRaises(IOError, self._write_test, [BadItem()], '') 0090 0091 def test_write_bigfield(self): 0092 # This exercises the buffer realloc functionality 0093 bigstring = 'X' * 50000 0094 self._write_test([bigstring,bigstring], '%s,%s' % \ 0095 (bigstring, bigstring)) 0096 0097 def test_write_quoting(self): 0098 self._write_test(['a','1','p,q'], 'a,1,"p,q"') 0099 self.assertRaises(csv.Error, 0100 self._write_test, 0101 ['a','1','p,q'], 'a,1,"p,q"', 0102 quoting = csv.QUOTE_NONE) 0103 self._write_test(['a','1','p,q'], 'a,1,"p,q"', 0104 quoting = csv.QUOTE_MINIMAL) 0105 self._write_test(['a','1','p,q'], '"a",1,"p,q"', 0106 quoting = csv.QUOTE_NONNUMERIC) 0107 self._write_test(['a','1','p,q'], '"a","1","p,q"', 0108 quoting = csv.QUOTE_ALL) 0109 0110 def test_write_escape(self): 0111 self._write_test(['a','1','p,q'], 'a,1,"p,q"', 0112 escapechar='\\') 0113 # FAILED - needs to be fixed [am]: 0114 # self._write_test(['a','1','p,"q"'], 'a,1,"p,\\"q\\"', 0115 # escapechar='\\', doublequote = 0) 0116 self._write_test(['a','1','p,q'], 'a,1,p\\,q', 0117 escapechar='\\', quoting = csv.QUOTE_NONE) 0118 0119 def test_writerows(self): 0120 class BrokenFile: 0121 def write(self, buf): 0122 raise IOError 0123 writer = csv.writer(BrokenFile()) 0124 self.assertRaises(IOError, writer.writerows, [['a']]) 0125 fd, name = tempfile.mkstemp() 0126 fileobj = os.fdopen(fd, "w+b") 0127 try: 0128 writer = csv.writer(fileobj) 0129 self.assertRaises(TypeError, writer.writerows, None) 0130 writer.writerows([['a','b'],['c','d']]) 0131 fileobj.seek(0) 0132 self.assertEqual(fileobj.read(), "a,b\r\nc,d\r\n") 0133 finally: 0134 fileobj.close() 0135 os.unlink(name) 0136 0137 def _read_test(self, input, expect, **kwargs): 0138 reader = csv.reader(input, **kwargs) 0139 result = list(reader) 0140 self.assertEqual(result, expect) 0141 0142 def test_read_oddinputs(self): 0143 self._read_test([], []) 0144 self._read_test([''], [[]]) 0145 self.assertRaises(csv.Error, self._read_test, 0146 ['"ab"c'], None, strict = 1) 0147 # cannot handle null bytes for the moment 0148 self.assertRaises(csv.Error, self._read_test, 0149 ['ab\0c'], None, strict = 1) 0150 self._read_test(['"ab"c'], [['abc']], doublequote = 0) 0151 0152 def test_read_eol(self): 0153 self._read_test(['a,b'], [['a','b']]) 0154 self._read_test(['a,b\n'], [['a','b']]) 0155 self._read_test(['a,b\r\n'], [['a','b']]) 0156 self._read_test(['a,b\r'], [['a','b']]) 0157 self.assertRaises(csv.Error, self._read_test, ['a,b\rc,d'], []) 0158 self.assertRaises(csv.Error, self._read_test, ['a,b\nc,d'], []) 0159 self.assertRaises(csv.Error, self._read_test, ['a,b\r\nc,d'], []) 0160 0161 def test_read_escape(self): 0162 self._read_test(['a,\\b,c'], [['a', '\\b', 'c']], escapechar='\\') 0163 self._read_test(['a,b\\,c'], [['a', 'b,c']], escapechar='\\') 0164 self._read_test(['a,"b\\,c"'], [['a', 'b,c']], escapechar='\\') 0165 self._read_test(['a,"b,\\c"'], [['a', 'b,\\c']], escapechar='\\') 0166 self._read_test(['a,"b,c\\""'], [['a', 'b,c"']], escapechar='\\') 0167 self._read_test(['a,"b,c"\\'], [['a', 'b,c\\']], escapechar='\\') 0168 0169 def test_read_bigfield(self): 0170 # This exercises the buffer realloc functionality 0171 bigstring = 'X' * 50000 0172 bigline = '%s,%s' % (bigstring, bigstring) 0173 self._read_test([bigline], [[bigstring, bigstring]]) 0174 0175 class TestDialectRegistry(unittest.TestCase): 0176 def test_registry_badargs(self): 0177 self.assertRaises(TypeError, csv.list_dialects, None) 0178 self.assertRaises(TypeError, csv.get_dialect) 0179 self.assertRaises(csv.Error, csv.get_dialect, None) 0180 self.assertRaises(csv.Error, csv.get_dialect, "nonesuch") 0181 self.assertRaises(TypeError, csv.unregister_dialect) 0182 self.assertRaises(csv.Error, csv.unregister_dialect, None) 0183 self.assertRaises(csv.Error, csv.unregister_dialect, "nonesuch") 0184 self.assertRaises(TypeError, csv.register_dialect, None) 0185 self.assertRaises(TypeError, csv.register_dialect, None, None) 0186 self.assertRaises(TypeError, csv.register_dialect, "nonesuch", None) 0187 class bogus: 0188 def __init__(self): 0189 raise KeyError 0190 self.assertRaises(KeyError, csv.register_dialect, "nonesuch", bogus) 0191 0192 def test_registry(self): 0193 class myexceltsv(csv.excel): 0194 delimiter = "\t" 0195 name = "myexceltsv" 0196 expected_dialects = csv.list_dialects() + [name] 0197 expected_dialects.sort() 0198 csv.register_dialect(name, myexceltsv) 0199 try: 0200 self.failUnless(isinstance(csv.get_dialect(name), myexceltsv)) 0201 got_dialects = csv.list_dialects() 0202 got_dialects.sort() 0203 self.assertEqual(expected_dialects, got_dialects) 0204 finally: 0205 csv.unregister_dialect(name) 0206 0207 def test_incomplete_dialect(self): 0208 class myexceltsv(csv.Dialect): 0209 delimiter = "\t" 0210 self.assertRaises(csv.Error, myexceltsv) 0211 0212 def test_space_dialect(self): 0213 class space(csv.excel): 0214 delimiter = " " 0215 quoting = csv.QUOTE_NONE 0216 escapechar = "\\" 0217 0218 fd, name = tempfile.mkstemp() 0219 fileobj = os.fdopen(fd, "w+b") 0220 try: 0221 fileobj.write("abc def\nc1ccccc1 benzene\n") 0222 fileobj.seek(0) 0223 rdr = csv.reader(fileobj, dialect=space()) 0224 self.assertEqual(rdr.next(), ["abc", "def"]) 0225 self.assertEqual(rdr.next(), ["c1ccccc1", "benzene"]) 0226 finally: 0227 fileobj.close() 0228 os.unlink(name) 0229 0230 def test_dialect_apply(self): 0231 class testA(csv.excel): 0232 delimiter = "\t" 0233 class testB(csv.excel): 0234 delimiter = ":" 0235 class testC(csv.excel): 0236 delimiter = "|" 0237 0238 csv.register_dialect('testC', testC) 0239 try: 0240 fd, name = tempfile.mkstemp() 0241 fileobj = os.fdopen(fd, "w+b") 0242 try: 0243 writer = csv.writer(fileobj) 0244 writer.writerow([1,2,3]) 0245 fileobj.seek(0) 0246 self.assertEqual(fileobj.read(), "1,2,3\r\n") 0247 finally: 0248 fileobj.close() 0249 os.unlink(name) 0250 0251 fd, name = tempfile.mkstemp() 0252 fileobj = os.fdopen(fd, "w+b") 0253 try: 0254 writer = csv.writer(fileobj, testA) 0255 writer.writerow([1,2,3]) 0256 fileobj.seek(0) 0257 self.assertEqual(fileobj.read(), "1\t2\t3\r\n") 0258 finally: 0259 fileobj.close() 0260 os.unlink(name) 0261 0262 fd, name = tempfile.mkstemp() 0263 fileobj = os.fdopen(fd, "w+b") 0264 try: 0265 writer = csv.writer(fileobj, dialect=testB()) 0266 writer.writerow([1,2,3]) 0267 fileobj.seek(0) 0268 self.assertEqual(fileobj.read(), "1:2:3\r\n") 0269 finally: 0270 fileobj.close() 0271 os.unlink(name) 0272 0273 fd, name = tempfile.mkstemp() 0274 fileobj = os.fdopen(fd, "w+b") 0275 try: 0276 writer = csv.writer(fileobj, dialect='testC') 0277 writer.writerow([1,2,3]) 0278 fileobj.seek(0) 0279 self.assertEqual(fileobj.read(), "1|2|3\r\n") 0280 finally: 0281 fileobj.close() 0282 os.unlink(name) 0283 0284 fd, name = tempfile.mkstemp() 0285 fileobj = os.fdopen(fd, "w+b") 0286 try: 0287 writer = csv.writer(fileobj, dialect=testA, delimiter=';') 0288 writer.writerow([1,2,3]) 0289 fileobj.seek(0) 0290 self.assertEqual(fileobj.read(), "1;2;3\r\n") 0291 finally: 0292 fileobj.close() 0293 os.unlink(name) 0294 0295 finally: 0296 csv.unregister_dialect('testC') 0297 0298 def test_bad_dialect(self): 0299 # Unknown parameter 0300 self.assertRaises(AttributeError, csv.reader, [], bad_attr = 0) 0301 # Bad values 0302 self.assertRaises(TypeError, csv.reader, [], delimiter = None) 0303 self.assertRaises(TypeError, csv.reader, [], quoting = -1) 0304 self.assertRaises(TypeError, csv.reader, [], quoting = 100) 0305 0306 class TestCsvBase(unittest.TestCase): 0307 def readerAssertEqual(self, input, expected_result): 0308 fd, name = tempfile.mkstemp() 0309 fileobj = os.fdopen(fd, "w+b") 0310 try: 0311 fileobj.write(input) 0312 fileobj.seek(0) 0313 reader = csv.reader(fileobj, dialect = self.dialect) 0314 fields = list(reader) 0315 self.assertEqual(fields, expected_result) 0316 finally: 0317 fileobj.close() 0318 os.unlink(name) 0319 0320 def writerAssertEqual(self, input, expected_result): 0321 fd, name = tempfile.mkstemp() 0322 fileobj = os.fdopen(fd, "w+b") 0323 try: 0324 writer = csv.writer(fileobj, dialect = self.dialect) 0325 writer.writerows(input) 0326 fileobj.seek(0) 0327 self.assertEqual(fileobj.read(), expected_result) 0328 finally: 0329 fileobj.close() 0330 os.unlink(name) 0331 0332 class TestDialectExcel(TestCsvBase): 0333 dialect = 'excel' 0334 0335 def test_single(self): 0336 self.readerAssertEqual('abc', [['abc']]) 0337 0338 def test_simple(self): 0339 self.readerAssertEqual('1,2,3,4,5', [['1','2','3','4','5']]) 0340 0341 def test_blankline(self): 0342 self.readerAssertEqual('', []) 0343 0344 def test_empty_fields(self): 0345 self.readerAssertEqual(',', [['', '']]) 0346 0347 def test_singlequoted(self): 0348 self.readerAssertEqual('""', [['']]) 0349 0350 def test_singlequoted_left_empty(self): 0351 self.readerAssertEqual('"",', [['','']]) 0352 0353 def test_singlequoted_right_empty(self): 0354 self.readerAssertEqual(',""', [['','']]) 0355 0356 def test_single_quoted_quote(self): 0357 self.readerAssertEqual('""""', [['"']]) 0358 0359 def test_quoted_quotes(self): 0360 self.readerAssertEqual('""""""', [['""']]) 0361 0362 def test_inline_quote(self): 0363 self.readerAssertEqual('a""b', [['a""b']]) 0364 0365 def test_inline_quotes(self): 0366 self.readerAssertEqual('a"b"c', [['a"b"c']]) 0367 0368 def test_quotes_and_more(self): 0369 self.readerAssertEqual('"a"b', [['ab']]) 0370 0371 def test_lone_quote(self): 0372 self.readerAssertEqual('a"b', [['a"b']]) 0373 0374 def test_quote_and_quote(self): 0375 self.readerAssertEqual('"a" "b"', [['a "b"']]) 0376 0377 def test_space_and_quote(self): 0378 self.readerAssertEqual(' "a"', [[' "a"']]) 0379 0380 def test_quoted(self): 0381 self.readerAssertEqual('1,2,3,"I think, therefore I am",5,6', 0382 [['1', '2', '3', 0383 'I think, therefore I am', 0384 '5', '6']]) 0385 0386 def test_quoted_quote(self): 0387 self.readerAssertEqual('1,2,3,"""I see,"" said the blind man","as he picked up his hammer and saw"', 0388 [['1', '2', '3', 0389 '"I see," said the blind man', 0390 'as he picked up his hammer and saw']]) 0391 0392 def test_quoted_nl(self): 0393 input = '''\ 0394 1,2,3,"""I see,"" 0395 said the blind man","as he picked up his 0396 hammer and saw" 0397 9,8,7,6''' 0398 self.readerAssertEqual(input, 0399 [['1', '2', '3', 0400 '"I see,"\nsaid the blind man', 0401 'as he picked up his\nhammer and saw'], 0402 ['9','8','7','6']]) 0403 0404 def test_dubious_quote(self): 0405 self.readerAssertEqual('12,12,1",', [['12', '12', '1"', '']]) 0406 0407 def test_null(self): 0408 self.writerAssertEqual([], '') 0409 0410 def test_single(self): 0411 self.writerAssertEqual([['abc']], 'abc\r\n') 0412 0413 def test_simple(self): 0414 self.writerAssertEqual([[1, 2, 'abc', 3, 4]], '1,2,abc,3,4\r\n') 0415 0416 def test_quotes(self): 0417 self.writerAssertEqual([[1, 2, 'a"bc"', 3, 4]], '1,2,"a""bc""",3,4\r\n') 0418 0419 def test_quote_fieldsep(self): 0420 self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') 0421 0422 def test_newlines(self): 0423 self.writerAssertEqual([[1, 2, 'a\nbc', 3, 4]], '1,2,"a\nbc",3,4\r\n') 0424 0425 class EscapedExcel(csv.excel): 0426 quoting = csv.QUOTE_NONE 0427 escapechar = '\\' 0428 0429 class TestEscapedExcel(TestCsvBase): 0430 dialect = EscapedExcel() 0431 0432 def test_escape_fieldsep(self): 0433 self.writerAssertEqual([['abc,def']], 'abc\\,def\r\n') 0434 0435 def test_read_escape_fieldsep(self): 0436 self.readerAssertEqual('abc\\,def\r\n', [['abc,def']]) 0437 0438 class QuotedEscapedExcel(csv.excel): 0439 quoting = csv.QUOTE_NONNUMERIC 0440 escapechar = '\\' 0441 0442 class TestQuotedEscapedExcel(TestCsvBase): 0443 dialect = QuotedEscapedExcel() 0444 0445 def test_write_escape_fieldsep(self): 0446 self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') 0447 0448 def test_read_escape_fieldsep(self): 0449 self.readerAssertEqual('"abc\\,def"\r\n', [['abc,def']]) 0450 0451 class TestDictFields(unittest.TestCase): 0452 ### "long" means the row is longer than the number of fieldnames 0453 ### "short" means there are fewer elements in the row than fieldnames 0454 def test_write_simple_dict(self): 0455 fd, name = tempfile.mkstemp() 0456 fileobj = os.fdopen(fd, "w+b") 0457 try: 0458 writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) 0459 writer.writerow({"f1": 10, "f3": "abc"}) 0460 fileobj.seek(0) 0461 self.assertEqual(fileobj.read(), "10,,abc\r\n") 0462 finally: 0463 fileobj.close() 0464 os.unlink(name) 0465 0466 def test_write_no_fields(self): 0467 fileobj = StringIO() 0468 self.assertRaises(TypeError, csv.DictWriter, fileobj) 0469 0470 def test_read_dict_fields(self): 0471 fd, name = tempfile.mkstemp() 0472 fileobj = os.fdopen(fd, "w+b") 0473 try: 0474 fileobj.write("1,2,abc\r\n") 0475 fileobj.seek(0) 0476 reader = csv.DictReader(fileobj, 0477 fieldnames=["f1", "f2", "f3"]) 0478 self.assertEqual(reader.next(), {"f1": '1', "f2": '2', "f3": 'abc'}) 0479 finally: 0480 fileobj.close() 0481 os.unlink(name) 0482 0483 def test_read_dict_no_fieldnames(self): 0484 fd, name = tempfile.mkstemp() 0485 fileobj = os.fdopen(fd, "w+b") 0486 try: 0487 fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") 0488 fileobj.seek(0) 0489 reader = csv.DictReader(fileobj) 0490 self.assertEqual(reader.next(), {"f1": '1', "f2": '2', "f3": 'abc'}) 0491 finally: 0492 fileobj.close() 0493 os.unlink(name) 0494 0495 def test_read_long(self): 0496 fd, name = tempfile.mkstemp() 0497 fileobj = os.fdopen(fd, "w+b") 0498 try: 0499 fileobj.write("1,2,abc,4,5,6\r\n") 0500 fileobj.seek(0) 0501 reader = csv.DictReader(fileobj, 0502 fieldnames=["f1", "f2"]) 0503 self.assertEqual(reader.next(), {"f1": '1', "f2": '2', 0504 None: ["abc", "4", "5", "6"]}) 0505 finally: 0506 fileobj.close() 0507 os.unlink(name) 0508 0509 def test_read_long_with_rest(self): 0510 fd, name = tempfile.mkstemp() 0511 fileobj = os.fdopen(fd, "w+b") 0512 try: 0513 fileobj.write("1,2,abc,4,5,6\r\n") 0514 fileobj.seek(0) 0515 reader = csv.DictReader(fileobj, 0516 fieldnames=["f1", "f2"], restkey="_rest") 0517 self.assertEqual(reader.next(), {"f1": '1', "f2": '2', 0518 "_rest": ["abc", "4", "5", "6"]}) 0519 finally: 0520 fileobj.close() 0521 os.unlink(name) 0522 0523 def test_read_long_with_rest_no_fieldnames(self): 0524 fd, name = tempfile.mkstemp() 0525 fileobj = os.fdopen(fd, "w+b") 0526 try: 0527 fileobj.write("f1,f2\r\n1,2,abc,4,5,6\r\n") 0528 fileobj.seek(0) 0529 reader = csv.DictReader(fileobj, restkey="_rest") 0530 self.assertEqual(reader.next(), {"f1": '1', "f2": '2', 0531 "_rest": ["abc", "4", "5", "6"]}) 0532 finally: 0533 fileobj.close() 0534 os.unlink(name) 0535 0536 def test_read_short(self): 0537 fd, name = tempfile.mkstemp() 0538 fileobj = os.fdopen(fd, "w+b") 0539 try: 0540 fileobj.write("1,2,abc,4,5,6\r\n1,2,abc\r\n") 0541 fileobj.seek(0) 0542 reader = csv.DictReader(fileobj, 0543 fieldnames="1 2 3 4 5 6".split(), 0544 restval="DEFAULT") 0545 self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', 0546 "4": '4', "5": '5', "6": '6'}) 0547 self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', 0548 "4": 'DEFAULT', "5": 'DEFAULT', 0549 "6": 'DEFAULT'}) 0550 finally: 0551 fileobj.close() 0552 os.unlink(name) 0553 0554 def test_read_multi(self): 0555 sample = [ 0556 '2147483648,43.0e12,17,abc,def\r\n', 0557 '147483648,43.0e2,17,abc,def\r\n', 0558 '47483648,43.0,170,abc,def\r\n' 0559 ] 0560 0561 reader = csv.DictReader(sample, 0562 fieldnames="i1 float i2 s1 s2".split()) 0563 self.assertEqual(reader.next(), {"i1": '2147483648', 0564 "float": '43.0e12', 0565 "i2": '17', 0566 "s1": 'abc', 0567 "s2": 'def'}) 0568 0569 def test_read_with_blanks(self): 0570 reader = csv.DictReader(["1,2,abc,4,5,6\r\n","\r\n", 0571 "1,2,abc,4,5,6\r\n"], 0572 fieldnames="1 2 3 4 5 6".split()) 0573 self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', 0574 "4": '4', "5": '5', "6": '6'}) 0575 self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', 0576 "4": '4', "5": '5', "6": '6'}) 0577 0578 def test_read_semi_sep(self): 0579 reader = csv.DictReader(["1;2;abc;4;5;6\r\n"], 0580 fieldnames="1 2 3 4 5 6".split(), 0581 delimiter=';') 0582 self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', 0583 "4": '4', "5": '5', "6": '6'}) 0584 0585 class TestArrayWrites(unittest.TestCase): 0586 def test_int_write(self): 0587 import array 0588 contents = [(20-i) for i in range(20)] 0589 a = array.array('i', contents) 0590 0591 fd, name = tempfile.mkstemp() 0592 fileobj = os.fdopen(fd, "w+b") 0593 try: 0594 writer = csv.writer(fileobj, dialect="excel") 0595 writer.writerow(a) 0596 expected = ",".join([str(i) for i in a])+"\r\n" 0597 fileobj.seek(0) 0598 self.assertEqual(fileobj.read(), expected) 0599 finally: 0600 fileobj.close() 0601 os.unlink(name) 0602 0603 def test_double_write(self): 0604 import array 0605 contents = [(20-i)*0.1 for i in range(20)] 0606 a = array.array('d', contents) 0607 fd, name = tempfile.mkstemp() 0608 fileobj = os.fdopen(fd, "w+b") 0609 try: 0610 writer = csv.writer(fileobj, dialect="excel") 0611 writer.writerow(a) 0612 expected = ",".join([str(i) for i in a])+"\r\n" 0613 fileobj.seek(0) 0614 self.assertEqual(fileobj.read(), expected) 0615 finally: 0616 fileobj.close() 0617 os.unlink(name) 0618 0619 def test_float_write(self): 0620 import array 0621 contents = [(20-i)*0.1 for i in range(20)] 0622 a = array.array('f', contents) 0623 fd, name = tempfile.mkstemp() 0624 fileobj = os.fdopen(fd, "w+b") 0625 try: 0626 writer = csv.writer(fileobj, dialect="excel") 0627 writer.writerow(a) 0628 expected = ",".join([str(i) for i in a])+"\r\n" 0629 fileobj.seek(0) 0630 self.assertEqual(fileobj.read(), expected) 0631 finally: 0632 fileobj.close() 0633 os.unlink(name) 0634 0635 def test_char_write(self): 0636 import array, string 0637 a = array.array('c', string.letters) 0638 fd, name = tempfile.mkstemp() 0639 fileobj = os.fdopen(fd, "w+b") 0640 try: 0641 writer = csv.writer(fileobj, dialect="excel") 0642 writer.writerow(a) 0643 expected = ",".join(a)+"\r\n" 0644 fileobj.seek(0) 0645 self.assertEqual(fileobj.read(), expected) 0646 finally: 0647 fileobj.close() 0648 os.unlink(name) 0649 0650 class TestDialectValidity(unittest.TestCase): 0651 def test_quoting(self): 0652 class mydialect(csv.Dialect): 0653 delimiter = ";" 0654 escapechar = '\\' 0655 doublequote = False 0656 skipinitialspace = True 0657 lineterminator = '\r\n' 0658 quoting = csv.QUOTE_NONE 0659 d = mydialect() 0660 0661 mydialect.quoting = None 0662 self.assertRaises(csv.Error, mydialect) 0663 0664 mydialect.quoting = csv.QUOTE_NONE 0665 mydialect.escapechar = None 0666 self.assertRaises(csv.Error, mydialect) 0667 0668 mydialect.doublequote = True 0669 mydialect.quoting = csv.QUOTE_ALL 0670 mydialect.quotechar = '"' 0671 d = mydialect() 0672 0673 mydialect.quotechar = "''" 0674 self.assertRaises(csv.Error, mydialect) 0675 0676 mydialect.quotechar = 4 0677 self.assertRaises(csv.Error, mydialect) 0678 0679 def test_delimiter(self): 0680 class mydialect(csv.Dialect): 0681 delimiter = ";" 0682 escapechar = '\\' 0683 doublequote = False 0684 skipinitialspace = True 0685 lineterminator = '\r\n' 0686 quoting = csv.QUOTE_NONE 0687 d = mydialect() 0688 0689 mydialect.delimiter = ":::" 0690 self.assertRaises(csv.Error, mydialect) 0691 0692 mydialect.delimiter = 4 0693 self.assertRaises(csv.Error, mydialect) 0694 0695 def test_lineterminator(self): 0696 class mydialect(csv.Dialect): 0697 delimiter = ";" 0698 escapechar = '\\' 0699 doublequote = False 0700 skipinitialspace = True 0701 lineterminator = '\r\n' 0702 quoting = csv.QUOTE_NONE 0703 d = mydialect() 0704 0705 mydialect.lineterminator = ":::" 0706 d = mydialect() 0707 0708 mydialect.lineterminator = 4 0709 self.assertRaises(csv.Error, mydialect) 0710 0711 0712 class TestSniffer(unittest.TestCase): 0713 sample1 = """\ 0714 Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes 0715 Shark City, Glendale Heights, IL, 12/28/02, Prezence 0716 Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow 0717 Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back 0718 """ 0719 sample2 = """\ 0720 'Harry''s':'Arlington Heights':'IL':'2/1/03':'Kimi Hayes' 0721 'Shark City':'Glendale Heights':'IL':'12/28/02':'Prezence' 0722 'Tommy''s Place':'Blue Island':'IL':'12/28/02':'Blue Sunday/White Crow' 0723 'Stonecutters Seafood and Chop House':'Lemont':'IL':'12/19/02':'Week Back' 0724 """ 0725 0726 header = '''\ 0727 "venue","city","state","date","performers" 0728 ''' 0729 sample3 = '''\ 0730 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 0731 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 0732 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 0733 ''' 0734 0735 sample4 = '''\ 0736 2147483648;43.0e12;17;abc;def 0737 147483648;43.0e2;17;abc;def 0738 47483648;43.0;170;abc;def 0739 ''' 0740 0741 def test_has_header(self): 0742 sniffer = csv.Sniffer() 0743 self.assertEqual(sniffer.has_header(self.sample1), False) 0744 self.assertEqual(sniffer.has_header(self.header+self.sample1), True) 0745 0746 def test_sniff(self): 0747 sniffer = csv.Sniffer() 0748 dialect = sniffer.sniff(self.sample1) 0749 self.assertEqual(dialect.delimiter, ",") 0750 self.assertEqual(dialect.quotechar, '"') 0751 self.assertEqual(dialect.skipinitialspace, True) 0752 0753 dialect = sniffer.sniff(self.sample2) 0754 self.assertEqual(dialect.delimiter, ":") 0755 self.assertEqual(dialect.quotechar, "'") 0756 self.assertEqual(dialect.skipinitialspace, False) 0757 0758 def test_delimiters(self): 0759 sniffer = csv.Sniffer() 0760 dialect = sniffer.sniff(self.sample3) 0761 self.assertEqual(dialect.delimiter, "0") 0762 dialect = sniffer.sniff(self.sample3, delimiters="?,") 0763 self.assertEqual(dialect.delimiter, "?") 0764 dialect = sniffer.sniff(self.sample3, delimiters="/,") 0765 self.assertEqual(dialect.delimiter, "/") 0766 dialect = sniffer.sniff(self.sample4) 0767 self.assertEqual(dialect.delimiter, ";") 0768 0769 if not hasattr(sys, "gettotalrefcount"): 0770 if test_support.verbose: print "*** skipping leakage tests ***" 0771 else: 0772 class NUL: 0773 def write(s, *args): 0774 pass 0775 writelines = write 0776 0777 class TestLeaks(unittest.TestCase): 0778 def test_create_read(self): 0779 delta = 0 0780 lastrc = sys.gettotalrefcount() 0781 for i in xrange(20): 0782 gc.collect() 0783 self.assertEqual(gc.garbage, []) 0784 rc = sys.gettotalrefcount() 0785 csv.reader(["a,b,c\r\n"]) 0786 csv.reader(["a,b,c\r\n"]) 0787 csv.reader(["a,b,c\r\n"]) 0788 delta = rc-lastrc 0789 lastrc = rc 0790 # if csv.reader() leaks, last delta should be 3 or more 0791 self.assertEqual(delta < 3, True) 0792 0793 def test_create_write(self): 0794 delta = 0 0795 lastrc = sys.gettotalrefcount() 0796 s = NUL() 0797 for i in xrange(20): 0798 gc.collect() 0799 self.assertEqual(gc.garbage, []) 0800 rc = sys.gettotalrefcount() 0801 csv.writer(s) 0802 csv.writer(s) 0803 csv.writer(s) 0804 delta = rc-lastrc 0805 lastrc = rc 0806 # if csv.writer() leaks, last delta should be 3 or more 0807 self.assertEqual(delta < 3, True) 0808 0809 def test_read(self): 0810 delta = 0 0811 rows = ["a,b,c\r\n"]*5 0812 lastrc = sys.gettotalrefcount() 0813 for i in xrange(20): 0814 gc.collect() 0815 self.assertEqual(gc.garbage, []) 0816 rc = sys.gettotalrefcount() 0817 rdr = csv.reader(rows) 0818 for row in rdr: 0819 pass 0820 delta = rc-lastrc 0821 lastrc = rc 0822 # if reader leaks during read, delta should be 5 or more 0823 self.assertEqual(delta < 5, True) 0824 0825 def test_write(self): 0826 delta = 0 0827 rows = [[1,2,3]]*5 0828 s = NUL() 0829 lastrc = sys.gettotalrefcount() 0830 for i in xrange(20): 0831 gc.collect() 0832 self.assertEqual(gc.garbage, []) 0833 rc = sys.gettotalrefcount() 0834 writer = csv.writer(s) 0835 for row in rows: 0836 writer.writerow(row) 0837 delta = rc-lastrc 0838 lastrc = rc 0839 # if writer leaks during write, last delta should be 5 or more 0840 self.assertEqual(delta < 5, True) 0841 0842 # commented out for now - csv module doesn't yet support Unicode 0843 ## class TestUnicode(unittest.TestCase): 0844 ## def test_unicode_read(self): 0845 ## import codecs 0846 ## f = codecs.EncodedFile(StringIO("Martin von Löwis," 0847 ## "Marc André Lemburg," 0848 ## "Guido van Rossum," 0849 ## "François Pinard\r\n"), 0850 ## data_encoding='iso-8859-1') 0851 ## reader = csv.reader(f) 0852 ## self.assertEqual(list(reader), [[u"Martin von Löwis", 0853 ## u"Marc André Lemburg", 0854 ## u"Guido van Rossum", 0855 ## u"François Pinardn"]]) 0856 0857 def test_main(): 0858 mod = sys.modules[__name__] 0859 test_support.run_unittest( 0860 *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')] 0861 ) 0862 0863 if __name__ == '__main__': 0864 test_main() 0865
Generated by PyXR 0.9.4