-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatement_test.go
62 lines (53 loc) · 1.49 KB
/
statement_test.go
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 pgq
import (
"reflect"
"testing"
)
func TestStatementBuilderWhere(t *testing.T) {
t.Parallel()
sb := Statement().Where("x = ?", 1)
sql, args, err := sb.Select("test").Where("y = ?", 2).SQL()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
want := "SELECT test WHERE x = $1 AND y = $2"
if want != sql {
t.Errorf("expected SQL to be %q, got %q instead", want, sql)
}
expectedArgs := []any{1, 2}
if !reflect.DeepEqual(expectedArgs, args) {
t.Errorf("wanted %v, got %v instead", args, expectedArgs)
}
}
func TestStatementBuilderUpdate(t *testing.T) {
t.Parallel()
b := Statement().Where("x = ?", "z").Update("foo").Set("a", "b")
want := "UPDATE foo SET a = $1 WHERE x = $2"
sql, args, err := b.SQL()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if sql != want {
t.Errorf("expected SQL to be %q, got %q instead", want, sql)
}
expectedArgs := []any{"b", "z"}
if !reflect.DeepEqual(args, expectedArgs) {
t.Errorf("expected arguments to be %q, got %q instead", expectedArgs, args)
}
}
func TestStatementBuilderDelete(t *testing.T) {
t.Parallel()
b := Statement().Where("x = ?", "z").Delete("foo")
want := "DELETE FROM foo WHERE x = $1"
sql, args, err := b.SQL()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if sql != want {
t.Errorf("expected SQL to be %q, got %q instead", want, sql)
}
expectedArgs := []any{"z"}
if !reflect.DeepEqual(args, expectedArgs) {
t.Errorf("expected arguments to be %q, got %q instead", expectedArgs, args)
}
}