fix: unit test (#6250)

* fix: unit test

* fix create test

https://github.com/go-gorm/gorm/pull/6127#discussion_r1171214125

* style: rename to adaptorSerializerModel
This commit is contained in:
black-06 2023-04-21 22:09:38 +08:00 committed by GitHub
parent e9637024d3
commit ac20d9e222
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 15 deletions

View File

@ -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)
}

View File

@ -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 => ../

View File

@ -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)
}