diff options
author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2019-02-02 19:21:39 +0000 |
---|---|---|
committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2019-02-02 19:29:20 +0000 |
commit | 35ec7ad9c1ecb165eaeb2b8d4dae84ed8bad182b (patch) | |
tree | 88669b2a2413bfc04ec68f7dca646e83c3e68643 /tests | |
parent | 805527fcd6bedc2671a37c59299c1fa9148f9e6e (diff) | |
download | psycopg2-35ec7ad9c1ecb165eaeb2b8d4dae84ed8bad182b.tar.gz |
Use a proper LRU cache for namedtuplesfast-namedtuple
Previous one didn't refresh by last use. Use the stdlib version for py3
and one of our own for py2.
Max size set to 512, which should be fine for everyone (tweaking is
still possible by monkeypatching, as the tests do, but I don't want to
make an interface of it).
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/test_extras_dictcursor.py | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/tests/test_extras_dictcursor.py b/tests/test_extras_dictcursor.py index e08da5f..92a25b6 100755 --- a/tests/test_extras_dictcursor.py +++ b/tests/test_extras_dictcursor.py @@ -592,27 +592,30 @@ class NamedTupleCursorTest(ConnectingTestCase): def test_max_cache(self): from psycopg2.extras import NamedTupleCursor - old_max_cache = NamedTupleCursor.MAX_CACHE - NamedTupleCursor.MAX_CACHE = 10 + from psycopg2.compat import lru_cache + + old_func = NamedTupleCursor._cached_make_nt + NamedTupleCursor._cached_make_nt = \ + lru_cache(8)(NamedTupleCursor._do_make_nt) try: - NamedTupleCursor._nt_cache.clear() + recs = [] curs = self.conn.cursor() for i in range(10): curs.execute("select 1 as f%s" % i) - curs.fetchone() + recs.append(curs.fetchone()) - self.assertEqual(len(NamedTupleCursor._nt_cache), 10) - for i in range(10): - self.assert_(('f%s' % i,) in NamedTupleCursor._nt_cache) + # Still in cache + curs.execute("select 1 as f9") + rec = curs.fetchone() + self.assert_(any(type(r) is type(rec) for r in recs)) - curs.execute("select 1 as f10") - curs.fetchone() - self.assertEqual(len(NamedTupleCursor._nt_cache), 10) - self.assert_(('f10',) in NamedTupleCursor._nt_cache) - self.assert_(('f0',) not in NamedTupleCursor._nt_cache) + # Gone from cache + curs.execute("select 1 as f0") + rec = curs.fetchone() + self.assert_(all(type(r) is not type(rec) for r in recs)) finally: - NamedTupleCursor.MAX_CACHE = old_max_cache + NamedTupleCursor._cached_make_nt = old_func def test_suite(): |