-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathnamed_polymorphic_test.go
147 lines (115 loc) · 4.2 KB
/
named_polymorphic_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
package tests_test
import (
"testing"
. "gorm.io/gorm/utils/tests"
)
type Hamster struct {
Id int
Name string
PreferredToy Toy `gorm:"polymorphic:Owner;polymorphicValue:hamster_preferred"`
OtherToy Toy `gorm:"polymorphic:Owner;polymorphicValue:hamster_other"`
}
func TestNamedPolymorphic(t *testing.T) {
DB.Migrator().DropTable(&Hamster{})
DB.AutoMigrate(&Hamster{})
hamster := Hamster{Name: "Mr. Hammond", PreferredToy: Toy{Name: "bike"}, OtherToy: Toy{Name: "treadmill"}}
DB.Save(&hamster)
hamster2 := Hamster{}
DB.Preload("PreferredToy").Preload("OtherToy").Find(&hamster2, hamster.Id)
if hamster2.PreferredToy.ID != hamster.PreferredToy.ID || hamster2.PreferredToy.Name != hamster.PreferredToy.Name {
t.Errorf("Hamster's preferred toy failed to preload")
}
if hamster2.OtherToy.ID != hamster.OtherToy.ID || hamster2.OtherToy.Name != hamster.OtherToy.Name {
t.Errorf("Hamster's other toy failed to preload")
}
// clear to omit Toy.ID in count
hamster2.PreferredToy = Toy{}
hamster2.OtherToy = Toy{}
if DB.Model(&hamster2).Association("PreferredToy").Count() != 1 {
t.Errorf("Hamster's preferred toy count should be 1")
}
if DB.Model(&hamster2).Association("OtherToy").Count() != 1 {
t.Errorf("Hamster's other toy count should be 1")
}
// Query
hamsterToy := Toy{}
DB.Model(&hamster).Association("PreferredToy").Find(&hamsterToy)
if hamsterToy.Name != hamster.PreferredToy.Name {
t.Errorf("Should find has one polymorphic association")
}
hamsterToy = Toy{}
DB.Model(&hamster).Association("OtherToy").Find(&hamsterToy)
if hamsterToy.Name != hamster.OtherToy.Name {
t.Errorf("Should find has one polymorphic association")
}
// Append
DB.Model(&hamster).Association("PreferredToy").Append(&Toy{
Name: "bike 2",
})
DB.Model(&hamster).Association("OtherToy").Append(&Toy{
Name: "treadmill 2",
})
hamsterToy = Toy{}
DB.Model(&hamster).Association("PreferredToy").Find(&hamsterToy)
if hamsterToy.Name != "bike 2" {
t.Errorf("Should update has one polymorphic association with Append")
}
hamsterToy = Toy{}
DB.Model(&hamster).Association("OtherToy").Find(&hamsterToy)
if hamsterToy.Name != "treadmill 2" {
t.Errorf("Should update has one polymorphic association with Append")
}
if DB.Model(&hamster2).Association("PreferredToy").Count() != 1 {
t.Errorf("Hamster's toys count should be 1 after Append")
}
if DB.Model(&hamster2).Association("OtherToy").Count() != 1 {
t.Errorf("Hamster's toys count should be 1 after Append")
}
// Replace
DB.Model(&hamster).Association("PreferredToy").Replace(&Toy{
Name: "bike 3",
})
DB.Model(&hamster).Association("OtherToy").Replace(&Toy{
Name: "treadmill 3",
})
hamsterToy = Toy{}
DB.Model(&hamster).Association("PreferredToy").Find(&hamsterToy)
if hamsterToy.Name != "bike 3" {
t.Errorf("Should update has one polymorphic association with Replace")
}
hamsterToy = Toy{}
DB.Model(&hamster).Association("OtherToy").Find(&hamsterToy)
if hamsterToy.Name != "treadmill 3" {
t.Errorf("Should update has one polymorphic association with Replace")
}
if DB.Model(&hamster2).Association("PreferredToy").Count() != 1 {
t.Errorf("hamster's toys count should be 1 after Replace")
}
if DB.Model(&hamster2).Association("OtherToy").Count() != 1 {
t.Errorf("hamster's toys count should be 1 after Replace")
}
// Clear
DB.Model(&hamster).Association("PreferredToy").Append(&Toy{
Name: "bike 2",
})
DB.Model(&hamster).Association("OtherToy").Append(&Toy{
Name: "treadmill 2",
})
if DB.Model(&hamster).Association("PreferredToy").Count() != 1 {
t.Errorf("Hamster's toys should be added with Append")
}
if DB.Model(&hamster).Association("OtherToy").Count() != 1 {
t.Errorf("Hamster's toys should be added with Append")
}
DB.Model(&hamster).Association("PreferredToy").Clear()
if DB.Model(&hamster2).Association("PreferredToy").Count() != 0 {
t.Errorf("Hamster's preferred toy should be cleared with Clear")
}
if DB.Model(&hamster2).Association("OtherToy").Count() != 1 {
t.Errorf("Hamster's other toy should be still available")
}
DB.Model(&hamster).Association("OtherToy").Clear()
if DB.Model(&hamster).Association("OtherToy").Count() != 0 {
t.Errorf("Hamster's other toy should be cleared with Clear")
}
}