-
Notifications
You must be signed in to change notification settings - Fork 7
/
query_test.go
319 lines (279 loc) 路 8.46 KB
/
query_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package proxysql
import (
"reflect"
"strings"
"testing"
)
func TestTable(t *testing.T) {
expected := "table"
result := Table(expected)(defaultHostQuery()).table
if result != expected {
t.Fatalf("did not set table properly: %s", result)
}
}
func TestHostgroupID(t *testing.T) {
expected := 1
result := HostgroupID(expected)(defaultHostQuery()).host.hostgroup_id
if result != expected {
t.Fatalf("did not set hostgroup properly: %d", result)
}
}
func TestPort(t *testing.T) {
expected := 3305
result := Port(expected)(defaultHostQuery()).host.port
if result != expected {
t.Fatalf("did not set port properly: %d", result)
}
}
func TestHostname(t *testing.T) {
expected := "hostname"
result := Hostname(expected)(defaultHostQuery()).host.hostname
if result != expected {
t.Fatalf("did not set hostname properly: %s", result)
}
}
func TestMaxConnections(t *testing.T) {
expected := 300
result := MaxConnections(expected)(defaultHostQuery()).host.max_connections
if result != expected {
t.Fatalf("did not set max_connections properly: %d", result)
}
}
func TestStatus(t *testing.T) {
expected := "OFFLINE_SOFT"
result := Status(expected)(defaultHostQuery()).host.status
if result != expected {
t.Fatalf("did not set status properly: %s", result)
}
}
func TestWeight(t *testing.T) {
expected := 2
result := Weight(expected)(defaultHostQuery()).host.weight
if result != expected {
t.Fatalf("did not set weight properly: %d", result)
}
}
func TestCompression(t *testing.T) {
expected := 1
result := Compression(expected)(defaultHostQuery()).host.compression
if result != expected {
t.Fatalf("did not set compression properly: %d", result)
}
}
func TestMaxReplicationLag(t *testing.T) {
expected := 1
result := MaxReplicationLag(expected)(defaultHostQuery()).host.max_replication_lag
if result != expected {
t.Fatalf("did not set max_replication_lag properly: %d", result)
}
}
func TestUseSSL(t *testing.T) {
expected := 1
result := UseSSL(expected)(defaultHostQuery()).host.use_ssl
if result != expected {
t.Fatalf("did not set use_ssl properly: %d", result)
}
}
func TestMaxLatencyMS(t *testing.T) {
expected := 1
result := MaxLatencyMS(expected)(defaultHostQuery()).host.max_latency_ms
if result != expected {
t.Fatalf("did not set max_latency_ms properly: %d", result)
}
}
func TestComment(t *testing.T) {
expected := "comment"
result := Comment(expected)(defaultHostQuery()).host.comment
if result != expected {
t.Fatalf("did not set comment properly: %s", result)
}
}
func TestBuildAndParseEmptyHostQuery(t *testing.T) {
opts, err := buildAndParseHostQuery()
if err != nil {
t.Logf("unexpected err: %v", err)
t.Fail()
}
if !reflect.DeepEqual(opts, defaultHostQuery()) {
t.Fatalf("parsed opts were not default: %v", opts)
}
}
func TestBuildAndParseEmptyHostQueryWithHostnameFailsHostnameValidation(t *testing.T) {
opts, err := buildAndParseHostQueryWithHostname()
if err != ErrConfigNoHostname {
t.Logf("did not get expected err: %v", err)
t.Fail()
}
if opts != nil {
t.Fatalf("did not receive non nil opts: %v", opts)
}
}
func TestBuildAndParseHostQueryWithHostnameFailsWhenParentFails(t *testing.T) {
opts, err := buildAndParseHostQueryWithHostname(Port(1), Port(2))
if err != ErrConfigDuplicateSpec {
t.Logf("did not get expected err: %v", err)
t.Fail()
}
if opts != nil {
t.Fatalf("did not receive non nil opts: %v", opts)
}
}
func TestBuildAndParseHostQueryWithHostnameSucceedsWithHostname(t *testing.T) {
opts, err := buildAndParseHostQueryWithHostname(Hostname("hostname"))
if err != nil {
t.Logf("unexpected err: %v", err)
t.Fail()
}
if !reflect.DeepEqual(opts, defaultHostQuery().Hostname("hostname")) {
t.Fatalf("parsed opts were not default: %v", opts)
}
}
func TestBuildAndParseHostQueryWithHostgroupID(t *testing.T) {
opts, err := buildAndParseHostQuery(HostgroupID(1))
if err != nil {
t.Logf("unexpected err: %v", err)
t.Fail()
}
if !reflect.DeepEqual(opts, defaultHostQuery().HostgroupID(1)) {
t.Fatalf("parsed opts were not default: %v", opts)
}
}
func TestBuildAndParseHostQueryError(t *testing.T) {
opts, err := buildAndParseHostQuery(HostgroupID(-1))
if err != ErrConfigBadHostgroupID {
t.Logf("did not receive expected err: %v", err)
t.Fail()
}
if opts != nil {
t.Fatalf("returned query obj was not null: %v", opts)
}
}
func TestBuildSpecifiedColumns(t *testing.T) {
opts, err := buildAndParseHostQuery(HostgroupID(1), Port(12))
if err != nil {
t.Logf("unexpected parse error: %v", err)
t.Fail()
}
queryString := buildSpecifiedColumns(opts.specifiedFields)
expected := "(hostgroup_id, port)"
if expected != queryString {
t.Fatalf("specified fields returned were not expected: %s != %s", expected, queryString)
}
}
func TestBuildSpecifiedColumnsDifferentOrder(t *testing.T) {
opts, err := buildAndParseHostQuery(Port(1), HostgroupID(12))
if err != nil {
t.Logf("unexpected parse error: %v", err)
t.Fail()
}
queryString := buildSpecifiedColumns(opts.specifiedFields)
expected := "(port, hostgroup_id)"
if expected != queryString {
t.Fatalf("specified fields returned were not expected: %s != %s", expected, queryString)
}
}
func TestBuildSpecifiedColumnsDoesntGiveUsDuplicates(t *testing.T) {
opts, err := buildAndParseHostQuery(Port(1), HostgroupID(12), Port(2))
if err != ErrConfigDuplicateSpec {
t.Logf("unexpected parse error: %v", err)
t.Fail()
}
if opts != nil {
t.Fatalf("did not receive nil value: %v", opts)
}
}
func TestBuildSpecifiedColumnsIsOrderDependent(t *testing.T) {
opts, err := buildAndParseHostQuery(Port(1), HostgroupID(12))
if err != nil {
t.Logf("unexpected parse error: %v", err)
t.Fail()
}
for i := 0; i < 10; i++ {
queryString := buildSpecifiedColumns(opts.specifiedFields)
expected := "(port, hostgroup_id)"
if expected != queryString {
t.Fatalf("specified fields returned were not expected: %s != %s", expected, queryString)
}
}
}
func TestBuildSpecifiedValues(t *testing.T) {
opts, err := buildAndParseHostQuery(Port(1), HostgroupID(3), Table("runtime_mysql_servers"), Hostname("host"))
if err != nil {
t.Logf("unexpected parse error: %v", err)
t.Fail()
}
queryString := buildSpecifiedValues(opts)
expected := "(1, 3, 'host')"
if expected != queryString {
t.Fatalf("did not receive expected string, %s != %s", expected, queryString)
}
}
func TestColumnsAndValuesHaveTheSameAmountOfCommas(t *testing.T) {
opts, err := buildAndParseHostQuery(Port(1), HostgroupID(3), Table("runtime_mysql_servers"), Hostname("host"))
if err != nil {
t.Logf("unexpected parse error: %v", err)
t.Fail()
}
values := buildSpecifiedValues(opts)
columns := buildSpecifiedColumns(opts.specifiedFields)
if len(strings.Split(values, " ")) != len(strings.Split(columns, " ")) {
t.Fatalf("val and cols for query building not same size: %s, %s", values, columns)
}
}
func TestBuildInsertQuery(t *testing.T) {
opts, err := buildAndParseHostQuery(Port(1), HostgroupID(3), Table("runtime_mysql_servers"), Hostname("host"))
if err != nil {
t.Logf("unexpected parse error: %v", err)
t.Fail()
}
q := buildInsertQuery(opts)
t.Logf("query: %s", q)
if !strings.Contains(q, "runtime_mysql_servers") {
t.Log("insert query built did use specified table runtime_mysql_servers")
t.Fail()
}
for _, field := range opts.specifiedFields {
if !strings.Contains(q, field) {
t.Logf("insert query built did not contain field %s", field)
t.Fail()
}
}
}
func TestBuildSelectQuery(t *testing.T) {
opts, err := buildAndParseHostQuery(Port(1), HostgroupID(3), Table("runtime_mysql_servers"), Hostname("host"))
if err != nil {
t.Logf("unexpected parse error: %v", err)
t.Fail()
}
q := buildSelectQuery(opts)
t.Logf("query: %s", q)
if !strings.Contains(q, "runtime_mysql_servers") {
t.Log("select query built did use specified table runtime_mysql_servers")
t.Fail()
}
for _, field := range opts.specifiedFields {
if !strings.Contains(q, field) {
t.Logf("select query built did not contain field %s", field)
t.Fail()
}
}
}
func TestBuildDeleteQueryLimit(t *testing.T) {
opts, err := buildAndParseHostQuery(Port(1), HostgroupID(3), Hostname("host"))
if err != nil {
t.Logf("unexpected parse error: %v", err)
t.Fail()
}
q := buildDeleteQuery(opts)
t.Logf("query: %s", q)
if !strings.Contains(q, "mysql_servers") {
t.Log("select query built did use default table mysql_servers")
t.Fail()
}
for _, field := range opts.specifiedFields {
if !strings.Contains(q, field) {
t.Logf("select query built did not contain field %s", field)
t.Fail()
}
}
}