(t *testing.T)
| 216 | } |
| 217 | |
| 218 | func 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 | |
| 259 | func TestHasOneAssociationReplaceWithNonValidValue(t *testing.T) { |
| 260 | user := User{Name: "jinzhu", Account: Account{Number: "1"}} |
nothing calls this directly
no test coverage detected