forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_many2many_test.go
54 lines (41 loc) · 1.34 KB
/
update_many2many_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
package tests_test
import (
"testing"
"gorm.io/gorm"
. "gorm.io/gorm/utils/tests"
)
func TestUpdateMany2ManyAssociations(t *testing.T) {
user := *GetUser("update-many2many", Config{})
if err := DB.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
user.Languages = []Language{{Code: "zh-CN", Name: "Chinese"}, {Code: "en", Name: "English"}}
for _, lang := range user.Languages {
DB.Create(&lang)
}
user.Friends = []*User{{Name: "friend-1"}, {Name: "friend-2"}}
if err := DB.Save(&user).Error; err != nil {
t.Fatalf("errors happened when update: %v", err)
}
var user2 User
DB.Preload("Languages").Preload("Friends").Find(&user2, "id = ?", user.ID)
CheckUser(t, user2, user)
for idx := range user.Friends {
user.Friends[idx].Name += "new"
}
for idx := range user.Languages {
user.Languages[idx].Name += "new"
}
if err := DB.Save(&user).Error; err != nil {
t.Fatalf("errors happened when update: %v", err)
}
var user3 User
DB.Preload("Languages").Preload("Friends").Find(&user3, "id = ?", user.ID)
CheckUser(t, user2, user3)
if err := DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(&user).Error; err != nil {
t.Fatalf("errors happened when update: %v", err)
}
var user4 User
DB.Preload("Languages").Preload("Friends").Find(&user4, "id = ?", user.ID)
CheckUser(t, user4, user)
}