-
Notifications
You must be signed in to change notification settings - Fork 2
/
DB_test.go
438 lines (376 loc) · 10.2 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package drugdose
import (
"context"
"database/sql"
"errors"
"fmt"
"os"
"strconv"
"testing"
"time"
)
// Set environment variable GPDNOMARIA to "1" in order to
// skip running all MariaDB tests.
const test_drug string = "test_drug"
const test_route string = "test_route"
const test_units string = "test_units"
const test_user string = "test_user"
const test_source string = "test"
func testWithDrivers() [2]string {
if os.Getenv("GPDNOMARIA") == "1" {
return [2]string{SqliteDriver, ""}
}
return [2]string{SqliteDriver, MysqlDriver}
}
func testUsernames(o int) string {
if o == 0 {
return "same"
} else if o == 1 {
return "different"
}
return ""
}
func useUser(i int, o int) int {
if testUsernames(o) == "same" {
return 0
}
return i
}
func initForTests(dbDriver string) (*sql.DB, context.Context, Config) {
gotsetcfg := InitAllSettings("test", DefaultDBDir, DefaultDBName,
DefaultMySQLAccess, false, false, true, "test")
if dbDriver == "" {
return nil, nil, gotsetcfg
}
gotsetcfg.AutoFetch = false
gotsetcfg.AutoRemove = false
gotsetcfg.UseSource = test_source
gotsetcfg.DBDriver = dbDriver
fmt.Println("\tinitForTests: DBDriver:", gotsetcfg.DBDriver)
ctx := context.Background()
gotsetcfg.InitAllDB(ctx)
db := gotsetcfg.OpenDBConnection(ctx)
err := gotsetcfg.AddToAllNamesTables(db, ctx, false)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var testsub []DrugInfo
tempsub := DrugInfo{
DrugName: test_drug,
DrugRoute: test_route,
DoseUnits: test_units,
}
testsub = append(testsub, tempsub)
errInfo := gotsetcfg.AddToInfoTable(db, ctx, nil, testsub, "")
err = errInfo.Err
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return db, ctx, gotsetcfg
}
func (cfg *Config) cleanAfterTest(db *sql.DB, ctx context.Context) {
err := cfg.CleanInfoTable(db, ctx)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = cfg.CleanNamesTables(db, ctx, true)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func logIsInvalid(ulogs []UserLog, uerr error, temp_doses []float32, temp_users []string, count int) bool {
if uerr != nil || ulogs == nil {
return true
}
found_count := 0
for i := 0; i < count; i++ {
for u := 0; u < len(temp_doses); u++ {
for y := 0; y < len(temp_users); y++ {
if ulogs[i].DrugName == test_drug &&
ulogs[i].DrugRoute == test_route &&
ulogs[i].DoseUnits == test_units &&
ulogs[i].Dose == temp_doses[u] &&
ulogs[i].Username == temp_users[y] {
found_count++
break
}
}
}
}
return found_count != count
}
func genLogDoses() []float32 {
return []float32{1.12, 2.12, 3.12, 4.12, 5.12}
}
func genLogUsers() []string {
var temp_users []string
for i := 0; i < 5; i++ {
temp_users = append(temp_users, test_user+"_"+strconv.Itoa(i))
}
return temp_users
}
func TestConcurrentGetLogs(t *testing.T) {
fmt.Println("\t---Starting TestConcurrentGetLogs()")
for _, v := range testWithDrivers() {
db, ctx, cfg := initForTests(v)
if db == nil {
return
}
defer db.Close()
temp_doses := genLogDoses()
temp_users := genLogUsers()
// Test valid logs
for o := 0; o < 2; o++ {
fmt.Println("\t=== Testing", testUsernames(o), "usernames ===")
synct := SyncTimestamps{}
errorChannel := make(chan ErrorInfo)
count := 0
for count < 5 {
go cfg.AddToDoseTable(db, ctx, errorChannel, &synct, temp_users[useUser(count, o)], test_drug,
test_route, temp_doses[count], test_units, 0, 0, "", true)
gotErrInfo := <-errorChannel
if gotErrInfo.Err != nil {
fmt.Println("\tFailed adding to database.")
t.Log(gotErrInfo.Err)
t.Fail()
break
}
count++
}
userLogsErrChan := make(chan UserLogsError)
for i := 0; i < count; i++ {
go cfg.GetLogs(db, ctx, userLogsErrChan, count,
0, temp_users[useUser(i, o)], true, "", "")
}
for i := 0; i < count; i++ {
gotUserLogsErr := <-userLogsErrChan
gotLog := gotUserLogsErr.UserLogs
gotErr := gotUserLogsErr.Err
snd_count := count
if testUsernames(o) == "different" {
snd_count = 1
}
if logIsInvalid(gotLog, gotErr, temp_doses, temp_users, snd_count) {
fmt.Println("\tFailed reading database.")
t.Log("Didn't read database properly concurrently, breaking. ; err:", gotErr)
t.Fail()
break
}
}
for i := 0; i < count; i++ {
go cfg.RemoveLogs(db, ctx, errorChannel, temp_users[useUser(i, o)], 1, true, 0, "", "")
gotErrInfo := <-errorChannel
if gotErrInfo.Err != nil {
fmt.Println(gotErrInfo.Err)
}
}
}
// Test invalid logs
userLogsErrChan := make(chan UserLogsError)
for i := 0; i < 5; i++ {
go cfg.GetLogs(db, ctx, userLogsErrChan, 1,
0, "W2IK&m9)abN8*(x9Ms90mMm", true, "", "")
}
for i := 0; i < 5; i++ {
gotUserLogsErr := <-userLogsErrChan
gotLog := gotUserLogsErr.UserLogs
gotErr := gotUserLogsErr.Err
if gotErr != nil {
fmt.Println("Testing invalid username:", gotErr, "; gotLog:", gotLog)
} else if gotErr == nil {
t.Log("getErr is nil, when it should've returned an error")
t.Fail()
}
}
cfg.cleanAfterTest(db, ctx)
}
}
func TestConcurrentAddToDoseTable(t *testing.T) {
fmt.Println("\t---Starting TestConcurrentAddToDoseTable()")
for _, v := range testWithDrivers() {
db, ctx, cfg := initForTests(v)
if db == nil {
return
}
defer db.Close()
temp_doses := genLogDoses()
temp_users := genLogUsers()
// Test valid logs
for o := 0; o < 2; o++ {
fmt.Println("\t=== Testing", testUsernames(o), "usernames ===")
synct := SyncTimestamps{}
errorChannel := make(chan ErrorInfo)
for i := 0; i < 5; i++ {
go cfg.AddToDoseTable(db, ctx, errorChannel, &synct, temp_users[useUser(i, o)], test_drug,
test_route, temp_doses[i], test_units, 0, 0, "", true)
}
count := 0
for count < 5 {
gotErrInfo := <-errorChannel
if gotErrInfo.Err != nil {
fmt.Println("\tFailed adding to database.")
t.Log(gotErrInfo.Err)
t.Fail()
break
}
count++
}
userLogsErrChan := make(chan UserLogsError)
for i := 0; i < count; i++ {
go cfg.GetLogs(db, ctx, userLogsErrChan, count,
0, temp_users[useUser(i, o)], true, "", "")
gotUserLogsErr := <-userLogsErrChan
gotLog := gotUserLogsErr.UserLogs
gotErr := gotUserLogsErr.Err
snd_count := count
if testUsernames(o) == "different" {
snd_count = 1
}
if logIsInvalid(gotLog, gotErr, temp_doses, temp_users, snd_count) {
fmt.Println("\tFailed reading database.")
t.Log("Didn't read database properly concurrently, breaking. ; err:", gotErr)
t.Fail()
break
}
}
for i := 0; i < count; i++ {
go cfg.RemoveLogs(db, ctx, errorChannel, temp_users[useUser(i, o)], 1, true, 0, "", "")
gotErrInfo := <-errorChannel
if gotErrInfo.Err != nil {
fmt.Println(gotErrInfo.Err)
}
}
}
// Test invalid logs
synct := SyncTimestamps{}
errorChannel := make(chan ErrorInfo)
for i := 0; i < 5; i++ {
go cfg.AddToDoseTable(db, ctx, errorChannel, &synct, "test_user", "W2IK&m9)abN\"8*(x9Ms90mMm",
"W2IK&m9)abN\"8*(x9Ms90mMm", 123.12, "W2IK&m9)abN\"8*(x9Ms90mMm", 0, 0, "", true)
}
for i := 0; i < 5; i++ {
gotErrInfo := <-errorChannel
if gotErrInfo.Err != nil {
fmt.Println("Testing invalid input:", gotErrInfo.Err)
} else if gotErrInfo.Err == nil {
t.Log("getErrInfo is nil, when it should've returned an error")
t.Fail()
}
}
cfg.cleanAfterTest(db, ctx)
}
}
func TestUseConfigTimeout(t *testing.T) {
fmt.Println("\t---Starting TestUseConfigTimeout()")
db, ctx, cfg := initForTests(SqliteDriver)
if db == nil {
return
}
defer db.Close()
cfg.Timeout = "1s"
ctx2, cancel, err := cfg.UseConfigTimeout()
defer cancel()
if err != nil {
cfg.cleanAfterTest(db, ctx)
t.Fatal(err)
}
time.Sleep(3 * time.Second)
userLogsErrChan := make(chan UserLogsError)
go cfg.GetLogs(db, ctx2, userLogsErrChan, 1, 0, test_user,
false, "", "")
gotUserLogsErr := <-userLogsErrChan
gotErr := gotUserLogsErr.Err
if gotErr != nil && errors.Is(gotErr, context.DeadlineExceeded) == false {
t.Log("Got wrong error message:", gotErr)
t.Fail()
}
if gotErr == nil {
t.Log("There should've been an error, but there is none:", gotErr)
t.Fail()
}
fmt.Println(gotErr)
cfg.cleanAfterTest(db, ctx)
}
func TestForcedRollback(t *testing.T) {
fmt.Println("\t---Starting TestForcedRollback()")
for _, v := range testWithDrivers() {
db, ctx, cfg := initForTests(v)
if db == nil {
return
}
defer db.Close()
tx, err := db.BeginTx(ctx, nil)
if err != nil {
cfg.cleanAfterTest(db, ctx)
t.Fatal(err)
}
err, set := settingsTables(settingTypeID)
if err != nil {
cfg.cleanAfterTest(db, ctx)
t.Fatal(err)
}
stmtStr := returnSetUserSetStmt(set)
err = errors.New("Test error.")
errChannel := make(chan ErrorInfo)
printN := "testNamed"
tempErrInfo := ErrorInfo{
Err: nil,
Action: "testing",
Username: "testing",
}
fmt.Println("\tConcurrent rollback")
stmt, _ := tx.Prepare(stmtStr)
defer stmt.Close()
var testConcurrentRollback = func(err error, tx *sql.Tx,
errChannel chan ErrorInfo, errInfo ErrorInfo, printN string,
xtra string, t *testing.T) {
if handleErrRollback(err, tx, errChannel, &errInfo, printN, xtra) {
fmt.Println("handleErrRollback():", err)
} else {
t.Log("handleErrRollback():", err)
t.Fail()
}
}
go testConcurrentRollback(err, tx, errChannel, tempErrInfo,
printN, "tx.Prepare(): ", t)
gotErrInfo := <-errChannel
fmt.Println("errChannel:", gotErrInfo.Err)
err = tx.Commit()
if err != nil {
fmt.Println("tx.Commit():", err)
} else {
t.Log("forced rollback err shouldn't have been nil: tx.Commit():", err)
t.Fail()
}
// End of concurrent rollback
fmt.Println("\tSequential rollback")
tx, err = db.BeginTx(ctx, nil)
if err != nil {
cfg.cleanAfterTest(db, ctx)
t.Fatal(err)
}
err = errors.New("Test error.")
stmt, _ = tx.Prepare(stmtStr)
err = handleErrRollbackSeq(err, tx, printN, "tx.Prepare(): ")
if err != nil {
fmt.Println("handleErrRollbackSeq():", err)
} else {
t.Log("handleErrRollbackSeq():", err)
t.Fail()
}
defer stmt.Close()
err = tx.Commit()
if err != nil {
fmt.Println("tx.Commit():", err)
} else {
t.Log("tx.Commit():", err)
t.Fail()
}
// End of sequential rollback
cfg.cleanAfterTest(db, ctx)
}
}