Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions
test.test_csv.Test_Csv Class Reference

List of all members.

Public Member Functions

def test_reader_arg_valid
def test_writer_arg_valid
def test_reader_attrs
def test_writer_attrs
def test_reader_kw_attrs
def test_writer_kw_attrs
def test_reader_dialect_attrs
def test_writer_dialect_attrs
def test_write_arg_valid
def test_write_bigfield
def test_write_quoting
def test_write_escape
def test_writerows
def test_read_oddinputs
def test_read_eol
def test_read_escape
def test_read_quoting
def test_read_bigfield
def test_read_linenum
def test_roundtrip_quoteed_newlines

Private Member Functions

def _test_arg_valid
def _test_default_attrs
def _test_kw_attrs
def _test_dialect_attrs
def _write_test
def _read_test

Detailed Description

Test the underlying C csv parser in ways that are not appropriate
from the high level interface. Further tests of this nature are done
in TestDialectRegistry.

Definition at line 14 of file test_csv.py.


Member Function Documentation

def test.test_csv.Test_Csv._read_test (   self,
  input,
  expect,
  kwargs 
) [private]

Definition at line 200 of file test_csv.py.

00200 
00201     def _read_test(self, input, expect, **kwargs):
00202         reader = csv.reader(input, **kwargs)
00203         result = list(reader)
00204         self.assertEqual(result, expect)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_csv.Test_Csv._test_arg_valid (   self,
  ctor,
  arg 
) [private]

Definition at line 20 of file test_csv.py.

00020 
00021     def _test_arg_valid(self, ctor, arg):
00022         self.assertRaises(TypeError, ctor)
00023         self.assertRaises(TypeError, ctor, None)
00024         self.assertRaises(TypeError, ctor, arg, bad_attr = 0)
00025         self.assertRaises(TypeError, ctor, arg, delimiter = 0)
00026         self.assertRaises(TypeError, ctor, arg, delimiter = 'XX')
00027         self.assertRaises(csv.Error, ctor, arg, 'foo')
00028         self.assertRaises(TypeError, ctor, arg, delimiter=None)
00029         self.assertRaises(TypeError, ctor, arg, delimiter=1)
00030         self.assertRaises(TypeError, ctor, arg, quotechar=1)
00031         self.assertRaises(TypeError, ctor, arg, lineterminator=None)
00032         self.assertRaises(TypeError, ctor, arg, lineterminator=1)
00033         self.assertRaises(TypeError, ctor, arg, quoting=None)
00034         self.assertRaises(TypeError, ctor, arg,
00035                           quoting=csv.QUOTE_ALL, quotechar='')
00036         self.assertRaises(TypeError, ctor, arg,
00037                           quoting=csv.QUOTE_ALL, quotechar=None)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_csv.Test_Csv._test_default_attrs (   self,
  ctor,
  args 
) [private]

Definition at line 44 of file test_csv.py.

00044 
00045     def _test_default_attrs(self, ctor, *args):
00046         obj = ctor(*args)
00047         # Check defaults
00048         self.assertEqual(obj.dialect.delimiter, ',')
00049         self.assertEqual(obj.dialect.doublequote, True)
00050         self.assertEqual(obj.dialect.escapechar, None)
00051         self.assertEqual(obj.dialect.lineterminator, "\r\n")
00052         self.assertEqual(obj.dialect.quotechar, '"')
00053         self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL)
00054         self.assertEqual(obj.dialect.skipinitialspace, False)
00055         self.assertEqual(obj.dialect.strict, False)
00056         # Try deleting or changing attributes (they are read-only)
00057         self.assertRaises(AttributeError, delattr, obj.dialect, 'delimiter')
00058         self.assertRaises(AttributeError, setattr, obj.dialect, 'delimiter', ':')
00059         self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting')
00060         self.assertRaises(AttributeError, setattr, obj.dialect,
00061                           'quoting', None)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_csv.Test_Csv._test_dialect_attrs (   self,
  ctor,
  args 
) [private]

Definition at line 90 of file test_csv.py.

00090 
00091     def _test_dialect_attrs(self, ctor, *args):
00092         # Now try with dialect-derived options
00093         class dialect:
00094             delimiter='-'
00095             doublequote=False
00096             escapechar='^'
00097             lineterminator='$'
00098             quotechar='#'
00099             quoting=csv.QUOTE_ALL
00100             skipinitialspace=True
00101             strict=False
00102         args = args + (dialect,)
00103         obj = ctor(*args)
00104         self.assertEqual(obj.dialect.delimiter, '-')
00105         self.assertEqual(obj.dialect.doublequote, False)
00106         self.assertEqual(obj.dialect.escapechar, '^')
00107         self.assertEqual(obj.dialect.lineterminator, "$")
00108         self.assertEqual(obj.dialect.quotechar, '#')
00109         self.assertEqual(obj.dialect.quoting, csv.QUOTE_ALL)
00110         self.assertEqual(obj.dialect.skipinitialspace, True)
00111         self.assertEqual(obj.dialect.strict, False)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_csv.Test_Csv._test_kw_attrs (   self,
  ctor,
  args 
) [private]

Definition at line 68 of file test_csv.py.

00068 
00069     def _test_kw_attrs(self, ctor, *args):
00070         # Now try with alternate options
00071         kwargs = dict(delimiter=':', doublequote=False, escapechar='\\',
00072                       lineterminator='\r', quotechar='*',
00073                       quoting=csv.QUOTE_NONE, skipinitialspace=True,
00074                       strict=True)
00075         obj = ctor(*args, **kwargs)
00076         self.assertEqual(obj.dialect.delimiter, ':')
00077         self.assertEqual(obj.dialect.doublequote, False)
00078         self.assertEqual(obj.dialect.escapechar, '\\')
00079         self.assertEqual(obj.dialect.lineterminator, "\r")
00080         self.assertEqual(obj.dialect.quotechar, '*')
00081         self.assertEqual(obj.dialect.quoting, csv.QUOTE_NONE)
00082         self.assertEqual(obj.dialect.skipinitialspace, True)
00083         self.assertEqual(obj.dialect.strict, True)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_csv.Test_Csv._write_test (   self,
  fields,
  expect,
  kwargs 
) [private]

Definition at line 119 of file test_csv.py.

00119 
00120     def _write_test(self, fields, expect, **kwargs):
00121         with TemporaryFile("w+", newline='') as fileobj:
00122             writer = csv.writer(fileobj, **kwargs)
00123             writer.writerow(fields)
00124             fileobj.seek(0)
00125             self.assertEqual(fileobj.read(),
00126                              expect + writer.dialect.lineterminator)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 250 of file test_csv.py.

00250 
00251     def test_read_bigfield(self):
00252         # This exercises the buffer realloc functionality and field size
00253         # limits.
00254         limit = csv.field_size_limit()
00255         try:
00256             size = 50000
00257             bigstring = 'X' * size
00258             bigline = '%s,%s' % (bigstring, bigstring)
00259             self._read_test([bigline], [[bigstring, bigstring]])
00260             csv.field_size_limit(size)
00261             self._read_test([bigline], [[bigstring, bigstring]])
00262             self.assertEqual(csv.field_size_limit(), size)
00263             csv.field_size_limit(size-1)
00264             self.assertRaises(csv.Error, self._read_test, [bigline], [])
00265             self.assertRaises(TypeError, csv.field_size_limit, None)
00266             self.assertRaises(TypeError, csv.field_size_limit, 1, None)
00267         finally:
00268             csv.field_size_limit(limit)

Here is the call graph for this function:

Definition at line 219 of file test_csv.py.

00219 
00220     def test_read_eol(self):
00221         self._read_test(['a,b'], [['a','b']])
00222         self._read_test(['a,b\n'], [['a','b']])
00223         self._read_test(['a,b\r\n'], [['a','b']])
00224         self._read_test(['a,b\r'], [['a','b']])
00225         self.assertRaises(csv.Error, self._read_test, ['a,b\rc,d'], [])
00226         self.assertRaises(csv.Error, self._read_test, ['a,b\nc,d'], [])
00227         self.assertRaises(csv.Error, self._read_test, ['a,b\r\nc,d'], [])

Here is the call graph for this function:

Definition at line 228 of file test_csv.py.

00228 
00229     def test_read_escape(self):
00230         self._read_test(['a,\\b,c'], [['a', 'b', 'c']], escapechar='\\')
00231         self._read_test(['a,b\\,c'], [['a', 'b,c']], escapechar='\\')
00232         self._read_test(['a,"b\\,c"'], [['a', 'b,c']], escapechar='\\')
00233         self._read_test(['a,"b,\\c"'], [['a', 'b,c']], escapechar='\\')
00234         self._read_test(['a,"b,c\\""'], [['a', 'b,c"']], escapechar='\\')
00235         self._read_test(['a,"b,c"\\'], [['a', 'b,c\\']], escapechar='\\')

Here is the call graph for this function:

Definition at line 269 of file test_csv.py.

00269 
00270     def test_read_linenum(self):
00271         r = csv.reader(['line,1', 'line,2', 'line,3'])
00272         self.assertEqual(r.line_num, 0)
00273         next(r)
00274         self.assertEqual(r.line_num, 1)
00275         next(r)
00276         self.assertEqual(r.line_num, 2)
00277         next(r)
00278         self.assertEqual(r.line_num, 3)
00279         self.assertRaises(StopIteration, next, r)
00280         self.assertEqual(r.line_num, 3)

Here is the call graph for this function:

Definition at line 205 of file test_csv.py.

00205 
00206     def test_read_oddinputs(self):
00207         self._read_test([], [])
00208         self._read_test([''], [[]])
00209         self.assertRaises(csv.Error, self._read_test,
00210                           ['"ab"c'], None, strict = 1)
00211         # cannot handle null bytes for the moment
00212         self.assertRaises(csv.Error, self._read_test,
00213                           ['ab\0c'], None, strict = 1)
00214         self._read_test(['"ab"c'], [['abc']], doublequote = 0)
00215 
00216         self.assertRaises(csv.Error, self._read_test,
00217                           [b'ab\0c'], None)
00218 

Here is the call graph for this function:

Definition at line 236 of file test_csv.py.

00236 
00237     def test_read_quoting(self):
00238         self._read_test(['1,",3,",5'], [['1', ',3,', '5']])
00239         self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
00240                         quotechar=None, escapechar='\\')
00241         self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
00242                         quoting=csv.QUOTE_NONE, escapechar='\\')
00243         # will this fail where locale uses comma for decimals?
00244         self._read_test([',3,"5",7.3, 9'], [['', 3, '5', 7.3, 9]],
00245                         quoting=csv.QUOTE_NONNUMERIC)
00246         self._read_test(['"a\nb", 7'], [['a\nb', ' 7']])
00247         self.assertRaises(ValueError, self._read_test,
00248                           ['abc,3'], [[]],
00249                           quoting=csv.QUOTE_NONNUMERIC)

Here is the call graph for this function:

Definition at line 38 of file test_csv.py.

00038 
00039     def test_reader_arg_valid(self):
00040         self._test_arg_valid(csv.reader, [])

Here is the call graph for this function:

Definition at line 62 of file test_csv.py.

00062 
00063     def test_reader_attrs(self):
00064         self._test_default_attrs(csv.reader, [])

Here is the call graph for this function:

Definition at line 112 of file test_csv.py.

00112 
00113     def test_reader_dialect_attrs(self):
00114         self._test_dialect_attrs(csv.reader, [])

Here is the call graph for this function:

Definition at line 84 of file test_csv.py.

00084 
00085     def test_reader_kw_attrs(self):
00086         self._test_kw_attrs(csv.reader, [])

Here is the call graph for this function:

Definition at line 281 of file test_csv.py.

00281 
00282     def test_roundtrip_quoteed_newlines(self):
00283         with TemporaryFile("w+", newline='') as fileobj:
00284             writer = csv.writer(fileobj)
00285             self.assertRaises(TypeError, writer.writerows, None)
00286             rows = [['a\nb','b'],['c','x\r\nd']]
00287             writer.writerows(rows)
00288             fileobj.seek(0)
00289             for i, row in enumerate(csv.reader(fileobj)):
00290                 self.assertEqual(row, rows[i])

Here is the call graph for this function:

Definition at line 127 of file test_csv.py.

00127 
00128     def test_write_arg_valid(self):
00129         self.assertRaises(csv.Error, self._write_test, None, '')
00130         self._write_test((), '')
00131         self._write_test([None], '""')
00132         self.assertRaises(csv.Error, self._write_test,
00133                           [None], None, quoting = csv.QUOTE_NONE)
00134         # Check that exceptions are passed up the chain
00135         class BadList:
00136             def __len__(self):
00137                 return 10;
00138             def __getitem__(self, i):
00139                 if i > 2:
00140                     raise IOError
00141         self.assertRaises(IOError, self._write_test, BadList(), '')
00142         class BadItem:
00143             def __str__(self):
00144                 raise IOError
00145         self.assertRaises(IOError, self._write_test, [BadItem()], '')

Here is the call graph for this function:

Definition at line 146 of file test_csv.py.

00146 
00147     def test_write_bigfield(self):
00148         # This exercises the buffer realloc functionality
00149         bigstring = 'X' * 50000
00150         self._write_test([bigstring,bigstring], '%s,%s' % \
00151                          (bigstring, bigstring))

Here is the call graph for this function:

Definition at line 167 of file test_csv.py.

00167 
00168     def test_write_escape(self):
00169         self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
00170                          escapechar='\\')
00171         self.assertRaises(csv.Error,
00172                           self._write_test,
00173                           ['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
00174                           escapechar=None, doublequote=False)
00175         self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
00176                          escapechar='\\', doublequote = False)
00177         self._write_test(['"'], '""""',
00178                          escapechar='\\', quoting = csv.QUOTE_MINIMAL)
00179         self._write_test(['"'], '\\"',
00180                          escapechar='\\', quoting = csv.QUOTE_MINIMAL,
00181                          doublequote = False)
00182         self._write_test(['"'], '\\"',
00183                          escapechar='\\', quoting = csv.QUOTE_NONE)
00184         self._write_test(['a',1,'p,q'], 'a,1,p\\,q',
00185                          escapechar='\\', quoting = csv.QUOTE_NONE)

Here is the call graph for this function:

Definition at line 152 of file test_csv.py.

00152 
00153     def test_write_quoting(self):
00154         self._write_test(['a',1,'p,q'], 'a,1,"p,q"')
00155         self.assertRaises(csv.Error,
00156                           self._write_test,
00157                           ['a',1,'p,q'], 'a,1,p,q',
00158                           quoting = csv.QUOTE_NONE)
00159         self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
00160                          quoting = csv.QUOTE_MINIMAL)
00161         self._write_test(['a',1,'p,q'], '"a",1,"p,q"',
00162                          quoting = csv.QUOTE_NONNUMERIC)
00163         self._write_test(['a',1,'p,q'], '"a","1","p,q"',
00164                          quoting = csv.QUOTE_ALL)
00165         self._write_test(['a\nb',1], '"a\nb","1"',
00166                          quoting = csv.QUOTE_ALL)

Here is the call graph for this function:

Definition at line 41 of file test_csv.py.

00041 
00042     def test_writer_arg_valid(self):
00043         self._test_arg_valid(csv.writer, StringIO())

Here is the call graph for this function:

Definition at line 65 of file test_csv.py.

00065 
00066     def test_writer_attrs(self):
00067         self._test_default_attrs(csv.writer, StringIO())

Here is the call graph for this function:

Definition at line 115 of file test_csv.py.

00115 
00116     def test_writer_dialect_attrs(self):
00117         self._test_dialect_attrs(csv.writer, StringIO())
00118 

Here is the call graph for this function:

Definition at line 87 of file test_csv.py.

00087 
00088     def test_writer_kw_attrs(self):
00089         self._test_kw_attrs(csv.writer, StringIO())

Here is the call graph for this function:

Definition at line 186 of file test_csv.py.

00186 
00187     def test_writerows(self):
00188         class BrokenFile:
00189             def write(self, buf):
00190                 raise IOError
00191         writer = csv.writer(BrokenFile())
00192         self.assertRaises(IOError, writer.writerows, [['a']])
00193 
00194         with TemporaryFile("w+", newline='') as fileobj:
00195             writer = csv.writer(fileobj)
00196             self.assertRaises(TypeError, writer.writerows, None)
00197             writer.writerows([['a','b'],['c','d']])
00198             fileobj.seek(0)
00199             self.assertEqual(fileobj.read(), "a,b\r\nc,d\r\n")

Here is the call graph for this function:


The documentation for this class was generated from the following file: