-
Notifications
You must be signed in to change notification settings - Fork 4
/
korm_test.go
586 lines (534 loc) · 13 KB
/
korm_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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package korm
import (
"context"
"fmt"
"strconv"
"testing"
"time"
)
var DB_TEST_NAME = "test"
func TestMain(m *testing.M) {
// err := New(SQLITE, DB_TEST_NAME, sqlitedriver.Use())
// if err != nil {
// log.Fatal(err)
// }
// // run tests
// exitCode := m.Run()
// // Cleanup for sqlite , remove file db
// err = os.Remove(DB_TEST_NAME + ".sqlite3")
// if err != nil {
// log.Fatal(err)
// }
// os.Exit(exitCode)
}
type TestUser struct {
Id *uint `korm:"pk"`
Uuid string `korm:"size:40;iunique"`
Email *string `korm:"size:100;iunique"`
Gen string `korm:"size:250;generated: concat(uuid,'working',len(password))"`
Password string
Gen2 int `korm:"generated:len(password)*2"`
IsAdmin *bool
CreatedAt time.Time `korm:"now"`
UpdatedAt time.Time `korm:"update"`
}
type Group struct {
Id uint `korm:"pk"`
Name string
}
type UserNotMigrated struct {
Id uint `korm:"pk"`
Uuid string `korm:"size:40;iunique"`
Email string `korm:"size:100;iunique"`
Password string
IsAdmin bool
CreatedAt time.Time `korm:"now"`
UpdatedAt time.Time `korm:"update"`
}
func TestMigrate(t *testing.T) {
err := AutoMigrate[TestUser]("users")
if err != nil {
t.Error(err)
}
err = AutoMigrate[Group]("groups")
if err != nil {
t.Error(err)
}
}
func TestInsertNonMigrated(t *testing.T) {
_, err := Model[UserNotMigrated]().Insert(&UserNotMigrated{
Uuid: GenerateUUID(),
Email: "user-will-not-work@example.com",
Password: "dqdqd",
IsAdmin: true,
})
if err == nil {
t.Error("TestInsertNonMigrated did not error for not migrated model")
}
}
func TestGetAllTables(t *testing.T) {
tables := GetAllTables(DB_TEST_NAME)
if len(tables) != 2 {
t.Error("GetAllTables not working", tables)
}
}
func TestManyToMany(t *testing.T) {
err := ManyToMany("users", "groups")
if err != nil {
t.Error(err)
}
found := false
for _, t := range GetAllTables() {
if t == "m2m_users_groups" {
found = true
}
}
if !found {
t.Error("m2m_users_groups has not been created:", GetAllTables())
}
}
func TestInsertUsersAndGroups(t *testing.T) {
for i := 0; i < 10; i++ {
iString := strconv.Itoa(i)
email := "user-" + iString + "@example.com"
admin := true
_, err := Model[TestUser]().Insert(&TestUser{
Uuid: GenerateUUID(),
Email: &email,
Password: "dqdqd",
IsAdmin: &admin,
CreatedAt: time.Now(),
})
if err != nil {
t.Error(err)
}
}
_, err := Model[Group]().BulkInsert(&Group{
Name: "admin",
}, &Group{Name: "normal"})
if err != nil {
t.Error(err)
}
_, err = Table("groups").BulkInsert(map[string]any{
"name": "another",
}, map[string]any{
"name": "last",
})
if err != nil {
t.Error(err)
}
}
func TestAddRelatedS(t *testing.T) {
_, err := Model[Group]().Where("name = ?", "admin").AddRelated("users", "id = ?", 1)
if err != nil {
t.Error(err)
}
_, err = Model[Group]().Where("name = ?", "admin").AddRelated("users", "id = ?", 2)
if err != nil {
t.Error(err)
}
}
func TestAddRelatedM(t *testing.T) {
_, err := Table("users").Where("id = ?", 3).AddRelated("groups", "name = ?", "admin")
if err != nil {
t.Error(err)
}
_, err = Table("users").Where("id = ?", 4).AddRelated("groups", "name = ?", "admin")
if err != nil {
t.Error(err)
}
}
func TestDeleteRelatedS(t *testing.T) {
_, err := Model[Group]().Where("name = ?", "admin").DeleteRelated("users", "id = ?", 1)
if err != nil {
t.Error(err)
}
}
func TestDeleteRelatedM(t *testing.T) {
_, err := Table("groups").Where("name = ?", "admin").DeleteRelated("users", "id = ?", 2)
if err != nil {
t.Error(err)
}
}
func TestGetRelatedM(t *testing.T) {
users := []map[string]any{}
err := Table("groups").Where("name", "admin").GetRelated("users", &users)
if err != nil {
t.Error(err)
}
if len(users) != 2 {
t.Error("len(users) != 2 , got: ", users)
}
}
func TestGetRelatedS(t *testing.T) {
users := []TestUser{}
err := Model[Group]().Where("name = ?", "admin").GetRelated("users", &users)
if err != nil {
t.Error(err)
}
if len(users) != 2 {
t.Error("len(users) != 2 , got: ", users)
}
}
func TestConcatANDLen(t *testing.T) {
groupes, err := Model[Group]().Where("name = concat(?,'min') AND len(name) = ?", "ad", 5).All()
// translated to select * from groups WHERE name = 'ad' || 'min' AND length(name) = 5 (sqlite)
// translated to select * from groups WHERE name = concat('ad','min') AND char_length(name) = 5 (postgres, mysql)
if err != nil {
t.Error(err)
}
if len(groupes) != 1 || groupes[0].Name != "admin" {
t.Error("len(groupes) != 1 , got: ", groupes)
}
}
func TestGetRelatedSWithLen(t *testing.T) {
users := []TestUser{}
err := Model[Group]().Where("name = ? AND len(name) = ?", "admin", 5).GetRelated("users", &users)
if err != nil {
t.Error(err)
}
if len(users) != 2 {
t.Error("len(users) != 2 , got: ", users)
}
}
func TestGetRelatedSWithConcatANDLen(t *testing.T) {
users := []TestUser{}
err := Model[Group]().Where("name = concat(?,'min') AND len(name) = ?", "ad", 5).GetRelated("users", &users)
if err != nil {
t.Error(err)
}
if len(users) != 2 {
t.Error("len(users) != 2 , got: ", users)
}
}
func TestGeneratedAs(t *testing.T) {
u, err := Model[TestUser]().Limit(3).All()
if err != nil {
t.Error(err)
}
if len(u) != 3 {
t.Error("len not 20")
}
if (u[0].Gen != u[0].Uuid+"working"+fmt.Sprintf("%d", len(u[0].Password))) || u[0].Gen2 != len(u[0].Password)*2 {
t.Error("generated not working:", u[0].Gen)
}
}
func TestJoinRelatedM(t *testing.T) {
users := []map[string]any{}
err := Table("groups").Where("name = ?", "admin").JoinRelated("users", &users)
if err != nil {
t.Error(err)
}
if len(users) != 2 {
t.Error("len(users) != 2 , got: ", users)
}
}
func TestInsertForeignKeyShouldError(t *testing.T) {
for i := 0; i < 10; i++ {
email := "user-0@example.com"
admin := true
_, err := Model[TestUser]().Insert(&TestUser{
Uuid: GenerateUUID(),
Email: &email,
Password: "dqdqd",
IsAdmin: &admin,
CreatedAt: time.Now(),
})
if err == nil {
t.Error("should error,did not")
}
}
}
func TestInsertM(t *testing.T) {
for i := 10; i < 20; i++ {
iString := strconv.Itoa(i)
_, err := Table("users").Insert(map[string]any{
"uuid": GenerateUUID(),
"email": "user-" + iString + "@example.com",
"password": "dqdqd",
"is_admin": true,
"created_at": time.Now(),
})
if err != nil {
t.Error(err)
}
}
}
func TestGetAll(t *testing.T) {
u, err := Model[TestUser]().All()
if err != nil {
t.Error(err)
}
if len(u) != 20 {
t.Error("len not 20")
}
}
func TestGetAllM(t *testing.T) {
u, err := Table("users").All()
if err != nil {
t.Error(err)
}
if len(u) != 20 {
t.Error("len not 20")
}
}
func TestQuery(t *testing.T) {
u, err := Table("users").QueryM("select * from users")
if err != nil {
t.Error(err)
}
if len(u) != 20 {
t.Error("len not 20")
}
}
func TestMemoryDatabases(t *testing.T) {
dbs := GetMemoryDatabases()
if len(dbs) != 1 {
t.Error("len(dbs) != 1")
}
if dbs[0].Name != DB_TEST_NAME {
t.Error("dbs[0].Name != DB_TEST_NAME:", dbs[0].Name)
}
}
func TestMemoryDatabase(t *testing.T) {
db, err := GetMemoryDatabase(DB_TEST_NAME)
if err != nil {
t.Error(err)
}
if db.Name != DB_TEST_NAME {
t.Error("db.Name != DB_TEST_NAME:", db.Name)
}
}
func TestGetOne(t *testing.T) {
u, err := Model[TestUser]().Where("id = ?", 1).One()
if err != nil {
t.Error(err)
}
if !*u.IsAdmin || *u.Email == "" || u.CreatedAt.IsZero() || u.Uuid == "" {
t.Error("wrong data:", u)
}
}
func TestGetOneM(t *testing.T) {
u, err := Table("users").Where("id = ?", 1).One()
if err != nil {
t.Error(err)
}
if u["is_admin"] != int64(1) || u["email"] == "" || u["uuid"] == "" {
t.Error("wrong data:", u["is_admin"] != int64(1), u["email"] == "", u["uuid"] == "")
}
}
func TestGetOneWithDebug(t *testing.T) {
u, err := Model[TestUser]().Debug().Where("id = ?", 1).One()
if err != nil {
t.Error(err)
}
if !*u.IsAdmin || *u.Email == "" || u.CreatedAt.IsZero() || u.Uuid == "" {
t.Error("wrong data:", u)
}
}
func TestGetOneWithDebugM(t *testing.T) {
u, err := Table("users").Debug().Where("id = ?", 1).One()
if err != nil {
t.Error(err)
}
if u["is_admin"] != int64(1) || u["email"] == "" || u["uuid"] == "" {
t.Error("wrong data:", u["is_admin"] != int64(1), u["email"] == "", u["uuid"] == "")
}
}
func TestOrderBy(t *testing.T) {
u, err := Model[TestUser]().Where("is_admin = ?", true).OrderBy("-id").All()
if err != nil {
t.Error(err)
}
if (len(u) > 1 && *(u[0]).Id < *(u[1]).Id) || !*(u[0]).IsAdmin || *(u[0]).Email == "" || u[0].CreatedAt.IsZero() || u[0].Uuid == "" {
t.Error("wrong data:", u[0], u[0].CreatedAt.IsZero())
}
}
func TestOrderByM(t *testing.T) {
u, err := Table("users").Where("is_admin = ?", true).OrderBy("-id").All()
if err != nil {
t.Error(err)
}
if (len(u) > 1 && u[0]["id"].(int64) < u[1]["id"].(int64)) || u[0]["is_admin"] != int64(1) || u[0]["email"] == "" || u[0]["uuid"] == "" {
t.Error("wrong data:", u[0])
}
}
func TestPagination(t *testing.T) {
u, err := Model[TestUser]().Where("is_admin = ?", true).Limit(5).Page(2).All()
if err != nil {
t.Error(err)
}
if len(u) != 5 || *(u[0]).Id != 6 {
t.Error("wrong data:", u[0])
}
}
func TestPaginationM(t *testing.T) {
u, err := Table("users").Where("is_admin = ?", true).Limit(5).Page(2).All()
if err != nil {
t.Error(err)
}
if len(u) != 5 || u[0]["id"] != int64(6) {
t.Error("wrong data:", u[0])
}
}
func TestWithCtx(t *testing.T) {
u, err := Model[TestUser]().Where("is_admin = ?", true).Context(context.Background()).All()
if err != nil {
t.Error(err)
}
if len(u) != 20 {
t.Error("missing data")
}
}
func TestWithCtxM(t *testing.T) {
u, err := Table("users").Where("is_admin = ?", true).Context(context.Background()).All()
if err != nil {
t.Error(err)
}
if len(u) != 20 {
t.Error("missing data")
}
}
func TestQueryS(t *testing.T) {
u, err := Model[TestUser]().QueryS("select * from users")
if err != nil {
t.Error(err)
}
if len(u) != 20 {
t.Error("missing data")
}
}
func TestQueryM(t *testing.T) {
u, err := Table("users").QueryM("select * from users")
if err != nil {
t.Error(err)
}
if len(u) != 20 {
t.Error("missing data")
}
}
func TestSelect(t *testing.T) {
u, err := Model[TestUser]().Select("email").All()
if err != nil {
t.Error(err)
}
if len(u) != 20 || !u[0].CreatedAt.IsZero() || *u[0].Email == "" || u[0].Password != "" {
t.Error("wrong data:", u[0])
}
}
func TestSelectM(t *testing.T) {
u, err := Table("users").Select("email").All()
if err != nil {
t.Error(err)
}
if len(u) != 20 || len(u[0]) != 1 {
t.Error("wrong data:", u[0])
}
}
func TestDatabase(t *testing.T) {
u, err := Model[TestUser]().Database(DB_TEST_NAME).All()
if err != nil {
t.Error(err)
}
if len(u) != 20 {
t.Error("wrong data len:", len(u))
}
}
func TestDatabaseM(t *testing.T) {
u, err := Table("users").Database(DB_TEST_NAME).All()
if err != nil {
t.Error(err)
}
if len(u) != 20 {
t.Error("wrong data len:", len(u))
}
}
func TestUpdateSet(t *testing.T) {
updatedEmail := "updated@example.com"
is_admin := true
n, err := Model[TestUser]().Where("id = ?", 3).Set("email,is_admin", updatedEmail, &is_admin)
if err != nil {
t.Error(err)
}
if n <= 0 {
t.Error("nothing updated, it should")
}
u, err := Model[TestUser]().Where("id = ?", 3).One()
if err != nil {
t.Error(err)
}
if *u.Email != updatedEmail || !*u.IsAdmin {
t.Errorf("expect %s got %v, bool is %v", updatedEmail, *u.Email, *u.IsAdmin)
}
}
func TestUpdateSetM(t *testing.T) {
updatedEmail := "updated2@example.com"
n, err := Table("users").Where("id = ?", 7).Set("email = ?", updatedEmail)
if err != nil {
t.Error(err)
}
if n <= 0 {
t.Error("nothing updated, it should")
}
u, err := Model[TestUser]().Where("id = ?", 7).One()
if err != nil {
t.Error(err)
}
if *u.Email != updatedEmail {
t.Errorf("expect %s got %v", updatedEmail, u.Email)
}
}
func TestDelete(t *testing.T) {
n, err := Model[TestUser]().Where("id = ?", 12).Delete()
if err != nil {
t.Error(err)
}
if n < 0 {
t.Error("nothing deleted, it should", n)
}
u, err := Model[TestUser]().Where("id = ?", 12).One()
if err == nil {
t.Error("not errored, it should : ", err, u)
}
}
func TestDeleteM(t *testing.T) {
n, err := Table("users").Where("id = ?", 13).Delete()
if err != nil {
t.Error(err)
}
if n < 0 {
t.Error("nothing deleted, it should")
}
_, err = Table("users").Where("id = ?", 12).One()
if err == nil {
t.Error("not errored, it should")
}
}
func TestDropM(t *testing.T) {
_, err := Table("m2m_users_groups").Drop()
if err != nil {
t.Error(err)
}
for _, table := range GetAllTables() {
if table == "m2m_users_groups groups" {
t.Error("m2m_users_groups groups table not dropped", GetAllTables())
}
}
}
func TestDropS(t *testing.T) {
_, err := Model[TestUser]().Drop()
if err != nil {
t.Error(err)
}
for _, table := range GetAllTables() {
if table == "users" {
t.Error("users table not dropped", GetAllTables())
}
}
}
func TestShutdown(t *testing.T) {
err := Shutdown(DB_TEST_NAME)
if err != nil {
t.Error(err)
}
}