Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
demoManito committed Aug 26, 2024
1 parent 158e86c commit 39efcb8
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions tests/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,35 +610,35 @@ func TestPropagateUnscoped(t *testing.T) {
}
}

type UserUpdate struct {
type StructUpdate struct {
ID uint `gorm:"column:id;primary_key"`
Version int `gorm:"column:version"`
Name string `gorm:"column:name"`
}

func (UserUpdate) TableName() string {
return "user_updates"
func (StructUpdate) TableName() string {
return "struct_updates"
}

func (up *UserUpdate) BeforeUpdate(tx *gorm.DB) error {
func (up *StructUpdate) BeforeUpdate(tx *gorm.DB) error {
up.Version++
return nil
}

func TestBeforeUpdateWithStructColumn(t *testing.T) {
DB.Migrator().DropTable(&UserUpdate{})
DB.AutoMigrate(&UserUpdate{})
DB.Migrator().DropTable(&StructUpdate{})
DB.AutoMigrate(&StructUpdate{})

user := UserUpdate{
su := StructUpdate{
ID: 1,
Version: 1,
}
DB.Create(&user)
DB.Create(&su)

sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
return DB.Model(&user).Update("name", "demoManito")
return DB.Model(&su).Update("name", "demoManito")
})
if sql != "UPDATE `user_updates` SET `user_updates`.`name` = `demoManito`, `user_updates`.`version` = 2 WHERE `user_updates`.`id` = 1" {
if sql != "UPDATE `struct_updates` SET `user_updates`.`name` = `demoManito`, `user_updates`.`version` = 2 WHERE `user_updates`.`id` = 1" {
t.Fatalf("invalid sql: %v", sql)
}
}

0 comments on commit 39efcb8

Please sign in to comment.