result = results[0]
status = result.status
- if status not in (pq.ExecStatus.COPY_IN, pq.ExecStatus.COPY_OUT):
+ if status in (pq.ExecStatus.COPY_IN, pq.ExecStatus.COPY_OUT):
+ return
+ elif status == pq.ExecStatus.FATAL_ERROR:
+ raise e.error_from_result(
+ result, encoding=self.connection.codec.name
+ )
+ else:
raise e.ProgrammingError(
- "copy() should be used only with COPY ... TO STDOUT"
- " or COPY ... FROM STDIN statements"
+ "copy() should be used only with COPY ... TO STDOUT or COPY ..."
+ f" FROM STDIN statements, got {pq.ExecStatus(status).name}"
)
assert conn.pgconn.transaction_status == conn.TransactionStatus.INERROR
+def test_copy_bad_result(conn):
+ conn.autocommit = True
+
+ cur = conn.cursor()
+
+ with pytest.raises(e.SyntaxError):
+ cur.copy("wat")
+
+ with pytest.raises(e.ProgrammingError):
+ cur.copy("select 1")
+
+ with pytest.raises(e.ProgrammingError):
+ cur.copy("reset timezone")
+
+
@pytest.mark.parametrize(
"format, buffer",
[(Format.TEXT, "sample_text"), (Format.BINARY, "sample_binary")],
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INERROR
+async def test_copy_bad_result(conn):
+ conn.autocommit = True
+
+ cur = conn.cursor()
+
+ with pytest.raises(e.SyntaxError):
+ await cur.copy("wat")
+
+ with pytest.raises(e.ProgrammingError):
+ await cur.copy("select 1")
+
+ with pytest.raises(e.ProgrammingError):
+ await cur.copy("reset timezone")
+
+
@pytest.mark.parametrize(
"format, buffer",
[(Format.TEXT, "sample_text"), (Format.BINARY, "sample_binary")],