]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fixed cursor close check + added tests for fetchone, fetchmany, fetchall
authorSuresh Kumar <sureshdsk91@gmail.com>
Sat, 30 Oct 2021 14:16:33 +0000 (19:46 +0530)
committerSuresh Kumar <sureshdsk91@gmail.com>
Sat, 30 Oct 2021 14:34:04 +0000 (20:04 +0530)
psycopg/psycopg/cursor.py
tests/test_cursor.py

index 71b66cb9096c4318000f567a3334e61f3a99f99b..7b3d79ce5d7c11425af858e361088f0c69c08790 100644 (file)
@@ -451,7 +451,7 @@ class BaseCursor(Generic[ConnectionType, Row]):
         )
 
     def _check_result(self) -> None:
-        if self._conn.closed:
+        if self.closed:
             raise e.InterfaceError("the cursor is closed")
         res = self.pgresult
         if not res:
index 5a4a7b8ccf545a2b3d2ed8d89b84ef5599096270..8948f50c5694686d46132c2af732c33539fee429 100644 (file)
@@ -26,6 +26,65 @@ def test_close(conn):
     assert cur.closed
 
 
+def test_cursor_close_fetchone(conn):
+    cur = conn.cursor()
+    assert not cur.closed
+
+    query = "select * from generate_series(1, 10)"
+    cur.execute(query)
+    for _ in range(5):
+        cur.fetchone()
+
+    cur.close()
+    assert cur.closed
+
+    with pytest.raises(psycopg.InterfaceError):
+        cur.fetchone()
+
+
+def test_cursor_close_fetchmany(conn):
+    cur = conn.cursor()
+    assert not cur.closed
+
+    query = "select * from generate_series(1, 10)"
+    cur.execute(query)
+    cur.close()
+    assert cur.closed
+
+    with pytest.raises(psycopg.InterfaceError):
+        cur.fetchmany(1)
+
+
+def test_cursor_close_fetchmany(conn):
+    cur = conn.cursor()
+    assert not cur.closed
+
+    query = "select * from generate_series(1, 10)"
+    cur.execute(query)
+    assert len(cur.fetchmany(2)) == 2
+
+    cur.close()
+    assert cur.closed
+
+    with pytest.raises(psycopg.InterfaceError):
+        cur.fetchmany(2)
+
+
+def test_cursor_close_fetchall(conn):
+    cur = conn.cursor()
+    assert not cur.closed
+
+    query = "select * from generate_series(1, 10)"
+    cur.execute(query)
+    assert len(cur.fetchall()) == 10
+
+    cur.close()
+    assert cur.closed
+
+    with pytest.raises(psycopg.InterfaceError):
+        cur.fetchall()
+
+
 def test_context(conn):
     with conn.cursor() as cur:
         assert not cur.closed