-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres_integration_test.go
More file actions
66 lines (56 loc) · 1.39 KB
/
Copy pathpostgres_integration_test.go
File metadata and controls
66 lines (56 loc) · 1.39 KB
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
56
57
58
59
60
61
62
63
64
65
66
package dbx
import (
"os"
"testing"
_ "github.com/jackc/pgx/v5/stdlib"
)
func openPostgres(t *testing.T) *DB {
t.Helper()
dsn := os.Getenv("DBX_PG_DSN")
if dsn == "" {
t.Skip("DBX_PG_DSN is not set; skipping postgres integration test")
}
db, err := Open("pgx", dsn)
if err != nil {
t.Fatalf("open postgres: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
return db
}
func TestPostgresIntegrationSelectModelUpsert(t *testing.T) {
db := openPostgres(t)
mustExec(t, db.NewQuery(`DROP TABLE IF EXISTS users`))
mustExec(t, db.NewQuery(`CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
status TEXT NOT NULL DEFAULT 'new'
)`))
u := &testUser{Name: "alice", Email: "a@pg.io"}
if err := db.Model(u).Insert(); err != nil {
t.Fatalf("model insert: %v", err)
}
if u.ID == 0 {
t.Fatalf("expected generated id, got %d", u.ID)
}
mustExec(t, db.Upsert("users", Params{
"name": "alice-v2",
"email": "a@pg.io",
"status": "active",
}, "email"))
var out []struct {
ID int64 `db:"id"`
Name string `db:"name"`
Status string `db:"status"`
}
err := db.Select("id", "name", "status").
From("users").
Where(HashExp{"email": "a@pg.io"}).
All(&out)
if err != nil {
t.Fatalf("select all: %v", err)
}
if len(out) != 1 || out[0].Name != "alice-v2" || out[0].Status != "active" {
t.Fatalf("unexpected row: %#v", out)
}
}