-
Notifications
You must be signed in to change notification settings - Fork 39
/
model_test.go
568 lines (476 loc) · 12.4 KB
/
model_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
package gosql
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"strconv"
"testing"
"time"
"github.com/jmoiron/sqlx"
"github.com/ilibs/gosql/v2/internal/example/models"
)
var (
createSchemas = map[string]string{
"moments": `
CREATE TABLE moments (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
user_id int(11) NOT NULL COMMENT '成员ID',
content text NOT NULL COMMENT '日记内容',
comment_total int(11) NOT NULL DEFAULT '0' COMMENT '评论总数',
like_total int(11) NOT NULL DEFAULT '0' COMMENT '点赞数',
status int(11) NOT NULL DEFAULT '1' COMMENT '1 正常 2删除',
created_at datetime NOT NULL COMMENT '创建时间',
updated_at datetime NOT NULL COMMENT '更新时间',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
"users": `
CREATE TABLE users (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL DEFAULT '',
status int(11) NOT NULL,
success_time datetime,
created_at datetime NOT NULL,
updated_at datetime NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
`,
"photos": `
CREATE TABLE photos (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
url varchar(255) NOT NULL DEFAULT '' COMMENT '照片路径',
moment_id int(11) NOT NULL COMMENT '日记ID',
created_at datetime NOT NULL COMMENT '创建时间',
updated_at datetime NOT NULL COMMENT '更新时间',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
`,
}
datas = map[string]string{
"users": `
INSERT INTO users (id,name, status, created_at, updated_at) VALUES
(5,'豆爸&玥爸',1,'2018-11-28 10:29:55','2018-11-28 10:29:55'),
(6,'呵呵',1,'2018-11-28 10:29:55','2018-11-28 10:29:55');`,
"photos": `
INSERT INTO photos (id, url, moment_id, created_at, updated_at)
VALUES
(1,'https://static.fifsky.com/kids/upload/20181128/5febe3b6e23623168cb70eac39d26412.png!blog',1,'2018-11-28 18:15:39','2018-11-28 18:15:39'),
(2,'https://static.fifsky.com/kids/upload/20181128/9c60f42f07d7a0e13293c91fc5740c9d.png!blog',10,'2018-11-28 18:15:39','2018-11-28 18:15:39'),
(3,'https://static.fifsky.com/kids/upload/20181128/458762098fb20128996c9cb21309aa9a.png!blog',1,'2018-11-28 18:31:37','2018-11-28 18:31:37'),
(4,'https://static.fifsky.com/kids/upload/20181128/5b90c5af1bc35375a08cbc990ed662d1.png!blog',14,'2018-11-28 18:31:37','2018-11-28 18:31:37'),
(5,'https://static.fifsky.com/kids/upload/20181128/db190be4184774d88abb31521123b14c.png!blog',14,'2018-11-28 18:31:37','2018-11-28 18:31:37'),
(6,'https://static.fifsky.com/kids/upload/20181128/e1bd15706a79edd1f92f54538622600e.png!blog',14,'2018-11-28 18:31:37','2018-11-28 18:31:37'),
(7,'https://static.fifsky.com/kids/upload/20181128/6bf495726054fa12ae7e6f5d0d4560a4.png!blog',14,'2018-11-28 18:31:37','2018-11-28 18:31:37'),
(8,'https://static.fifsky.com/kids/upload/20181128/e1bd15706a79edd1f92f54538622600e.png!blog',15,'2018-11-28 18:34:45','2018-11-28 18:34:45'),
(9,'https://static.fifsky.com/kids/upload/20181128/c6cc28b912f805b6ef402603e0f67852.png!blog',9,'2018-11-28 18:34:45','2018-11-28 18:34:45'),
(10,'https://static.fifsky.com/kids/upload/20181128/a63a0798d098272a39e76c88f39f2f29.png!blog',16,'2018-11-28 18:35:24','2018-11-28 18:35:24'),
(11,'https://static.fifsky.com/kids/upload/20181128/381de1930d970183ab083fe08e2677ac.png!blog',16,'2018-11-28 18:35:24','2018-11-28 18:35:24');
`,
"moments": `
INSERT INTO moments (id, user_id, content, comment_total, like_total, status, created_at, updated_at)
VALUES
(1,5,'sdfsdfsdfsdfsdf',0,0,1,'2018-11-28 14:04:02','2018-11-28 14:04:02'),
(2,5,'sdfsdfsdfsdfsdf',0,0,1,'2018-11-28 17:14:23','2018-11-28 17:14:23'),
(3,6,'123123123',0,0,1,'2018-11-28 17:19:38','2018-11-28 17:19:38'),
(4,5,'13212312313',0,0,1,'2018-11-28 17:22:25','2018-11-28 17:22:25'),
(5,5,'123123123123',0,0,1,'2018-11-28 17:24:21','2018-11-28 17:24:21'),
(6,6,'131231232345tasvdf',0,0,1,'2018-11-28 17:24:27','2018-11-28 17:24:27'),
(7,5,'1231231231231231',0,0,1,'2018-11-28 18:07:48','2018-11-28 18:07:48'),
(8,5,'1231231231231231',0,0,1,'2018-11-28 18:09:20','2018-11-28 18:09:20'),
(9,6,'1231231231231231',0,0,1,'2018-11-28 18:11:19','2018-11-28 18:11:19'),
(10,5,'1231231231231231',0,0,1,'2018-11-28 18:13:52','2018-11-28 18:13:52'),
(11,5,'1231231231231231',0,0,1,'2018-11-28 18:15:02','2018-11-28 18:15:02'),
(12,5,'1231231231231231',0,0,1,'2018-11-28 18:15:13','2018-11-28 18:15:13'),
(13,5,'1231231231231231',0,0,1,'2018-11-28 18:15:39','2018-11-28 18:15:39'),
(14,6,'开开信息想你',0,0,1,'2018-11-28 18:31:37','2018-11-28 18:31:37'),
(15,5,'网友们已经开始争相给宝宝取名字了',0,0,1,'2018-11-28 18:34:45','2018-11-28 18:34:45'),
(16,6,' B2B事业部的对外报价显示',0,0,1,'2018-11-28 18:35:24','2018-11-28 18:35:24');
`,
}
)
func RunWithSchema(t *testing.T, test func(t *testing.T)) {
var dbs = []*sqlx.DB{
Sqlx(),
Sqlx("db2"),
}
defer func() {
// for k := range createSchemas {
// _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", k))
// if err != nil {
// t.Error(err)
// }
// }
}()
for _, db := range dbs {
for k, v := range createSchemas {
_, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", k))
if err != nil {
t.Error(err)
}
_, err = db.Exec(v)
if err != nil {
t.Fatalf("create schema %s error:%s", k, err)
}
}
}
test(t)
}
func initDatas(t *testing.T) {
db := Sqlx()
db2 := Sqlx("db2")
for k, v := range datas {
udb := db
if k == "photos" {
udb = db2
}
_, err := udb.Exec(v)
if err != nil {
t.Fatalf("init %s data error:%s", k, err)
}
}
}
func insert(id int) {
user := &models.Users{
Id: id,
Name: "test" + strconv.Itoa(id),
Status: 1,
}
_, err := Model(user).Create()
if err != nil {
log.Fatal(err)
}
}
func insertStatus(id int, status int) {
user := &models.Users{
Id: id,
Name: "test" + strconv.Itoa(id),
Status: status,
}
Model(user).Create()
}
func TestBuilder_Get(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
{
user := &models.Users{}
err := Model(user).Where("id = ?", 1).Get()
if err != nil {
t.Error(err)
}
//fmt.Println(user)
}
{
user := &models.Users{
Name: "test1",
Status: 1,
}
err := Model(user).Get()
if err != nil {
t.Error(err)
}
fmt.Println(user)
}
{
insertStatus(2, 0)
user := &models.Users{
Status: 0,
}
err := Model(user).Where("id = ?", 2).Get("status")
if err != nil {
t.Error(err)
}
fmt.Println(user)
}
})
}
func TestBuilder_Hint(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
user := make([]*models.Users, 0)
err := Model(&user).Hint("/*+TDDL:slave()*/").All()
if err != nil {
t.Error(err)
}
fmt.Println(jsonEncode(user))
})
}
func jsonEncode(i interface{}) string {
ret, _ := json.Marshal(i)
return string(ret)
}
func TestBuilder_All(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
user := make([]*models.Users, 0)
err := Model(&user).All()
if err != nil {
t.Error(err)
}
fmt.Println(jsonEncode(user))
})
}
func TestBuilder_Select(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
user := make([]*models.Users, 0)
err := Model(&user).Select("id,name").All()
if err != nil {
t.Error(err)
}
fmt.Println(jsonEncode(user))
})
}
func TestBuilder_InAll(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
insert(3)
insert(4)
insert(5)
user := make([]*models.Users, 0)
err := Model(&user).Where("status = ? and id in(?)", 1, []int{1, 3, 4}).All()
if err != nil {
t.Error(err)
}
fmt.Println(jsonEncode(user))
})
}
func TestBuilder_Update(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
{
user := &models.Users{
Name: "test2",
}
affected, err := Model(user).Where("id=?", 1).Update()
if err != nil {
t.Error("update user error", err)
}
if affected == 0 {
t.Error("update user affected error", err)
}
}
{
user := &models.Users{
Id: 1,
Name: "test3",
}
affected, err := Model(user).Update()
if err != nil {
t.Error("update user error", err)
}
if affected == 0 {
t.Error("update user affected error", err)
}
}
})
}
func TestBuilder_Delete(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
{
insert(1)
affected, err := Model(&models.Users{}).Where("id=?", 1).Delete()
if err != nil {
t.Error("delete user error", err)
}
if affected == 0 {
t.Error("delete user affected error", err)
}
}
{
insert(1)
affected, err := Model(&models.Users{Id: 1}).Delete()
if err != nil {
t.Error("delete user error", err)
}
if affected == 0 {
t.Error("delete user affected error", err)
}
}
{
insertStatus(1, 0)
insertStatus(2, 0)
insertStatus(3, 0)
affected, err := Model(&models.Users{Status: 0}).Delete("status")
if err != nil {
t.Error("delete user error", err)
}
if affected != 3 {
t.Error("delete user affected error", err)
}
}
})
}
func TestBuilder_Count(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
{
num, err := Model(&models.Users{}).Count()
if err != nil {
t.Error(err)
}
if num != 1 {
t.Error("count user error")
}
}
{
insertStatus(2, 0)
insertStatus(3, 0)
num, err := Model(&models.Users{Status: 0}).Count("status")
if err != nil {
t.Error(err)
}
if num != 2 {
t.Error("count user error")
}
}
})
}
func TestBuilder_Create(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
user := &models.Users{
//Id: 1,
Name: "test",
}
id, err := Model(user).Create()
if err != nil {
t.Error(err)
}
if id != 1 {
t.Error("lastInsertId error", id)
}
if int(id) != user.Id {
t.Error("fill primaryKey error", id)
}
})
}
func TestBuilder_Limit(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
insert(3)
user := &models.Users{}
err := Model(user).Limit(1).Get()
if err != nil {
t.Error(err)
}
})
}
func TestBuilder_Offset(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
insert(3)
user := &models.Users{}
err := Model(user).Limit(1).Offset(1).Get()
if err != nil {
t.Error(err)
}
})
}
func TestBuilder_OrderBy(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
insert(3)
user := &models.Users{}
err := Model(user).OrderBy("id desc").Limit(1).Offset(1).Get()
if err != nil {
t.Error(err)
}
if user.Id != 2 {
t.Error("order by error")
}
//fmt.Println(user)
})
}
func TestBuilder_Where(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
insert(3)
user := make([]*models.Users, 0)
err := Model(&user).Where("id in(?,?)", 2, 3).OrderBy("id desc").All()
if err != nil {
t.Error(err)
}
if len(user) != 2 {
t.Error("where error")
}
//fmt.Println(user)
})
}
func TestBuilder_NullString(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
ct, _ := time.Parse("2006-01-02 15:04:05", "2018-09-02 00:00:00")
{
user := &models.Users{
Id: 1,
Name: "test",
Status: 1,
SuccessTime: sql.NullString{
String: "2018-09-03 00:00:00",
Valid: true,
},
CreatedAt: ct,
}
_, err := Model(user).Create()
if err != nil {
log.Fatal(err)
}
}
{
user := &models.Users{}
err := Model(user).Where("id=1").Get()
if err != nil {
t.Error(err)
}
fmt.Println(jsonEncode(user))
}
{
user := &models.Users{
Id: 1,
SuccessTime: sql.NullString{
String: "2018-09-03 00:00:00",
Valid: true,
},
CreatedAt: ct,
}
err := Model(user).Get()
if err != nil {
t.Error(err)
}
fmt.Println(jsonEncode(user))
}
})
}
func TestBuilder_Relation1(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
initDatas(t)
moment := &MomentList{}
err := Model(moment).Relation("User", func(b *Builder) {
b.Where("status = 1")
}).Where("status = 1 and id = ?", 14).Get()
b, _ := json.MarshalIndent(moment, "", " ")
fmt.Println(string(b), err)
if err != nil {
t.Fatal(err)
}
})
}
func TestBuilder_Relation2(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
var moments = make([]*MomentList, 0)
err := Model(&moments).Relation("User", func(b *Builder) {
b.Where("status = 0")
}).Where("status = 1").Limit(10).All()
b, _ := json.MarshalIndent(moments, "", " ")
fmt.Println(string(b), err)
if err != nil {
t.Fatal(err)
}
})
}