Changeset 894 for trunk/tests


Ignore:
Timestamp:
Sep 23, 2016, 10:04:06 AM (3 years ago)
Author:
cito
Message:

Cache the namedtuple classes used for query result rows

Location:
trunk/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/test_classic_connection.py

    r876 r894  
    492492            ' 0 as "A long name with Blanks"')
    493493        r = self.c.query(q).listfields()
     494        self.assertIsInstance(r, tuple)
    494495        result = ('a', 'b', 'c', 'c', 'b', 'a',
    495496            'lowercase', 'uppercase', 'mixedcase', 'MixedCase',
     
    18041805        self.assertIs(r, namedresult)
    18051806
     1807    def testSetRowFactorySize(self):
     1808        try:
     1809            from functools import lru_cache
     1810        except ImportError:  # Python < 3.2
     1811            lru_cache = None
     1812        queries = ['select 1 as a, 2 as b, 3 as c', 'select 123 as abc']
     1813        query = self.c.query
     1814        for maxsize in (None, 0, 1, 2, 3, 10, 1024):
     1815            pg.set_row_factory_size(maxsize)
     1816            for i in range(3):
     1817                for q in queries:
     1818                    r = query(q).namedresult()[0]
     1819                    if q.endswith('abc'):
     1820                        self.assertEqual(r, (123,))
     1821                        self.assertEqual(r._fields, ('abc',))
     1822                    else:
     1823                        self.assertEqual(r, (1, 2, 3))
     1824                        self.assertEqual(r._fields, ('a', 'b', 'c'))
     1825            if lru_cache:
     1826                info = pg._row_factory.cache_info()
     1827                self.assertEqual(info.maxsize, maxsize)
     1828                self.assertEqual(info.hits + info.misses, 6)
     1829                self.assertEqual(info.hits,
     1830                    0 if maxsize is not None and maxsize < 2 else 4)
     1831
    18061832
    18071833class TestStandaloneEscapeFunctions(unittest.TestCase):
  • trunk/tests/test_dbapi20.py

    r893 r894  
    10561056        self.assertEqual(row, (value, 'hello'))
    10571057
    1058 
    10591058    def test_json(self):
    10601059        inval = {"employees":
     
    12571256        self.assertEqual(row, data)
    12581257
     1258    def test_set_row_factory_size(self):
     1259        try:
     1260            from functools import lru_cache
     1261        except ImportError:  # Python < 3.2
     1262            lru_cache = None
     1263        queries = ['select 1 as a, 2 as b, 3 as c', 'select 123 as abc']
     1264        con = self._connect()
     1265        cur = con.cursor()
     1266        for maxsize in (None, 0, 1, 2, 3, 10, 1024):
     1267            pgdb.set_row_factory_size(maxsize)
     1268            for i in range(3):
     1269                for q in queries:
     1270                    cur.execute(q)
     1271                    r = cur.fetchone()
     1272                    if q.endswith('abc'):
     1273                        self.assertEqual(r, (123,))
     1274                        self.assertEqual(r._fields, ('abc',))
     1275                    else:
     1276                        self.assertEqual(r, (1, 2, 3))
     1277                        self.assertEqual(r._fields, ('a', 'b', 'c'))
     1278            if lru_cache:
     1279                info = pgdb._row_factory.cache_info()
     1280                self.assertEqual(info.maxsize, maxsize)
     1281                self.assertEqual(info.hits + info.misses, 6)
     1282                self.assertEqual(info.hits,
     1283                    0 if maxsize is not None and maxsize < 2 else 4)
     1284
    12591285    def test_memory_leaks(self):
    12601286        ids = set()
Note: See TracChangeset for help on using the changeset viewer.