MCPcopy
hub / github.com/go-gorm/gorm / TestPolymorphicHasOneAssociationForSlice

Function TestPolymorphicHasOneAssociationForSlice

tests/associations_has_one_test.go:218–257  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

216}
217
218func TestPolymorphicHasOneAssociationForSlice(t *testing.T) {
219 pets := []Pet{
220 {Name: "hasone-1", Toy: Toy{Name: "toy-has-one"}},
221 {Name: "hasone-2", Toy: Toy{}},
222 {Name: "hasone-3", Toy: Toy{Name: "toy-has-one"}},
223 }
224
225 DB.Create(&pets)
226
227 // Count
228 AssertAssociationCount(t, pets, "Toy", 2, "")
229
230 // Find
231 var toys []Toy
232 if DB.Model(&pets).Association("Toy").Find(&toys); len(toys) != 2 {
233 t.Errorf("toys count should be %v, but got %v", 3, len(toys))
234 }
235
236 // Append
237 DB.Model(&pets).Association("Toy").Append(
238 &Toy{Name: "toy-slice-append-1"},
239 &Toy{Name: "toy-slice-append-2"},
240 &Toy{Name: "toy-slice-append-3"},
241 )
242
243 AssertAssociationCount(t, pets, "Toy", 3, "After Append")
244
245 // Replace -> same as append
246
247 // Delete
248 if err := DB.Model(&pets).Association("Toy").Delete(&pets[0].Toy); err != nil {
249 t.Errorf("no error should happened when deleting toy, but got %v", err)
250 }
251
252 AssertAssociationCount(t, pets, "Toy", 2, "after delete")
253
254 // Clear
255 DB.Model(&pets).Association("Toy").Clear()
256 AssertAssociationCount(t, pets, "Toy", 0, "After Clear")
257}
258
259func TestHasOneAssociationReplaceWithNonValidValue(t *testing.T) {
260 user := User{Name: "jinzhu", Account: Account{Number: "1"}}

Callers

nothing calls this directly

Calls 8

AssertAssociationCountFunction · 0.85
ModelMethod · 0.80
AppendMethod · 0.80
ClearMethod · 0.80
CreateMethod · 0.65
FindMethod · 0.65
DeleteMethod · 0.65
AssociationMethod · 0.45

Tested by

no test coverage detected