package gorm_test import ( "fmt" "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) fmt.Println(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 // Delete // Clear }