-
Notifications
You must be signed in to change notification settings - Fork 123
/
args_test.go
140 lines (106 loc) · 3.13 KB
/
args_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2018 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package sqlbuilder
import (
"bytes"
"database/sql"
"fmt"
"strings"
"testing"
"github.com/huandu/go-assert"
)
func TestArgs(t *testing.T) {
a := assert.New(t)
start := sql.Named("start", 1234567890)
end := sql.Named("end", 1234599999)
named1 := Named("named1", "foo")
named2 := Named("named2", "bar")
cases := map[string][]interface{}{
"abc ? def\n[123]": {"abc $? def", 123},
"abc ? def\n[456]": {"abc $0 def", 456},
"abc /* INVALID ARG $1 */ def\n[]": {"abc $1 def", 123},
"abc def \n[]": {"abc ${unknown} def ", 123},
"abc $ def\n[]": {"abc $$ def", 123},
"abcdef$\n[]": {"abcdef$", 123},
"abc ? ? ? ? def\n[123 456 123 456]": {"abc $? $? $0 $? def", 123, 456, 789},
"abc ? raw ? raw def\n[123 123]": {"abc $? $? $0 $? def", 123, Raw("raw"), 789},
"abc $-1 $a def\n[]": {"abc $-1 $a def", 123},
"abc ? def ? ?\n[foo bar foo]": {"abc ${named1} def ${named2} ${named1}", named2, named1, named2},
"@end @start @end\n[{{} end 1234599999} {{} start 1234567890}]": {"$? $? $?", end, start, end},
}
for expected, c := range cases {
args := new(Args)
for i := 1; i < len(c); i++ {
args.Add(c[i])
}
sql, values := args.Compile(c[0].(string))
actual := fmt.Sprintf("%v\n%v", sql, values)
a.Equal(actual, expected)
}
old := DefaultFlavor
defer func() {
DefaultFlavor = old
}()
DefaultFlavor = PostgreSQL
// PostgreSQL flavor compiled sql.
for expected, c := range cases {
args := new(Args)
for i := 1; i < len(c); i++ {
args.Add(c[i])
}
sql, values := args.Compile(c[0].(string))
actual := fmt.Sprintf("%v\n%v", sql, values)
expected = toPostgreSQL(expected)
a.Equal(actual, expected)
}
DefaultFlavor = SQLServer
// SQLServer flavor compiled sql.
for expected, c := range cases {
args := new(Args)
for i := 1; i < len(c); i++ {
args.Add(c[i])
}
sql, values := args.Compile(c[0].(string))
actual := fmt.Sprintf("%v\n%v", sql, values)
expected = toSQLServerSQL(expected)
a.Equal(actual, expected)
}
DefaultFlavor = CQL
for expected, c := range cases {
args := new(Args)
for i := 1; i < len(c); i++ {
args.Add(c[i])
}
sql, values := args.Compile(c[0].(string))
actual := fmt.Sprintf("%v\n%v", sql, values)
a.Equal(actual, expected)
}
}
func toPostgreSQL(sql string) string {
parts := strings.Split(sql, "?")
buf := &bytes.Buffer{}
buf.WriteString(parts[0])
for i, p := range parts[1:] {
fmt.Fprintf(buf, "$%v", i+1)
buf.WriteString(p)
}
return buf.String()
}
func toSQLServerSQL(sql string) string {
parts := strings.Split(sql, "?")
buf := &bytes.Buffer{}
buf.WriteString(parts[0])
for i, p := range parts[1:] {
fmt.Fprintf(buf, "@p%v", i+1)
buf.WriteString(p)
}
return buf.String()
}
func TestArgsAdd(t *testing.T) {
a := assert.New(t)
args := &Args{}
for i := 0; i < maxPredefinedArgs*2; i++ {
actual := args.Add(i)
a.Equal(actual, fmt.Sprintf("$%v", i))
}
}