-
Notifications
You must be signed in to change notification settings - Fork 122
/
modifiers_test.go
101 lines (86 loc) · 1.8 KB
/
modifiers_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
// Copyright 2018 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package sqlbuilder
import (
"fmt"
"testing"
"github.com/huandu/go-assert"
)
func TestEscape(t *testing.T) {
a := assert.New(t)
cases := map[string]string{
"foo": "foo",
"$foo": "$$foo",
"$$$": "$$$$$$",
}
var inputs, expects []string
for s, expected := range cases {
inputs = append(inputs, s)
expects = append(expects, expected)
actual := Escape(s)
a.Equal(actual, expected)
}
actuals := EscapeAll(inputs...)
a.Equal(actuals, expects)
}
func TestFlatten(t *testing.T) {
a := assert.New(t)
cases := [][2]interface{}{
{
"foo",
[]interface{}{"foo"},
},
{
[]int{1, 2, 3},
[]interface{}{1, 2, 3},
},
{
[]interface{}{"abc", []int{1, 2, 3}, [3]string{"def", "ghi"}},
[]interface{}{"abc", 1, 2, 3, "def", "ghi", ""},
},
}
for _, c := range cases {
input, expected := c[0], c[1]
actual := Flatten(input)
a.Equal(actual, expected)
}
}
func TestTuple(t *testing.T) {
a := assert.New(t)
cases := []struct {
values []interface{}
expected string
}{
{
nil,
"()",
},
{
[]interface{}{1, "bar", nil, Tuple("foo", Tuple(2, "baz"))},
"(1, 'bar', NULL, ('foo', (2, 'baz')))",
},
}
for _, c := range cases {
sql, args := Build("$?", Tuple(c.values...)).Build()
actual, err := DefaultFlavor.Interpolate(sql, args)
a.NilError(err)
a.Equal(actual, c.expected)
}
}
func ExampleTuple() {
sb := Select("id", "name").From("user")
sb.Where(
sb.In(
TupleNames("type", "status"),
Tuple("web", 1),
Tuple("app", 1),
Tuple("app", 2),
),
)
sql, args := sb.Build()
fmt.Println(sql)
fmt.Println(args)
// Output:
// SELECT id, name FROM user WHERE (type, status) IN ((?, ?), (?, ?), (?, ?))
// [web 1 app 1 app 2]
}