-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_test.go
169 lines (163 loc) · 3.08 KB
/
db_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package db
import (
"fmt"
"testing"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
)
type testStruct struct {
StrValue string `json:"str_value,omitempty"`
IntValue int `json:"int_value"`
}
func TestQprintf(t *testing.T) {
var strPtr *string
var decimalPtr *decimal.Decimal
var notInitializedSlice []testStruct
var nullPointerToSlice *[]testStruct
var nullPointerToArray *[2]testStruct
var nullPointerToStruct *testStruct
var cases = []struct {
SQL string
params Params
expectedResult string
}{
// empty source
{
"",
Params{},
"",
},
// strings
{
":a, :b",
Params{"a": "value_a", "b": "value_b"},
"'value_a', 'value_b'",
},
// strings with null word
{
":a, :b, :c",
Params{"a": "null", "b": "NULL", "c": "nil"},
"'null', 'NULL', 'nil'",
},
// nil, nil pointer
{
":a, :b",
Params{"a": nil, "b": strPtr},
"NULL, NULL",
},
// overlapping param names
{
":a, :a_b, :a b",
Params{"a": "value_a", "a_b": "value_b"},
"'value_a', 'value_b', 'value_a' b",
},
// decimal, pointer to decimal
{
":a, :b",
Params{"a": decimal.NewFromFloat(0.0005), "b": decimalPtr},
"0.0005, NULL",
},
// param name collides with a type specification
{
"'1'::int",
Params{"int": 1},
"'1'::int",
},
// comma list
{
"WHERE field IN (:comma_list)",
Params{"comma_list": CommaListParam{1, 2, 3, 4, 5, nil, 6, "as"}},
"WHERE field IN (1, 2, 3, 4, 5, NULL, 6, 'as')",
},
// slice of scalars converts to json
{
":a, :b",
Params{"a": []string{"AAA", "BBB"}, "b": []int{0, 2, 4}},
"'[\"AAA\",\"BBB\"]', '[0,2,4]'",
},
// slice of struct converts to json
{
":a",
Params{"a": []testStruct{{StrValue: "", IntValue: 42}}},
"'[{\"int_value\":42}]'",
},
// empty slice
{
":a",
Params{"a": []testStruct{}},
"'[]'",
},
// not initialized slice
{
":a",
Params{"a": notInitializedSlice},
"NULL",
},
// null pointer to slice
{
":a",
Params{"a": nullPointerToSlice},
"NULL",
},
// null pointer to array
{
":a",
Params{"a": nullPointerToArray},
"NULL",
},
// pointer to empty slice
{
":a",
Params{"a": &[]testStruct{}},
"'[]'",
},
// pointer to slice of struct
{
":a",
Params{"a": &[]testStruct{{StrValue: "24", IntValue: 42}}},
"'[{\"str_value\":\"24\",\"int_value\":42}]'",
},
// struct
{
":a",
Params{"a": testStruct{}},
"'{\"int_value\":0}'",
},
// pointer to struct
{
":a",
Params{"a": &testStruct{}},
"'{\"int_value\":0}'",
},
// null pointer to struct
{
":a",
Params{"a": nullPointerToStruct},
"NULL",
},
// some edge cases
{
":",
Params{},
":",
},
{
"::",
Params{},
"::",
},
// params with digits
{
":a1_2, :b3_4",
Params{"a1_2": "value_a", "b3_4": "value_b"},
"'value_a', 'value_b'",
},
}
for i, c := range cases {
t.Run(fmt.Sprintf("case %d", i+1), func(t *testing.T) {
result, err := qprintf(c.SQL, c.params)
assert.NoError(t, err)
assert.Equal(t, c.expectedResult, result)
})
}
}