aboutsummaryrefslogtreecommitdiff
path: root/internal/database/database_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/database/database_test.go')
-rw-r--r--internal/database/database_test.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/internal/database/database_test.go b/internal/database/database_test.go
index 668da288..54e3f572 100644
--- a/internal/database/database_test.go
+++ b/internal/database/database_test.go
@@ -17,6 +17,9 @@ import (
"time"
"github.com/google/go-cmp/cmp"
+ "github.com/jackc/pgconn"
+ "github.com/jackc/pgx/v4"
+ "github.com/jackc/pgx/v4/stdlib"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/testing/dbtest"
)
@@ -445,3 +448,39 @@ func TestTransactSerializable(t *testing.T) {
}
}
+
+func TestCopyDoesNotUpsert(t *testing.T) {
+ // This test verifies that copying rows into a table will not overwrite existing rows.
+ ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
+ defer cancel()
+ conn, err := testDB.db.Conn(ctx)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ for _, stmt := range []string{
+ `DROP TABLE IF EXISTS test_copy`,
+ `CREATE TABLE test_copy (i INTEGER PRIMARY KEY)`,
+ `INSERT INTO test_copy (i) VALUES (1)`,
+ } {
+ if _, err := testDB.Exec(ctx, stmt); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ err = conn.Raw(func(c interface{}) error {
+ stdConn, ok := c.(*stdlib.Conn)
+ if !ok {
+ t.Skip("DB driver is not pgx")
+ }
+ rows := [][]interface{}{{1}, {2}}
+ _, err = stdConn.Conn().CopyFrom(ctx, []string{"test_copy"}, []string{"i"}, pgx.CopyFromRows(rows))
+ return err
+ })
+
+ const constraintViolationCode = "23505"
+ var gerr *pgconn.PgError
+ if !errors.As(err, &gerr) || gerr.Code != constraintViolationCode {
+ t.Errorf("got %v, wanted code %s", gerr, constraintViolationCode)
+ }
+}