This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtestCascade.go
110 lines (92 loc) · 2.41 KB
/
testCascade.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
package tests
import (
"fmt"
"testing"
"time"
"github.com/go-xorm/xorm"
)
func testCascade(engine *xorm.Engine, t *testing.T) {
cascadeGet(engine, t)
UserTest1(engine, t)
}
func cascadeGet(engine *xorm.Engine, t *testing.T) {
user := Userinfo{Uid: 11}
has, err := engine.Get(&user)
if err != nil {
t.Error(err)
panic(err)
}
if has {
fmt.Println(user)
} else {
fmt.Println("no record id is 2")
}
}
type Users struct {
Uid string `xorm:"notnull pk UUID"`
UserName string `xorm:"notnull unique VARCHAR(30)"`
NickName string `xorm:"notnull VARCHAR(30)"`
Password string `xorm:"notnull VARCHAR(44)"`
Email string `xorm:"notnull unique VARCHAR(80)"`
Profile *Profile `xorm:"profile_id UUID"`
CreatedAt time.Time `xorm:"notnull TIMESTAMPZ created"`
UpdatedAt time.Time `xorm:"notnull TIMESTAMPZ updated"`
CreatedUser *Users `xorm:"created_user_id UUID"`
UpdatedUser *Users `xorm:"update_user_id UUID"`
}
type Profile struct {
Uid string `xorm:"notnull pk UUID"`
User *Users `xorm:"user_id varchar(50)"`
CreatedAt time.Time `xorm:"notnull TIMESTAMPZ created"`
UpdatedAt time.Time `xorm:"notnull TIMESTAMPZ updated"`
CreatedUser *Users `xorm:"created_user_id varchar(50)"`
UpdatedUser *Users `xorm:"update_user_id varchar(50)"`
}
func UserTest1(engine *xorm.Engine, t *testing.T) {
table1, table2 := new(Users), new(Profile)
engine.DropTables(table1, table2)
err := engine.Sync2(table1, table2)
if err != nil {
t.Error(err)
panic(err)
}
profile := &Profile{
Uid: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
User: nil,
CreatedUser: nil,
UpdatedUser: nil,
}
users := &Users{
Uid: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
UserName: "sfafdafds",
NickName: "sfsfds",
Password: "ssss",
Email: "[email protected]",
Profile: &Profile{
Uid: profile.Uid,
},
CreatedUser: nil,
UpdatedUser: nil,
}
_, err = engine.Insert(users)
if err != nil {
t.Fatal("insert UUID PK users failed:", err)
}
profile.CreatedUser = users
_, err = engine.Insert(profile)
if err != nil {
t.Fatal("insert UUID PK profile failed:", err)
}
var newUsers Users
has, err := engine.Id(users.Uid).Get(&newUsers)
if err != nil {
t.Fatal("get UUID pk users failed:", err)
}
if !has {
t.Fatal("get UUID pk users failed: get none")
}
fmt.Println(newUsers.Profile, "==", profile)
if newUsers.Profile.Uid != profile.Uid {
t.Fatal("should equal profile.uid")
}
}