-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_handler_test.go
351 lines (314 loc) · 10.7 KB
/
db_handler_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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package delayed_job
import (
"database/sql"
"flag"
"runtime"
"strings"
"testing"
)
var (
test_db_url = flag.String("test_db_url", "", "the db url for test")
test_db_drv = flag.String("test_db_drv", "", "the db driver for test")
)
func TestTransformUrl(t *testing.T) {
for idx, test := range []struct{ drv, input, output string }{{drv: "postgres",
input: "gdbc:host=127.0.0.1;port=33;dbname=cc;user=aa;password=abc",
output: "host=127.0.0.1 port=33 dbname=cc user=aa password=abc sslmode=disable"},
{drv: "mysql",
input: "gdbc:host=127.0.0.1;port=33;dbname=cc;user=aa;password=abc",
output: "aa:abc@tcp(127.0.0.1:33)/cc?autocommit=true&parseTime=true"},
{drv: "mysql",
input: "gdbc:host=127.0.0.1;port=33;dbname=cc;user=aa;password=abc;a1=e1",
output: "aa:abc@tcp(127.0.0.1:33)/cc?autocommit=true&parseTime=true&a1=e1"},
{drv: "mymysql",
input: "gdbc:host=127.0.0.1;port=33;dbname=cc;user=aa;password=abc",
output: "tcp:127.0.0.1:33*cc/aa/abc"},
{drv: "odbc_with_mssql",
input: "gdbc:dsn=tt;user=aa;password=abc",
output: "DSN=tt;UID=aa;PWD=abc"},
{drv: "odbc_with_oracle",
input: "gdbc:dsn=tt;dbname=cc;user=aa;password=abc",
output: "DSN=tt;UID=aa;PWD=abc;dbname=cc"},
{drv: "oci8",
input: "gdbc:tns=tt;user=aa;password=abc",
output: "aa/abc@tt"}} {
out, e := transformUrl(test.drv, test.input)
if nil != e {
t.Errorf("test[%v] is failed, %v", idx, e)
} else if out != test.output {
t.Error("transform failed, excepted is `", test.output, "`, actual is `", out, "`")
}
}
}
func TestDbHandlerParameterIsError(t *testing.T) {
_, e := newDbHandler(nil, map[string]interface{}{})
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "ctx is nil" != e.Error() {
t.Error("excepted error is 'ctx is nil', but actual is", e)
}
_, e = newDbHandler(map[string]interface{}{}, nil)
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "params is nil" != e.Error() {
t.Error("excepted error is 'params is nil', but actual is", e)
}
_, e = newDbHandler(map[string]interface{}{}, map[string]interface{}{})
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "'script' is required" != e.Error() {
t.Error("excepted error is ['script' is required.], but actual is", e)
}
}
func TestDbHandlerConnectError(t *testing.T) {
for _, test := range []struct{ drv, url string }{{drv: "postgres", url: "host=127.0.0.1 port=45 dbname=delayed_test user=delayedtest password=123456 sslmode=disable"},
//{drv: "oci8", url: "gdbc:tns=tt;user=aa;password=abc"},
{drv: "mymysql", url: "gdbc:host=127.0.0.1;port=33;dbname=cc;user=aa;password=abc"},
{drv: "mysql", url: "gdbc:host=127.0.0.1;port=33;dbname=cc;user=aa;password=abc;a1=e1"}} {
handler, e := newDbHandler(map[string]interface{}{}, map[string]interface{}{"script": "a", "drv": test.drv, "url": test.url})
if nil != e {
t.Error(e)
return
}
e = handler.Perform()
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
return
}
switch ToDbType(test.drv) {
case POSTGRESQL, KINGBASE:
if !strings.Contains(e.Error(), "dial tcp") {
t.Error("test[", test.drv, "] excepted error contains [dial tcp], but actual is", e)
}
case MYSQL:
if "mymysql" == test.drv {
if !strings.Contains(e.Error(), "bad connection") {
t.Error("test[", test.drv, "] excepted error contains [bad connection], but actual is", e)
}
} else {
if !strings.Contains(e.Error(), "dial tcp") {
t.Error("test[", test.drv, "] excepted error contains [dial tcp], but actual is", e)
}
}
case ORACLE:
if !strings.Contains(e.Error(), "ORA-12154") {
t.Error("test[", test.drv, "] excepted error contains [ORA-12154], but actual is", e)
}
default:
if !strings.Contains(e.Error(), "dial tcp") {
t.Error("test[", test.drv, "] excepted error contains [dial tcp], but actual is", e)
}
}
}
}
func TestDbHandlerConnectOkAndDbError(t *testing.T) {
handler, e := newDbHandler(map[string]interface{}{}, map[string]interface{}{"script": "a", "drv": "postgres", "url": "host=127.0.0.1 dbname=sssghssssetdata user=delayedtest password=123456 sslmode=disable"})
if nil != e {
t.Error(e)
return
}
e = handler.Perform()
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
return
}
if !strings.Contains(e.Error(), "bad connection") && !strings.Contains(e.Error(), " database \"sssghssssetdata\" does not exist") {
t.Error("excepted error contains [bad connection], but actual is", e)
}
}
func TestDbHandlerAuthError(t *testing.T) {
handler, e := newDbHandler(map[string]interface{}{}, map[string]interface{}{"script": "select 2", "drv": "postgres", "url": "host=127.0.0.1 dbname=delayed_test user=ssdelayedtest password=ww123456 sslmode=disable"})
if nil != e {
t.Error(e)
return
}
e = handler.Perform()
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
return
}
if "windows" == runtime.GOOS {
if !strings.Contains(strings.ToLower(e.Error()), "password") {
t.Error("excepted error contains [Password], but actual is", e)
}
}
}
func dbTest(t *testing.T, cb func(backend *sql.DB)) {
if 0 == len(*test_db_url) {
*test_db_url = GetTestConnURL()
}
if 0 == len(*test_db_drv) {
*test_db_drv = GetTestConnDrv()
}
dbType := ToDbType(*test_db_drv)
drv := *test_db_drv
if strings.HasPrefix(*test_db_drv, "odbc_with_") {
drv = "odbc"
}
db, e := sql.Open(drv, *test_db_url)
if nil != e {
t.Error(i18nString(dbType, drv, e))
return
}
defer db.Close()
switch dbType {
case MSSQL:
script := `
if object_id('dbo.tpt_test_for_handler', 'U') is not null BEGIN DROP TABLE tpt_test_for_handler; END
if object_id('dbo.tpt_test_for_handler', 'U') is null BEGIN CREATE TABLE tpt_test_for_handler (
id INT IDENTITY(1,1) PRIMARY KEY,
priority int DEFAULT 0,
queue varchar(200)
); END`
t.Log("execute sql:", script)
_, e = db.Exec(script)
if nil != e {
t.Error(e)
return
}
case ORACLE, DM:
for _, s := range []string{`BEGIN EXECUTE IMMEDIATE 'DROP TABLE tpt_test_for_handler'; EXCEPTION WHEN OTHERS THEN NULL; END;`,
`CREATE TABLE tpt_test_for_handler(priority int, queue varchar(200))`} {
t.Log("execute sql:", s)
_, e = db.Exec(s)
if nil != e {
msg := i18nString(dbType, drv, e)
if strings.Contains(msg, "ORA-00911") {
t.Skip("skip it becase init db failed with error is ORA-00911")
return
}
t.Error(msg)
return
}
}
default:
for _, s := range []string{`DROP TABLE IF EXISTS tpt_test_for_handler;`,
`CREATE TABLE IF NOT EXISTS tpt_test_for_handler (
id SERIAL PRIMARY KEY,
priority int DEFAULT 0,
queue varchar(200)
)`} {
t.Log("execute sql:", s)
_, e = db.Exec(s)
if nil != e {
t.Error(e)
return
}
}
}
cb(db)
}
func TestDbHandlerScriptError(t *testing.T) {
if 0 == len(*test_db_url) {
*test_db_url = GetTestConnURL()
}
if 0 == len(*test_db_drv) {
*test_db_drv = GetTestConnDrv()
}
handler, e := newDbHandler(map[string]interface{}{}, map[string]interface{}{"drv": *test_db_drv, "url": *test_db_url, "script": "insert aa"})
if nil != e {
t.Error(e)
return
}
e = handler.Perform()
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
return
}
switch ToDbType(*test_db_drv) {
case MSSQL:
if !strings.Contains(e.Error(), "SQLExecute: {42000} [Microsoft]") {
t.Error("excepted error contains [[Microsoft]], but actual is", e)
}
case MYSQL:
if !strings.Contains(e.Error(), "Error 1064:") &&
!strings.Contains(e.Error(), "#1064 error from MySQL server:") {
t.Error("excepted error contains [Error 1064:], but actual is", e)
}
case ORACLE:
if !strings.Contains(e.Error(), "ORA-00925:") {
t.Error("excepted error contains [ORA-00925:], but actual is", e)
}
case DM:
if !strings.Contains(e.Error(), "-2007:") {
t.Error("excepted error contains [-2007:], but actual is", e)
}
case POSTGRESQL, KINGBASE:
if !strings.Contains(e.Error(), "scanner_yyerror") &&
!strings.Contains(e.Error(), "syntax error at or near \"aa\"") {
t.Error("excepted error contains [scanner_yyerror], but actual is", e)
}
default:
if !strings.Contains(e.Error(), "scanner_yyerror") {
t.Error("excepted error contains [scanner_yyerror], but actual is", e)
}
}
}
func assertCount(t *testing.T, db *sql.DB, sql string, excepted int64) {
count := int64(-1)
e := db.QueryRow(sql).Scan(&count)
if nil != e {
t.Error(e)
return
}
if count != excepted {
t.Error("excepted \"", sql, "\" is ", excepted, ", actual is ", count)
}
}
func TestDbHandlerSimple(t *testing.T) {
dbTest(t, func(db *sql.DB) {
handler, e := newDbHandler(map[string]interface{}{}, map[string]interface{}{"drv": *test_db_drv, "url": *test_db_url,
"script": "insert into tpt_test_for_handler(priority, queue) values(12, 'sss')"})
if nil != e {
t.Error(e)
return
}
e = handler.Perform()
if nil != e {
t.Error(e)
return
}
assertCount(t, db, "SELECT count(*) FROM tpt_test_for_handler WHERE priority = 12 and queue = 'sss'", 1)
assertCount(t, db, "SELECT count(*) FROM tpt_test_for_handler", 1)
})
}
func TestDbHandlerMuti(t *testing.T) {
dbTest(t, func(db *sql.DB) {
handler, e := newDbHandler(map[string]interface{}{}, map[string]interface{}{"drv": *test_db_drv, "url": *test_db_url,
"script": `insert into tpt_test_for_handler(priority, queue) values(12, 'sss');
insert into tpt_test_for_handler(priority, queue) values(112, 'aa')`})
if nil != e {
t.Error(e)
return
}
e = handler.Perform()
if nil != e {
t.Error(e)
return
}
assertCount(t, db, "SELECT count(*) FROM tpt_test_for_handler WHERE priority = 12 and queue = 'sss'", 1)
assertCount(t, db, "SELECT count(*) FROM tpt_test_for_handler WHERE priority = 112 and queue = 'aa'", 1)
assertCount(t, db, "SELECT count(*) FROM tpt_test_for_handler", 2)
})
}
func TestDbHandlerArguments(t *testing.T) {
dbTest(t, func(db *sql.DB) {
handler, e := newDbHandler(map[string]interface{}{},
map[string]interface{}{"arguments": map[string]interface{}{"priority1": 23, "queue1": "q1", "priority2": 24, "queue2": "q2"},
"drv": *test_db_drv, "url": *test_db_url,
"script": `insert into tpt_test_for_handler(priority, queue) values({{.priority1}}, '{{.queue1}}');
insert into tpt_test_for_handler(priority, queue) values({{.priority2}}, '{{.queue2}}');`})
if nil != e {
t.Error(e)
return
}
e = handler.Perform()
if nil != e {
t.Error(e)
return
}
assertCount(t, db, "SELECT count(*) FROM tpt_test_for_handler WHERE priority = 23 and queue = 'q1'", 1)
assertCount(t, db, "SELECT count(*) FROM tpt_test_for_handler WHERE priority = 24 and queue = 'q2'", 1)
assertCount(t, db, "SELECT count(*) FROM tpt_test_for_handler", 2)
})
}