test: add modify query test

This commit is contained in:
MhmdGol 2025-02-16 17:09:41 +03:30
parent 4777b42b5b
commit a9943e9a5e

View File

@ -655,25 +655,28 @@ func (s *Product8) BeforeFind(tx *gorm.DB) error {
return nil return nil
} }
// Fails for postgres
// ERROR: invalid input syntax for type bigint: "t" (SQLSTATE 22P02)
func TestBeforeFindModifiesQuery(t *testing.T) { func TestBeforeFindModifiesQuery(t *testing.T) {
DB.Migrator().DropTable(&Product8{}) DB.Migrator().DropTable(&Product8{})
DB.AutoMigrate(&Product8{}) DB.AutoMigrate(&Product8{})
products := []Product8{ p1 := Product8{Code: "A", Price: 30}
{Code: "A", Price: 100}, DB.Create(&p1)
{Code: "B", Price: 30},
var result Product8
DB.Find(&result)
if (result != Product8{}) {
t.Errorf("BeforeFind should filter results, got %v", result)
} }
DB.Create(&products)
var results []Product8 p2 := Product8{Code: "B", Price: 100}
DB.Create(&p2)
// Without condition, hooks will be skipped DB.Find(&result)
DB.Find(&results, true)
if len(results) != 1 || results[0].Code != "A" { if result.Code != "B" {
t.Errorf("BeforeFind should filter results, got %v", results) t.Errorf("BeforeFind should filter results, got %v", result)
} }
} }