-
Notifications
You must be signed in to change notification settings - Fork 222
/
xorm_test.go
765 lines (645 loc) · 24 KB
/
xorm_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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
package xorm
import (
"fmt"
"log"
"reflect"
"testing"
"time"
"github.com/xormplus/xorm"
_ "github.com/lib/pq"
)
type Article struct {
Id int `xorm:"not null pk autoincr unique INTEGER"`
Content string `xorm:"not null TEXT"`
Title string `xorm:"not null VARCHAR(255)"`
Categorysubid int `xorm:"not null INTEGER"`
Remark string `xorm:"not null VARCHAR(2555)"`
Userid int `xorm:"not null INTEGER"`
Viewcount int `xorm:"not null default 0 INTEGER"`
Replycount int `xorm:"not null default 0 INTEGER"`
Tags string `xorm:"not null VARCHAR(300)"`
Createdatetime JSONTime `xorm:"not null default 'now()' DATETIME"`
Isdraft int `xorm:"SMALLINT"`
Lastupdatetime time.Time `xorm:"not null default 'now()' DATETIME"`
}
type Category struct {
Id int `xorm:"not null pk autoincr unique INTEGER"`
Name string `xorm:"not null VARCHAR(200)"`
Counts int `xorm:"not null default 0 INTEGER"`
Orders int `xorm:"not null default 0 INTEGER"`
Createtime time.Time `xorm:"not null default 'now()' created DATETIME"`
Pid int `xorm:"not null default 0 INTEGER"`
Lastupdatetime time.Time `xorm:"not null default 'now()' updated DATETIME"`
Status int `xorm:"not null default 1 SMALLINT"`
}
type JSONTime time.Time
func (t JSONTime) MarshalJSON() ([]byte, error) {
//do your serializing here
stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format("2006/01/08 15:04:05"))
return []byte(stamp), nil
}
var db *xorm.Engine
func Test_InitDB(t *testing.T) {
var err error
db, err = xorm.NewPostgreSQL("postgres://postgres:root@localhost:5432/mblog?sslmode=disable")
if err != nil {
t.Fatal(err)
}
err = db.RegisterSqlMap(xorm.Xml("./sql/oracle", ".xml"))
if err != nil {
t.Fatal(err)
}
err = db.RegisterSqlMap(xorm.Json("./sql/oracle", ".json"))
if err != nil {
t.Fatal(err)
}
err = db.RegisterSqlTemplate(xorm.Default("./sql/oracle", ".tpl"))
if err != nil {
t.Fatal(err)
}
err = db.StartFSWatcher()
if err != nil {
t.Fatal(err)
}
db.ShowSQL(true)
log.Println(db)
// db.NewSession().SqlMapClient().Execute()
log.Println(db.GetSqlMap("json_category-16-17"))
}
func Test_Get_Struct(t *testing.T) {
var article Article
has, err := db.Id(2).Get(&article)
if err != nil {
t.Fatal(err)
}
if !has {
t.Log("[Test_Get_Struct]->rows: not exist\n")
}
t.Log("[Test_Get_Struct]->rows:\n", article)
}
func Test_Sql_Get_Struct(t *testing.T) {
var article Article
has, err := db.Sql("select * from article where id=?", 2).Get(&article)
if err != nil {
t.Fatal(err)
}
if !has {
t.Log("[Test_Sql_Get_Struct]->rows: not exist\n")
}
t.Log("[Test_Sql_Get_Struct]->rows:\n", article)
}
func Test_Sql_Get_String(t *testing.T) {
var title string
has, err := db.Sql("select title from article where id=?", 2).Get(&title)
if err != nil {
t.Fatal(err)
}
if !has {
t.Log("[Test_Sql_Get_String]->rows: not exist\n")
}
t.Log("[Test_Sql_Get_String]->titles:\n", title)
}
func Test_Sql_Get_Int(t *testing.T) {
var id int
has, err := db.Sql("select id from article where id=?", 2).Get(&id)
if err != nil {
t.Fatal(err)
}
if !has {
t.Log("[Test_Sql_Get_Int]->rows: not exist\n")
}
t.Log("[Test_Sql_Get_Int]->id:\n", id)
}
func Test_Sql_Get_Map(t *testing.T) {
var valuesMap = make(map[string]string)
has, err := db.Sql("select * from article where id=?", 2).Get(&valuesMap)
if err != nil {
t.Fatal(err)
}
if !has {
t.Log("[Test_Sql_Get_Map]->rows: not exist\n")
}
t.Log("[Test_Sql_Get_Map]->id:\n", valuesMap)
}
func Test_Sql_Get_Map2(t *testing.T) {
var valuesMap = make(map[string]interface{})
has, err := db.Sql("select * from article where id=?", 2).Get(&valuesMap)
if err != nil {
t.Fatal(err)
}
if !has {
t.Log("[Test_Sql_Get_Map2]->rows: not exist\n")
}
t.Log("[Test_Sql_Get_Map2]->id:\n", valuesMap)
id, ok := valuesMap["id"].(int64)
if ok {
t.Log("[Test_Sql_Get_Map2]->id:\n", id)
}
}
func Test_Sql_Get_Slice(t *testing.T) {
var valuesSlice = make([]interface{}, 2)
has, err := db.Sql("select id,title from article where id=?", 2).Get(&valuesSlice)
if err != nil {
t.Fatal(err)
}
if !has {
t.Log("[Test_Sql_Get_Slice]->rows: not exist\n")
}
t.Log("[Test_Sql_Get_Slice]->id:\n", valuesSlice)
}
func Test_GetFirst_Json(t *testing.T) {
var article Article
has, rows, err := db.Id(2).
GetFirst(&article).
Json()
if err != nil {
t.Fatal(err)
}
if !has {
t.Log("[Test_GetFirst_Json]->rows: not exist\n")
}
t.Log("[Test_GetFirst_Json]->rows:\n" + rows)
}
func Test_GetFirst_Xml(t *testing.T) {
var article Article
has, rows, err := db.Where("userid =?", 2).GetFirst(&article).Xml()
if err != nil {
t.Fatal(err)
}
if !has {
t.Log("[Test_GetFirst_Xml]->rows: not exist\n")
}
t.Log("[Test_GetFirst_Xml]->rows:\n" + rows)
}
func Test_GetFirst_XmlIndent(t *testing.T) {
var article Article
has, rows, err := db.Where("userid =?", 2).GetFirst(&article).XmlIndent("", " ", "article")
if err != nil {
t.Fatal(err)
}
if !has {
t.Log("[Test_GetFirst_XmlIndent]->rows: not exist\n")
}
t.Log("[Test_GetFirst_XmlIndent]->rows:\n" + rows)
}
func Test_Search(t *testing.T) {
var article []Article
result := db.Sql("select id,title,createdatetime,content from article where id = ?", 25).Search(&article)
if result.Error != nil {
t.Fatal(result.Error)
}
t.Log("[Test_Find]->article[0].Id:\n", article[0].Id)
t.Log("[Test_Find]->article[0].Content:\n", article[0].Content)
t.Log("[Test_Find]->article[0].Title:\n", article[0].Title)
t.Log("[Test_Find]->article[0].Categorysubid:\n", article[0].Categorysubid)
t.Log("[Test_Find]->article[0].Createdatetime:\n", article[0].Createdatetime)
t.Log("[Test_Find]->article[0].Isdraft:\n", article[0].Isdraft)
t.Log("[Test_Find]->article[0].Lastupdatetime:\n", article[0].Lastupdatetime)
t.Log("[Test_Find]->article[0].Remark:\n", article[0].Remark)
t.Log("[Test_Find]->article[0].Replycount:\n", article[0].Replycount)
t.Log("[Test_Find]->article[0].Tags:\n", article[0].Tags)
t.Log("[Test_Find]->article[0].Userid:\n", article[0].Userid)
t.Log("[Test_Find]->article[0].Viewcount:\n", article[0].Viewcount)
t.Log("[Test_Find]-> result.Result:\n", result.Result)
resultJson, err := result.Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Search]-> result.Json():\n", resultJson)
}
func Test_Session_Search2(t *testing.T) {
session := db.NewSession()
defer session.Close()
var article []Article
result := session.Sql("select id,title,createdatetime,content from article where id = ?", 25).Search(&article)
if result.Error != nil {
t.Fatal(result.Error)
}
resultJson, err := result.Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Search]-> result.Json():\n", resultJson)
}
func Test_Session_Search(t *testing.T) {
session := db.NewSession()
defer session.Close()
var article []Article
result := session.Sql("select id,title,createdatetime,content from article where id = ?id", &map[string]interface{}{"id": 25}).Search(&article)
if result.Error != nil {
t.Fatal(result.Error)
}
resultJson, err := result.Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Search]-> result.Json():\n", resultJson)
}
func Test_Session_SqlMapClient_Search(t *testing.T) {
session := db.NewSession()
defer session.Close()
var article []Article
paramMap := map[string]interface{}{"1": 2, "2": 5}
result := session.SqlMapClient("selectAllArticle", ¶mMap).Search(&article)
if result.Error != nil {
t.Fatal(result.Error)
}
t.Log("[Test_SqlMapClient_QueryByParamMap_Xml]->rows:\n", result)
t.Log("[Test_SqlMapClient_QueryByParamMap_Xml]->article:\n", article)
}
func Test_Query_Json(t *testing.T) {
rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 139).Query().Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Query_Json]->rows:\n" + rows)
}
func Test_Query_Result(t *testing.T) {
rows := db.Sql("select id,title,createdatetime,content from article where id = ?", 139).Query()
if rows.Error != nil {
t.Fatal(rows.Error)
}
t.Log("[Test_Query_Result]->rows[0][\"id\"]:\n", rows.Results[0]["id"])
t.Log("[Test_Query_Result]->reflect.TypeOf(rows.Result[0][\"id\"]):\n", reflect.TypeOf(rows.Results[0]["id"]))
t.Log("[Test_Query_Result]->rows[0][\"title\"]:\n", rows.Results[0]["title"])
t.Log("[Test_Query_Result]->reflect.TypeOf(rows.Result[0][\"title\"]):\n", reflect.TypeOf(rows.Results[0]["title"]))
t.Log("[Test_Query_Result]->rows[0][\"createdatetime\"]:\n", rows.Results[0]["createdatetime"])
t.Log("[Test_Query_Result]->reflect.TypeOf(rows.Result[0][\"createdatetime\"]):\n", reflect.TypeOf(rows.Results[0]["createdatetime"]))
}
func Test_Query_Xml(t *testing.T) {
rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 139).Query().Xml()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Query_Xml]->rows:\n" + rows)
}
func Test_Query_XmlIndent(t *testing.T) {
rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 139).Query().XmlIndent("", " ", "article")
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Query_XmlIndent]->rows:\n" + rows)
}
func Test_QueryWithDateFormat_Json(t *testing.T) {
rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 139).QueryWithDateFormat("20060102").Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_QueryWithDateFormat_Json]->rows:\n" + rows)
}
func Test_QueryWithDateFormat_Xml(t *testing.T) {
rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 33).QueryWithDateFormat("20060102").Xml()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_QueryWithDateFormat_Xml]->rows:\n" + rows)
}
func Test_QueryWithDateFormat_XmlIndent(t *testing.T) {
rows, err := db.Sql("select id,title,createdatetime,content from article where id in (?,?)", 139, 33).QueryWithDateFormat("20060102").XmlIndent("", " ", "article")
if err != nil {
t.Fatal(err)
}
t.Log("[Test_QueryWithDateFormat_XmlIndent]->rows:\n" + rows)
}
func Test_QueryByParamMap_Json(t *testing.T) {
paramMap := map[string]interface{}{"id": 32, "userid": 1}
rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", ¶mMap).Query().Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_QueryByParamMap_Json]->rows:\n" + rows)
}
func Test_QueryByParamMap_Xml(t *testing.T) {
paramMap := map[string]interface{}{"id": 6, "userid": 1}
rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", ¶mMap).Query().Xml()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_QueryByParamMap_Xml]->rows:\n" + rows)
}
func Test_QueryByParamMap_XmlIndent(t *testing.T) {
paramMap := map[string]interface{}{"id": 6, "userid": 1}
rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", ¶mMap).Query().XmlIndent("", " ", "article")
if err != nil {
t.Fatal(err)
}
t.Log("[Test_QueryByParamMap_XmlIndent]->rows:\n" + rows)
}
func Test_QueryByParamMapWithDateFormat_XmlIndent(t *testing.T) {
paramMap := map[string]interface{}{"id": 6, "userid": 1}
rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", ¶mMap).QueryWithDateFormat("2006/01/02").XmlIndent("", " ", "article")
if err != nil {
t.Fatal(err)
}
t.Log("[Test_QueryByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
}
func Test_SqlMapClient_QueryByParamMap_Json(t *testing.T) {
paramMap := map[string]interface{}{"1": 14, "2": 139}
rows, err := db.SqlMapClient("json_selectAllArticle", ¶mMap).Query().Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlMapClient_QueryByParamMap_Json]->rows:\n" + rows)
}
func Test_SqlMapClient_QueryByParamMapWithDateFormat_Json(t *testing.T) {
paramMap := map[string]interface{}{"1": 14, "2": 139}
rows, err := db.SqlMapClient("json_selectAllArticle", ¶mMap).QueryWithDateFormat("2006-01-02 15:04").Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlMapClient_QueryByParamMapWithDateFormat_Json]->rows:\n" + rows)
}
func Test_SqlMapClient_QueryByParamMap_Xml(t *testing.T) {
paramMap := map[string]interface{}{"1": 14, "2": 139}
rows, err := db.SqlMapClient("json_selectAllArticle", ¶mMap).Query().Xml()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlMapClient_QueryByParamMap_Xml]->rows:\n" + rows)
}
func Test_SqlMapClient_QueryByParamMapWithDateFormat_Xml(t *testing.T) {
paramMap := map[string]interface{}{"1": 14, "2": 139}
rows, err := db.SqlMapClient("json_selectAllArticle", ¶mMap).QueryWithDateFormat("2006-01-02 15:04").Xml()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlMapClient_QueryByParamMapWithDateFormat_Xml]->rows:\n" + rows)
}
func Test_SqlMapClient_QueryByParamMap_XmlIndent(t *testing.T) {
paramMap := map[string]interface{}{"1": 14, "2": 139}
rows, err := db.SqlMapClient("json_selectAllArticle", ¶mMap).Query().XmlIndent("", " ", "article")
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlMapClient_QueryByParamMap_XmlIndent]->rows:\n" + rows)
}
func Test_SqlMapClient_QueryByParamMapWithDateFormat_XmlIndent(t *testing.T) {
paramMap := map[string]interface{}{"1": 14, "2": 139}
rows, err := db.SqlMapClient("json_selectAllArticle", ¶mMap).QueryWithDateFormat("2006-01-02 15:04").XmlIndent("", " ", "article")
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlMapClient_QueryByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
}
func Test_SqlTemplateClient_QueryByParamMap_Json(t *testing.T) {
paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
rows, err := db.SqlTemplateClient("select.example.tpl", ¶mMap).Query().Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlTemplateClient_QueryByParamMap_Json]->rows:\n" + rows)
}
func Test_Session_SqlTemplateClient_QueryByParamMap_Json(t *testing.T) {
session := db.NewSession()
defer session.Close()
paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
rows, err := session.SqlTemplateClient("select.example.tpl", ¶mMap).Query().Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlTemplateClient_QueryByParamMap_Json]->rows:\n" + rows)
}
func Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Json(t *testing.T) {
paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
rows, err := db.SqlTemplateClient("select.example.tpl", ¶mMap).QueryWithDateFormat("01/02/2006").Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Json]->rows:\n" + rows)
}
func Test_SqlTemplateClient_QueryByParamMap_Xml(t *testing.T) {
paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
rows, err := db.SqlTemplateClient("select.example.tpl", ¶mMap).Query().Xml()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlTemplateClient_QueryByParamMap_Xml]->rows:\n" + rows)
}
func Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Xml(t *testing.T) {
paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
rows, err := db.SqlTemplateClient("select.example.tpl", ¶mMap).QueryWithDateFormat("01/02/2006").Xml()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Xml]->rows:\n" + rows)
}
func Test_SqlTemplateClient_QueryByParamMap_XmlIndent(t *testing.T) {
paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
rows, err := db.SqlTemplateClient("select.example.tpl", ¶mMap).Query().XmlIndent("", " ", "article")
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlTemplateClient_QueryByParamMap_XmlIndent]->rows:\n" + rows)
}
func Test_SqlTemplateClient_QueryByParamMapWithDateFormat_XmlIndent(t *testing.T) {
paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
rows, err := db.SqlTemplateClient("select.example.stpl", ¶mMap).QueryWithDateFormat("01/02/2006").XmlIndent("", " ", "article")
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlTemplateClient_QueryByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
}
func Test_Where_Search_Structs_Json(t *testing.T) {
var articles []Article
json, err := db.Where("id=?", 6).Search(&articles).Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Where_Search_Structs_Json]->rows:\n" + json)
}
func Test_Search_Structs_Xml(t *testing.T) {
var articles []Article
xml, err := db.Where("id=?", 6).Search(&articles).Xml()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Search_Structs_Xml]->rows:\n" + xml)
}
func Test_Search_Structs_XmlIndent(t *testing.T) {
var articles []Article
xml, err := db.Where("id=?", 6).Search(&articles).XmlIndent("", " ", "Article")
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Search_Structs_XmlIndent]->rows:\n" + xml)
}
func Test_Search_Structs_Json(t *testing.T) {
var categories []Category
Json, err := db.Select("id").Search(&categories).Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Search_Structs_Json]->rows:\n", Json)
}
func Test_Sql_Find_Structs(t *testing.T) {
var categories2 []Category
err := db.Sql("select * from category where id =?", 16).Find(&categories2)
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Sql_Find_Structs]->rows:\n", categories2)
}
func Test_SqlMapClient_Find_Structs(t *testing.T) {
var categories2 []Category
db.AddSql("1", "select * from category where id =?")
err := db.SqlMapClient("1", 16).Find(&categories2)
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlMapClient_Find_Structs]->rows:\n", categories2)
}
func Test_SqlTemplateClient_Find_Structs(t *testing.T) {
var categories2 []Category
db.AddSqlTemplate("1", "select * from category where id =?id")
err := db.SqlTemplateClient("1", &map[string]interface{}{"id": 25}).Find(&categories2)
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlTemplateClient_Find_Structs]->rows:\n", categories2)
}
func Test_Session_SqlTemplateClient_Find_Structs(t *testing.T) {
session := db.NewSession()
defer session.Close()
var categories2 []Category
db.AddSqlTemplate("1", "select * from category where id =?id")
err := session.SqlTemplateClient("1", &map[string]interface{}{"id": 25}).Find(&categories2)
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlTemplateClient_Find_Structs]->rows:\n", categories2)
}
func Test_Sql_Search_Json(t *testing.T) {
var categories2 []Category
json, err := db.Sql("select * from category where id =?", 16).Search(&categories2).Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Sql_Search_Json]->rows:\n", json)
}
func Test_SqlMapClient_Search_Json(t *testing.T) {
var categories2 []Category
db.AddSql("1", "select * from category where id =?")
json, err := db.SqlMapClient("1", 16).Search(&categories2).Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlMapClient_Search_Json]->rows:\n", json)
}
func Test_SqlTemplateClient_Search_Json(t *testing.T) {
var categories2 []Category
db.AddSqlTemplate("1", "select * from category where id =?id")
json, err := db.SqlTemplateClient("1", &map[string]interface{}{"id": 25}).Search(&categories2).Json()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlTemplateClient_Search_Json]->rows:\n", json)
}
func Test_Query(t *testing.T) {
result, err := db.QueryInterface("select * from category where id =25")
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Query]->rows:\n", result)
}
func STest_Sql_Execute(t *testing.T) {
result, err := db.Sql("INSERT INTO categories VALUES (?, ?, ?, ?, ?)", 148, "xiaozhang", 1, 1, 1).Execute()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_Sql_Execute]->rows:\n", result)
}
func STest_SqlMapClient_Execute(t *testing.T) {
db.AddSql("Test_SqlMapClient_Execute", "INSERT INTO categories VALUES (?id, ?name, ?counts, ?orders, ?pid)")
result, err := db.SqlMapClient("Test_SqlMapClient_Execute", &map[string]interface{}{"id": 149, "name": "xiaowang", "counts": 1, "orders": 1, "pid": 1}).Execute()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlMapClient_Execute]->rows:\n", result)
}
func STest_SqlTemplateClientt_Execute(t *testing.T) {
db.AddSqlTemplate("Test_SqlTemplateClientt_Execute", "INSERT INTO categories VALUES (?id, ?name, ?counts, ?orders, ?pid)")
result, err := db.SqlTemplateClient("Test_SqlTemplateClientt_Execute", &map[string]interface{}{"id": 240, "name": "laowang", "counts": 1, "orders": 1, "pid": 1}).Execute()
if err != nil {
t.Fatal(err)
}
t.Log("[Test_SqlTemplateClientt_Execute]->rows:\n", result)
}
func Test_GetSQL(t *testing.T) {
db.AddSql("Test_GetSQL_1", "select * from Test_GetSQL_1")
t.Log("[Test_GetSQL]->Test_GetSQL_1:\n", db.GetSql("Test_GetSQL_1"))
t.Log("[Test_GetSQL]->Test_GetSQL_2:\n", db.GetSql("Test_GetSQL_1"))
}
func Test_GetSqlMap(t *testing.T) {
t.Log("[Test_GetSqlMap]->3:\n")
t.Log(xorm.JSONString(db.GetSqlMap(3), true))
sqlmap := db.GetSqlMap(3)
t.Log("[Test_GetSqlMap]->len(sqlmap):\n", len(sqlmap))
db.AddSql("Test_GetSqlMap_1", "select * from Test_GetSqlMap_1")
db.AddSql("Test_GetSqlMap_2", "select * from Test_GetSqlMap_2")
db.AddSql("Test_GetSqlMap_3", "select * from Test_GetSqlMap_3")
db.AddSql("Test_GetSqlMap_4", "select * from Test_GetSqlMap_4")
db.AddSql("Test_GetSqlMap_5", "select * from Test_GetSqlMap_5")
t.Log("[Test_GetSqlMap]->init->3:\n")
t.Log(xorm.JSONString(db.GetSqlMap(3), true))
sqlmap = db.GetSqlMap(3)
t.Log("[Test_GetSqlMap]->init->len(sqlmap):\n", len(sqlmap))
t.Log("[Test_GetSqlMap]->Test_GetSqlMap_null:\n")
t.Log(xorm.JSONString(db.GetSqlMap("Test_GetSqlMap_null"), true))
t.Log("[Test_GetSqlMap]->Test_GetSqlMap_1:\n")
t.Log(xorm.JSONString(db.GetSqlMap("Test_GetSqlMap_1"), true))
t.Log("[Test_GetSqlMap]->Test_GetSqlMap_1,Test_GetSqlMap_3:\n")
t.Log(xorm.JSONString(db.GetSqlMap("Test_GetSqlMap_1", "Test_GetSqlMap_3"), true))
t.Log("[Test_GetSqlMap]->Test_GetSqlMap_1,Test_GetSqlMap_3,3:\n")
t.Log(xorm.JSONString(db.GetSqlMap("Test_GetSqlMap_1", "Test_GetSqlMap_3", 3), true))
t.Log("[Test_GetSqlMap]->Test_GetSqlMap_1,Test_GetSqlMap_3,3,Test_GetSqlMap_null:\n")
t.Log(xorm.JSONString(db.GetSqlMap("Test_GetSqlMap_1", "Test_GetSqlMap_3", 3, "Test_GetSqlMap_null"), true))
t.Log("[Test_GetSqlMap]->Test_GetSqlMap_1,Test_GetSqlMap_3,[]string{Test_GetSqlMap_2, Test_GetSqlMap_4}:\n")
t.Log(xorm.JSONString(db.GetSqlMap("Test_GetSqlMap_1", "Test_GetSqlMap_3", []string{"Test_GetSqlMap_2", "Test_GetSqlMap_4"}), true))
t.Log("[Test_GetSqlMap]->Test_GetSqlMap_1,Test_GetSqlMap_3,[]string{Test_GetSqlMap_2, Test_GetSqlMap_4},2:\n")
t.Log(xorm.JSONString(db.GetSqlMap("Test_GetSqlMap_1", "Test_GetSqlMap_3", []string{"Test_GetSqlMap_2", "Test_GetSqlMap_4"}, 2), true))
t.Log("[Test_GetSqlMap]->Test_GetSqlMap_1,Test_GetSqlMap_3,[]string{Test_GetSqlMap_2, Test_GetSqlMap_4},2 ,Test_GetSqlMap_null:\n")
t.Log(xorm.JSONString(db.GetSqlMap("Test_GetSqlMap_1", "Test_GetSqlMap_3", []string{"Test_GetSqlMap_2", "Test_GetSqlMap_4"}, 2, "Test_GetSqlMap_null"), true))
}
func Test_Limit_Func(t *testing.T) {
res, _ := db.Sql("SELECT b.id,a.name,b.title FROM category a,article b where a.id=b.categorysubid ORDER BY b.id").Limit(10, 3).Query().List()
t.Log(res)
}
func Test_Find(t *testing.T) {
var category []Category
err := db.Find(&category)
if err != nil {
t.Fatal(err)
}
t.Log(category)
}
func Test_EngineGroup(t *testing.T) {
conns := []string{
"postgres://postgres:root@localhost:5432/mblog?sslmode=disable;",
"postgres://postgres:root@localhost:5432/mblog1?sslmode=disable;",
"postgres://postgres:root@localhost:5432/mblog2?sslmode=disable",
}
var err error
xg, err := xorm.NewEngineGroup("postgres", conns)
if err != nil {
t.Fatal(err)
}
xg.ShowSQL(true)
xg.SetPolicy(xorm.WeightRoundRobinPolicy([]int{2, 3}))
t.Log("[xg.Slave():\n", xg.Slave().DataSourceName())
t.Log("[xg.Slave():\n", xg.Slave().DataSourceName())
t.Log("[xg.Slave():\n", xg.Slave().DataSourceName())
t.Log("[xg.Slave():\n", xg.Slave().DataSourceName())
t.Log("[xg.Slave():\n", xg.Slave().DataSourceName())
t.Log("[xg.Slave():\n", xg.Slave().DataSourceName())
t.Log("[xg.Slave():\n", xg.Slave().DataSourceName())
t.Log("[xg.Slave():\n", xg.Slave().DataSourceName())
t.Log("[xg.Slave():\n", xg.Slave().DataSourceName())
t.Log("[xg.Slave():\n", xg.Slave().DataSourceName())
t.Log("[xg.Slave():\n", xg.Slave().DataSourceName())
var category []Category
err = xg.Find(&category)
if err != nil {
t.Fatal(err)
}
t.Log(category)
}