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 |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 6 | import os |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 7 | import unittest |
| 8 | from StringIO import StringIO |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 9 | import tempfile |
Skip Montanaro | 594adac | 2003-04-10 17:16:15 +0000 | [diff] [blame] | 10 | import csv |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 11 | import gc |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 12 | from test import test_support |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 13 | |
| 14 | class Test_Csv(unittest.TestCase): |
| 15 | """ |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 16 | Test the underlying C csv parser in ways that are not appropriate |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 17 | from the high level interface. Further tests of this nature are done |
| 18 | in TestDialectRegistry. |
| 19 | """ |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 20 | def _test_arg_valid(self, ctor, arg): |
| 21 | self.assertRaises(TypeError, ctor) |
| 22 | self.assertRaises(TypeError, ctor, None) |
| 23 | self.assertRaises(TypeError, ctor, arg, bad_attr = 0) |
| 24 | self.assertRaises(TypeError, ctor, arg, delimiter = 0) |
| 25 | self.assertRaises(TypeError, ctor, arg, delimiter = 'XX') |
| 26 | self.assertRaises(csv.Error, ctor, arg, 'foo') |
| 27 | self.assertRaises(TypeError, ctor, arg, None) |
| 28 | self.assertRaises(TypeError, ctor, arg, delimiter=None) |
| 29 | self.assertRaises(TypeError, ctor, arg, delimiter=1) |
| 30 | self.assertRaises(TypeError, ctor, arg, quotechar=1) |
| 31 | self.assertRaises(TypeError, ctor, arg, lineterminator=None) |
| 32 | self.assertRaises(TypeError, ctor, arg, lineterminator=1) |
| 33 | self.assertRaises(TypeError, ctor, arg, quoting=None) |
| 34 | # We now allow this, only raising an exception if quoting is needed. |
| 35 | # self.assertRaises(TypeError, ctor, arg, quotechar=None) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 36 | # self.assertRaises(TypeError, ctor, arg, |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 37 | # quoting=csv.QUOTE_NONE, escapechar=None) |
| 38 | # No longer complains about dialects with invalid attributes [AM] |
| 39 | # class BadDialect: |
| 40 | # bad_attr = 0 |
| 41 | # self.assertRaises(AttributeError, csv.reader, [], BadDialect) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 42 | class BadClass: |
| 43 | def __init__(self): |
| 44 | raise IOError |
| 45 | self.assertRaises(IOError, csv.reader, [], BadClass) |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 46 | |
| 47 | def test_reader_arg_valid(self): |
| 48 | self._test_arg_valid(csv.reader, []) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 49 | |
| 50 | def test_writer_arg_valid(self): |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 51 | self._test_arg_valid(csv.writer, StringIO()) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 52 | |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 53 | def _test_default_attrs(self, ctor, *args): |
| 54 | obj = ctor(*args) |
| 55 | # Check defaults |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 56 | self.assertEqual(obj.dialect.delimiter, ',') |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 57 | self.assertEqual(obj.dialect.doublequote, True) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 58 | self.assertEqual(obj.dialect.escapechar, None) |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 59 | self.assertEqual(obj.dialect.lineterminator, "\r\n") |
| 60 | self.assertEqual(obj.dialect.quotechar, '"') |
| 61 | self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL) |
| 62 | self.assertEqual(obj.dialect.skipinitialspace, False) |
| 63 | self.assertEqual(obj.dialect.strict, False) |
| 64 | # Try deleting or changing attributes (they are read-only) |
| 65 | self.assertRaises(TypeError, delattr, obj.dialect, 'delimiter') |
| 66 | self.assertRaises(TypeError, setattr, obj.dialect, 'delimiter', ':') |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 67 | self.assertRaises(TypeError, delattr, obj.dialect, 'quoting') |
| 68 | self.assertRaises(TypeError, setattr, obj.dialect, 'quoting', None) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 69 | |
| 70 | def test_reader_attrs(self): |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 71 | self._test_default_attrs(csv.reader, []) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 72 | |
| 73 | def test_writer_attrs(self): |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 74 | self._test_default_attrs(csv.writer, StringIO()) |
| 75 | |
| 76 | def _test_kw_attrs(self, ctor, *args): |
| 77 | # Now try with alternate options |
| 78 | kwargs = dict(delimiter=':', doublequote=False, escapechar='\\', |
| 79 | lineterminator='\r', quotechar='*', |
| 80 | quoting=csv.QUOTE_NONE, skipinitialspace=True, |
| 81 | strict=True) |
| 82 | obj = ctor(*args, **kwargs) |
| 83 | self.assertEqual(obj.dialect.delimiter, ':') |
| 84 | self.assertEqual(obj.dialect.doublequote, False) |
| 85 | self.assertEqual(obj.dialect.escapechar, '\\') |
| 86 | self.assertEqual(obj.dialect.lineterminator, "\r") |
| 87 | self.assertEqual(obj.dialect.quotechar, '*') |
| 88 | self.assertEqual(obj.dialect.quoting, csv.QUOTE_NONE) |
| 89 | self.assertEqual(obj.dialect.skipinitialspace, True) |
| 90 | self.assertEqual(obj.dialect.strict, True) |
| 91 | |
| 92 | def test_reader_kw_attrs(self): |
| 93 | self._test_kw_attrs(csv.reader, []) |
| 94 | |
| 95 | def test_writer_kw_attrs(self): |
| 96 | self._test_kw_attrs(csv.writer, StringIO()) |
| 97 | |
| 98 | def _test_dialect_attrs(self, ctor, *args): |
| 99 | # Now try with dialect-derived options |
| 100 | class dialect: |
| 101 | delimiter='-' |
| 102 | doublequote=False |
| 103 | escapechar='^' |
| 104 | lineterminator='$' |
| 105 | quotechar='#' |
| 106 | quoting=csv.QUOTE_ALL |
| 107 | skipinitialspace=True |
| 108 | strict=False |
| 109 | args = args + (dialect,) |
| 110 | obj = ctor(*args) |
| 111 | self.assertEqual(obj.dialect.delimiter, '-') |
| 112 | self.assertEqual(obj.dialect.doublequote, False) |
| 113 | self.assertEqual(obj.dialect.escapechar, '^') |
| 114 | self.assertEqual(obj.dialect.lineterminator, "$") |
| 115 | self.assertEqual(obj.dialect.quotechar, '#') |
| 116 | self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL) |
| 117 | self.assertEqual(obj.dialect.skipinitialspace, True) |
| 118 | self.assertEqual(obj.dialect.strict, False) |
| 119 | |
| 120 | def test_reader_dialect_attrs(self): |
| 121 | self._test_dialect_attrs(csv.reader, []) |
| 122 | |
| 123 | def test_writer_dialect_attrs(self): |
| 124 | self._test_dialect_attrs(csv.writer, StringIO()) |
| 125 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 126 | |
| 127 | def _write_test(self, fields, expect, **kwargs): |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 128 | fd, name = tempfile.mkstemp() |
| 129 | fileobj = os.fdopen(fd, "w+b") |
| 130 | try: |
| 131 | writer = csv.writer(fileobj, **kwargs) |
| 132 | writer.writerow(fields) |
| 133 | fileobj.seek(0) |
| 134 | self.assertEqual(fileobj.read(), |
| 135 | expect + writer.dialect.lineterminator) |
| 136 | finally: |
| 137 | fileobj.close() |
| 138 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 139 | |
| 140 | def test_write_arg_valid(self): |
| 141 | self.assertRaises(csv.Error, self._write_test, None, '') |
| 142 | self._write_test((), '') |
| 143 | self._write_test([None], '""') |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 144 | self.assertRaises(csv.Error, self._write_test, |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 145 | [None], None, quoting = csv.QUOTE_NONE) |
| 146 | # Check that exceptions are passed up the chain |
| 147 | class BadList: |
| 148 | def __len__(self): |
| 149 | return 10; |
| 150 | def __getitem__(self, i): |
| 151 | if i > 2: |
| 152 | raise IOError |
| 153 | self.assertRaises(IOError, self._write_test, BadList(), '') |
| 154 | class BadItem: |
| 155 | def __str__(self): |
| 156 | raise IOError |
| 157 | self.assertRaises(IOError, self._write_test, [BadItem()], '') |
| 158 | |
| 159 | def test_write_bigfield(self): |
| 160 | # This exercises the buffer realloc functionality |
| 161 | bigstring = 'X' * 50000 |
| 162 | self._write_test([bigstring,bigstring], '%s,%s' % \ |
| 163 | (bigstring, bigstring)) |
| 164 | |
| 165 | def test_write_quoting(self): |
| 166 | self._write_test(['a','1','p,q'], 'a,1,"p,q"') |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 167 | self.assertRaises(csv.Error, |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 168 | self._write_test, |
| 169 | ['a','1','p,q'], 'a,1,"p,q"', |
| 170 | quoting = csv.QUOTE_NONE) |
| 171 | self._write_test(['a','1','p,q'], 'a,1,"p,q"', |
| 172 | quoting = csv.QUOTE_MINIMAL) |
| 173 | self._write_test(['a','1','p,q'], '"a",1,"p,q"', |
| 174 | quoting = csv.QUOTE_NONNUMERIC) |
| 175 | self._write_test(['a','1','p,q'], '"a","1","p,q"', |
| 176 | quoting = csv.QUOTE_ALL) |
| 177 | |
| 178 | def test_write_escape(self): |
| 179 | self._write_test(['a','1','p,q'], 'a,1,"p,q"', |
| 180 | escapechar='\\') |
| 181 | # FAILED - needs to be fixed [am]: |
| 182 | # self._write_test(['a','1','p,"q"'], 'a,1,"p,\\"q\\"', |
| 183 | # escapechar='\\', doublequote = 0) |
| 184 | self._write_test(['a','1','p,q'], 'a,1,p\\,q', |
| 185 | escapechar='\\', quoting = csv.QUOTE_NONE) |
| 186 | |
| 187 | def test_writerows(self): |
| 188 | class BrokenFile: |
| 189 | def write(self, buf): |
| 190 | raise IOError |
| 191 | writer = csv.writer(BrokenFile()) |
| 192 | self.assertRaises(IOError, writer.writerows, [['a']]) |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 193 | fd, name = tempfile.mkstemp() |
| 194 | fileobj = os.fdopen(fd, "w+b") |
| 195 | try: |
| 196 | writer = csv.writer(fileobj) |
| 197 | self.assertRaises(TypeError, writer.writerows, None) |
| 198 | writer.writerows([['a','b'],['c','d']]) |
| 199 | fileobj.seek(0) |
| 200 | self.assertEqual(fileobj.read(), "a,b\r\nc,d\r\n") |
| 201 | finally: |
| 202 | fileobj.close() |
| 203 | os.unlink(name) |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 204 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 205 | def _read_test(self, input, expect, **kwargs): |
| 206 | reader = csv.reader(input, **kwargs) |
| 207 | result = list(reader) |
| 208 | self.assertEqual(result, expect) |
| 209 | |
| 210 | def test_read_oddinputs(self): |
| 211 | self._read_test([], []) |
| 212 | self._read_test([''], [[]]) |
| 213 | self.assertRaises(csv.Error, self._read_test, |
| 214 | ['"ab"c'], None, strict = 1) |
| 215 | # cannot handle null bytes for the moment |
| 216 | self.assertRaises(csv.Error, self._read_test, |
| 217 | ['ab\0c'], None, strict = 1) |
| 218 | self._read_test(['"ab"c'], [['abc']], doublequote = 0) |
| 219 | |
| 220 | def test_read_eol(self): |
| 221 | self._read_test(['a,b'], [['a','b']]) |
| 222 | self._read_test(['a,b\n'], [['a','b']]) |
| 223 | self._read_test(['a,b\r\n'], [['a','b']]) |
| 224 | self._read_test(['a,b\r'], [['a','b']]) |
| 225 | self.assertRaises(csv.Error, self._read_test, ['a,b\rc,d'], []) |
| 226 | self.assertRaises(csv.Error, self._read_test, ['a,b\nc,d'], []) |
| 227 | self.assertRaises(csv.Error, self._read_test, ['a,b\r\nc,d'], []) |
| 228 | |
| 229 | def test_read_escape(self): |
Andrew McNamara | 36a7691 | 2005-01-10 01:04:40 +0000 | [diff] [blame^] | 230 | self._read_test(['a,\\b,c'], [['a', 'b', 'c']], escapechar='\\') |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 231 | self._read_test(['a,b\\,c'], [['a', 'b,c']], escapechar='\\') |
| 232 | self._read_test(['a,"b\\,c"'], [['a', 'b,c']], escapechar='\\') |
Andrew McNamara | 36a7691 | 2005-01-10 01:04:40 +0000 | [diff] [blame^] | 233 | self._read_test(['a,"b,\\c"'], [['a', 'b,c']], escapechar='\\') |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 234 | self._read_test(['a,"b,c\\""'], [['a', 'b,c"']], escapechar='\\') |
| 235 | self._read_test(['a,"b,c"\\'], [['a', 'b,c\\']], escapechar='\\') |
| 236 | |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 237 | def test_read_quoting(self): |
| 238 | self._read_test(['1,",3,",5'], [['1', ',3,', '5']]) |
| 239 | self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']], |
| 240 | quotechar=None, escapechar='\\') |
| 241 | self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']], |
| 242 | quoting=csv.QUOTE_NONE, escapechar='\\') |
| 243 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 244 | def test_read_bigfield(self): |
| 245 | # This exercises the buffer realloc functionality |
| 246 | bigstring = 'X' * 50000 |
| 247 | bigline = '%s,%s' % (bigstring, bigstring) |
| 248 | self._read_test([bigline], [[bigstring, bigstring]]) |
| 249 | |
| 250 | class TestDialectRegistry(unittest.TestCase): |
| 251 | def test_registry_badargs(self): |
| 252 | self.assertRaises(TypeError, csv.list_dialects, None) |
| 253 | self.assertRaises(TypeError, csv.get_dialect) |
| 254 | self.assertRaises(csv.Error, csv.get_dialect, None) |
| 255 | self.assertRaises(csv.Error, csv.get_dialect, "nonesuch") |
| 256 | self.assertRaises(TypeError, csv.unregister_dialect) |
| 257 | self.assertRaises(csv.Error, csv.unregister_dialect, None) |
| 258 | self.assertRaises(csv.Error, csv.unregister_dialect, "nonesuch") |
| 259 | self.assertRaises(TypeError, csv.register_dialect, None) |
| 260 | self.assertRaises(TypeError, csv.register_dialect, None, None) |
| 261 | self.assertRaises(TypeError, csv.register_dialect, "nonesuch", None) |
| 262 | class bogus: |
| 263 | def __init__(self): |
| 264 | raise KeyError |
| 265 | self.assertRaises(KeyError, csv.register_dialect, "nonesuch", bogus) |
| 266 | |
| 267 | def test_registry(self): |
| 268 | class myexceltsv(csv.excel): |
| 269 | delimiter = "\t" |
| 270 | name = "myexceltsv" |
| 271 | expected_dialects = csv.list_dialects() + [name] |
| 272 | expected_dialects.sort() |
| 273 | csv.register_dialect(name, myexceltsv) |
| 274 | try: |
| 275 | self.failUnless(isinstance(csv.get_dialect(name), myexceltsv)) |
| 276 | got_dialects = csv.list_dialects() |
| 277 | got_dialects.sort() |
| 278 | self.assertEqual(expected_dialects, got_dialects) |
| 279 | finally: |
| 280 | csv.unregister_dialect(name) |
| 281 | |
| 282 | def test_incomplete_dialect(self): |
| 283 | class myexceltsv(csv.Dialect): |
| 284 | delimiter = "\t" |
| 285 | self.assertRaises(csv.Error, myexceltsv) |
| 286 | |
| 287 | def test_space_dialect(self): |
| 288 | class space(csv.excel): |
| 289 | delimiter = " " |
| 290 | quoting = csv.QUOTE_NONE |
| 291 | escapechar = "\\" |
| 292 | |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 293 | fd, name = tempfile.mkstemp() |
| 294 | fileobj = os.fdopen(fd, "w+b") |
| 295 | try: |
| 296 | fileobj.write("abc def\nc1ccccc1 benzene\n") |
| 297 | fileobj.seek(0) |
| 298 | rdr = csv.reader(fileobj, dialect=space()) |
| 299 | self.assertEqual(rdr.next(), ["abc", "def"]) |
| 300 | self.assertEqual(rdr.next(), ["c1ccccc1", "benzene"]) |
| 301 | finally: |
| 302 | fileobj.close() |
| 303 | os.unlink(name) |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 304 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 305 | def test_dialect_apply(self): |
| 306 | class testA(csv.excel): |
| 307 | delimiter = "\t" |
| 308 | class testB(csv.excel): |
| 309 | delimiter = ":" |
| 310 | class testC(csv.excel): |
| 311 | delimiter = "|" |
| 312 | |
| 313 | csv.register_dialect('testC', testC) |
| 314 | try: |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 315 | fd, name = tempfile.mkstemp() |
| 316 | fileobj = os.fdopen(fd, "w+b") |
| 317 | try: |
| 318 | writer = csv.writer(fileobj) |
| 319 | writer.writerow([1,2,3]) |
| 320 | fileobj.seek(0) |
| 321 | self.assertEqual(fileobj.read(), "1,2,3\r\n") |
| 322 | finally: |
| 323 | fileobj.close() |
| 324 | os.unlink(name) |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 325 | |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 326 | fd, name = tempfile.mkstemp() |
| 327 | fileobj = os.fdopen(fd, "w+b") |
| 328 | try: |
| 329 | writer = csv.writer(fileobj, testA) |
| 330 | writer.writerow([1,2,3]) |
| 331 | fileobj.seek(0) |
| 332 | self.assertEqual(fileobj.read(), "1\t2\t3\r\n") |
| 333 | finally: |
| 334 | fileobj.close() |
| 335 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 336 | |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 337 | fd, name = tempfile.mkstemp() |
| 338 | fileobj = os.fdopen(fd, "w+b") |
| 339 | try: |
| 340 | writer = csv.writer(fileobj, dialect=testB()) |
| 341 | writer.writerow([1,2,3]) |
| 342 | fileobj.seek(0) |
| 343 | self.assertEqual(fileobj.read(), "1:2:3\r\n") |
| 344 | finally: |
| 345 | fileobj.close() |
| 346 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 347 | |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 348 | fd, name = tempfile.mkstemp() |
| 349 | fileobj = os.fdopen(fd, "w+b") |
| 350 | try: |
| 351 | writer = csv.writer(fileobj, dialect='testC') |
| 352 | writer.writerow([1,2,3]) |
| 353 | fileobj.seek(0) |
| 354 | self.assertEqual(fileobj.read(), "1|2|3\r\n") |
| 355 | finally: |
| 356 | fileobj.close() |
| 357 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 358 | |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 359 | fd, name = tempfile.mkstemp() |
| 360 | fileobj = os.fdopen(fd, "w+b") |
| 361 | try: |
| 362 | writer = csv.writer(fileobj, dialect=testA, delimiter=';') |
| 363 | writer.writerow([1,2,3]) |
| 364 | fileobj.seek(0) |
| 365 | self.assertEqual(fileobj.read(), "1;2;3\r\n") |
| 366 | finally: |
| 367 | fileobj.close() |
| 368 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 369 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 370 | finally: |
| 371 | csv.unregister_dialect('testC') |
| 372 | |
| 373 | def test_bad_dialect(self): |
| 374 | # Unknown parameter |
Andrew McNamara | 1196cf1 | 2005-01-07 04:42:45 +0000 | [diff] [blame] | 375 | self.assertRaises(TypeError, csv.reader, [], bad_attr = 0) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 376 | # Bad values |
| 377 | self.assertRaises(TypeError, csv.reader, [], delimiter = None) |
| 378 | self.assertRaises(TypeError, csv.reader, [], quoting = -1) |
| 379 | self.assertRaises(TypeError, csv.reader, [], quoting = 100) |
| 380 | |
| 381 | class TestCsvBase(unittest.TestCase): |
| 382 | def readerAssertEqual(self, input, expected_result): |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 383 | fd, name = tempfile.mkstemp() |
| 384 | fileobj = os.fdopen(fd, "w+b") |
| 385 | try: |
| 386 | fileobj.write(input) |
| 387 | fileobj.seek(0) |
| 388 | reader = csv.reader(fileobj, dialect = self.dialect) |
| 389 | fields = list(reader) |
| 390 | self.assertEqual(fields, expected_result) |
| 391 | finally: |
| 392 | fileobj.close() |
| 393 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 394 | |
| 395 | def writerAssertEqual(self, input, expected_result): |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 396 | fd, name = tempfile.mkstemp() |
| 397 | fileobj = os.fdopen(fd, "w+b") |
| 398 | try: |
| 399 | writer = csv.writer(fileobj, dialect = self.dialect) |
| 400 | writer.writerows(input) |
| 401 | fileobj.seek(0) |
| 402 | self.assertEqual(fileobj.read(), expected_result) |
| 403 | finally: |
| 404 | fileobj.close() |
| 405 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 406 | |
| 407 | class TestDialectExcel(TestCsvBase): |
| 408 | dialect = 'excel' |
| 409 | |
| 410 | def test_single(self): |
| 411 | self.readerAssertEqual('abc', [['abc']]) |
| 412 | |
| 413 | def test_simple(self): |
| 414 | self.readerAssertEqual('1,2,3,4,5', [['1','2','3','4','5']]) |
| 415 | |
| 416 | def test_blankline(self): |
| 417 | self.readerAssertEqual('', []) |
| 418 | |
| 419 | def test_empty_fields(self): |
| 420 | self.readerAssertEqual(',', [['', '']]) |
| 421 | |
| 422 | def test_singlequoted(self): |
| 423 | self.readerAssertEqual('""', [['']]) |
| 424 | |
| 425 | def test_singlequoted_left_empty(self): |
| 426 | self.readerAssertEqual('"",', [['','']]) |
| 427 | |
| 428 | def test_singlequoted_right_empty(self): |
| 429 | self.readerAssertEqual(',""', [['','']]) |
| 430 | |
| 431 | def test_single_quoted_quote(self): |
| 432 | self.readerAssertEqual('""""', [['"']]) |
| 433 | |
| 434 | def test_quoted_quotes(self): |
| 435 | self.readerAssertEqual('""""""', [['""']]) |
| 436 | |
| 437 | def test_inline_quote(self): |
| 438 | self.readerAssertEqual('a""b', [['a""b']]) |
| 439 | |
| 440 | def test_inline_quotes(self): |
| 441 | self.readerAssertEqual('a"b"c', [['a"b"c']]) |
| 442 | |
| 443 | def test_quotes_and_more(self): |
| 444 | self.readerAssertEqual('"a"b', [['ab']]) |
| 445 | |
| 446 | def test_lone_quote(self): |
| 447 | self.readerAssertEqual('a"b', [['a"b']]) |
| 448 | |
| 449 | def test_quote_and_quote(self): |
| 450 | self.readerAssertEqual('"a" "b"', [['a "b"']]) |
| 451 | |
| 452 | def test_space_and_quote(self): |
| 453 | self.readerAssertEqual(' "a"', [[' "a"']]) |
| 454 | |
| 455 | def test_quoted(self): |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 456 | self.readerAssertEqual('1,2,3,"I think, therefore I am",5,6', |
| 457 | [['1', '2', '3', |
| 458 | 'I think, therefore I am', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 459 | '5', '6']]) |
| 460 | |
| 461 | def test_quoted_quote(self): |
| 462 | 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] | 463 | [['1', '2', '3', |
| 464 | '"I see," said the blind man', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 465 | 'as he picked up his hammer and saw']]) |
| 466 | |
| 467 | def test_quoted_nl(self): |
| 468 | input = '''\ |
| 469 | 1,2,3,"""I see,"" |
| 470 | said the blind man","as he picked up his |
| 471 | hammer and saw" |
| 472 | 9,8,7,6''' |
| 473 | self.readerAssertEqual(input, |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 474 | [['1', '2', '3', |
| 475 | '"I see,"\nsaid the blind man', |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 476 | 'as he picked up his\nhammer and saw'], |
| 477 | ['9','8','7','6']]) |
| 478 | |
| 479 | def test_dubious_quote(self): |
| 480 | self.readerAssertEqual('12,12,1",', [['12', '12', '1"', '']]) |
| 481 | |
| 482 | def test_null(self): |
| 483 | self.writerAssertEqual([], '') |
| 484 | |
| 485 | def test_single(self): |
| 486 | self.writerAssertEqual([['abc']], 'abc\r\n') |
| 487 | |
| 488 | def test_simple(self): |
| 489 | self.writerAssertEqual([[1, 2, 'abc', 3, 4]], '1,2,abc,3,4\r\n') |
| 490 | |
| 491 | def test_quotes(self): |
| 492 | self.writerAssertEqual([[1, 2, 'a"bc"', 3, 4]], '1,2,"a""bc""",3,4\r\n') |
| 493 | |
| 494 | def test_quote_fieldsep(self): |
| 495 | self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') |
| 496 | |
| 497 | def test_newlines(self): |
| 498 | self.writerAssertEqual([[1, 2, 'a\nbc', 3, 4]], '1,2,"a\nbc",3,4\r\n') |
| 499 | |
| 500 | class EscapedExcel(csv.excel): |
| 501 | quoting = csv.QUOTE_NONE |
| 502 | escapechar = '\\' |
| 503 | |
| 504 | class TestEscapedExcel(TestCsvBase): |
| 505 | dialect = EscapedExcel() |
| 506 | |
| 507 | def test_escape_fieldsep(self): |
| 508 | self.writerAssertEqual([['abc,def']], 'abc\\,def\r\n') |
| 509 | |
| 510 | def test_read_escape_fieldsep(self): |
| 511 | self.readerAssertEqual('abc\\,def\r\n', [['abc,def']]) |
| 512 | |
| 513 | class QuotedEscapedExcel(csv.excel): |
| 514 | quoting = csv.QUOTE_NONNUMERIC |
| 515 | escapechar = '\\' |
| 516 | |
| 517 | class TestQuotedEscapedExcel(TestCsvBase): |
| 518 | dialect = QuotedEscapedExcel() |
| 519 | |
| 520 | def test_write_escape_fieldsep(self): |
| 521 | self.writerAssertEqual([['abc,def']], '"abc,def"\r\n') |
| 522 | |
| 523 | def test_read_escape_fieldsep(self): |
| 524 | self.readerAssertEqual('"abc\\,def"\r\n', [['abc,def']]) |
| 525 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 526 | class TestDictFields(unittest.TestCase): |
| 527 | ### "long" means the row is longer than the number of fieldnames |
| 528 | ### "short" means there are fewer elements in the row than fieldnames |
| 529 | def test_write_simple_dict(self): |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 530 | fd, name = tempfile.mkstemp() |
| 531 | fileobj = os.fdopen(fd, "w+b") |
| 532 | try: |
| 533 | writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) |
| 534 | writer.writerow({"f1": 10, "f3": "abc"}) |
| 535 | fileobj.seek(0) |
| 536 | self.assertEqual(fileobj.read(), "10,,abc\r\n") |
| 537 | finally: |
| 538 | fileobj.close() |
| 539 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 540 | |
| 541 | def test_write_no_fields(self): |
| 542 | fileobj = StringIO() |
| 543 | self.assertRaises(TypeError, csv.DictWriter, fileobj) |
| 544 | |
| 545 | def test_read_dict_fields(self): |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 546 | fd, name = tempfile.mkstemp() |
| 547 | fileobj = os.fdopen(fd, "w+b") |
| 548 | try: |
| 549 | fileobj.write("1,2,abc\r\n") |
| 550 | fileobj.seek(0) |
| 551 | reader = csv.DictReader(fileobj, |
| 552 | fieldnames=["f1", "f2", "f3"]) |
| 553 | self.assertEqual(reader.next(), {"f1": '1', "f2": '2', "f3": 'abc'}) |
| 554 | finally: |
| 555 | fileobj.close() |
| 556 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 557 | |
Skip Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 558 | def test_read_dict_no_fieldnames(self): |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 559 | fd, name = tempfile.mkstemp() |
| 560 | fileobj = os.fdopen(fd, "w+b") |
| 561 | try: |
| 562 | fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") |
| 563 | fileobj.seek(0) |
| 564 | reader = csv.DictReader(fileobj) |
| 565 | self.assertEqual(reader.next(), {"f1": '1', "f2": '2', "f3": 'abc'}) |
| 566 | finally: |
| 567 | fileobj.close() |
| 568 | os.unlink(name) |
Skip Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 569 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 570 | def test_read_long(self): |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 571 | fd, name = tempfile.mkstemp() |
| 572 | fileobj = os.fdopen(fd, "w+b") |
| 573 | try: |
| 574 | fileobj.write("1,2,abc,4,5,6\r\n") |
| 575 | fileobj.seek(0) |
| 576 | reader = csv.DictReader(fileobj, |
| 577 | fieldnames=["f1", "f2"]) |
| 578 | self.assertEqual(reader.next(), {"f1": '1', "f2": '2', |
| 579 | None: ["abc", "4", "5", "6"]}) |
| 580 | finally: |
| 581 | fileobj.close() |
| 582 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 583 | |
| 584 | def test_read_long_with_rest(self): |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 585 | fd, name = tempfile.mkstemp() |
| 586 | fileobj = os.fdopen(fd, "w+b") |
| 587 | try: |
| 588 | fileobj.write("1,2,abc,4,5,6\r\n") |
| 589 | fileobj.seek(0) |
| 590 | reader = csv.DictReader(fileobj, |
| 591 | fieldnames=["f1", "f2"], restkey="_rest") |
| 592 | self.assertEqual(reader.next(), {"f1": '1', "f2": '2', |
| 593 | "_rest": ["abc", "4", "5", "6"]}) |
| 594 | finally: |
| 595 | fileobj.close() |
| 596 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 597 | |
Skip Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 598 | def test_read_long_with_rest_no_fieldnames(self): |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 599 | fd, name = tempfile.mkstemp() |
| 600 | fileobj = os.fdopen(fd, "w+b") |
| 601 | try: |
| 602 | fileobj.write("f1,f2\r\n1,2,abc,4,5,6\r\n") |
| 603 | fileobj.seek(0) |
| 604 | reader = csv.DictReader(fileobj, restkey="_rest") |
| 605 | self.assertEqual(reader.next(), {"f1": '1', "f2": '2', |
| 606 | "_rest": ["abc", "4", "5", "6"]}) |
| 607 | finally: |
| 608 | fileobj.close() |
| 609 | os.unlink(name) |
Skip Montanaro | dffeed3 | 2003-10-03 14:03:01 +0000 | [diff] [blame] | 610 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 611 | def test_read_short(self): |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 612 | fd, name = tempfile.mkstemp() |
| 613 | fileobj = os.fdopen(fd, "w+b") |
| 614 | try: |
| 615 | fileobj.write("1,2,abc,4,5,6\r\n1,2,abc\r\n") |
| 616 | fileobj.seek(0) |
| 617 | reader = csv.DictReader(fileobj, |
| 618 | fieldnames="1 2 3 4 5 6".split(), |
| 619 | restval="DEFAULT") |
| 620 | self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', |
| 621 | "4": '4', "5": '5', "6": '6'}) |
| 622 | self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', |
| 623 | "4": 'DEFAULT', "5": 'DEFAULT', |
| 624 | "6": 'DEFAULT'}) |
| 625 | finally: |
| 626 | fileobj.close() |
| 627 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 628 | |
Skip Montanaro | 1546bc4 | 2003-06-12 02:40:22 +0000 | [diff] [blame] | 629 | def test_read_multi(self): |
| 630 | sample = [ |
| 631 | '2147483648,43.0e12,17,abc,def\r\n', |
| 632 | '147483648,43.0e2,17,abc,def\r\n', |
| 633 | '47483648,43.0,170,abc,def\r\n' |
| 634 | ] |
| 635 | |
| 636 | reader = csv.DictReader(sample, |
| 637 | fieldnames="i1 float i2 s1 s2".split()) |
| 638 | self.assertEqual(reader.next(), {"i1": '2147483648', |
| 639 | "float": '43.0e12', |
| 640 | "i2": '17', |
| 641 | "s1": 'abc', |
| 642 | "s2": 'def'}) |
| 643 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 644 | def test_read_with_blanks(self): |
| 645 | reader = csv.DictReader(["1,2,abc,4,5,6\r\n","\r\n", |
| 646 | "1,2,abc,4,5,6\r\n"], |
| 647 | fieldnames="1 2 3 4 5 6".split()) |
| 648 | self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', |
| 649 | "4": '4', "5": '5', "6": '6'}) |
| 650 | self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', |
| 651 | "4": '4', "5": '5', "6": '6'}) |
| 652 | |
Skip Montanaro | 3f7a948 | 2003-09-06 19:52:12 +0000 | [diff] [blame] | 653 | def test_read_semi_sep(self): |
| 654 | reader = csv.DictReader(["1;2;abc;4;5;6\r\n"], |
| 655 | fieldnames="1 2 3 4 5 6".split(), |
| 656 | delimiter=';') |
| 657 | self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', |
| 658 | "4": '4', "5": '5', "6": '6'}) |
| 659 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 660 | class TestArrayWrites(unittest.TestCase): |
| 661 | def test_int_write(self): |
| 662 | import array |
| 663 | contents = [(20-i) for i in range(20)] |
| 664 | a = array.array('i', contents) |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 665 | |
| 666 | fd, name = tempfile.mkstemp() |
| 667 | fileobj = os.fdopen(fd, "w+b") |
| 668 | try: |
| 669 | writer = csv.writer(fileobj, dialect="excel") |
| 670 | writer.writerow(a) |
| 671 | expected = ",".join([str(i) for i in a])+"\r\n" |
| 672 | fileobj.seek(0) |
| 673 | self.assertEqual(fileobj.read(), expected) |
| 674 | finally: |
| 675 | fileobj.close() |
| 676 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 677 | |
| 678 | def test_double_write(self): |
| 679 | import array |
| 680 | contents = [(20-i)*0.1 for i in range(20)] |
| 681 | a = array.array('d', contents) |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 682 | fd, name = tempfile.mkstemp() |
| 683 | fileobj = os.fdopen(fd, "w+b") |
| 684 | try: |
| 685 | writer = csv.writer(fileobj, dialect="excel") |
| 686 | writer.writerow(a) |
| 687 | expected = ",".join([str(i) for i in a])+"\r\n" |
| 688 | fileobj.seek(0) |
| 689 | self.assertEqual(fileobj.read(), expected) |
| 690 | finally: |
| 691 | fileobj.close() |
| 692 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 693 | |
| 694 | def test_float_write(self): |
| 695 | import array |
| 696 | contents = [(20-i)*0.1 for i in range(20)] |
| 697 | a = array.array('f', contents) |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 698 | fd, name = tempfile.mkstemp() |
| 699 | fileobj = os.fdopen(fd, "w+b") |
| 700 | try: |
| 701 | writer = csv.writer(fileobj, dialect="excel") |
| 702 | writer.writerow(a) |
| 703 | expected = ",".join([str(i) for i in a])+"\r\n" |
| 704 | fileobj.seek(0) |
| 705 | self.assertEqual(fileobj.read(), expected) |
| 706 | finally: |
| 707 | fileobj.close() |
| 708 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 709 | |
| 710 | def test_char_write(self): |
| 711 | import array, string |
| 712 | a = array.array('c', string.letters) |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 713 | fd, name = tempfile.mkstemp() |
| 714 | fileobj = os.fdopen(fd, "w+b") |
| 715 | try: |
| 716 | writer = csv.writer(fileobj, dialect="excel") |
| 717 | writer.writerow(a) |
| 718 | expected = ",".join(a)+"\r\n" |
| 719 | fileobj.seek(0) |
| 720 | self.assertEqual(fileobj.read(), expected) |
| 721 | finally: |
| 722 | fileobj.close() |
| 723 | os.unlink(name) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 724 | |
| 725 | class TestDialectValidity(unittest.TestCase): |
| 726 | def test_quoting(self): |
| 727 | class mydialect(csv.Dialect): |
| 728 | delimiter = ";" |
| 729 | escapechar = '\\' |
| 730 | doublequote = False |
| 731 | skipinitialspace = True |
| 732 | lineterminator = '\r\n' |
| 733 | quoting = csv.QUOTE_NONE |
| 734 | d = mydialect() |
| 735 | |
| 736 | mydialect.quoting = None |
| 737 | self.assertRaises(csv.Error, mydialect) |
| 738 | |
| 739 | mydialect.quoting = csv.QUOTE_NONE |
| 740 | mydialect.escapechar = None |
| 741 | self.assertRaises(csv.Error, mydialect) |
| 742 | |
| 743 | mydialect.doublequote = True |
| 744 | mydialect.quoting = csv.QUOTE_ALL |
| 745 | mydialect.quotechar = '"' |
| 746 | d = mydialect() |
| 747 | |
| 748 | mydialect.quotechar = "''" |
| 749 | self.assertRaises(csv.Error, mydialect) |
| 750 | |
| 751 | mydialect.quotechar = 4 |
| 752 | self.assertRaises(csv.Error, mydialect) |
| 753 | |
| 754 | def test_delimiter(self): |
| 755 | class mydialect(csv.Dialect): |
| 756 | delimiter = ";" |
| 757 | escapechar = '\\' |
| 758 | doublequote = False |
| 759 | skipinitialspace = True |
| 760 | lineterminator = '\r\n' |
| 761 | quoting = csv.QUOTE_NONE |
| 762 | d = mydialect() |
| 763 | |
| 764 | mydialect.delimiter = ":::" |
| 765 | self.assertRaises(csv.Error, mydialect) |
| 766 | |
| 767 | mydialect.delimiter = 4 |
| 768 | self.assertRaises(csv.Error, mydialect) |
| 769 | |
| 770 | def test_lineterminator(self): |
| 771 | class mydialect(csv.Dialect): |
| 772 | delimiter = ";" |
| 773 | escapechar = '\\' |
| 774 | doublequote = False |
| 775 | skipinitialspace = True |
| 776 | lineterminator = '\r\n' |
| 777 | quoting = csv.QUOTE_NONE |
| 778 | d = mydialect() |
| 779 | |
| 780 | mydialect.lineterminator = ":::" |
| 781 | d = mydialect() |
| 782 | |
| 783 | mydialect.lineterminator = 4 |
| 784 | self.assertRaises(csv.Error, mydialect) |
| 785 | |
| 786 | |
Skip Montanaro | 48816c6 | 2003-04-25 14:43:14 +0000 | [diff] [blame] | 787 | class TestSniffer(unittest.TestCase): |
| 788 | sample1 = """\ |
| 789 | Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes |
| 790 | Shark City, Glendale Heights, IL, 12/28/02, Prezence |
| 791 | Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow |
| 792 | Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back |
| 793 | """ |
| 794 | sample2 = """\ |
| 795 | 'Harry''s':'Arlington Heights':'IL':'2/1/03':'Kimi Hayes' |
| 796 | 'Shark City':'Glendale Heights':'IL':'12/28/02':'Prezence' |
| 797 | 'Tommy''s Place':'Blue Island':'IL':'12/28/02':'Blue Sunday/White Crow' |
| 798 | 'Stonecutters Seafood and Chop House':'Lemont':'IL':'12/19/02':'Week Back' |
| 799 | """ |
| 800 | |
| 801 | header = '''\ |
| 802 | "venue","city","state","date","performers" |
| 803 | ''' |
Skip Montanaro | 7789237 | 2003-05-19 15:33:36 +0000 | [diff] [blame] | 804 | sample3 = '''\ |
| 805 | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
| 806 | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
| 807 | 05/05/03?05/05/03?05/05/03?05/05/03?05/05/03?05/05/03 |
| 808 | ''' |
| 809 | |
Skip Montanaro | 1546bc4 | 2003-06-12 02:40:22 +0000 | [diff] [blame] | 810 | sample4 = '''\ |
| 811 | 2147483648;43.0e12;17;abc;def |
| 812 | 147483648;43.0e2;17;abc;def |
| 813 | 47483648;43.0;170;abc;def |
| 814 | ''' |
| 815 | |
Skip Montanaro | 48816c6 | 2003-04-25 14:43:14 +0000 | [diff] [blame] | 816 | def test_has_header(self): |
| 817 | sniffer = csv.Sniffer() |
| 818 | self.assertEqual(sniffer.has_header(self.sample1), False) |
| 819 | self.assertEqual(sniffer.has_header(self.header+self.sample1), True) |
| 820 | |
| 821 | def test_sniff(self): |
| 822 | sniffer = csv.Sniffer() |
| 823 | dialect = sniffer.sniff(self.sample1) |
| 824 | self.assertEqual(dialect.delimiter, ",") |
| 825 | self.assertEqual(dialect.quotechar, '"') |
| 826 | self.assertEqual(dialect.skipinitialspace, True) |
| 827 | |
| 828 | dialect = sniffer.sniff(self.sample2) |
| 829 | self.assertEqual(dialect.delimiter, ":") |
| 830 | self.assertEqual(dialect.quotechar, "'") |
| 831 | self.assertEqual(dialect.skipinitialspace, False) |
| 832 | |
Skip Montanaro | 7789237 | 2003-05-19 15:33:36 +0000 | [diff] [blame] | 833 | def test_delimiters(self): |
| 834 | sniffer = csv.Sniffer() |
| 835 | dialect = sniffer.sniff(self.sample3) |
| 836 | self.assertEqual(dialect.delimiter, "0") |
| 837 | dialect = sniffer.sniff(self.sample3, delimiters="?,") |
| 838 | self.assertEqual(dialect.delimiter, "?") |
| 839 | dialect = sniffer.sniff(self.sample3, delimiters="/,") |
| 840 | self.assertEqual(dialect.delimiter, "/") |
Skip Montanaro | 1546bc4 | 2003-06-12 02:40:22 +0000 | [diff] [blame] | 841 | dialect = sniffer.sniff(self.sample4) |
| 842 | self.assertEqual(dialect.delimiter, ";") |
Skip Montanaro | 7789237 | 2003-05-19 15:33:36 +0000 | [diff] [blame] | 843 | |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 844 | if not hasattr(sys, "gettotalrefcount"): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 845 | if test_support.verbose: print "*** skipping leakage tests ***" |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 846 | else: |
| 847 | class NUL: |
| 848 | def write(s, *args): |
| 849 | pass |
| 850 | writelines = write |
| 851 | |
| 852 | class TestLeaks(unittest.TestCase): |
| 853 | def test_create_read(self): |
| 854 | delta = 0 |
| 855 | lastrc = sys.gettotalrefcount() |
| 856 | for i in xrange(20): |
| 857 | gc.collect() |
| 858 | self.assertEqual(gc.garbage, []) |
| 859 | rc = sys.gettotalrefcount() |
| 860 | csv.reader(["a,b,c\r\n"]) |
| 861 | csv.reader(["a,b,c\r\n"]) |
| 862 | csv.reader(["a,b,c\r\n"]) |
| 863 | delta = rc-lastrc |
| 864 | lastrc = rc |
| 865 | # if csv.reader() leaks, last delta should be 3 or more |
| 866 | self.assertEqual(delta < 3, True) |
| 867 | |
| 868 | def test_create_write(self): |
| 869 | delta = 0 |
| 870 | lastrc = sys.gettotalrefcount() |
| 871 | s = NUL() |
| 872 | for i in xrange(20): |
| 873 | gc.collect() |
| 874 | self.assertEqual(gc.garbage, []) |
| 875 | rc = sys.gettotalrefcount() |
| 876 | csv.writer(s) |
| 877 | csv.writer(s) |
| 878 | csv.writer(s) |
| 879 | delta = rc-lastrc |
| 880 | lastrc = rc |
| 881 | # if csv.writer() leaks, last delta should be 3 or more |
| 882 | self.assertEqual(delta < 3, True) |
| 883 | |
| 884 | def test_read(self): |
| 885 | delta = 0 |
| 886 | rows = ["a,b,c\r\n"]*5 |
| 887 | lastrc = sys.gettotalrefcount() |
| 888 | for i in xrange(20): |
| 889 | gc.collect() |
| 890 | self.assertEqual(gc.garbage, []) |
| 891 | rc = sys.gettotalrefcount() |
| 892 | rdr = csv.reader(rows) |
| 893 | for row in rdr: |
| 894 | pass |
| 895 | delta = rc-lastrc |
| 896 | lastrc = rc |
| 897 | # if reader leaks during read, delta should be 5 or more |
| 898 | self.assertEqual(delta < 5, True) |
| 899 | |
| 900 | def test_write(self): |
| 901 | delta = 0 |
| 902 | rows = [[1,2,3]]*5 |
| 903 | s = NUL() |
| 904 | lastrc = sys.gettotalrefcount() |
| 905 | for i in xrange(20): |
| 906 | gc.collect() |
| 907 | self.assertEqual(gc.garbage, []) |
| 908 | rc = sys.gettotalrefcount() |
| 909 | writer = csv.writer(s) |
| 910 | for row in rows: |
| 911 | writer.writerow(row) |
| 912 | delta = rc-lastrc |
| 913 | lastrc = rc |
| 914 | # if writer leaks during write, last delta should be 5 or more |
| 915 | self.assertEqual(delta < 5, True) |
| 916 | |
Skip Montanaro | 1a56665 | 2003-05-06 15:56:05 +0000 | [diff] [blame] | 917 | # commented out for now - csv module doesn't yet support Unicode |
Skip Montanaro | 58fc5d08 | 2004-06-05 17:03:20 +0000 | [diff] [blame] | 918 | ## class TestUnicode(unittest.TestCase): |
| 919 | ## def test_unicode_read(self): |
| 920 | ## import codecs |
| 921 | ## f = codecs.EncodedFile(StringIO("Martin von Löwis," |
| 922 | ## "Marc André Lemburg," |
| 923 | ## "Guido van Rossum," |
| 924 | ## "François Pinard\r\n"), |
| 925 | ## data_encoding='iso-8859-1') |
| 926 | ## reader = csv.reader(f) |
| 927 | ## self.assertEqual(list(reader), [[u"Martin von Löwis", |
| 928 | ## u"Marc André Lemburg", |
| 929 | ## u"Guido van Rossum", |
| 930 | ## u"François Pinardn"]]) |
Skip Montanaro | 1a56665 | 2003-05-06 15:56:05 +0000 | [diff] [blame] | 931 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 932 | def test_main(): |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 933 | mod = sys.modules[__name__] |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 934 | test_support.run_unittest( |
| 935 | *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')] |
| 936 | ) |
Skip Montanaro | b4a0417 | 2003-03-20 23:29:12 +0000 | [diff] [blame] | 937 | |
| 938 | if __name__ == '__main__': |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 939 | test_main() |