forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassociations_test.go
396 lines (322 loc) · 10.9 KB
/
associations_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
package tests_test
import (
"testing"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
. "gorm.io/gorm/utils/tests"
)
func AssertAssociationCount(t *testing.T, data interface{}, name string, result int64, reason string) {
if count := DB.Model(data).Association(name).Count(); count != result {
t.Fatalf("invalid %v count %v, expects: %v got %v", name, reason, result, count)
}
var newUser User
if user, ok := data.(User); ok {
DB.Find(&newUser, "id = ?", user.ID)
} else if user, ok := data.(*User); ok {
DB.Find(&newUser, "id = ?", user.ID)
}
if newUser.ID != 0 {
if count := DB.Model(&newUser).Association(name).Count(); count != result {
t.Fatalf("invalid %v count %v, expects: %v got %v", name, reason, result, count)
}
}
}
func TestInvalidAssociation(t *testing.T) {
user := *GetUser("invalid", Config{Company: true, Manager: true})
if err := DB.Model(&user).Association("Invalid").Find(&user.Company).Error; err == nil {
t.Fatalf("should return errors for invalid association, but got nil")
}
}
func TestAssociationNotNullClear(t *testing.T) {
type Profile struct {
gorm.Model
Number string
MemberID uint `gorm:"not null"`
}
type Member struct {
gorm.Model
Profiles []Profile
}
DB.Migrator().DropTable(&Member{}, &Profile{})
if err := DB.AutoMigrate(&Member{}, &Profile{}); err != nil {
t.Fatalf("Failed to migrate, got error: %v", err)
}
member := &Member{
Profiles: []Profile{{
Number: "1",
}, {
Number: "2",
}},
}
if err := DB.Create(&member).Error; err != nil {
t.Fatalf("Failed to create test data, got error: %v", err)
}
if err := DB.Model(member).Association("Profiles").Clear(); err == nil {
t.Fatalf("No error occurred during clearind not null association")
}
}
func TestForeignKeyConstraints(t *testing.T) {
tidbSkip(t, "not support the foreign key feature")
type Profile struct {
ID uint
Name string
MemberID uint
}
type Member struct {
ID uint
Refer uint `gorm:"uniqueIndex"`
Name string
Profile Profile `gorm:"Constraint:OnUpdate:CASCADE,OnDelete:CASCADE;FOREIGNKEY:MemberID;References:Refer"`
}
DB.Migrator().DropTable(&Profile{}, &Member{})
if err := DB.AutoMigrate(&Profile{}, &Member{}); err != nil {
t.Fatalf("Failed to migrate, got error: %v", err)
}
member := Member{Refer: 1, Name: "foreign_key_constraints", Profile: Profile{Name: "my_profile"}}
DB.Create(&member)
var profile Profile
if err := DB.First(&profile, "id = ?", member.Profile.ID).Error; err != nil {
t.Fatalf("failed to find profile, got error: %v", err)
} else if profile.MemberID != member.ID {
t.Fatalf("member id is not equal: expects: %v, got: %v", member.ID, profile.MemberID)
}
member.Profile = Profile{}
DB.Model(&member).Update("Refer", 100)
var profile2 Profile
if err := DB.First(&profile2, "id = ?", profile.ID).Error; err != nil {
t.Fatalf("failed to find profile, got error: %v", err)
} else if profile2.MemberID != 100 {
t.Fatalf("member id is not equal: expects: %v, got: %v", 100, profile2.MemberID)
}
if r := DB.Delete(&member); r.Error != nil || r.RowsAffected != 1 {
t.Fatalf("Should delete member, got error: %v, affected: %v", r.Error, r.RowsAffected)
}
var result Member
if err := DB.First(&result, member.ID).Error; err == nil {
t.Fatalf("Should not find deleted member")
}
if err := DB.First(&profile2, profile.ID).Error; err == nil {
t.Fatalf("Should not find deleted profile")
}
}
func TestForeignKeyConstraintsBelongsTo(t *testing.T) {
tidbSkip(t, "not support the foreign key feature")
type Profile struct {
ID uint
Name string
Refer uint `gorm:"uniqueIndex"`
}
type Member struct {
ID uint
Name string
ProfileID uint
Profile Profile `gorm:"Constraint:OnUpdate:CASCADE,OnDelete:CASCADE;FOREIGNKEY:ProfileID;References:Refer"`
}
DB.Migrator().DropTable(&Profile{}, &Member{})
if err := DB.AutoMigrate(&Profile{}, &Member{}); err != nil {
t.Fatalf("Failed to migrate, got error: %v", err)
}
member := Member{Name: "foreign_key_constraints_belongs_to", Profile: Profile{Name: "my_profile_belongs_to", Refer: 1}}
DB.Create(&member)
var profile Profile
if err := DB.First(&profile, "id = ?", member.Profile.ID).Error; err != nil {
t.Fatalf("failed to find profile, got error: %v", err)
} else if profile.Refer != member.ProfileID {
t.Fatalf("member id is not equal: expects: %v, got: %v", profile.Refer, member.ProfileID)
}
DB.Model(&profile).Update("Refer", 100)
var member2 Member
if err := DB.First(&member2, "id = ?", member.ID).Error; err != nil {
t.Fatalf("failed to find member, got error: %v", err)
} else if member2.ProfileID != 100 {
t.Fatalf("member id is not equal: expects: %v, got: %v", 100, member2.ProfileID)
}
if r := DB.Delete(&profile); r.Error != nil || r.RowsAffected != 1 {
t.Fatalf("Should delete member, got error: %v, affected: %v", r.Error, r.RowsAffected)
}
var result Member
if err := DB.First(&result, member.ID).Error; err == nil {
t.Fatalf("Should not find deleted member")
}
if err := DB.First(&profile, profile.ID).Error; err == nil {
t.Fatalf("Should not find deleted profile")
}
}
func TestFullSaveAssociations(t *testing.T) {
coupon := &Coupon{
AppliesToProduct: []*CouponProduct{
{ProductId: "full-save-association-product1"},
},
AmountOff: 10,
PercentOff: 0.0,
}
err := DB.
Session(&gorm.Session{FullSaveAssociations: true}).
Create(coupon).Error
if err != nil {
t.Errorf("Failed, got error: %v", err)
}
if DB.First(&Coupon{}, "id = ?", coupon.ID).Error != nil {
t.Errorf("Failed to query saved coupon")
}
if DB.First(&CouponProduct{}, "coupon_id = ? AND product_id = ?", coupon.ID, "full-save-association-product1").Error != nil {
t.Errorf("Failed to query saved association")
}
orders := []Order{{Num: "order1", Coupon: coupon}, {Num: "order2", Coupon: coupon}}
if err := DB.Create(&orders).Error; err != nil {
t.Errorf("failed to create orders, got %v", err)
}
coupon2 := Coupon{
AppliesToProduct: []*CouponProduct{{Desc: "coupon-description"}},
}
DB.Session(&gorm.Session{FullSaveAssociations: true}).Create(&coupon2)
var result Coupon
if err := DB.Preload("AppliesToProduct").First(&result, "id = ?", coupon2.ID).Error; err != nil {
t.Errorf("Failed to create coupon w/o name, got error: %v", err)
}
if len(result.AppliesToProduct) != 1 {
t.Errorf("Failed to preload AppliesToProduct")
}
}
func TestSaveBelongsCircularReference(t *testing.T) {
parent := Parent{}
DB.Create(&parent)
child := Child{ParentID: &parent.ID, Parent: &parent}
DB.Create(&child)
parent.FavChildID = child.ID
parent.FavChild = &child
DB.Save(&parent)
var parent1 Parent
DB.First(&parent1, parent.ID)
AssertObjEqual(t, parent, parent1, "ID", "FavChildID")
// Save and Updates is the same
DB.Updates(&parent)
DB.First(&parent1, parent.ID)
AssertObjEqual(t, parent, parent1, "ID", "FavChildID")
}
func TestSaveHasManyCircularReference(t *testing.T) {
parent := Parent{}
DB.Create(&parent)
child := Child{ParentID: &parent.ID, Parent: &parent, Name: "HasManyCircularReference"}
child1 := Child{ParentID: &parent.ID, Parent: &parent, Name: "HasManyCircularReference1"}
parent.Children = []*Child{&child, &child1}
DB.Save(&parent)
var children []*Child
DB.Where("parent_id = ?", parent.ID).Find(&children)
if len(children) != len(parent.Children) ||
children[0].ID != parent.Children[0].ID ||
children[1].ID != parent.Children[1].ID {
t.Errorf("circular reference children save not equal children:%v parent.Children:%v",
children, parent.Children)
}
}
func TestAssociationError(t *testing.T) {
user := *GetUser("TestAssociationError", Config{Pets: 2, Company: true, Account: true, Languages: 2})
DB.Create(&user)
var user1 User
DB.Preload("Company").Preload("Pets").Preload("Account").Preload("Languages").First(&user1)
var emptyUser User
var err error
// belongs to
err = DB.Model(&emptyUser).Association("Company").Delete(&user1.Company)
AssertEqual(t, err, gorm.ErrPrimaryKeyRequired)
// has many
err = DB.Model(&emptyUser).Association("Pets").Delete(&user1.Pets)
AssertEqual(t, err, gorm.ErrPrimaryKeyRequired)
// has one
err = DB.Model(&emptyUser).Association("Account").Delete(&user1.Account)
AssertEqual(t, err, gorm.ErrPrimaryKeyRequired)
// many to many
err = DB.Model(&emptyUser).Association("Languages").Delete(&user1.Languages)
AssertEqual(t, err, gorm.ErrPrimaryKeyRequired)
}
type (
myType string
emptyQueryClause struct {
Field *schema.Field
}
)
func (myType) QueryClauses(f *schema.Field) []clause.Interface {
return []clause.Interface{emptyQueryClause{Field: f}}
}
func (sd emptyQueryClause) Name() string {
return "empty"
}
func (sd emptyQueryClause) Build(clause.Builder) {
}
func (sd emptyQueryClause) MergeClause(*clause.Clause) {
}
func (sd emptyQueryClause) ModifyStatement(stmt *gorm.Statement) {
// do nothing
}
func TestAssociationEmptyQueryClause(t *testing.T) {
type Organization struct {
gorm.Model
Name string
}
type Region struct {
gorm.Model
Name string
Organizations []Organization `gorm:"many2many:region_orgs;"`
}
type RegionOrg struct {
RegionId uint
OrganizationId uint
Empty myType
}
if err := DB.SetupJoinTable(&Region{}, "Organizations", &RegionOrg{}); err != nil {
t.Fatalf("Failed to set up join table, got error: %s", err)
}
if err := DB.Migrator().DropTable(&Organization{}, &Region{}); err != nil {
t.Fatalf("Failed to migrate, got error: %s", err)
}
if err := DB.AutoMigrate(&Organization{}, &Region{}); err != nil {
t.Fatalf("Failed to migrate, got error: %v", err)
}
region := &Region{Name: "Region1"}
if err := DB.Create(region).Error; err != nil {
t.Fatalf("fail to create region %v", err)
}
var orgs []Organization
if err := DB.Model(&Region{}).Association("Organizations").Find(&orgs); err != nil {
t.Fatalf("fail to find region organizations %v", err)
} else {
AssertEqual(t, len(orgs), 0)
}
}
type AssociationEmptyUser struct {
ID uint
Name string
Pets []AssociationEmptyPet
}
type AssociationEmptyPet struct {
AssociationEmptyUserID *uint `gorm:"uniqueIndex:uniq_user_id_name"`
Name string `gorm:"uniqueIndex:uniq_user_id_name;size:256"`
}
func TestAssociationEmptyPrimaryKey(t *testing.T) {
if DB.Dialector.Name() != "mysql" {
t.Skip()
}
DB.Migrator().DropTable(&AssociationEmptyUser{}, &AssociationEmptyPet{})
DB.AutoMigrate(&AssociationEmptyUser{}, &AssociationEmptyPet{})
id := uint(100)
user := AssociationEmptyUser{
ID: id,
Name: "jinzhu",
Pets: []AssociationEmptyPet{
{AssociationEmptyUserID: &id, Name: "bar"},
{AssociationEmptyUserID: &id, Name: "foo"},
},
}
err := DB.Session(&gorm.Session{FullSaveAssociations: true}).Create(&user).Error
if err != nil {
t.Fatalf("Failed to create, got error: %v", err)
}
var result AssociationEmptyUser
err = DB.Preload("Pets").First(&result, &id).Error
if err != nil {
t.Fatalf("Failed to find, got error: %v", err)
}
AssertEqual(t, result, user)
}