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_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):
Note: See TracChangeset for help on using the changeset viewer.