-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_builder_join_error_test.go
More file actions
55 lines (49 loc) · 1.48 KB
/
query_builder_join_error_test.go
File metadata and controls
55 lines (49 loc) · 1.48 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
package norm
import (
"context"
"errors"
"strings"
"testing"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type execErrQB struct{ err error }
func (e execErrQB) Exec(_ context.Context, _ string, _ ...any) (pgconn.CommandTag, error) {
return pgconn.CommandTag{}, e.err
}
func (e execErrQB) Query(_ context.Context, _ string, _ ...any) (pgx.Rows, error) { return nil, e.err }
func (e execErrQB) QueryRow(_ context.Context, _ string, _ ...any) pgx.Row {
return errorRow{err: e.err}
}
func TestQueryBuilder_JoinAndExecError(t *testing.T) {
kn := &KintsNorm{}
qb := (&QueryBuilder{kn: kn, exec: execErrQB{err: errors.New("context canceled")}}).Table("a").Join("b", "a.id=b.aid").Raw("update a set x = ?", 1)
if err := qb.Exec(context.Background()); err == nil {
t.Fatalf("expected error")
}
}
func TestQueryBuilder_JoinVariants_Build(t *testing.T) {
kn := &KintsNorm{}
qb := (&QueryBuilder{kn: kn, exec: execErrQB{err: errors.New("boom")}}).
Table("a").
InnerJoin("b", "a.id=b.aid").
LeftJoin("c", "a.id=c.aid").
RightJoin("d", "a.id=d.aid").
FullJoin("e", "a.id=e.aid").
CrossJoin("f")
// ensure SQL contains all join forms in order
sql, _ := qb.buildSelect()
wantParts := []string{
" FROM a ",
"JOIN b ON a.id=b.aid",
"LEFT JOIN c ON a.id=c.aid",
"RIGHT JOIN d ON a.id=d.aid",
"FULL JOIN e ON a.id=e.aid",
"CROSS JOIN f",
}
for _, p := range wantParts {
if !strings.Contains(sql, p) {
t.Fatalf("expected sql to contain %q, got %s", p, sql)
}
}
}