Ignore:
Timestamp:
Jan 6, 2017, 7:25:02 AM (3 years ago)
Author:
cito
Message:

Improve creation of named tuples in Python 2.6 and 3.0

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/test_dbapi20.py

    r894 r901  
    260260        else:
    261261            self.assertEqual(res._fields, ('one', '_1', 'three'))
     262
     263    def test_cursor_with_badly_named_columns(self):
     264        con = self._connect()
     265        cur = con.cursor()
    262266        cur.execute("select 1 as abc, 2 as def")
    263267        res = cur.fetchone()
    264268        self.assertIsInstance(res, tuple)
    265269        self.assertEqual(res, (1, 2))
     270        old_py = OrderedDict is None  # Python 2.6 or 3.0
    266271        if old_py:
    267             self.assertEqual(res._fields, ('column_0', 'column_1'))
     272            self.assertEqual(res._fields, ('abc', 'column_1'))
    268273        else:
    269274            self.assertEqual(res._fields, ('abc', '_1'))
     275        cur.execute('select 1 as snake_case, 2 as "CamelCase",'
     276            ' 3 as "kebap-case", 4 as "_bad", 5 as "0bad", 6 as "bad$"')
     277        res = cur.fetchone()
     278        self.assertIsInstance(res, tuple)
     279        self.assertEqual(res, (1, 2, 3, 4, 5, 6))
     280        # old Python versions cannot rename tuple fields with underscore
     281        self.assertEqual(res._fields[:2], ('snake_case', 'CamelCase'))
     282        fields = ('_2', '_3', '_4', '_5')
     283        if old_py:
     284            fields = tuple('column' + field for field in fields)
     285        self.assertEqual(res._fields[2:], fields)
    270286
    271287    def test_colnames(self):
Note: See TracChangeset for help on using the changeset viewer.