add testcase

This commit is contained in:
Caelansar 2020-08-10 15:34:20 +08:00
parent 39c8d6220b
commit 15b96ed3f4
1 changed files with 53 additions and 16 deletions

View File

@ -36,6 +36,8 @@ func TestScannerValuer(t *testing.T) {
{"name2", "value2"}, {"name2", "value2"},
}, },
Role: Role{Name: "admin"}, Role: Role{Name: "admin"},
ExampleStruct: ExampleStruct1{"name", "value"},
ExampleStructPtr: &ExampleStruct1{"name", "value"},
} }
if err := DB.Create(&data).Error; err != nil { if err := DB.Create(&data).Error; err != nil {
@ -49,6 +51,14 @@ func TestScannerValuer(t *testing.T) {
} }
AssertObjEqual(t, data, result, "Name", "Gender", "Age", "Male", "Height", "Birthday", "Password", "Bytes", "Num", "Strings", "Structs") AssertObjEqual(t, data, result, "Name", "Gender", "Age", "Male", "Height", "Birthday", "Password", "Bytes", "Num", "Strings", "Structs")
if result.ExampleStructPtr.Val != "value" {
t.Errorf(`ExampleStructPtr.Val should equal to "value", but got %v`, result.ExampleStructPtr.Val)
}
if result.ExampleStruct.Val != "value" {
t.Errorf(`ExampleStruct.Val should equal to "value", but got %v`, result.ExampleStruct.Val)
}
} }
func TestScannerValuerWithFirstOrCreate(t *testing.T) { func TestScannerValuerWithFirstOrCreate(t *testing.T) {
@ -139,6 +149,8 @@ type ScannerValuerStruct struct {
UserID *sql.NullInt64 UserID *sql.NullInt64
User User User User
EmptyTime EmptyTime EmptyTime EmptyTime
ExampleStruct ExampleStruct1
ExampleStructPtr *ExampleStruct1
} }
type EncryptedData []byte type EncryptedData []byte
@ -207,6 +219,31 @@ type ExampleStruct struct {
Value string Value string
} }
type ExampleStruct1 struct {
Name string `json:"name,omitempty"`
Val string `json:"val,omitempty"`
}
func (s ExampleStruct1) Value() (driver.Value, error) {
if len(s.Name) == 0 {
return nil, nil
}
//for test, has no practical meaning
s.Name = ""
return json.Marshal(s)
}
func (s *ExampleStruct1) Scan(src interface{}) error {
switch value := src.(type) {
case string:
return json.Unmarshal([]byte(value), s)
case []byte:
return json.Unmarshal(value, s)
default:
return errors.New("not supported")
}
}
type StructsSlice []ExampleStruct type StructsSlice []ExampleStruct
func (l StructsSlice) Value() (driver.Value, error) { func (l StructsSlice) Value() (driver.Value, error) {