-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsqlf_test.go
71 lines (63 loc) · 2.09 KB
/
sqlf_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
63
64
65
66
67
68
69
70
71
package sqlf_test
import (
"reflect"
"testing"
"github.com/keegancsmith/sqlf"
)
func TestSprintf(t *testing.T) {
cases := map[string]struct {
Fmt string
FmtArgs []interface{}
Want string
WantArgs []interface{}
}{
"simple_substitute": {
"SELECT * FROM test_table WHERE a = %s AND b = %d",
[]interface{}{"foo", 1},
"SELECT * FROM test_table WHERE a = $1 AND b = $2",
[]interface{}{"foo", 1},
},
"simple_embedded": {
"SELECT * FROM test_table WHERE a = (%s)",
[]interface{}{sqlf.Sprintf("SELECT b FROM b_table WHERE x = %d", 1)},
"SELECT * FROM test_table WHERE a = (SELECT b FROM b_table WHERE x = $1)",
[]interface{}{1},
},
"embedded": {
"SELECT * FROM test_table WHERE a = %s AND c = (%s) AND d = %s",
[]interface{}{"foo", sqlf.Sprintf("SELECT b FROM b_table WHERE x = %d", 1), "bar"},
"SELECT * FROM test_table WHERE a = $1 AND c = (SELECT b FROM b_table WHERE x = $2) AND d = $3",
[]interface{}{"foo", 1, "bar"},
},
"embedded_embedded": {
"SELECT * FROM test_table WHERE a = %s AND c = (%s) AND d = %s",
[]interface{}{
"foo",
sqlf.Sprintf("SELECT b FROM b_table WHERE x = %d AND y = (%s)", 1, sqlf.Sprintf("SELECT %s", "baz")),
"bar",
},
"SELECT * FROM test_table WHERE a = $1 AND c = (SELECT b FROM b_table WHERE x = $2 AND y = (SELECT $3)) AND d = $4",
[]interface{}{"foo", 1, "baz", "bar"},
},
"literal_percent_operator": {
"SELECT * FROM test_table WHERE a <<%% %s AND b = %d",
[]interface{}{"foo", 1},
"SELECT * FROM test_table WHERE a <<% $1 AND b = $2",
[]interface{}{"foo", 1},
},
}
for tn, tc := range cases {
q := sqlf.Sprintf(tc.Fmt, tc.FmtArgs...)
if got := q.Query(sqlf.PostgresBindVar); got != tc.Want {
t.Errorf("%s: expected query: %q, got: %q", tn, tc.Want, got)
}
if got := q.Args(); !reflect.DeepEqual(got, tc.WantArgs) {
t.Errorf("%s: expected args: %q, got: %q", tn, tc.WantArgs, got)
}
}
}
func BenchmarkSprintf(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sqlf.Sprintf("SELECT * FROM test_table WHERE a = %s AND b = %d", "foo", 1).Query(sqlf.PostgresBindVar)
}
}