forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymorphic_test.go
219 lines (173 loc) · 5.72 KB
/
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
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
package gorm_test
import (
"reflect"
"sort"
"testing"
)
type Cat struct {
Id int
Name string
Toy Toy `gorm:"polymorphic:Owner;"`
}
type Dog struct {
Id int
Name string
Toys []Toy `gorm:"polymorphic:Owner;"`
}
type Toy struct {
Id int
Name string
OwnerId int
OwnerType string
}
var compareToys = func(toys []Toy, contents []string) bool {
var toyContents []string
for _, toy := range toys {
toyContents = append(toyContents, toy.Name)
}
sort.Strings(toyContents)
sort.Strings(contents)
return reflect.DeepEqual(toyContents, contents)
}
func TestPolymorphic(t *testing.T) {
cat := Cat{Name: "Mr. Bigglesworth", Toy: Toy{Name: "cat toy"}}
dog := Dog{Name: "Pluto", Toys: []Toy{Toy{Name: "dog toy 1"}, Toy{Name: "dog toy 2"}}}
DB.Save(&cat).Save(&dog)
if DB.Model(&cat).Association("Toy").Count() != 1 {
t.Errorf("Cat's toys count should be 1")
}
if DB.Model(&cat).Association("Toy").Count() != 1 {
t.Errorf("Dog's toys count should be 2")
}
// Query
var catToys []Toy
if DB.Model(&cat).Related(&catToys, "Toy").RecordNotFound() {
t.Errorf("Did not find any has one polymorphic association")
} else if len(catToys) != 1 {
t.Errorf("Should have found only one polymorphic has one association")
} else if catToys[0].Name != cat.Toy.Name {
t.Errorf("Should have found the proper has one polymorphic association")
}
var dogToys []Toy
if DB.Model(&dog).Related(&dogToys, "Toys").RecordNotFound() {
t.Errorf("Did not find any polymorphic has many associations")
} else if len(dogToys) != len(dog.Toys) {
t.Errorf("Should have found all polymorphic has many associations")
}
var catToy Toy
DB.Model(&cat).Association("Toy").Find(&catToy)
if catToy.Name != cat.Toy.Name {
t.Errorf("Should find has one polymorphic association")
}
var dogToys1 []Toy
DB.Model(&dog).Association("Toys").Find(&dogToys1)
if !compareToys(dogToys1, []string{"dog toy 1", "dog toy 2"}) {
t.Errorf("Should find has many polymorphic association")
}
// Append
DB.Model(&cat).Association("Toy").Append(&Toy{
Name: "cat toy 2",
})
var catToy2 Toy
DB.Model(&cat).Association("Toy").Find(&catToy2)
if catToy2.Name != "cat toy 2" {
t.Errorf("Should update has one polymorphic association with Append")
}
if DB.Model(&cat).Association("Toy").Count() != 1 {
t.Errorf("Cat's toys count should be 1 after Append")
}
if DB.Model(&dog).Association("Toys").Count() != 2 {
t.Errorf("Should return two polymorphic has many associations")
}
DB.Model(&dog).Association("Toys").Append(&Toy{
Name: "dog toy 3",
})
var dogToys2 []Toy
DB.Model(&dog).Association("Toys").Find(&dogToys2)
if !compareToys(dogToys2, []string{"dog toy 1", "dog toy 2", "dog toy 3"}) {
t.Errorf("Dog's toys should be updated with Append")
}
if DB.Model(&dog).Association("Toys").Count() != 3 {
t.Errorf("Should return three polymorphic has many associations")
}
// Replace
DB.Model(&cat).Association("Toy").Replace(&Toy{
Name: "cat toy 3",
})
var catToy3 Toy
DB.Model(&cat).Association("Toy").Find(&catToy3)
if catToy3.Name != "cat toy 3" {
t.Errorf("Should update has one polymorphic association with Replace")
}
if DB.Model(&cat).Association("Toy").Count() != 1 {
t.Errorf("Cat's toys count should be 1 after Replace")
}
if DB.Model(&dog).Association("Toys").Count() != 3 {
t.Errorf("Should return three polymorphic has many associations")
}
DB.Model(&dog).Association("Toys").Replace(&Toy{
Name: "dog toy 4",
}, []Toy{
{Name: "dog toy 5"}, {Name: "dog toy 6"}, {Name: "dog toy 7"},
})
var dogToys3 []Toy
DB.Model(&dog).Association("Toys").Find(&dogToys3)
if !compareToys(dogToys3, []string{"dog toy 4", "dog toy 5", "dog toy 6", "dog toy 7"}) {
t.Errorf("Dog's toys should be updated with Replace")
}
if DB.Model(&dog).Association("Toys").Count() != 4 {
t.Errorf("Should return three polymorphic has many associations")
}
// Delete
DB.Model(&cat).Association("Toy").Delete(&catToy2)
var catToy4 Toy
DB.Model(&cat).Association("Toy").Find(&catToy4)
if catToy4.Name != "cat toy 3" {
t.Errorf("Should not update has one polymorphic association when Delete a unrelated Toy")
}
if DB.Model(&cat).Association("Toy").Count() != 1 {
t.Errorf("Cat's toys count should be 1")
}
if DB.Model(&dog).Association("Toys").Count() != 4 {
t.Errorf("Dog's toys count should be 4")
}
DB.Model(&cat).Association("Toy").Delete(&catToy3)
if !DB.Model(&cat).Related(&Toy{}, "Toy").RecordNotFound() {
t.Errorf("Toy should be deleted with Delete")
}
if DB.Model(&cat).Association("Toy").Count() != 0 {
t.Errorf("Cat's toys count should be 0 after Delete")
}
if DB.Model(&dog).Association("Toys").Count() != 4 {
t.Errorf("Dog's toys count should not be changed when delete cat's toy")
}
DB.Model(&dog).Association("Toys").Delete(&dogToys2)
if DB.Model(&dog).Association("Toys").Count() != 4 {
t.Errorf("Dog's toys count should not be changed when delete unrelated toys")
}
DB.Model(&dog).Association("Toys").Delete(&dogToys3)
if DB.Model(&dog).Association("Toys").Count() != 0 {
t.Errorf("Dog's toys count should be deleted with Delete")
}
// Clear
DB.Model(&cat).Association("Toy").Append(&Toy{
Name: "cat toy 2",
})
if DB.Model(&cat).Association("Toy").Count() != 1 {
t.Errorf("Cat's toys should be added with Append")
}
DB.Model(&cat).Association("Toy").Clear()
if DB.Model(&cat).Association("Toy").Count() != 0 {
t.Errorf("Cat's toys should be cleared with Clear")
}
DB.Model(&dog).Association("Toys").Append(&Toy{
Name: "dog toy 8",
})
if DB.Model(&dog).Association("Toys").Count() != 1 {
t.Errorf("Dog's toys should be added with Append")
}
DB.Model(&dog).Association("Toys").Clear()
if DB.Model(&dog).Association("Toys").Count() != 0 {
t.Errorf("Dog's toys should be cleared with Clear")
}
}