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 58ffab8 commit 86a95e5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
1 change: 0 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import "time"
// gorm.Model
// }
type Model struct {
ID uint `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt DeletedAt `gorm:"index"`
Expand Down
31 changes: 30 additions & 1 deletion tests/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestRunCallbacks(t *testing.T) {
}

p.Price = 200
DB.Debug().Save(&p)
DB.Save(&p)
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 1, 1, 0, 0, 1}) {
t.Fatalf("After update callbacks should be invoked successfully, %v", p.GetCallTimes())
}
Expand Down Expand Up @@ -609,3 +609,32 @@ func TestPropagateUnscoped(t *testing.T) {
t.Fatalf("unscoped did not propagate")
}
}

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

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

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

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

sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
return DB.Model(&user).Update("name", "demoManito")
})
if sql != "UPDATE `user_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 86a95e5

Please sign in to comment.