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