Changeset 901 for trunk/tests


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

Location:
trunk/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/test_classic_connection.py

    r894 r901  
    1919import os
    2020
     21from collections import namedtuple
     22from decimal import Decimal
     23
    2124import pg  # the module under test
    22 
    23 from decimal import Decimal
    2425
    2526# We need a database to test against.  If LOCAL_PyGreSQL.py exists we will
     
    406407        self.assertEqual(v._fields, ('alias0',))
    407408        self.assertEqual(v.alias0, 0)
     409
     410    def testNamedresultWithGoodFieldnames(self):
     411        q = 'select 1 as snake_case_alias, 2 as "CamelCaseAlias"'
     412        result = [(1, 2)]
     413        r = self.c.query(q).namedresult()
     414        self.assertEqual(r, result)
     415        v = r[0]
     416        self.assertEqual(v._fields, ('snake_case_alias', 'CamelCaseAlias'))
     417
     418    def testNamedresultWithBadFieldnames(self):
     419        try:
     420            r = namedtuple('Bad', ['?'] * 6, rename=True)
     421        except TypeError:  # Python 2.6 or 3.0
     422            fields = tuple('column_%d' % n for n in range(6))
     423        else:
     424            fields = r._fields
     425        q = ('select 3 as "0alias", 4 as _alias, 5 as "alias$", 6 as "alias?",'
     426            ' 7 as "kebap-case-alias", 8 as break, 9 as and_a_good_one')
     427        result = [tuple(range(3, 10))]
     428        r = self.c.query(q).namedresult()
     429        self.assertEqual(r, result)
     430        v = r[0]
     431        self.assertEqual(v._fields[:6], fields)
     432        self.assertEqual(v._fields[6], 'and_a_good_one')
    408433
    409434    def testGet3Cols(self):
  • 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.