From ac20d9e222400d7ad1963251b4aa2c589afe6901 Mon Sep 17 00:00:00 2001 From: black-06 Date: Fri, 21 Apr 2023 22:09:38 +0800 Subject: [PATCH] fix: unit test (#6250) * fix: unit test * fix create test https://github.com/go-gorm/gorm/pull/6127#discussion_r1171214125 * style: rename to adaptorSerializerModel --- tests/create_test.go | 3 +++ tests/go.mod | 13 ++++++------- tests/serializer_test.go | 40 ++++++++++++++++++++++++++++++++-------- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/tests/create_test.go b/tests/create_test.go index 75aa8cba..02613b72 100644 --- a/tests/create_test.go +++ b/tests/create_test.go @@ -556,6 +556,9 @@ func TestCreateWithAutoIncrementCompositeKey(t *testing.T) { Name string } + if err := DB.Migrator().DropTable(&CompositeKeyProduct{}); err != nil { + t.Fatalf("failed to migrate, got error %v", err) + } if err := DB.AutoMigrate(&CompositeKeyProduct{}); err != nil { t.Fatalf("failed to migrate, got error %v", err) } diff --git a/tests/go.mod b/tests/go.mod index 306a530e..f47d175f 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -6,15 +6,14 @@ require ( github.com/google/uuid v1.3.0 github.com/jackc/pgx/v5 v5.3.1 // indirect github.com/jinzhu/now v1.1.5 - github.com/lib/pq v1.10.7 + github.com/lib/pq v1.10.8 github.com/mattn/go-sqlite3 v1.14.16 // indirect - github.com/microsoft/go-mssqldb v0.20.0 // indirect - golang.org/x/crypto v0.7.0 // indirect - gorm.io/driver/mysql v1.4.7 + golang.org/x/crypto v0.8.0 // indirect + gorm.io/driver/mysql v1.5.0 gorm.io/driver/postgres v1.5.0 - gorm.io/driver/sqlite v1.4.4 - gorm.io/driver/sqlserver v1.4.2 - gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11 + gorm.io/driver/sqlite v1.5.0 + gorm.io/driver/sqlserver v1.4.3 + gorm.io/gorm v1.25.0 ) replace gorm.io/gorm => ../ diff --git a/tests/serializer_test.go b/tests/serializer_test.go index a040a4db..f1b8a336 100644 --- a/tests/serializer_test.go +++ b/tests/serializer_test.go @@ -22,12 +22,36 @@ type SerializerStruct struct { Roles3 *Roles `gorm:"serializer:json;not null"` Contracts map[string]interface{} `gorm:"serializer:json"` JobInfo Job `gorm:"type:bytes;serializer:gob"` - CreatedTime int64 `gorm:"serializer:unixtime;type:time"` // store time in db, use int as field type - UpdatedTime *int64 `gorm:"serializer:unixtime;type:time"` // store time in db, use int as field type + CreatedTime int64 `gorm:"serializer:unixtime;type:datetime"` // store time in db, use int as field type + UpdatedTime *int64 `gorm:"serializer:unixtime;type:datetime"` // store time in db, use int as field type CustomSerializerString string `gorm:"serializer:custom"` EncryptedString EncryptedString } +type SerializerPostgresStruct struct { + gorm.Model + Name []byte `gorm:"json"` + Roles Roles `gorm:"serializer:json"` + Roles2 *Roles `gorm:"serializer:json"` + Roles3 *Roles `gorm:"serializer:json;not null"` + Contracts map[string]interface{} `gorm:"serializer:json"` + JobInfo Job `gorm:"type:bytes;serializer:gob"` + CreatedTime int64 `gorm:"serializer:unixtime;type:timestamptz"` // store time in db, use int as field type + UpdatedTime *int64 `gorm:"serializer:unixtime;type:timestamptz"` // store time in db, use int as field type + CustomSerializerString string `gorm:"serializer:custom"` + EncryptedString EncryptedString +} + +func (*SerializerPostgresStruct) TableName() string { return "serializer_structs" } + +func adaptorSerializerModel(s *SerializerStruct) interface{} { + if DB.Dialector.Name() == "postgres" { + sps := SerializerPostgresStruct(*s) + return &sps + } + return s +} + type Roles []string type Job struct { @@ -81,8 +105,8 @@ func (c *CustomSerializer) Value(ctx context.Context, field *schema.Field, dst r func TestSerializer(t *testing.T) { schema.RegisterSerializer("custom", NewCustomSerializer("hello")) - DB.Migrator().DropTable(&SerializerStruct{}) - if err := DB.Migrator().AutoMigrate(&SerializerStruct{}); err != nil { + DB.Migrator().DropTable(adaptorSerializerModel(&SerializerStruct{})) + if err := DB.Migrator().AutoMigrate(adaptorSerializerModel(&SerializerStruct{})); err != nil { t.Fatalf("no error should happen when migrate scanner, valuer struct, got error %v", err) } @@ -127,8 +151,8 @@ func TestSerializer(t *testing.T) { func TestSerializerZeroValue(t *testing.T) { schema.RegisterSerializer("custom", NewCustomSerializer("hello")) - DB.Migrator().DropTable(&SerializerStruct{}) - if err := DB.Migrator().AutoMigrate(&SerializerStruct{}); err != nil { + DB.Migrator().DropTable(adaptorSerializerModel(&SerializerStruct{})) + if err := DB.Migrator().AutoMigrate(adaptorSerializerModel(&SerializerStruct{})); err != nil { t.Fatalf("no error should happen when migrate scanner, valuer struct, got error %v", err) } @@ -156,8 +180,8 @@ func TestSerializerZeroValue(t *testing.T) { func TestSerializerAssignFirstOrCreate(t *testing.T) { schema.RegisterSerializer("custom", NewCustomSerializer("hello")) - DB.Migrator().DropTable(&SerializerStruct{}) - if err := DB.Migrator().AutoMigrate(&SerializerStruct{}); err != nil { + DB.Migrator().DropTable(adaptorSerializerModel(&SerializerStruct{})) + if err := DB.Migrator().AutoMigrate(adaptorSerializerModel(&SerializerStruct{})); err != nil { t.Fatalf("no error should happen when migrate scanner, valuer struct, got error %v", err) }