Skip to content

Commit a9943e9

Browse files
committed
test: add modify query test
1 parent 4777b42 commit a9943e9

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

tests/hooks_test.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -655,25 +655,28 @@ func (s *Product8) BeforeFind(tx *gorm.DB) error {
655655
return nil
656656
}
657657

658-
// Fails for postgres
659-
// ERROR: invalid input syntax for type bigint: "t" (SQLSTATE 22P02)
660658
func TestBeforeFindModifiesQuery(t *testing.T) {
661659
DB.Migrator().DropTable(&Product8{})
662660
DB.AutoMigrate(&Product8{})
663661

664-
products := []Product8{
665-
{Code: "A", Price: 100},
666-
{Code: "B", Price: 30},
662+
p1 := Product8{Code: "A", Price: 30}
663+
DB.Create(&p1)
664+
665+
var result Product8
666+
667+
DB.Find(&result)
668+
669+
if (result != Product8{}) {
670+
t.Errorf("BeforeFind should filter results, got %v", result)
667671
}
668-
DB.Create(&products)
669672

670-
var results []Product8
673+
p2 := Product8{Code: "B", Price: 100}
674+
DB.Create(&p2)
671675

672-
// Without condition, hooks will be skipped
673-
DB.Find(&results, true)
676+
DB.Find(&result)
674677

675-
if len(results) != 1 || results[0].Code != "A" {
676-
t.Errorf("BeforeFind should filter results, got %v", results)
678+
if result.Code != "B" {
679+
t.Errorf("BeforeFind should filter results, got %v", result)
677680
}
678681
}
679682

0 commit comments

Comments
 (0)