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