]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Fix setting row maker on nextset()
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 12 Feb 2021 01:44:06 +0000 (02:44 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 12 Feb 2021 01:44:06 +0000 (02:44 +0100)
The test was broken and didn't test that the row_factory function was
really called.

psycopg3/psycopg3/cursor.py
tests/test_cursor.py
tests/test_cursor_async.py

index db87c83d247be08a1517849651281d4ceef08282..e281cb6692f0a5d70a1ac7598cf78bd245920ff6 100644 (file)
@@ -177,6 +177,8 @@ class BaseCursor(Generic[ConnectionType]):
         if self._iresult < len(self._results):
             self.pgresult = self._results[self._iresult]
             self._tx.set_pgresult(self._results[self._iresult])
+            if self._row_factory:
+                self._tx.make_row = self._row_factory(self)
             self._pos = 0
             nrows = self.pgresult.command_tuples
             self._rowcount = nrows if nrows is not None else -1
index 7ff25f0a326e40e7e7fc2511e6138424265b4390..e6e39ccd539668b7dc4fe5a6a8286d75c2d5ee55 100644 (file)
@@ -287,18 +287,15 @@ def test_iter_stop(conn):
 
 
 def test_row_factory(conn):
-    def my_row_factory(cur):
-        return lambda values: [-v for v in values]
-
     cur = conn.cursor(row_factory=my_row_factory)
-    cur.execute("select generate_series(1, 3)")
-    r = cur.fetchall()
-    assert r == [[-1], [-2], [-3]]
+    cur.execute("select 'foo' as bar")
+    (r,) = cur.fetchone()
+    assert r == "FOObar"
 
-    cur.execute("select 42; select generate_series(1,3)")
-    assert cur.fetchall() == [[-42]]
+    cur.execute("select 'x' as x; select 'y' as y, 'z' as z")
+    assert cur.fetchall() == [["Xx"]]
     assert cur.nextset()
-    assert cur.fetchall() == [[-1], [-2], [-3]]
+    assert cur.fetchall() == [["Yy", "Zz"]]
     assert cur.nextset() is None
 
 
@@ -569,3 +566,15 @@ def test_leak(dsn, faker, fmt, fetch):
     assert (
         n[0] == n[1] == n[2]
     ), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
+
+
+def my_row_factory(cursor):
+    assert cursor.description is not None
+    titles = [c.name for c in cursor.description]
+
+    def mkrow(values):
+        return [
+            f"{value.upper()}{title}" for title, value in zip(titles, values)
+        ]
+
+    return mkrow
index e56777323c2017d0391a80787c0985b5d27dad42..88835b1c734adbec462f351ec03f3dfe4b8c2922 100644 (file)
@@ -6,6 +6,7 @@ import datetime as dt
 import psycopg3
 from psycopg3 import sql
 from psycopg3.adapt import Format
+from .test_cursor import my_row_factory
 
 pytestmark = pytest.mark.asyncio
 
@@ -292,17 +293,6 @@ async def test_iter_stop(aconn):
 
 
 async def test_row_factory(aconn):
-    def my_row_factory(cursor):
-        def mkrow(values):
-            assert cursor.description is not None
-            titles = [c.name for c in cursor.description]
-            return [
-                f"{value.upper()}{title}"
-                for title, value in zip(titles, values)
-            ]
-
-        return mkrow
-
     cur = aconn.cursor(row_factory=my_row_factory)
     await cur.execute("select 'foo' as bar")
     (r,) = await cur.fetchone()