Skip to content

Commit

Permalink
add more tests for json_pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
jianzhiyao committed May 17, 2021
1 parent b27074d commit a3e5a9d
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions expression/builtin_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,3 +995,44 @@ func (s *testEvaluatorSuite) TestJSONStorageSize(c *C) {
}
}
}

func (s *testEvaluatorSuite) TestJSONPretty(c *C) {
fc := funcs[ast.JSONPretty]
tbl := []struct {
input []interface{}
expected interface{}
success bool
}{
// Tests scalar arguments
{[]interface{}{nil}, nil, true},
{[]interface{}{`true`}, "true", true},
{[]interface{}{`false`}, "false", true},
{[]interface{}{`2223`}, "2223", true},
// Tests simple json
{[]interface{}{`{"a":1}`}, "{\n \"a\": 1\n}", true},
{[]interface{}{`[1]`}, "[\n 1\n]", true},
// Test complex json
{[]interface{}{`{"a":1,"b":[{"d":1},{"e":2},{"f":3}],"c":"eee"}`}, "{\n \"a\": 1,\n \"b\": [\n {\n \"d\": 1\n },\n {\n \"e\": 2\n },\n {\n \"f\": 3\n }\n ],\n \"c\": \"eee\"\n}", true},
{[]interface{}{`{"a":1,"b":"qwe","c":[1,2,3,"123",null],"d":{"d1":1,"d2":2}}`}, "{\n \"a\": 1,\n \"b\": \"qwe\",\n \"c\": [\n 1,\n 2,\n 3,\n \"123\",\n null\n ],\n \"d\": {\n \"d1\": 1,\n \"d2\": 2\n }\n}", true},
// Tests invalid json data
{[]interface{}{`{1}`}, nil, false},
{[]interface{}{`[1,3,4,5]]`}, nil, false},
}
for _, t := range tbl {
args := types.MakeDatums(t.input...)
f, err := fc.getFunction(s.ctx, s.datumsToConstants(args))
c.Assert(err, IsNil)
d, err := evalBuiltinFunc(f, chunk.Row{})
if t.success {
c.Assert(err, IsNil)

if t.expected == nil {
c.Assert(d.IsNull(), IsTrue)
} else {
c.Assert(d.GetString(), Equals, t.expected.(string))
}
} else {
c.Assert(err, NotNil)
}
}
}

0 comments on commit a3e5a9d

Please sign in to comment.