80 lines
1.7 KiB
Go
80 lines
1.7 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestDictKeysMan(t *testing.T) {
|
|
testData := map[string]interface{}{"k1": "v1", "k2": "v2"}
|
|
result1 := DictKeysMan(testData, []string{"k1"}, nil).(map[string]interface{})
|
|
if _, ok := result1["k1"]; ok {
|
|
t.Error("Params keysToRemove can not remove key!")
|
|
}
|
|
if k2Value, ok := result1["k2"]; !ok {
|
|
t.Error("Params keysToRemove removed wrong key!")
|
|
} else {
|
|
k2ValueStr, _ := k2Value.(string)
|
|
if k2ValueStr != "v2" {
|
|
t.Logf("k2Value:%s", k2ValueStr)
|
|
t.Error("wrong data when removeKeys")
|
|
}
|
|
}
|
|
|
|
result2 := DictKeysMan([]interface{}{testData, map[string]string{"k1": "v11", "k2": "v22"}}, nil, []string{"k1"}).([]interface{})
|
|
result20 := result2[0].(map[string]interface{})
|
|
if _, ok := result20["k1"]; !ok {
|
|
t.Error("Params keysToKeep can not keep key!")
|
|
}
|
|
if _, ok := result20["k2"]; ok {
|
|
t.Error("Params keysToKeep keep wrong key!")
|
|
}
|
|
}
|
|
|
|
type TestModel struct {
|
|
IntData int
|
|
Int64Data int64
|
|
FloatData float64
|
|
StrData string
|
|
}
|
|
|
|
type TestModel2 struct {
|
|
IntData int
|
|
Int64Data int64
|
|
FloatData float64
|
|
AddData string
|
|
}
|
|
|
|
func unmarshStr(t *testing.T, jsonStr string, obj interface{}) {
|
|
|
|
err := UnmarshalUseNumber([]byte(jsonStr), obj)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
} else {
|
|
fmt.Println(obj)
|
|
}
|
|
}
|
|
|
|
func TestJson(t *testing.T) {
|
|
jsonStr := `
|
|
{
|
|
"IntData": 1,
|
|
"Int64Data": 1234567890123456,
|
|
"FloatData": 1234.5678,
|
|
"StrData": "str"
|
|
}
|
|
`
|
|
obj1 := make(map[string]interface{})
|
|
var obj2 interface{}
|
|
var obj3 TestModel
|
|
var obj4 TestModel2
|
|
|
|
unmarshStr(t, jsonStr, &obj1)
|
|
unmarshStr(t, jsonStr, &obj2)
|
|
unmarshStr(t, jsonStr, &obj3)
|
|
unmarshStr(t, jsonStr, &obj4)
|
|
t.Skip("skip the flollowing")
|
|
t.Fatal(1)
|
|
t.Fail()
|
|
}
|