| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"log" |
| 6 |
"os" |
| 7 |
"reflect" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"gorm.io/gorm" |
| 12 |
. "gorm.io/gorm/utils/tests" |
| 13 |
) |
| 14 |
|
| 15 |
type Product struct { |
| 16 |
gorm.Model |
| 17 |
Name string |
| 18 |
Code string |
| 19 |
Price float64 |
| 20 |
AfterFindCallTimes int64 |
| 21 |
BeforeCreateCallTimes int64 |
| 22 |
AfterCreateCallTimes int64 |
| 23 |
BeforeUpdateCallTimes int64 |
| 24 |
AfterUpdateCallTimes int64 |
| 25 |
BeforeSaveCallTimes int64 |
| 26 |
AfterSaveCallTimes int64 |
| 27 |
BeforeDeleteCallTimes int64 |
| 28 |
AfterDeleteCallTimes int64 |
| 29 |
} |
| 30 |
|
| 31 |
func (s *Product) BeforeCreate(tx *gorm.DB) (err error) { |
| 32 |
if s.Code == "Invalid" { |
| 33 |
err = errors.New("invalid product") |
| 34 |
} |
| 35 |
s.BeforeCreateCallTimes = s.BeforeCreateCallTimes + 1 |
| 36 |
return |
| 37 |
} |
| 38 |
|
| 39 |
func (s *Product) BeforeUpdate(tx *gorm.DB) (err error) { |
| 40 |
if s.Code == "dont_update" { |
| 41 |
err = errors.New("can't update") |
| 42 |
} |
| 43 |
s.BeforeUpdateCallTimes = s.BeforeUpdateCallTimes + 1 |
| 44 |
return |
| 45 |
} |
| 46 |
|
| 47 |
func (s *Product) BeforeSave(tx *gorm.DB) (err error) { |
| 48 |
if s.Code == "dont_save" { |
| 49 |
err = errors.New("can't save") |
| 50 |
} |
| 51 |
s.BeforeSaveCallTimes = s.BeforeSaveCallTimes + 1 |
| 52 |
return |
| 53 |
} |
| 54 |
|
| 55 |
func (s *Product) AfterFind(tx *gorm.DB) (err error) { |
| 56 |
s.AfterFindCallTimes = s.AfterFindCallTimes + 1 |
| 57 |
return |
| 58 |
} |
| 59 |
|
| 60 |
func (s *Product) AfterCreate(tx *gorm.DB) (err error) { |
| 61 |
return tx.Model(s).UpdateColumn("AfterCreateCallTimes", s.AfterCreateCallTimes+1).Error |
| 62 |
} |
| 63 |
|
| 64 |
func (s *Product) AfterUpdate(tx *gorm.DB) (err error) { |
| 65 |
s.AfterUpdateCallTimes = s.AfterUpdateCallTimes + 1 |
| 66 |
return |
| 67 |
} |
| 68 |
|
| 69 |
func (s *Product) AfterSave(tx *gorm.DB) (err error) { |
| 70 |
if s.Code == "after_save_error" { |
| 71 |
err = errors.New("can't save") |
| 72 |
} |
| 73 |
s.AfterSaveCallTimes = s.AfterSaveCallTimes + 1 |
| 74 |
return |
| 75 |
} |
| 76 |
|
| 77 |
func (s *Product) BeforeDelete(tx *gorm.DB) (err error) { |
| 78 |
if s.Code == "dont_delete" { |
| 79 |
err = errors.New("can't delete") |
| 80 |
} |
| 81 |
s.BeforeDeleteCallTimes = s.BeforeDeleteCallTimes + 1 |
| 82 |
return |
| 83 |
} |
| 84 |
|
| 85 |
func (s *Product) AfterDelete(tx *gorm.DB) (err error) { |
| 86 |
if s.Code == "after_delete_error" { |
| 87 |
err = errors.New("can't delete") |
| 88 |
} |
| 89 |
s.AfterDeleteCallTimes = s.AfterDeleteCallTimes + 1 |
| 90 |
return |
| 91 |
} |
| 92 |
|
| 93 |
func (s *Product) GetCallTimes() []int64 { |
| 94 |
return []int64{s.BeforeCreateCallTimes, s.BeforeSaveCallTimes, s.BeforeUpdateCallTimes, s.AfterCreateCallTimes, s.AfterSaveCallTimes, s.AfterUpdateCallTimes, s.BeforeDeleteCallTimes, s.AfterDeleteCallTimes, s.AfterFindCallTimes} |
| 95 |
} |
| 96 |
|
| 97 |
func TestRunCallbacks(t *testing.T) { |
| 98 |
DB.Migrator().DropTable(&Product{}) |
| 99 |
DB.AutoMigrate(&Product{}) |
| 100 |
|
| 101 |
p := Product{Code: "unique_code", Price: 100} |
| 102 |
DB.Save(&p) |
| 103 |
|
| 104 |
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 1, 0, 1, 1, 0, 0, 0, 0}) { |
| 105 |
t.Fatalf("Callbacks should be invoked successfully, %v", p.GetCallTimes()) |
| 106 |
} |
| 107 |
|
| 108 |
DB.Where("Code = ?", "unique_code").First(&p) |
| 109 |
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 1, 0, 1, 0, 0, 0, 0, 1}) { |
| 110 |
t.Fatalf("After callbacks values are not saved, %v", p.GetCallTimes()) |
| 111 |
} |
| 112 |
|
| 113 |
p.Price = 200 |
| 114 |
DB.Save(&p) |
| 115 |
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 1, 1, 0, 0, 1}) { |
| 116 |
t.Fatalf("After update callbacks should be invoked successfully, %v", p.GetCallTimes()) |
| 117 |
} |
| 118 |
|
| 119 |
var products []Product |
| 120 |
DB.Find(&products, "code = ?", "unique_code") |
| 121 |
if products[0].AfterFindCallTimes != 2 { |
| 122 |
t.Fatalf("AfterFind callbacks should work with slice, called %v", products[0].AfterFindCallTimes) |
| 123 |
} |
| 124 |
|
| 125 |
DB.Where("Code = ?", "unique_code").First(&p) |
| 126 |
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 0, 0, 0, 0, 2}) { |
| 127 |
t.Fatalf("After update callbacks values are not saved, %v", p.GetCallTimes()) |
| 128 |
} |
| 129 |
|
| 130 |
DB.Delete(&p) |
| 131 |
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 0, 0, 1, 1, 2}) { |
| 132 |
t.Fatalf("After delete callbacks should be invoked successfully, %v", p.GetCallTimes()) |
| 133 |
} |
| 134 |
|
| 135 |
if DB.Where("Code = ?", "unique_code").First(&p).Error == nil { |
| 136 |
t.Fatalf("Can't find a deleted record") |
| 137 |
} |
| 138 |
|
| 139 |
beforeCallTimes := p.AfterFindCallTimes |
| 140 |
if DB.Where("Code = ?", "unique_code").Find(&p).Error != nil { |
| 141 |
t.Fatalf("Find don't raise error when record not found") |
| 142 |
} |
| 143 |
|
| 144 |
if p.AfterFindCallTimes != beforeCallTimes { |
| 145 |
t.Fatalf("AfterFind should not be called") |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestCallbacksWithErrors(t *testing.T) { |
| 150 |
DB.Migrator().DropTable(&Product{}) |
| 151 |
DB.AutoMigrate(&Product{}) |
| 152 |
|
| 153 |
p := Product{Code: "Invalid", Price: 100} |
| 154 |
if DB.Save(&p).Error == nil { |
| 155 |
t.Fatalf("An error from before create callbacks happened when create with invalid value") |
| 156 |
} |
| 157 |
|
| 158 |
if DB.Where("code = ?", "Invalid").First(&Product{}).Error == nil { |
| 159 |
t.Fatalf("Should not save record that have errors") |
| 160 |
} |
| 161 |
|
| 162 |
if DB.Save(&Product{Code: "dont_save", Price: 100}).Error == nil { |
| 163 |
t.Fatalf("An error from after create callbacks happened when create with invalid value") |
| 164 |
} |
| 165 |
|
| 166 |
p2 := Product{Code: "update_callback", Price: 100} |
| 167 |
DB.Save(&p2) |
| 168 |
|
| 169 |
p2.Code = "dont_update" |
| 170 |
if DB.Save(&p2).Error == nil { |
| 171 |
t.Fatalf("An error from before update callbacks happened when update with invalid value") |
| 172 |
} |
| 173 |
|
| 174 |
if DB.Where("code = ?", "update_callback").First(&Product{}).Error != nil { |
| 175 |
t.Fatalf("Record Should not be updated due to errors happened in before update callback") |
| 176 |
} |
| 177 |
|
| 178 |
if DB.Where("code = ?", "dont_update").First(&Product{}).Error == nil { |
| 179 |
t.Fatalf("Record Should not be updated due to errors happened in before update callback") |
| 180 |
} |
| 181 |
|
| 182 |
p2.Code = "dont_save" |
| 183 |
if DB.Save(&p2).Error == nil { |
| 184 |
t.Fatalf("An error from before save callbacks happened when update with invalid value") |
| 185 |
} |
| 186 |
|
| 187 |
p3 := Product{Code: "dont_delete", Price: 100} |
| 188 |
DB.Save(&p3) |
| 189 |
if DB.Delete(&p3).Error == nil { |
| 190 |
t.Fatalf("An error from before delete callbacks happened when delete") |
| 191 |
} |
| 192 |
|
| 193 |
if DB.Where("Code = ?", "dont_delete").First(&p3).Error != nil { |
| 194 |
t.Fatalf("An error from before delete callbacks happened") |
| 195 |
} |
| 196 |
|
| 197 |
p4 := Product{Code: "after_save_error", Price: 100} |
| 198 |
DB.Save(&p4) |
| 199 |
if err := DB.First(&Product{}, "code = ?", "after_save_error").Error; err == nil { |
| 200 |
t.Fatalf("Record should be reverted if get an error in after save callback") |
| 201 |
} |
| 202 |
|
| 203 |
p5 := Product{Code: "after_delete_error", Price: 100} |
| 204 |
DB.Save(&p5) |
| 205 |
if err := DB.First(&Product{}, "code = ?", "after_delete_error").Error; err != nil { |
| 206 |
t.Fatalf("Record should be found") |
| 207 |
} |
| 208 |
|
| 209 |
DB.Delete(&p5) |
| 210 |
if err := DB.First(&Product{}, "code = ?", "after_delete_error").Error; err != nil { |
| 211 |
t.Fatalf("Record shouldn't be deleted because of an error happened in after delete callback") |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
type Product2 struct { |
| 216 |
gorm.Model |
| 217 |
Name string |
| 218 |
Code string |
| 219 |
Price int64 |
| 220 |
Owner string |
| 221 |
} |
| 222 |
|
| 223 |
func (s Product2) BeforeCreate(tx *gorm.DB) (err error) { |
| 224 |
if !strings.HasSuffix(s.Name, "_clone") { |
| 225 |
newProduft := s |
| 226 |
newProduft.Price *= 2 |
| 227 |
newProduft.Name += "_clone" |
| 228 |
err = tx.Create(&newProduft).Error |
| 229 |
} |
| 230 |
|
| 231 |
if s.Name == "Invalid" { |
| 232 |
return errors.New("invalid") |
| 233 |
} |
| 234 |
|
| 235 |
return nil |
| 236 |
} |
| 237 |
|
| 238 |
func (s *Product2) BeforeUpdate(tx *gorm.DB) (err error) { |
| 239 |
tx.Statement.Where("owner != ?", "admin") |
| 240 |
return |
| 241 |
} |
| 242 |
|
| 243 |
func TestUseDBInHooks(t *testing.T) { |
| 244 |
DB.Migrator().DropTable(&Product2{}) |
| 245 |
DB.AutoMigrate(&Product2{}) |
| 246 |
|
| 247 |
product := Product2{Name: "Invalid", Price: 100} |
| 248 |
|
| 249 |
if err := DB.Create(&product).Error; err == nil { |
| 250 |
t.Fatalf("should returns error %v when creating product, but got nil", err) |
| 251 |
} |
| 252 |
|
| 253 |
product2 := Product2{Name: "Nice", Price: 100} |
| 254 |
|
| 255 |
if err := DB.Create(&product2).Error; err != nil { |
| 256 |
t.Fatalf("Failed to create product, got error: %v", err) |
| 257 |
} |
| 258 |
|
| 259 |
var result Product2 |
| 260 |
if err := DB.First(&result, "name = ?", "Nice").Error; err != nil { |
| 261 |
t.Fatalf("Failed to query product, got error: %v", err) |
| 262 |
} |
| 263 |
|
| 264 |
var resultClone Product2 |
| 265 |
if err := DB.First(&resultClone, "name = ?", "Nice_clone").Error; err != nil { |
| 266 |
t.Fatalf("Failed to find cloned product, got error: %v", err) |
| 267 |
} |
| 268 |
|
| 269 |
result.Price *= 2 |
| 270 |
result.Name += "_clone" |
| 271 |
AssertObjEqual(t, result, resultClone, "Price", "Name") |
| 272 |
|
| 273 |
DB.Model(&result).Update("Price", 500) |
| 274 |
var result2 Product2 |
| 275 |
DB.First(&result2, "name = ?", "Nice") |
| 276 |
|
| 277 |
if result2.Price != 500 { |
| 278 |
t.Errorf("Failed to update product's price, expects: %v, got %v", 500, result2.Price) |
| 279 |
} |
| 280 |
|
| 281 |
product3 := Product2{Name: "Nice2", Price: 600, Owner: "admin"} |
| 282 |
if err := DB.Create(&product3).Error; err != nil { |
| 283 |
t.Fatalf("Failed to create product, got error: %v", err) |
| 284 |
} |
| 285 |
|
| 286 |
var result3 Product2 |
| 287 |
if err := DB.First(&result3, "name = ?", "Nice2").Error; err != nil { |
| 288 |
t.Fatalf("Failed to query product, got error: %v", err) |
| 289 |
} |
| 290 |
|
| 291 |
DB.Model(&result3).Update("Price", 800) |
| 292 |
var result4 Product2 |
| 293 |
DB.First(&result4, "name = ?", "Nice2") |
| 294 |
|
| 295 |
if result4.Price != 600 { |
| 296 |
t.Errorf("Admin product's price should not be changed, expects: %v, got %v", 600, result4.Price) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
type Product3 struct { |
| 301 |
gorm.Model |
| 302 |
Name string |
| 303 |
Code string |
| 304 |
Price int64 |
| 305 |
Owner string |
| 306 |
} |
| 307 |
|
| 308 |
func (s Product3) BeforeCreate(tx *gorm.DB) (err error) { |
| 309 |
tx.Statement.SetColumn("Price", s.Price+100) |
| 310 |
return nil |
| 311 |
} |
| 312 |
|
| 313 |
func (s Product3) BeforeUpdate(tx *gorm.DB) (err error) { |
| 314 |
if tx.Statement.Changed() { |
| 315 |
tx.Statement.SetColumn("Price", s.Price+10) |
| 316 |
} |
| 317 |
|
| 318 |
if tx.Statement.Changed("Code") { |
| 319 |
s.Price += 20 |
| 320 |
tx.Statement.SetColumn("Price", s.Price+30) |
| 321 |
} |
| 322 |
return nil |
| 323 |
} |
| 324 |
|
| 325 |
func TestSetColumn(t *testing.T) { |
| 326 |
DB.Migrator().DropTable(&Product3{}) |
| 327 |
DB.AutoMigrate(&Product3{}) |
| 328 |
|
| 329 |
product := Product3{Name: "Product", Price: 0} |
| 330 |
DB.Create(&product) |
| 331 |
|
| 332 |
if product.Price != 100 { |
| 333 |
t.Errorf("invalid price after create, got %+v", product) |
| 334 |
} |
| 335 |
|
| 336 |
DB.Model(&product).Select("code", "price").Updates(map[string]interface{}{"code": "L1212"}) |
| 337 |
|
| 338 |
if product.Price != 150 || product.Code != "L1212" { |
| 339 |
t.Errorf("invalid data after update, got %+v", product) |
| 340 |
} |
| 341 |
|
| 342 |
// Code not changed, price should not change |
| 343 |
DB.Model(&product).Updates(map[string]interface{}{"Name": "Product New"}) |
| 344 |
|
| 345 |
if product.Name != "Product New" || product.Price != 160 || product.Code != "L1212" { |
| 346 |
t.Errorf("invalid data after update, got %+v", product) |
| 347 |
} |
| 348 |
|
| 349 |
// Code changed, but not selected, price should not change |
| 350 |
DB.Model(&product).Select("Name", "Price").Updates(map[string]interface{}{"Name": "Product New2", "code": "L1213"}) |
| 351 |
|
| 352 |
if product.Name != "Product New2" || product.Price != 170 || product.Code != "L1212" { |
| 353 |
t.Errorf("invalid data after update, got %+v", product) |
| 354 |
} |
| 355 |
|
| 356 |
// Code changed, price should changed |
| 357 |
DB.Model(&product).Select("Name", "Code", "Price").Updates(map[string]interface{}{"Name": "Product New3", "code": "L1213"}) |
| 358 |
|
| 359 |
if product.Name != "Product New3" || product.Price != 220 || product.Code != "L1213" { |
| 360 |
t.Errorf("invalid data after update, got %+v", product) |
| 361 |
} |
| 362 |
|
| 363 |
var result Product3 |
| 364 |
DB.First(&result, product.ID) |
| 365 |
|
| 366 |
AssertEqual(t, result, product) |
| 367 |
|
| 368 |
// Select to change Code, but nothing updated, price should not change |
| 369 |
DB.Model(&product).Select("code").Updates(Product3{Name: "L1214", Code: "L1213"}) |
| 370 |
|
| 371 |
if product.Price != 220 || product.Code != "L1213" || product.Name != "Product New3" { |
| 372 |
t.Errorf("invalid data after update, got %+v", product) |
| 373 |
} |
| 374 |
|
| 375 |
DB.Model(&product).Updates(Product3{Code: "L1214"}) |
| 376 |
if product.Price != 270 || product.Code != "L1214" { |
| 377 |
t.Errorf("invalid data after update, got %+v", product) |
| 378 |
} |
| 379 |
|
| 380 |
// Code changed, price should changed |
| 381 |
DB.Model(&product).Select("Name", "Code", "Price").Updates(Product3{Name: "Product New4", Code: ""}) |
| 382 |
if product.Name != "Product New4" || product.Price != 320 || product.Code != "" { |
| 383 |
t.Errorf("invalid data after update, got %+v", product) |
| 384 |
} |
| 385 |
|
| 386 |
DB.Model(&product).UpdateColumns(Product3{Code: "L1215"}) |
| 387 |
if product.Price != 320 || product.Code != "L1215" { |
| 388 |
t.Errorf("invalid data after update, got %+v", product) |
| 389 |
} |
| 390 |
|
| 391 |
DB.Model(&product).Session(&gorm.Session{SkipHooks: true}).Updates(Product3{Code: "L1216"}) |
| 392 |
if product.Price != 320 || product.Code != "L1216" { |
| 393 |
t.Errorf("invalid data after update, got %+v", product) |
| 394 |
} |
| 395 |
|
| 396 |
var result2 Product3 |
| 397 |
DB.First(&result2, product.ID) |
| 398 |
|
| 399 |
AssertEqual(t, result2, product) |
| 400 |
|
| 401 |
product2 := Product3{Name: "Product", Price: 0} |
| 402 |
DB.Session(&gorm.Session{SkipHooks: true}).Create(&product2) |
| 403 |
|
| 404 |
if product2.Price != 0 { |
| 405 |
t.Errorf("invalid price after create without hooks, got %+v", product2) |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
func TestHooksForSlice(t *testing.T) { |
| 410 |
DB.Migrator().DropTable(&Product3{}) |
| 411 |
DB.AutoMigrate(&Product3{}) |
| 412 |
|
| 413 |
products := []*Product3{ |
| 414 |
{Name: "Product-1", Price: 100}, |
| 415 |
{Name: "Product-2", Price: 200}, |
| 416 |
{Name: "Product-3", Price: 300}, |
| 417 |
} |
| 418 |
|
| 419 |
DB.Create(&products) |
| 420 |
|
| 421 |
for idx, value := range []int64{200, 300, 400} { |
| 422 |
if products[idx].Price != value { |
| 423 |
t.Errorf("invalid price for product #%v, expects: %v, got %v", idx, value, products[idx].Price) |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
DB.Model(&products).Update("Name", "product-name") |
| 428 |
|
| 429 |
// will set all product's price to last product's price + 10 |
| 430 |
for idx, value := range []int64{410, 410, 410} { |
| 431 |
if products[idx].Price != value { |
| 432 |
t.Errorf("invalid price for product #%v, expects: %v, got %v", idx, value, products[idx].Price) |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
products2 := []Product3{ |
| 437 |
{Name: "Product-1", Price: 100}, |
| 438 |
{Name: "Product-2", Price: 200}, |
| 439 |
{Name: "Product-3", Price: 300}, |
| 440 |
} |
| 441 |
|
| 442 |
DB.Create(&products2) |
| 443 |
|
| 444 |
for idx, value := range []int64{200, 300, 400} { |
| 445 |
if products2[idx].Price != value { |
| 446 |
t.Errorf("invalid price for product #%v, expects: %v, got %v", idx, value, products2[idx].Price) |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
DB.Model(&products2).Update("Name", "product-name") |
| 451 |
|
| 452 |
// will set all product's price to last product's price + 10 |
| 453 |
for idx, value := range []int64{410, 410, 410} { |
| 454 |
if products2[idx].Price != value { |
| 455 |
t.Errorf("invalid price for product #%v, expects: %v, got %v", idx, value, products2[idx].Price) |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
type Product4 struct { |
| 461 |
gorm.Model |
| 462 |
Name string |
| 463 |
Code string |
| 464 |
Price int64 |
| 465 |
Owner string |
| 466 |
Item ProductItem |
| 467 |
} |
| 468 |
|
| 469 |
type ProductItem struct { |
| 470 |
gorm.Model |
| 471 |
Code string |
| 472 |
Product4ID uint |
| 473 |
AfterFindCallTimes int |
| 474 |
} |
| 475 |
|
| 476 |
func (pi ProductItem) BeforeCreate(*gorm.DB) error { |
| 477 |
if pi.Code == "invalid" { |
| 478 |
return errors.New("invalid item") |
| 479 |
} |
| 480 |
return nil |
| 481 |
} |
| 482 |
|
| 483 |
func (pi *ProductItem) AfterFind(*gorm.DB) error { |
| 484 |
pi.AfterFindCallTimes = pi.AfterFindCallTimes + 1 |
| 485 |
return nil |
| 486 |
} |
| 487 |
|
| 488 |
func TestFailedToSaveAssociationShouldRollback(t *testing.T) { |
| 489 |
DB.Migrator().DropTable(&Product4{}, &ProductItem{}) |
| 490 |
DB.AutoMigrate(&Product4{}, &ProductItem{}) |
| 491 |
|
| 492 |
product := Product4{Name: "Product-1", Price: 100, Item: ProductItem{Code: "invalid"}} |
| 493 |
if err := DB.Create(&product).Error; err == nil { |
| 494 |
t.Errorf("should got failed to save, but error is nil") |
| 495 |
} |
| 496 |
|
| 497 |
if DB.First(&Product4{}, "name = ?", product.Name).Error == nil { |
| 498 |
t.Errorf("should got RecordNotFound, but got nil") |
| 499 |
} |
| 500 |
|
| 501 |
product = Product4{Name: "Product-2", Price: 100, Item: ProductItem{Code: "valid"}} |
| 502 |
if err := DB.Create(&product).Error; err != nil { |
| 503 |
t.Errorf("should create product, but got error %v", err) |
| 504 |
} |
| 505 |
|
| 506 |
if err := DB.First(&Product4{}, "name = ?", product.Name).Error; err != nil { |
| 507 |
t.Errorf("should find product, but got error %v", err) |
| 508 |
} |
| 509 |
|
| 510 |
var productWithItem Product4 |
| 511 |
if err := DB.Session(&gorm.Session{SkipHooks: true}).Preload("Item").First(&productWithItem, "name = ?", product.Name).Error; err != nil { |
| 512 |
t.Errorf("should find product, but got error %v", err) |
| 513 |
} |
| 514 |
|
| 515 |
if productWithItem.Item.AfterFindCallTimes != 0 { |
| 516 |
t.Fatalf("AfterFind should not be called times:%d", productWithItem.Item.AfterFindCallTimes) |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
type Product5 struct { |
| 521 |
gorm.Model |
| 522 |
Name string |
| 523 |
} |
| 524 |
|
| 525 |
var beforeUpdateCall int |
| 526 |
|
| 527 |
func (p *Product5) BeforeUpdate(*gorm.DB) error { |
| 528 |
beforeUpdateCall = beforeUpdateCall + 1 |
| 529 |
return nil |
| 530 |
} |
| 531 |
|
| 532 |
func TestUpdateCallbacks(t *testing.T) { |
| 533 |
DB.Migrator().DropTable(&Product5{}) |
| 534 |
DB.AutoMigrate(&Product5{}) |
| 535 |
|
| 536 |
p := Product5{Name: "unique_code"} |
| 537 |
DB.Model(&Product5{}).Create(&p) |
| 538 |
|
| 539 |
err := DB.Model(&Product5{}).Where("id", p.ID).Update("name", "update_name_1").Error |
| 540 |
if err != nil { |
| 541 |
t.Fatalf("should update success, but got err %v", err) |
| 542 |
} |
| 543 |
if beforeUpdateCall != 1 { |
| 544 |
t.Fatalf("before update should be called") |
| 545 |
} |
| 546 |
|
| 547 |
err = DB.Model(Product5{}).Where("id", p.ID).Update("name", "update_name_2").Error |
| 548 |
if !errors.Is(err, gorm.ErrInvalidValue) { |
| 549 |
t.Fatalf("should got RecordNotFound, but got %v", err) |
| 550 |
} |
| 551 |
if beforeUpdateCall != 1 { |
| 552 |
t.Fatalf("before update should not be called") |
| 553 |
} |
| 554 |
|
| 555 |
err = DB.Model([1]*Product5{&p}).Update("name", "update_name_3").Error |
| 556 |
if err != nil { |
| 557 |
t.Fatalf("should update success, but got err %v", err) |
| 558 |
} |
| 559 |
if beforeUpdateCall != 2 { |
| 560 |
t.Fatalf("before update should be called") |
| 561 |
} |
| 562 |
|
| 563 |
err = DB.Model([1]Product5{p}).Update("name", "update_name_4").Error |
| 564 |
if !errors.Is(err, gorm.ErrInvalidValue) { |
| 565 |
t.Fatalf("should got RecordNotFound, but got %v", err) |
| 566 |
} |
| 567 |
if beforeUpdateCall != 2 { |
| 568 |
t.Fatalf("before update should not be called") |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
type Product6 struct { |
| 573 |
gorm.Model |
| 574 |
Name string |
| 575 |
Item *ProductItem2 |
| 576 |
} |
| 577 |
|
| 578 |
type ProductItem2 struct { |
| 579 |
gorm.Model |
| 580 |
Product6ID uint |
| 581 |
} |
| 582 |
|
| 583 |
func (p *Product6) BeforeDelete(tx *gorm.DB) error { |
| 584 |
if err := tx.Delete(&p.Item).Error; err != nil { |
| 585 |
return err |
| 586 |
} |
| 587 |
return nil |
| 588 |
} |
| 589 |
|
| 590 |
func TestPropagateUnscoped(t *testing.T) { |
| 591 |
_DB, err := OpenTestConnection(&gorm.Config{ |
| 592 |
PropagateUnscoped: true, |
| 593 |
}) |
| 594 |
if err != nil { |
| 595 |
log.Printf("failed to connect database, got error %v", err) |
| 596 |
os.Exit(1) |
| 597 |
} |
| 598 |
|
| 599 |
_DB.Migrator().DropTable(&Product6{}, &ProductItem2{}) |
| 600 |
_DB.AutoMigrate(&Product6{}, &ProductItem2{}) |
| 601 |
|
| 602 |
p := Product6{ |
| 603 |
Name: "unique_code", |
| 604 |
Item: &ProductItem2{}, |
| 605 |
} |
| 606 |
_DB.Model(&Product6{}).Create(&p) |
| 607 |
|
| 608 |
if err := _DB.Unscoped().Delete(&p).Error; err != nil { |
| 609 |
t.Fatalf("unscoped did not propagate") |
| 610 |
} |
| 611 |
} |