Skip to content

Commit 8b7188a

Browse files
authored
tests: add tests for go/json2 (#14964)
Signed-off-by: Manik Rana <manikrana54@gmail.com>
1 parent b15e9f6 commit 8b7188a

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

go/json2/marshal_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ package json2
1919
import (
2020
"testing"
2121

22+
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
24+
2225
querypb "vitess.io/vitess/go/vt/proto/query"
2326
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
2427
)
@@ -29,11 +32,21 @@ func TestMarshalPB(t *testing.T) {
2932
Type: querypb.Type_VARCHAR,
3033
}
3134
b, err := MarshalPB(col)
32-
if err != nil {
33-
t.Fatal(err)
34-
}
35+
36+
require.NoErrorf(t, err, "MarshalPB(%+v) error", col)
3537
want := "{\"name\":\"c1\",\"type\":\"VARCHAR\"}"
36-
if string(b) != want {
37-
t.Errorf("MarshalPB(col): %q, want %q", b, want)
38+
assert.Equalf(t, want, string(b), "MarshalPB(%+v)", col)
39+
}
40+
41+
func TestMarshalIndentPB(t *testing.T) {
42+
col := &vschemapb.Column{
43+
Name: "c1",
44+
Type: querypb.Type_VARCHAR,
3845
}
46+
indent := " "
47+
b, err := MarshalIndentPB(col, indent)
48+
49+
require.NoErrorf(t, err, "MarshalIndentPB(%+v, %q) error", col, indent)
50+
want := "{\n \"name\": \"c1\",\n \"type\": \"VARCHAR\"\n}"
51+
assert.Equal(t, want, string(b), "MarshalIndentPB(%+v, %q)", col, indent)
3952
}

go/json2/unmarshal_test.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ limitations under the License.
1717
package json2
1818

1919
import (
20+
"fmt"
2021
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
26+
"google.golang.org/protobuf/encoding/protojson"
27+
"google.golang.org/protobuf/types/known/emptypb"
2128
)
2229

2330
func TestUnmarshal(t *testing.T) {
@@ -37,14 +44,50 @@ func TestUnmarshal(t *testing.T) {
3744
err: "",
3845
}}
3946
for _, tcase := range tcases {
40-
out := make(map[string]any)
47+
out := make(map[string]interface{})
4148
err := Unmarshal([]byte(tcase.in), &out)
49+
4250
got := ""
4351
if err != nil {
4452
got = err.Error()
4553
}
46-
if got != tcase.err {
47-
t.Errorf("Unmarshal(%v) err: %v, want %v", tcase.in, got, tcase.err)
48-
}
54+
assert.Equal(t, tcase.err, got, "Unmarshal(%v) err", tcase.in)
55+
}
56+
}
57+
58+
func TestUnmarshalProto(t *testing.T) {
59+
protoData := &emptypb.Empty{}
60+
protoJSONData, err := protojson.Marshal(protoData)
61+
assert.Nil(t, err, "protojson.Marshal error")
62+
63+
tcase := struct {
64+
in string
65+
out *emptypb.Empty
66+
}{
67+
in: string(protoJSONData),
68+
out: &emptypb.Empty{},
69+
}
70+
71+
err = Unmarshal([]byte(tcase.in), tcase.out)
72+
73+
assert.Nil(t, err, "Unmarshal(%v) protobuf message", tcase.in)
74+
assert.Equal(t, protoData, tcase.out, "Unmarshal(%v) protobuf message result", tcase.in)
75+
}
76+
77+
func TestAnnotate(t *testing.T) {
78+
tcases := []struct {
79+
data []byte
80+
err error
81+
}{
82+
{
83+
data: []byte("invalid JSON"),
84+
err: fmt.Errorf("line: 1, position 1: invalid character 'i' looking for beginning of value"),
85+
},
86+
}
87+
88+
for _, tcase := range tcases {
89+
err := annotate(tcase.data, tcase.err)
90+
91+
require.Equal(t, tcase.err, err, "annotate(%s, %v) error", string(tcase.data), tcase.err)
4992
}
5093
}

0 commit comments

Comments
 (0)