| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
. "gorm.io/gorm/utils/tests" |
| 7 |
) |
| 8 |
|
| 9 |
type Hamster struct { |
| 10 |
Id int |
| 11 |
Name string |
| 12 |
PreferredToy Toy `gorm:"polymorphic:Owner;polymorphicValue:hamster_preferred"` |
| 13 |
OtherToy Toy `gorm:"polymorphic:Owner;polymorphicValue:hamster_other"` |
| 14 |
} |
| 15 |
|
| 16 |
func TestNamedPolymorphic(t *testing.T) { |
| 17 |
DB.Migrator().DropTable(&Hamster{}) |
| 18 |
DB.AutoMigrate(&Hamster{}) |
| 19 |
|
| 20 |
hamster := Hamster{Name: "Mr. Hammond", PreferredToy: Toy{Name: "bike"}, OtherToy: Toy{Name: "treadmill"}} |
| 21 |
DB.Save(&hamster) |
| 22 |
|
| 23 |
hamster2 := Hamster{} |
| 24 |
DB.Preload("PreferredToy").Preload("OtherToy").Find(&hamster2, hamster.Id) |
| 25 |
|
| 26 |
if hamster2.PreferredToy.ID != hamster.PreferredToy.ID || hamster2.PreferredToy.Name != hamster.PreferredToy.Name { |
| 27 |
t.Errorf("Hamster's preferred toy failed to preload") |
| 28 |
} |
| 29 |
|
| 30 |
if hamster2.OtherToy.ID != hamster.OtherToy.ID || hamster2.OtherToy.Name != hamster.OtherToy.Name { |
| 31 |
t.Errorf("Hamster's other toy failed to preload") |
| 32 |
} |
| 33 |
|
| 34 |
// clear to omit Toy.ID in count |
| 35 |
hamster2.PreferredToy = Toy{} |
| 36 |
hamster2.OtherToy = Toy{} |
| 37 |
|
| 38 |
if DB.Model(&hamster2).Association("PreferredToy").Count() != 1 { |
| 39 |
t.Errorf("Hamster's preferred toy count should be 1") |
| 40 |
} |
| 41 |
|
| 42 |
if DB.Model(&hamster2).Association("OtherToy").Count() != 1 { |
| 43 |
t.Errorf("Hamster's other toy count should be 1") |
| 44 |
} |
| 45 |
|
| 46 |
// Query |
| 47 |
hamsterToy := Toy{} |
| 48 |
DB.Model(&hamster).Association("PreferredToy").Find(&hamsterToy) |
| 49 |
if hamsterToy.Name != hamster.PreferredToy.Name { |
| 50 |
t.Errorf("Should find has one polymorphic association") |
| 51 |
} |
| 52 |
|
| 53 |
hamsterToy = Toy{} |
| 54 |
DB.Model(&hamster).Association("OtherToy").Find(&hamsterToy) |
| 55 |
if hamsterToy.Name != hamster.OtherToy.Name { |
| 56 |
t.Errorf("Should find has one polymorphic association") |
| 57 |
} |
| 58 |
|
| 59 |
// Append |
| 60 |
DB.Model(&hamster).Association("PreferredToy").Append(&Toy{ |
| 61 |
Name: "bike 2", |
| 62 |
}) |
| 63 |
|
| 64 |
DB.Model(&hamster).Association("OtherToy").Append(&Toy{ |
| 65 |
Name: "treadmill 2", |
| 66 |
}) |
| 67 |
|
| 68 |
hamsterToy = Toy{} |
| 69 |
DB.Model(&hamster).Association("PreferredToy").Find(&hamsterToy) |
| 70 |
if hamsterToy.Name != "bike 2" { |
| 71 |
t.Errorf("Should update has one polymorphic association with Append") |
| 72 |
} |
| 73 |
|
| 74 |
hamsterToy = Toy{} |
| 75 |
DB.Model(&hamster).Association("OtherToy").Find(&hamsterToy) |
| 76 |
if hamsterToy.Name != "treadmill 2" { |
| 77 |
t.Errorf("Should update has one polymorphic association with Append") |
| 78 |
} |
| 79 |
|
| 80 |
if DB.Model(&hamster2).Association("PreferredToy").Count() != 1 { |
| 81 |
t.Errorf("Hamster's toys count should be 1 after Append") |
| 82 |
} |
| 83 |
|
| 84 |
if DB.Model(&hamster2).Association("OtherToy").Count() != 1 { |
| 85 |
t.Errorf("Hamster's toys count should be 1 after Append") |
| 86 |
} |
| 87 |
|
| 88 |
// Replace |
| 89 |
DB.Model(&hamster).Association("PreferredToy").Replace(&Toy{ |
| 90 |
Name: "bike 3", |
| 91 |
}) |
| 92 |
|
| 93 |
DB.Model(&hamster).Association("OtherToy").Replace(&Toy{ |
| 94 |
Name: "treadmill 3", |
| 95 |
}) |
| 96 |
|
| 97 |
hamsterToy = Toy{} |
| 98 |
DB.Model(&hamster).Association("PreferredToy").Find(&hamsterToy) |
| 99 |
if hamsterToy.Name != "bike 3" { |
| 100 |
t.Errorf("Should update has one polymorphic association with Replace") |
| 101 |
} |
| 102 |
|
| 103 |
hamsterToy = Toy{} |
| 104 |
DB.Model(&hamster).Association("OtherToy").Find(&hamsterToy) |
| 105 |
if hamsterToy.Name != "treadmill 3" { |
| 106 |
t.Errorf("Should update has one polymorphic association with Replace") |
| 107 |
} |
| 108 |
|
| 109 |
if DB.Model(&hamster2).Association("PreferredToy").Count() != 1 { |
| 110 |
t.Errorf("hamster's toys count should be 1 after Replace") |
| 111 |
} |
| 112 |
|
| 113 |
if DB.Model(&hamster2).Association("OtherToy").Count() != 1 { |
| 114 |
t.Errorf("hamster's toys count should be 1 after Replace") |
| 115 |
} |
| 116 |
|
| 117 |
// Clear |
| 118 |
DB.Model(&hamster).Association("PreferredToy").Append(&Toy{ |
| 119 |
Name: "bike 2", |
| 120 |
}) |
| 121 |
DB.Model(&hamster).Association("OtherToy").Append(&Toy{ |
| 122 |
Name: "treadmill 2", |
| 123 |
}) |
| 124 |
|
| 125 |
if DB.Model(&hamster).Association("PreferredToy").Count() != 1 { |
| 126 |
t.Errorf("Hamster's toys should be added with Append") |
| 127 |
} |
| 128 |
|
| 129 |
if DB.Model(&hamster).Association("OtherToy").Count() != 1 { |
| 130 |
t.Errorf("Hamster's toys should be added with Append") |
| 131 |
} |
| 132 |
|
| 133 |
DB.Model(&hamster).Association("PreferredToy").Clear() |
| 134 |
|
| 135 |
if DB.Model(&hamster2).Association("PreferredToy").Count() != 0 { |
| 136 |
t.Errorf("Hamster's preferred toy should be cleared with Clear") |
| 137 |
} |
| 138 |
|
| 139 |
if DB.Model(&hamster2).Association("OtherToy").Count() != 1 { |
| 140 |
t.Errorf("Hamster's other toy should be still available") |
| 141 |
} |
| 142 |
|
| 143 |
DB.Model(&hamster).Association("OtherToy").Clear() |
| 144 |
if DB.Model(&hamster).Association("OtherToy").Count() != 0 { |
| 145 |
t.Errorf("Hamster's other toy should be cleared with Clear") |
| 146 |
} |
| 147 |
} |