-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy_from_test.go
More file actions
55 lines (44 loc) · 951 Bytes
/
copy_from_test.go
File metadata and controls
55 lines (44 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package sqlpro
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
type row struct {
Id int `db:"id,pk"`
Name string `db:"name"`
Value int `db:"value"`
}
func TestCopyFrom(t *testing.T) {
// Replace with your PostgreSQL connection string
db, err := Open(POSTGRES, "host=localhost port=5432 dbname=apitest password=egal sslmode=disable")
if !assert.NoError(t, err) {
return
}
defer db.Close()
ctx := context.Background()
// Sample data to copy
rows := []row{
{0, "Alice", 100},
{0, "Bob", 200},
{0, "Bob", 200},
{0, "Charlie", 300},
}
err = db.ExecTX(ctx, func(ctx context.Context) error {
tx := CtxTX(ctx)
err = tx.ExecContext(ctx, `
CREATE TEMP TABLE temp_example (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
value INTEGER
)
`)
if err != nil {
return err
}
return tx.InsertBulk("temp_example", rows)
}, nil)
if !assert.NoError(t, err) {
return
}
}