Issue #27895: Strengthen the dict reader tests.

This commit is contained in:
Raymond Hettinger 2016-08-30 12:57:26 -07:00
parent 43ca452887
commit 8ca35707c0
1 changed files with 38 additions and 0 deletions

View File

@ -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__'}