2022-02-16 10:30:43 +03:00
|
|
|
package tests_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/schema"
|
|
|
|
. "gorm.io/gorm/utils/tests"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SerializerStruct struct {
|
|
|
|
gorm.Model
|
2022-06-25 11:32:47 +03:00
|
|
|
Name []byte `gorm:"json"`
|
|
|
|
Roles Roles `gorm:"serializer:json"`
|
2022-10-18 10:44:47 +03:00
|
|
|
Roles2 *Roles `gorm:"serializer:json"`
|
2022-10-18 11:25:39 +03:00
|
|
|
Roles3 *Roles `gorm:"serializer:json;not null"`
|
2022-06-25 11:32:47 +03:00
|
|
|
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
|
|
|
|
CustomSerializerString string `gorm:"serializer:custom"`
|
|
|
|
EncryptedString EncryptedString
|
2022-02-16 10:30:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Roles []string
|
2022-02-24 05:21:27 +03:00
|
|
|
|
|
|
|
type Job struct {
|
|
|
|
Title string
|
|
|
|
Number int
|
|
|
|
Location string
|
|
|
|
IsIntern bool
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
type EncryptedString string
|
|
|
|
|
|
|
|
func (es *EncryptedString) Scan(ctx context.Context, field *schema.Field, dst reflect.Value, dbValue interface{}) (err error) {
|
|
|
|
switch value := dbValue.(type) {
|
|
|
|
case []byte:
|
|
|
|
*es = EncryptedString(bytes.TrimPrefix(value, []byte("hello")))
|
|
|
|
case string:
|
|
|
|
*es = EncryptedString(strings.TrimPrefix(value, "hello"))
|
|
|
|
default:
|
2022-03-18 12:12:17 +03:00
|
|
|
return fmt.Errorf("unsupported data %#v", dbValue)
|
2022-02-16 10:30:43 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es EncryptedString) Value(ctx context.Context, field *schema.Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) {
|
|
|
|
return "hello" + string(es), nil
|
|
|
|
}
|
|
|
|
|
2022-06-25 11:32:47 +03:00
|
|
|
type CustomSerializer struct {
|
|
|
|
prefix []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCustomSerializer(prefix string) *CustomSerializer {
|
|
|
|
return &CustomSerializer{prefix: []byte(prefix)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CustomSerializer) Scan(ctx context.Context, field *schema.Field, dst reflect.Value, dbValue interface{}) (err error) {
|
|
|
|
switch value := dbValue.(type) {
|
|
|
|
case []byte:
|
|
|
|
err = field.Set(ctx, dst, bytes.TrimPrefix(value, c.prefix))
|
|
|
|
case string:
|
|
|
|
err = field.Set(ctx, dst, strings.TrimPrefix(value, string(c.prefix)))
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unsupported data %#v", dbValue)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CustomSerializer) Value(ctx context.Context, field *schema.Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) {
|
|
|
|
return fmt.Sprintf("%s%s", c.prefix, fieldValue), nil
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
func TestSerializer(t *testing.T) {
|
2022-06-25 11:32:47 +03:00
|
|
|
schema.RegisterSerializer("custom", NewCustomSerializer("hello"))
|
2022-02-16 10:30:43 +03:00
|
|
|
DB.Migrator().DropTable(&SerializerStruct{})
|
|
|
|
if err := DB.Migrator().AutoMigrate(&SerializerStruct{}); err != nil {
|
|
|
|
t.Fatalf("no error should happen when migrate scanner, valuer struct, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
createdAt := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
2022-04-11 17:07:40 +03:00
|
|
|
updatedAt := createdAt.Unix()
|
2022-02-16 10:30:43 +03:00
|
|
|
|
|
|
|
data := SerializerStruct{
|
|
|
|
Name: []byte("jinzhu"),
|
|
|
|
Roles: []string{"r1", "r2"},
|
|
|
|
Contracts: map[string]interface{}{"name": "jinzhu", "age": 10},
|
|
|
|
EncryptedString: EncryptedString("pass"),
|
|
|
|
CreatedTime: createdAt.Unix(),
|
2022-04-11 17:07:40 +03:00
|
|
|
UpdatedTime: &updatedAt,
|
2022-02-24 05:21:27 +03:00
|
|
|
JobInfo: Job{
|
|
|
|
Title: "programmer",
|
|
|
|
Number: 9920,
|
|
|
|
Location: "Kenmawr",
|
|
|
|
IsIntern: false,
|
|
|
|
},
|
2022-06-25 11:32:47 +03:00
|
|
|
CustomSerializerString: "world",
|
2022-02-16 10:30:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Create(&data).Error; err != nil {
|
|
|
|
t.Fatalf("failed to create data, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result SerializerStruct
|
2022-10-18 11:25:39 +03:00
|
|
|
if err := DB.Where("roles2 IS NULL AND roles3 = ?", "").First(&result, data.ID).Error; err != nil {
|
2022-07-27 08:59:47 +03:00
|
|
|
t.Fatalf("failed to query data, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertEqual(t, result, data)
|
|
|
|
|
|
|
|
if err := DB.Model(&result).Update("roles", "").Error; err != nil {
|
|
|
|
t.Fatalf("failed to update data's roles, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.First(&result, data.ID).Error; err != nil {
|
|
|
|
t.Fatalf("failed to query data, got error %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSerializerZeroValue(t *testing.T) {
|
|
|
|
schema.RegisterSerializer("custom", NewCustomSerializer("hello"))
|
|
|
|
DB.Migrator().DropTable(&SerializerStruct{})
|
|
|
|
if err := DB.Migrator().AutoMigrate(&SerializerStruct{}); err != nil {
|
|
|
|
t.Fatalf("no error should happen when migrate scanner, valuer struct, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := SerializerStruct{}
|
|
|
|
|
|
|
|
if err := DB.Create(&data).Error; err != nil {
|
|
|
|
t.Fatalf("failed to create data, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result SerializerStruct
|
2022-02-16 10:30:43 +03:00
|
|
|
if err := DB.First(&result, data.ID).Error; err != nil {
|
|
|
|
t.Fatalf("failed to query data, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertEqual(t, result, data)
|
2022-07-11 06:51:05 +03:00
|
|
|
|
|
|
|
if err := DB.Model(&result).Update("roles", "").Error; err != nil {
|
|
|
|
t.Fatalf("failed to update data's roles, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.First(&result, data.ID).Error; err != nil {
|
|
|
|
t.Fatalf("failed to query data, got error %v", err)
|
|
|
|
}
|
2022-03-18 12:12:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSerializerAssignFirstOrCreate(t *testing.T) {
|
2022-06-25 11:32:47 +03:00
|
|
|
schema.RegisterSerializer("custom", NewCustomSerializer("hello"))
|
2022-03-18 12:12:17 +03:00
|
|
|
DB.Migrator().DropTable(&SerializerStruct{})
|
|
|
|
if err := DB.Migrator().AutoMigrate(&SerializerStruct{}); err != nil {
|
|
|
|
t.Fatalf("no error should happen when migrate scanner, valuer struct, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
createdAt := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
data := SerializerStruct{
|
|
|
|
Name: []byte("ag9920"),
|
|
|
|
Roles: []string{"r1", "r2"},
|
|
|
|
Contracts: map[string]interface{}{"name": "jing1", "age": 11},
|
|
|
|
EncryptedString: EncryptedString("pass"),
|
|
|
|
CreatedTime: createdAt.Unix(),
|
|
|
|
JobInfo: Job{
|
|
|
|
Title: "programmer",
|
|
|
|
Number: 9920,
|
|
|
|
Location: "Shadyside",
|
|
|
|
IsIntern: false,
|
|
|
|
},
|
2022-06-25 11:32:47 +03:00
|
|
|
CustomSerializerString: "world",
|
2022-03-18 12:12:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// first time insert record
|
|
|
|
out := SerializerStruct{}
|
|
|
|
if err := DB.Assign(data).FirstOrCreate(&out).Error; err != nil {
|
|
|
|
t.Fatalf("failed to FirstOrCreate Assigned data, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result SerializerStruct
|
|
|
|
if err := DB.First(&result, out.ID).Error; err != nil {
|
|
|
|
t.Fatalf("failed to query data, got error %v", err)
|
|
|
|
}
|
|
|
|
AssertEqual(t, result, out)
|
|
|
|
|
2022-07-01 10:12:15 +03:00
|
|
|
// update record
|
2022-03-18 12:12:17 +03:00
|
|
|
data.Roles = append(data.Roles, "r3")
|
|
|
|
data.JobInfo.Location = "Gates Hillman Complex"
|
|
|
|
if err := DB.Assign(data).FirstOrCreate(&out).Error; err != nil {
|
|
|
|
t.Fatalf("failed to FirstOrCreate Assigned data, got error %v", err)
|
|
|
|
}
|
|
|
|
if err := DB.First(&result, out.ID).Error; err != nil {
|
|
|
|
t.Fatalf("failed to query data, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertEqual(t, result.Roles, data.Roles)
|
|
|
|
AssertEqual(t, result.JobInfo.Location, data.JobInfo.Location)
|
2022-02-16 10:30:43 +03:00
|
|
|
}
|