completed the test file for check_subset_model_change_test.go
This commit is contained in:
parent
6edb577dc1
commit
16d14fe0b3
@ -2,42 +2,87 @@ package tests_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Man struct {
|
type Man struct {
|
||||||
ID int
|
ID int
|
||||||
Age int
|
Age int
|
||||||
Name string
|
Name string
|
||||||
|
Detail string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Panic-safe BeforeUpdate hook that checks for Changed("age")
|
||||||
|
func (m *Man) BeforeUpdate(tx *gorm.DB) (err error) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
err = fmt.Errorf("panic in BeforeUpdate: %v", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if !tx.Statement.Changed("age") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Man) update(data interface{}) error {
|
||||||
|
return DB.Set("data", data).Model(m).Where("id = ?", m.ID).Updates(data).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBeforeUpdateStatementChanged(t *testing.T) {
|
func TestBeforeUpdateStatementChanged(t *testing.T) {
|
||||||
|
DB.AutoMigrate(&Man{})
|
||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
BaseObjects Man
|
BaseObjects Man
|
||||||
change interface{}
|
change interface{}
|
||||||
|
expectError bool
|
||||||
}
|
}
|
||||||
fmt.Println("Running Eshan Jogwar Test")
|
|
||||||
testCases := []TestCase{
|
testCases := []TestCase{
|
||||||
{
|
{
|
||||||
BaseObjects: Man{ID: 12231234, Age: 18, Name: "random-name"},
|
BaseObjects: Man{ID: 1, Age: 18, Name: "random-name"},
|
||||||
change: struct {
|
change: struct {
|
||||||
Age int
|
Age int
|
||||||
}{Age: 20},
|
}{Age: 20},
|
||||||
|
expectError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
BaseObjects: Man{ID: 12231234, Age: 18, Name: "random-name"},
|
BaseObjects: Man{ID: 2, Age: 18, Name: "random-name"},
|
||||||
change: struct {
|
change: struct {
|
||||||
Age int
|
|
||||||
Name string
|
Name string
|
||||||
}{Age: 20, Name: "another-random-name"},
|
}{Name: "name-only"},
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
BaseObjects: Man{ID: 2, Age: 18, Name: "random-name"},
|
||||||
|
change: struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}{Name: "name-only", Age: 20},
|
||||||
|
expectError: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
// err := test.BaseObjects.update(test.change)
|
DB.Create(&test.BaseObjects)
|
||||||
err := DB.Set("data", test.change).Model(test.BaseObjects).Where("id = ?", test.BaseObjects.ID).Updates(test.change).Error
|
|
||||||
if err != nil {
|
// below comment is stored for future reference
|
||||||
t.Errorf(err.Error())
|
// err := DB.Set("data", test.change).Model(&test.BaseObjects).Where("id = ?", test.BaseObjects.ID).Updates(test.change).Error
|
||||||
|
err := test.BaseObjects.update(test.change)
|
||||||
|
if strings.Contains(fmt.Sprint(err), "panic in BeforeUpdate") {
|
||||||
|
if !test.expectError {
|
||||||
|
t.Errorf("unexpected panic in BeforeUpdate for input: %+v\nerror: %v", test.change, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if test.expectError {
|
||||||
|
t.Errorf("expected panic did not occur for input: %+v", test.change)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected GORM error: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user