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