From 782133158f8c3bcd99aed28674053a82574c8d4c Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 3 Sep 2022 09:28:42 -0500 Subject: [PATCH] Test sending CopyData before CopyFrom responds with error --- pgconn/pgconn_test.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pgconn/pgconn_test.go b/pgconn/pgconn_test.go index 601cbc8e..5b6ca284 100644 --- a/pgconn/pgconn_test.go +++ b/pgconn/pgconn_test.go @@ -1779,7 +1779,18 @@ func TestConnCopyFromQuerySyntaxError(t *testing.T) { srcBuf := &bytes.Buffer{} - res, err := pgConn.CopyFrom(context.Background(), srcBuf, "cropy foo to stdout") + // Send data even though the COPY FROM command will be rejected with a syntax error. This ensures that this does not + // break the connection. See https://github.com/jackc/pgconn/pull/127 for context. + inputRows := [][][]byte{} + for i := 0; i < 1000; i++ { + a := strconv.Itoa(i) + b := "foo " + a + " bar" + inputRows = append(inputRows, [][]byte{[]byte(a), []byte(b)}) + _, err = srcBuf.Write([]byte(fmt.Sprintf("%s,\"%s\"\n", a, b))) + require.NoError(t, err) + } + + res, err := pgConn.CopyFrom(context.Background(), srcBuf, "cropy foo FROM STDIN WITH (FORMAT csv)") require.Error(t, err) assert.IsType(t, &pgconn.PgError{}, err) assert.Equal(t, int64(0), res.RowsAffected())