Changeset 784 for trunk/tests


Ignore:
Timestamp:
Jan 26, 2016, 12:17:23 PM (3 years ago)
Author:
cito
Message:

Make type cache and cursor description more useful

The type cache now stores some more information, e.g. whether a type is a base
type or a composite type and the category of the type. This may be later used
for casting composite types, or exposed to the user.

The cursor description now contains proper information on the size of numeric
types (including precision and scale).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/test_dbapi20.py

    r781 r784  
    263263        con = self._connect()
    264264        cur = con.cursor()
    265         cur.execute("select 123456789::int8 as col")
     265        cur.execute("select 123456789::int8 col0,"
     266            " 123456.789::numeric(41, 13) as col1,"
     267            " 'foobar'::char(39) as col2")
    266268        desc = cur.description
    267269        self.assertIsInstance(desc, list)
    268         self.assertEqual(len(desc), 1)
    269         desc = desc[0]
    270         self.assertIsInstance(desc, tuple)
    271         self.assertEqual(len(desc), 7)
    272         self.assertEqual(desc.name, 'col')
    273         self.assertEqual(desc.type_code, 'int8')
    274         self.assertIsNone(desc.display_size)
    275         self.assertIsInstance(desc.internal_size, int)
    276         self.assertEqual(desc.internal_size, 8)
    277         self.assertIsNone(desc.precision)
    278         self.assertIsNone(desc.scale)
    279         self.assertIsNone(desc.null_ok)
     270        self.assertEqual(len(desc), 3)
     271        cols = [('int8', 8, None), ('numeric', 41, 13), ('bpchar', 39, None)]
     272        for i in range(3):
     273            c, d = cols[i], desc[i]
     274            self.assertIsInstance(d, tuple)
     275            self.assertEqual(len(d), 7)
     276            self.assertIsInstance(d.name, str)
     277            self.assertEqual(d.name, 'col%d' % i)
     278            self.assertIsInstance(d.type_code, str)
     279            self.assertEqual(d.type_code, c[0])
     280            self.assertIsNone(d.display_size)
     281            self.assertIsInstance(d.internal_size, int)
     282            self.assertEqual(d.internal_size, c[1])
     283            if c[2] is not None:
     284                self.assertIsInstance(d.precision, int)
     285                self.assertEqual(d.precision, c[1])
     286                self.assertIsInstance(d.scale, int)
     287                self.assertEqual(d.scale, c[2])
     288            else:
     289                self.assertIsNone(d.precision)
     290                self.assertIsNone(d.scale)
     291            self.assertIsNone(d.null_ok)
     292
     293    def test_type_cache(self):
     294        con = self._connect()
     295        cur = con.cursor()
     296        type_info = cur._type_cache['numeric']
     297        self.assertEqual(type_info.oid, 1700)
     298        self.assertEqual(type_info.name, 'numeric')
     299        self.assertEqual(type_info.type, 'b')  # base
     300        self.assertEqual(type_info.category, 'N')  # numeric
     301        self.assertEqual(type_info.delim, ',')
     302        self.assertIs(cur._type_cache[1700], type_info)
    280303
    281304    def test_cursor_iteration(self):
Note: See TracChangeset for help on using the changeset viewer.