| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
. "gorm.io/gorm/utils/tests" |
| 7 |
) |
| 8 |
|
| 9 |
func TestHasOneAssociation(t *testing.T) { |
| 10 |
user := *GetUser("hasone", Config{Account: true}) |
| 11 |
|
| 12 |
if err := DB.Create(&user).Error; err != nil { |
| 13 |
t.Fatalf("errors happened when create: %v", err) |
| 14 |
} |
| 15 |
|
| 16 |
CheckUser(t, user, user) |
| 17 |
|
| 18 |
// Find |
| 19 |
var user2 User |
| 20 |
DB.Find(&user2, "id = ?", user.ID) |
| 21 |
DB.Model(&user2).Association("Account").Find(&user2.Account) |
| 22 |
CheckUser(t, user2, user) |
| 23 |
|
| 24 |
// Count |
| 25 |
AssertAssociationCount(t, user, "Account", 1, "") |
| 26 |
|
| 27 |
// Append |
| 28 |
account := Account{Number: "account-has-one-append"} |
| 29 |
|
| 30 |
if err := DB.Model(&user2).Association("Account").Append(&account); err != nil { |
| 31 |
t.Fatalf("Error happened when append account, got %v", err) |
| 32 |
} |
| 33 |
|
| 34 |
if account.ID == 0 { |
| 35 |
t.Fatalf("Account's ID should be created") |
| 36 |
} |
| 37 |
|
| 38 |
user.Account = account |
| 39 |
CheckUser(t, user2, user) |
| 40 |
|
| 41 |
AssertAssociationCount(t, user, "Account", 1, "AfterAppend") |
| 42 |
|
| 43 |
// Replace |
| 44 |
account2 := Account{Number: "account-has-one-replace"} |
| 45 |
|
| 46 |
if err := DB.Model(&user2).Association("Account").Replace(&account2); err != nil { |
| 47 |
t.Fatalf("Error happened when append Account, got %v", err) |
| 48 |
} |
| 49 |
|
| 50 |
if account2.ID == 0 { |
| 51 |
t.Fatalf("account2's ID should be created") |
| 52 |
} |
| 53 |
|
| 54 |
user.Account = account2 |
| 55 |
CheckUser(t, user2, user) |
| 56 |
|
| 57 |
AssertAssociationCount(t, user2, "Account", 1, "AfterReplace") |
| 58 |
|
| 59 |
// Delete |
| 60 |
if err := DB.Model(&user2).Association("Account").Delete(&Account{}); err != nil { |
| 61 |
t.Fatalf("Error happened when delete account, got %v", err) |
| 62 |
} |
| 63 |
AssertAssociationCount(t, user2, "Account", 1, "after delete non-existing data") |
| 64 |
|
| 65 |
if err := DB.Model(&user2).Association("Account").Delete(&account2); err != nil { |
| 66 |
t.Fatalf("Error happened when delete Account, got %v", err) |
| 67 |
} |
| 68 |
AssertAssociationCount(t, user2, "Account", 0, "after delete") |
| 69 |
|
| 70 |
// Prepare Data for Clear |
| 71 |
account = Account{Number: "account-has-one-append"} |
| 72 |
if err := DB.Model(&user2).Association("Account").Append(&account); err != nil { |
| 73 |
t.Fatalf("Error happened when append Account, got %v", err) |
| 74 |
} |
| 75 |
|
| 76 |
AssertAssociationCount(t, user2, "Account", 1, "after prepare data") |
| 77 |
|
| 78 |
// Clear |
| 79 |
if err := DB.Model(&user2).Association("Account").Clear(); err != nil { |
| 80 |
t.Errorf("Error happened when clear Account, got %v", err) |
| 81 |
} |
| 82 |
|
| 83 |
AssertAssociationCount(t, user2, "Account", 0, "after clear") |
| 84 |
} |
| 85 |
|
| 86 |
func TestHasOneAssociationWithSelect(t *testing.T) { |
| 87 |
user := *GetUser("hasone", Config{Account: true}) |
| 88 |
|
| 89 |
DB.Omit("Account.Number").Create(&user) |
| 90 |
|
| 91 |
AssertAssociationCount(t, user, "Account", 1, "") |
| 92 |
|
| 93 |
var account Account |
| 94 |
DB.Model(&user).Association("Account").Find(&account) |
| 95 |
if account.Number != "" { |
| 96 |
t.Errorf("account's number should not be saved") |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func TestHasOneAssociationForSlice(t *testing.T) { |
| 101 |
users := []User{ |
| 102 |
*GetUser("slice-hasone-1", Config{Account: true}), |
| 103 |
*GetUser("slice-hasone-2", Config{Account: false}), |
| 104 |
*GetUser("slice-hasone-3", Config{Account: true}), |
| 105 |
} |
| 106 |
|
| 107 |
DB.Create(&users) |
| 108 |
|
| 109 |
// Count |
| 110 |
AssertAssociationCount(t, users, "Account", 2, "") |
| 111 |
|
| 112 |
// Find |
| 113 |
var accounts []Account |
| 114 |
if DB.Model(&users).Association("Account").Find(&accounts); len(accounts) != 2 { |
| 115 |
t.Errorf("accounts count should be %v, but got %v", 3, len(accounts)) |
| 116 |
} |
| 117 |
|
| 118 |
// Append |
| 119 |
DB.Model(&users).Association("Account").Append( |
| 120 |
&Account{Number: "account-slice-append-1"}, |
| 121 |
&Account{Number: "account-slice-append-2"}, |
| 122 |
&Account{Number: "account-slice-append-3"}, |
| 123 |
) |
| 124 |
|
| 125 |
AssertAssociationCount(t, users, "Account", 3, "After Append") |
| 126 |
|
| 127 |
// Replace -> same as append |
| 128 |
|
| 129 |
// Delete |
| 130 |
if err := DB.Model(&users).Association("Account").Delete(&users[0].Account); err != nil { |
| 131 |
t.Errorf("no error should happened when deleting account, but got %v", err) |
| 132 |
} |
| 133 |
|
| 134 |
AssertAssociationCount(t, users, "Account", 2, "after delete") |
| 135 |
|
| 136 |
// Clear |
| 137 |
DB.Model(&users).Association("Account").Clear() |
| 138 |
AssertAssociationCount(t, users, "Account", 0, "After Clear") |
| 139 |
} |
| 140 |
|
| 141 |
func TestPolymorphicHasOneAssociation(t *testing.T) { |
| 142 |
pet := Pet{Name: "hasone", Toy: Toy{Name: "toy-has-one"}} |
| 143 |
|
| 144 |
if err := DB.Create(&pet).Error; err != nil { |
| 145 |
t.Fatalf("errors happened when create: %v", err) |
| 146 |
} |
| 147 |
|
| 148 |
CheckPet(t, pet, pet) |
| 149 |
|
| 150 |
// Find |
| 151 |
var pet2 Pet |
| 152 |
DB.Find(&pet2, "id = ?", pet.ID) |
| 153 |
DB.Model(&pet2).Association("Toy").Find(&pet2.Toy) |
| 154 |
CheckPet(t, pet2, pet) |
| 155 |
|
| 156 |
// Count |
| 157 |
AssertAssociationCount(t, pet, "Toy", 1, "") |
| 158 |
|
| 159 |
// Append |
| 160 |
toy := Toy{Name: "toy-has-one-append"} |
| 161 |
|
| 162 |
if err := DB.Model(&pet2).Association("Toy").Append(&toy); err != nil { |
| 163 |
t.Fatalf("Error happened when append toy, got %v", err) |
| 164 |
} |
| 165 |
|
| 166 |
if toy.ID == 0 { |
| 167 |
t.Fatalf("Toy's ID should be created") |
| 168 |
} |
| 169 |
|
| 170 |
pet.Toy = toy |
| 171 |
CheckPet(t, pet2, pet) |
| 172 |
|
| 173 |
AssertAssociationCount(t, pet, "Toy", 1, "AfterAppend") |
| 174 |
|
| 175 |
// Replace |
| 176 |
toy2 := Toy{Name: "toy-has-one-replace"} |
| 177 |
|
| 178 |
if err := DB.Model(&pet2).Association("Toy").Replace(&toy2); err != nil { |
| 179 |
t.Fatalf("Error happened when append Toy, got %v", err) |
| 180 |
} |
| 181 |
|
| 182 |
if toy2.ID == 0 { |
| 183 |
t.Fatalf("toy2's ID should be created") |
| 184 |
} |
| 185 |
|
| 186 |
pet.Toy = toy2 |
| 187 |
CheckPet(t, pet2, pet) |
| 188 |
|
| 189 |
AssertAssociationCount(t, pet2, "Toy", 1, "AfterReplace") |
| 190 |
|
| 191 |
// Delete |
| 192 |
if err := DB.Model(&pet2).Association("Toy").Delete(&Toy{}); err != nil { |
| 193 |
t.Fatalf("Error happened when delete toy, got %v", err) |
| 194 |
} |
| 195 |
AssertAssociationCount(t, pet2, "Toy", 1, "after delete non-existing data") |
| 196 |
|
| 197 |
if err := DB.Model(&pet2).Association("Toy").Delete(&toy2); err != nil { |
| 198 |
t.Fatalf("Error happened when delete Toy, got %v", err) |
| 199 |
} |
| 200 |
AssertAssociationCount(t, pet2, "Toy", 0, "after delete") |
| 201 |
|
| 202 |
// Prepare Data for Clear |
| 203 |
toy = Toy{Name: "toy-has-one-append"} |
| 204 |
if err := DB.Model(&pet2).Association("Toy").Append(&toy); err != nil { |
| 205 |
t.Fatalf("Error happened when append Toy, got %v", err) |
| 206 |
} |
| 207 |
|
| 208 |
AssertAssociationCount(t, pet2, "Toy", 1, "after prepare data") |
| 209 |
|
| 210 |
// Clear |
| 211 |
if err := DB.Model(&pet2).Association("Toy").Clear(); err != nil { |
| 212 |
t.Errorf("Error happened when clear Toy, got %v", err) |
| 213 |
} |
| 214 |
|
| 215 |
AssertAssociationCount(t, pet2, "Toy", 0, "after clear") |
| 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"}} |
| 261 |
|
| 262 |
if err := DB.Create(&user).Error; err != nil { |
| 263 |
t.Fatalf("errors happened when create: %v", err) |
| 264 |
} |
| 265 |
|
| 266 |
if err := DB.Model(&user).Association("Languages").Replace(Account{Number: "2"}); err == nil { |
| 267 |
t.Error("expected association error to be not nil") |
| 268 |
} |
| 269 |
} |