正在显示
2 个修改的文件
包含
38 行增加
和
46 行删除
1 | -package k3cloud | ||
2 | - | ||
3 | -import ( | ||
4 | - "encoding/json" | ||
5 | - "log" | ||
6 | - "testing" | ||
7 | - | ||
8 | - "github.com/tidwall/gjson" | ||
9 | -) | ||
10 | - | ||
11 | -func TestDecodeJson1(t *testing.T) { | ||
12 | - // | ||
13 | - str := `[[{"Result":{"ResponseStatus":{"ErrorCode":500,"IsSuccess":false,"Errors":[{"FieldName":null,"Message":"元数据中标识为FUseOrg的字段不存在","DIndex":0}],"SuccessEntitys":[],"SuccessMessages":[],"MsgCode":9}}}]]` | ||
14 | - jResult := gjson.Parse(str) | ||
15 | - arr1 := jResult.Array() | ||
16 | - if len(arr1) == 0 { | ||
17 | - return | ||
18 | - } | ||
19 | - arr2 := arr1[0].Array() | ||
20 | - if len(arr2) == 0 { | ||
21 | - return | ||
22 | - } | ||
23 | - var errResult QueryError | ||
24 | - if !arr2[0].IsObject() { | ||
25 | - return | ||
26 | - } | ||
27 | - rw := arr2[0].Raw | ||
28 | - err := json.Unmarshal([]byte(rw), &errResult) | ||
29 | - if err != nil { | ||
30 | - return | ||
31 | - } | ||
32 | - log.Println(errResult) | ||
33 | -} | ||
34 | - | ||
35 | -func TestDecodeJson2(t *testing.T) { | ||
36 | - str := `[["xxx","abc",2345]]` | ||
37 | - jResult := gjson.Parse(str) | ||
38 | - jResult.ForEach(func(key, value1 gjson.Result) bool { | ||
39 | - value1.ForEach(func(key, value2 gjson.Result) bool { | ||
40 | - log.Println(key.Int(), value2.String()) | ||
41 | - return true | ||
42 | - }) | ||
43 | - return true | ||
44 | - }) | ||
45 | - | ||
46 | -} |
1 | +package k3cloud | ||
2 | + | ||
3 | +import "testing" | ||
4 | + | ||
5 | +func TestErrorResult(t *testing.T) { | ||
6 | + jsonByte := []byte(`[[{"Result":{"ResponseStatus":{"ErrorCode":500,"IsSuccess":false,"Errors":[{"FieldName":null,"Message":"元数据中标识为FUseOrg的字段不存在","DIndex":0}],"SuccessEntitys":[],"SuccessMessages":[],"MsgCode":9}}}]]`) | ||
7 | + result := newQueryResult(jsonByte, "") | ||
8 | + if !result.IsError() { | ||
9 | + t.Error("没有正确识别错误的消息数据") | ||
10 | + return | ||
11 | + } | ||
12 | + m := result.ToMap() | ||
13 | + if len(m) > 0 { | ||
14 | + t.Error("解析了错误的数据") | ||
15 | + return | ||
16 | + } | ||
17 | + e := result.Error() | ||
18 | + if e == nil { | ||
19 | + t.Error("没有正常的获得错误信息") | ||
20 | + return | ||
21 | + } | ||
22 | + t.Logf("%v", result.Error()) | ||
23 | +} | ||
24 | + | ||
25 | +func TestRightResult(t *testing.T) { | ||
26 | + jsonByte := []byte(`[["xxx","abc",2345],["xxa","abc1",23457],["xxb","abc2",23458]]`) | ||
27 | + result := newQueryResult(jsonByte, "name,desc,number") | ||
28 | + if result.IsError() { | ||
29 | + t.Error("数据解析失败") | ||
30 | + return | ||
31 | + } | ||
32 | + m := result.ToMapString() | ||
33 | + if len(m) == 0 { | ||
34 | + t.Error("没有返回数据") | ||
35 | + return | ||
36 | + } | ||
37 | + t.Logf("%v", m) | ||
38 | +} |
-
请 注册 或 登录 后发表评论