-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepository_more_exec_test.go
More file actions
62 lines (53 loc) · 1.85 KB
/
Copy pathrepository_more_exec_test.go
File metadata and controls
62 lines (53 loc) · 1.85 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
package norm
import (
"context"
"testing"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type recExecRepo struct {
lastSQL string
lastArgs []any
}
func (r *recExecRepo) Exec(_ context.Context, sql string, args ...any) (pgconn.CommandTag, error) {
r.lastSQL, r.lastArgs = sql, args
return pgconn.CommandTag{}, nil
}
// Satisfy dbExecuter for repository paths that do not call Query
func (r *recExecRepo) Query(_ context.Context, _ string, _ ...any) (pgx.Rows, error) { return nil, nil }
func (r *recExecRepo) QueryRow(_ context.Context, _ string, _ ...any) pgx.Row {
return errorRow{err: nil}
}
type repUser struct {
ID int64 `db:"id" norm:"primary_key,auto_increment"`
Name string `db:"name"`
Version int64 `db:"version" norm:"version"`
}
func TestRepository_Update_SQL(t *testing.T) {
kn := &KintsNorm{}
ex := &recExecRepo{}
r := &repo[repUser]{kn: kn, exec: ex}
_ = r.Update(context.Background(), &repUser{ID: 1, Name: "a", Version: 3})
if ex.lastSQL != "UPDATE rep_users SET \"name\" = $1, \"version\" = \"version\" + 1 WHERE \"id\" = $2 AND \"version\" = $3" {
t.Fatalf("sql=%s", ex.lastSQL)
}
}
func TestRepository_Upsert_SQL(t *testing.T) {
kn := &KintsNorm{}
ex := &recExecRepo{}
r := &repo[repUser]{kn: kn, exec: ex}
_ = r.Upsert(context.Background(), &repUser{ID: 1, Name: "a"}, []string{"id"}, []string{"name"})
if ex.lastSQL != "INSERT INTO rep_users (\"name\", \"version\") VALUES ($1, $2) ON CONFLICT (\"id\") DO UPDATE SET \"name\" = EXCLUDED.\"name\"" {
t.Fatalf("sql=%s", ex.lastSQL)
}
}
func TestRepository_Delete_CreateBatch(t *testing.T) {
kn := &KintsNorm{}
ex := &recExecRepo{}
r := &repo[repUser]{kn: kn, exec: ex}
_ = r.Delete(context.Background(), 1)
if ex.lastSQL == "" {
t.Fatalf("delete no sql")
}
_ = r.CreateBatch(context.Background(), []*repUser{{ID: 1, Name: "a"}, {ID: 2, Name: "b"}})
}