test(MigrateColumn): mock alter column to improve field compare (#5499)

* test(MigrateColumn): mock alter column to improve field compare

* Update migrate_test.go

* Update migrate_test.go

* Update migrate_test.go

Co-authored-by: Jinzhu <wosmvp@gmail.com>
This commit is contained in:
Cr 2022-12-24 17:42:16 +08:00 committed by GitHub
parent 1935eb0adb
commit 794edad60e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 45 additions and 2 deletions

View File

@ -11,7 +11,6 @@ import (
"time" "time"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/logger" "gorm.io/gorm/logger"
"gorm.io/gorm/schema" "gorm.io/gorm/schema"
@ -29,7 +28,7 @@ func TestMigrate(t *testing.T) {
} }
if err := DB.AutoMigrate(allModels...); err != nil { if err := DB.AutoMigrate(allModels...); err != nil {
t.Fatalf("Failed to auto migrate, but got error %v", err) t.Fatalf("Failed to auto migrate, got error %v", err)
} }
if tables, err := DB.Migrator().GetTables(); err != nil { if tables, err := DB.Migrator().GetTables(); err != nil {
@ -1123,6 +1122,50 @@ func TestMigrateArrayTypeModel(t *testing.T) {
AssertEqual(t, "integer[]", ct.DatabaseTypeName()) AssertEqual(t, "integer[]", ct.DatabaseTypeName())
} }
type mockMigrator struct {
gorm.Migrator
}
func (mm mockMigrator) AlterColumn(dst interface{}, field string) error {
err := mm.Migrator.AlterColumn(dst, field)
if err != nil {
return err
}
return fmt.Errorf("trigger alter column error, field: %s", field)
}
func TestMigrateDonotAlterColumn(t *testing.T) {
var wrapMockMigrator = func(m gorm.Migrator) mockMigrator {
return mockMigrator{
Migrator: m,
}
}
m := DB.Migrator()
mockM := wrapMockMigrator(m)
type NotTriggerUpdate struct {
ID uint
F1 uint16
F2 uint32
F3 int
F4 int64
F5 string
F6 float32
F7 float64
F8 time.Time
F9 bool
F10 []byte
}
var err error
err = mockM.DropTable(&NotTriggerUpdate{})
AssertEqual(t, err, nil)
err = mockM.AutoMigrate(&NotTriggerUpdate{})
AssertEqual(t, err, nil)
err = mockM.AutoMigrate(&NotTriggerUpdate{})
AssertEqual(t, err, nil)
}
func TestMigrateSameEmbeddedFieldName(t *testing.T) { func TestMigrateSameEmbeddedFieldName(t *testing.T) {
type UserStat struct { type UserStat struct {
GroundDestroyCount int GroundDestroyCount int