作者 tangxuhui

添加测试文件

package k3cloud
import (
"encoding/json"
"log"
"testing"
"github.com/tidwall/gjson"
)
func TestDecodeJson1(t *testing.T) {
//
str := `[[{"Result":{"ResponseStatus":{"ErrorCode":500,"IsSuccess":false,"Errors":[{"FieldName":null,"Message":"元数据中标识为FUseOrg的字段不存在","DIndex":0}],"SuccessEntitys":[],"SuccessMessages":[],"MsgCode":9}}}]]`
jResult := gjson.Parse(str)
arr1 := jResult.Array()
if len(arr1) == 0 {
return
}
arr2 := arr1[0].Array()
if len(arr2) == 0 {
return
}
var errResult QueryError
if !arr2[0].IsObject() {
return
}
rw := arr2[0].Raw
err := json.Unmarshal([]byte(rw), &errResult)
if err != nil {
return
}
log.Println(errResult)
}
func TestDecodeJson2(t *testing.T) {
str := `[["xxx","abc",2345]]`
jResult := gjson.Parse(str)
jResult.ForEach(func(key, value1 gjson.Result) bool {
value1.ForEach(func(key, value2 gjson.Result) bool {
log.Println(key.Int(), value2.String())
return true
})
return true
})
}
package k3cloud
import "testing"
func TestErrorResult(t *testing.T) {
jsonByte := []byte(`[[{"Result":{"ResponseStatus":{"ErrorCode":500,"IsSuccess":false,"Errors":[{"FieldName":null,"Message":"元数据中标识为FUseOrg的字段不存在","DIndex":0}],"SuccessEntitys":[],"SuccessMessages":[],"MsgCode":9}}}]]`)
result := newQueryResult(jsonByte, "")
if !result.IsError() {
t.Error("没有正确识别错误的消息数据")
return
}
m := result.ToMap()
if len(m) > 0 {
t.Error("解析了错误的数据")
return
}
e := result.Error()
if e == nil {
t.Error("没有正常的获得错误信息")
return
}
t.Logf("%v", result.Error())
}
func TestRightResult(t *testing.T) {
jsonByte := []byte(`[["xxx","abc",2345],["xxa","abc1",23457],["xxb","abc2",23458]]`)
result := newQueryResult(jsonByte, "name,desc,number")
if result.IsError() {
t.Error("数据解析失败")
return
}
m := result.ToMapString()
if len(m) == 0 {
t.Error("没有返回数据")
return
}
t.Logf("%v", m)
}
... ...