diff options
author | Raymond Hettinger <python@rcn.com> | 2016-08-30 19:57:26 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2016-08-30 19:57:26 (GMT) |
commit | 8ca35707c0e866bca3c917f9abe90b144357850b (patch) | |
tree | 0118ce44ff59c81e0d12f414a09b18163ac95aa8 | |
parent | 43ca4528873b8d0bc8c9e63b3871f4fe4286259c (diff) | |
download | cpython-8ca35707c0e866bca3c917f9abe90b144357850b.zip cpython-8ca35707c0e866bca3c917f9abe90b144357850b.tar.gz cpython-8ca35707c0e866bca3c917f9abe90b144357850b.tar.bz2 |
Issue #27895: Strengthen the dict reader tests.
-rw-r--r-- | Lib/test/test_csv.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index 9df4080..7dcea9c 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -11,6 +11,8 @@ import gc import pickle from test import support from itertools import permutations +from textwrap import dedent +from collections import OrderedDict class Test_Csv(unittest.TestCase): """ @@ -1109,6 +1111,42 @@ class KeyOrderingTest(unittest.TestCase): # Final sanity check: were all permutations unique? self.assertEqual(len(resultset), 120, "Key ordering: some key permutations not collected (expected 120)") + def test_ordered_dict_reader(self): + data = dedent('''\ + FirstName,LastName + Eric,Idle + Graham,Chapman,Over1,Over2 + + Under1 + John,Cleese + ''').splitlines() + + self.assertEqual(list(csv.DictReader(data)), + [OrderedDict([('FirstName', 'Eric'), ('LastName', 'Idle')]), + OrderedDict([('FirstName', 'Graham'), ('LastName', 'Chapman'), + (None, ['Over1', 'Over2'])]), + OrderedDict([('FirstName', 'Under1'), ('LastName', None)]), + OrderedDict([('FirstName', 'John'), ('LastName', 'Cleese')]), + ]) + + self.assertEqual(list(csv.DictReader(data, restkey='OtherInfo')), + [OrderedDict([('FirstName', 'Eric'), ('LastName', 'Idle')]), + OrderedDict([('FirstName', 'Graham'), ('LastName', 'Chapman'), + ('OtherInfo', ['Over1', 'Over2'])]), + OrderedDict([('FirstName', 'Under1'), ('LastName', None)]), + OrderedDict([('FirstName', 'John'), ('LastName', 'Cleese')]), + ]) + + del data[0] # Remove the header row + self.assertEqual(list(csv.DictReader(data, fieldnames=['fname', 'lname'])), + [OrderedDict([('fname', 'Eric'), ('lname', 'Idle')]), + OrderedDict([('fname', 'Graham'), ('lname', 'Chapman'), + (None, ['Over1', 'Over2'])]), + OrderedDict([('fname', 'Under1'), ('lname', None)]), + OrderedDict([('fname', 'John'), ('lname', 'Cleese')]), + ]) + + class MiscTestCase(unittest.TestCase): def test__all__(self): extra = {'__doc__', '__version__'} |