@@ -17,6 +17,74 @@ import (
17
17
"testing"
18
18
)
19
19
20
+ /*
21
+ mysql> show tables;
22
+ +----------------+
23
+ | Tables_in_test |
24
+ +----------------+
25
+ | user |
26
+ | user_detail |
27
+ | user_score |
28
+ +----------------+
29
+ 3 rows in set (0.01 sec)
30
+
31
+ mysql> select * from `user`;
32
+ +----+--------+
33
+ | id | name |
34
+ +----+--------+
35
+ | 1 | name_1 |
36
+ | 2 | name_2 |
37
+ | 3 | name_3 |
38
+ | 4 | name_4 |
39
+ | 5 | name_5 |
40
+ +----+--------+
41
+ 5 rows in set (0.01 sec)
42
+
43
+ mysql> select * from `user_detail`;
44
+ +-----+-----------+
45
+ | uid | address |
46
+ +-----+-----------+
47
+ | 1 | address_1 |
48
+ | 2 | address_2 |
49
+ | 3 | address_3 |
50
+ | 4 | address_4 |
51
+ | 5 | address_5 |
52
+ +-----+-----------+
53
+ 5 rows in set (0.00 sec)
54
+
55
+ mysql> select * from `user_score`;
56
+ +----+-----+-------+
57
+ | id | uid | score |
58
+ +----+-----+-------+
59
+ | 1 | 1 | 1 |
60
+ | 2 | 1 | 2 |
61
+ | 3 | 1 | 3 |
62
+ | 4 | 1 | 4 |
63
+ | 5 | 1 | 5 |
64
+ | 6 | 2 | 1 |
65
+ | 7 | 2 | 2 |
66
+ | 8 | 2 | 3 |
67
+ | 9 | 2 | 4 |
68
+ | 10 | 2 | 5 |
69
+ | 11 | 3 | 1 |
70
+ | 12 | 3 | 2 |
71
+ | 13 | 3 | 3 |
72
+ | 14 | 3 | 4 |
73
+ | 15 | 3 | 5 |
74
+ | 16 | 4 | 1 |
75
+ | 17 | 4 | 2 |
76
+ | 18 | 4 | 3 |
77
+ | 19 | 4 | 4 |
78
+ | 20 | 4 | 5 |
79
+ | 21 | 5 | 1 |
80
+ | 22 | 5 | 2 |
81
+ | 23 | 5 | 3 |
82
+ | 24 | 5 | 4 |
83
+ | 25 | 5 | 5 |
84
+ +----+-----+-------+
85
+ 25 rows in set (0.00 sec)
86
+ */
87
+
20
88
func Test_Table_Relation_With_Scan (t * testing.T ) {
21
89
var (
22
90
tableUser = "user"
@@ -670,6 +738,151 @@ PRIMARY KEY (id)
670
738
})
671
739
}
672
740
741
+ //func Test_Table_Relation_WithAllCondition_List(t *testing.T) {
742
+ // var (
743
+ // tableUser = "user"
744
+ // tableUserDetail = "user_detail"
745
+ // tableUserScores = "user_scores"
746
+ // )
747
+ // if _, err := db.Exec(fmt.Sprintf(`
748
+ //CREATE TABLE IF NOT EXISTS %s (
749
+ //id int(10) unsigned NOT NULL AUTO_INCREMENT,
750
+ //name varchar(45) NOT NULL,
751
+ //PRIMARY KEY (id)
752
+ //) ENGINE=InnoDB DEFAULT CHARSET=utf8;
753
+ // `, tableUser)); err != nil {
754
+ // gtest.Error(err)
755
+ // }
756
+ // defer dropTable(tableUser)
757
+ //
758
+ // if _, err := db.Exec(fmt.Sprintf(`
759
+ //CREATE TABLE IF NOT EXISTS %s (
760
+ //uid int(10) unsigned NOT NULL AUTO_INCREMENT,
761
+ //address varchar(45) NOT NULL,
762
+ //PRIMARY KEY (uid)
763
+ //) ENGINE=InnoDB DEFAULT CHARSET=utf8;
764
+ // `, tableUserDetail)); err != nil {
765
+ // gtest.Error(err)
766
+ // }
767
+ // defer dropTable(tableUserDetail)
768
+ //
769
+ // if _, err := db.Exec(fmt.Sprintf(`
770
+ //CREATE TABLE IF NOT EXISTS %s (
771
+ //id int(10) unsigned NOT NULL AUTO_INCREMENT,
772
+ //uid int(10) unsigned NOT NULL,
773
+ //score int(10) unsigned NOT NULL,
774
+ //PRIMARY KEY (id)
775
+ //) ENGINE=InnoDB DEFAULT CHARSET=utf8;
776
+ // `, tableUserScores)); err != nil {
777
+ // gtest.Error(err)
778
+ // }
779
+ // defer dropTable(tableUserScores)
780
+ //
781
+ // type UserDetail struct {
782
+ // gmeta.Meta `orm:"table:user_detail"`
783
+ // Uid int `json:"uid"`
784
+ // Address string `json:"address"`
785
+ // }
786
+ //
787
+ // type UserScores struct {
788
+ // gmeta.Meta `orm:"table:user_scores"`
789
+ // Id int `json:"id"`
790
+ // Uid int `json:"uid"`
791
+ // Score int `json:"score"`
792
+ // }
793
+ //
794
+ // type User struct {
795
+ // gmeta.Meta `orm:"table:user"`
796
+ // Id int `json:"id"`
797
+ // Name string `json:"name"`
798
+ // UserDetail *UserDetail `orm:"with:uid=id"`
799
+ // UserScores []*UserScores `orm:"with:uid=id, score>1 and score<5"`
800
+ // }
801
+ //
802
+ // // Initialize the data.
803
+ // var err error
804
+ // for i := 1; i <= 5; i++ {
805
+ // // User.
806
+ // _, err = db.Insert(tableUser, g.Map{
807
+ // "id": i,
808
+ // "name": fmt.Sprintf(`name_%d`, i),
809
+ // })
810
+ // gtest.Assert(err, nil)
811
+ // // Detail.
812
+ // _, err = db.Insert(tableUserDetail, g.Map{
813
+ // "uid": i,
814
+ // "address": fmt.Sprintf(`address_%d`, i),
815
+ // })
816
+ // gtest.Assert(err, nil)
817
+ // // Scores.
818
+ // for j := 1; j <= 5; j++ {
819
+ // _, err = db.Insert(tableUserScores, g.Map{
820
+ // "uid": i,
821
+ // "score": j,
822
+ // })
823
+ // gtest.Assert(err, nil)
824
+ // }
825
+ // }
826
+ //
827
+ // db.SetDebug(true)
828
+ // defer db.SetDebug(false)
829
+ //
830
+ // gtest.C(t, func(t *gtest.T) {
831
+ // var users []*User
832
+ // err := db.Model(tableUser).WithAll().Where("id", []int{3, 4}).Scan(&users)
833
+ // t.AssertNil(err)
834
+ // t.Assert(len(users), 2)
835
+ // t.Assert(users[0].Id, 3)
836
+ // t.Assert(users[0].Name, "name_3")
837
+ // t.AssertNE(users[0].UserDetail, nil)
838
+ // t.Assert(users[0].UserDetail.Uid, 3)
839
+ // t.Assert(users[0].UserDetail.Address, "address_3")
840
+ // t.Assert(len(users[0].UserScores), 5)
841
+ // t.Assert(users[0].UserScores[0].Uid, 3)
842
+ // t.Assert(users[0].UserScores[0].Score, 1)
843
+ // t.Assert(users[0].UserScores[4].Uid, 3)
844
+ // t.Assert(users[0].UserScores[4].Score, 5)
845
+ //
846
+ // t.Assert(users[1].Id, 4)
847
+ // t.Assert(users[1].Name, "name_4")
848
+ // t.AssertNE(users[1].UserDetail, nil)
849
+ // t.Assert(users[1].UserDetail.Uid, 4)
850
+ // t.Assert(users[1].UserDetail.Address, "address_4")
851
+ // t.Assert(len(users[1].UserScores), 5)
852
+ // t.Assert(users[1].UserScores[0].Uid, 4)
853
+ // t.Assert(users[1].UserScores[0].Score, 1)
854
+ // t.Assert(users[1].UserScores[4].Uid, 4)
855
+ // t.Assert(users[1].UserScores[4].Score, 5)
856
+ // })
857
+ // gtest.C(t, func(t *gtest.T) {
858
+ // var users []User
859
+ // err := db.Model(tableUser).WithAll().Where("id", []int{3, 4}).Scan(&users)
860
+ // t.AssertNil(err)
861
+ // t.Assert(len(users), 2)
862
+ // t.Assert(users[0].Id, 3)
863
+ // t.Assert(users[0].Name, "name_3")
864
+ // t.AssertNE(users[0].UserDetail, nil)
865
+ // t.Assert(users[0].UserDetail.Uid, 3)
866
+ // t.Assert(users[0].UserDetail.Address, "address_3")
867
+ // t.Assert(len(users[0].UserScores), 5)
868
+ // t.Assert(users[0].UserScores[0].Uid, 3)
869
+ // t.Assert(users[0].UserScores[0].Score, 1)
870
+ // t.Assert(users[0].UserScores[4].Uid, 3)
871
+ // t.Assert(users[0].UserScores[4].Score, 5)
872
+ //
873
+ // t.Assert(users[1].Id, 4)
874
+ // t.Assert(users[1].Name, "name_4")
875
+ // t.AssertNE(users[1].UserDetail, nil)
876
+ // t.Assert(users[1].UserDetail.Uid, 4)
877
+ // t.Assert(users[1].UserDetail.Address, "address_4")
878
+ // t.Assert(len(users[1].UserScores), 5)
879
+ // t.Assert(users[1].UserScores[0].Uid, 4)
880
+ // t.Assert(users[1].UserScores[0].Score, 1)
881
+ // t.Assert(users[1].UserScores[4].Uid, 4)
882
+ // t.Assert(users[1].UserScores[4].Score, 5)
883
+ // })
884
+ //}
885
+
673
886
func Test_Table_Relation_WithAll_Embedded (t * testing.T ) {
674
887
var (
675
888
tableUser = "user"
@@ -1380,6 +1593,7 @@ func Test_Table_Relation_With_MultipleDepends1(t *testing.T) {
1380
1593
t .Assert (tableA [1 ].TableB .TableC .Id , 300 )
1381
1594
})
1382
1595
}
1596
+
1383
1597
func Test_Table_Relation_With_MultipleDepends2 (t * testing.T ) {
1384
1598
defer func () {
1385
1599
dropTable ("table_a" )
@@ -1466,6 +1680,7 @@ func Test_Table_Relation_With_MultipleDepends2(t *testing.T) {
1466
1680
t .Assert (tableA [1 ].TableB [1 ].TableC , nil )
1467
1681
})
1468
1682
}
1683
+
1469
1684
func Test_Table_Relation_With_MultipleDepends_Embedded (t * testing.T ) {
1470
1685
defer func () {
1471
1686
dropTable ("table_a" )
0 commit comments