From 3fff4fbcd0d09189480071018092ed665ca17398 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Tue, 16 Jan 2024 20:19:33 +0530 Subject: [PATCH 1/6] tests: add tests for go/json2/marshal.go Signed-off-by: Manik Rana --- go/json2/marshal_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/go/json2/marshal_test.go b/go/json2/marshal_test.go index 96b7f508d73..620347cafb0 100644 --- a/go/json2/marshal_test.go +++ b/go/json2/marshal_test.go @@ -37,3 +37,19 @@ func TestMarshalPB(t *testing.T) { t.Errorf("MarshalPB(col): %q, want %q", b, want) } } + +func TestMarshalIndentPB(t *testing.T) { + col := &vschemapb.Column{ + Name: "c1", + Type: querypb.Type_VARCHAR, + } + indent := " " + b, err := MarshalIndentPB(col, indent) + if err != nil { + t.Fatal(err) + } + want := "{\n \"name\": \"c1\",\n \"type\": \"VARCHAR\"\n}" + if string(b) != want { + t.Errorf("MarshalIndentPB(col, indent): %q, want %q", b, want) + } +} From 5cbf86d039b9d5627d9d13eaf9257f2958125365 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 17 Jan 2024 07:50:26 +0530 Subject: [PATCH 2/6] refactor: use assert/require Signed-off-by: Manik Rana --- go/json2/marshal_test.go | 20 ++++++++------------ go/json2/unmarshal_test.go | 9 +++++---- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/go/json2/marshal_test.go b/go/json2/marshal_test.go index 620347cafb0..b87b16a0e7b 100644 --- a/go/json2/marshal_test.go +++ b/go/json2/marshal_test.go @@ -19,6 +19,8 @@ package json2 import ( "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" querypb "vitess.io/vitess/go/vt/proto/query" vschemapb "vitess.io/vitess/go/vt/proto/vschema" ) @@ -29,13 +31,10 @@ func TestMarshalPB(t *testing.T) { Type: querypb.Type_VARCHAR, } b, err := MarshalPB(col) - if err != nil { - t.Fatal(err) - } + + require.NoError(t, err, "MarshalPB(col) error") want := "{\"name\":\"c1\",\"type\":\"VARCHAR\"}" - if string(b) != want { - t.Errorf("MarshalPB(col): %q, want %q", b, want) - } + assert.Equal(t, want, string(b), "MarshalPB(col)") } func TestMarshalIndentPB(t *testing.T) { @@ -45,11 +44,8 @@ func TestMarshalIndentPB(t *testing.T) { } indent := " " b, err := MarshalIndentPB(col, indent) - if err != nil { - t.Fatal(err) - } + + require.NoError(t, err, "MarshalIndentPB(col, indent) error") want := "{\n \"name\": \"c1\",\n \"type\": \"VARCHAR\"\n}" - if string(b) != want { - t.Errorf("MarshalIndentPB(col, indent): %q, want %q", b, want) - } + assert.Equal(t, want, string(b), "MarshalIndentPB(col, indent)") } diff --git a/go/json2/unmarshal_test.go b/go/json2/unmarshal_test.go index 9b6a6af1ca2..c316cc40050 100644 --- a/go/json2/unmarshal_test.go +++ b/go/json2/unmarshal_test.go @@ -18,6 +18,8 @@ package json2 import ( "testing" + + "gotest.tools/assert" ) func TestUnmarshal(t *testing.T) { @@ -37,14 +39,13 @@ func TestUnmarshal(t *testing.T) { err: "", }} for _, tcase := range tcases { - out := make(map[string]any) + out := make(map[string]interface{}) err := Unmarshal([]byte(tcase.in), &out) + got := "" if err != nil { got = err.Error() } - if got != tcase.err { - t.Errorf("Unmarshal(%v) err: %v, want %v", tcase.in, got, tcase.err) - } + assert.Equal(t, tcase.err, got, "Unmarshal(%v) err", tcase.in) } } From 7e1fd94da0b1590b83e7b2b19095783366ccc54a Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 17 Jan 2024 07:51:57 +0530 Subject: [PATCH 3/6] tests: add TestAnnotate Signed-off-by: Manik Rana --- go/json2/unmarshal_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/go/json2/unmarshal_test.go b/go/json2/unmarshal_test.go index c316cc40050..453f492b54a 100644 --- a/go/json2/unmarshal_test.go +++ b/go/json2/unmarshal_test.go @@ -17,8 +17,10 @@ limitations under the License. package json2 import ( + "fmt" "testing" + "github.com/stretchr/testify/require" "gotest.tools/assert" ) @@ -49,3 +51,21 @@ func TestUnmarshal(t *testing.T) { assert.Equal(t, tcase.err, got, "Unmarshal(%v) err", tcase.in) } } + +func TestAnnotate(t *testing.T) { + tcases := []struct { + data []byte + err error + }{ + { + data: []byte("invalid JSON"), + err: fmt.Errorf("line: 1, position 1: invalid character 'i' looking for beginning of value"), + }, + } + + for _, tcase := range tcases { + err := annotate(tcase.data, tcase.err) + + require.Equal(t, tcase.err, err, "annotate(%s, %v) error", string(tcase.data), tcase.err) + } +} From 275f92fe1ce37e987a3a379997899b6959608bdb Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 17 Jan 2024 08:13:49 +0530 Subject: [PATCH 4/6] chore: lint imports Signed-off-by: Manik Rana --- go/json2/marshal_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/go/json2/marshal_test.go b/go/json2/marshal_test.go index b87b16a0e7b..067a6545954 100644 --- a/go/json2/marshal_test.go +++ b/go/json2/marshal_test.go @@ -21,6 +21,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + querypb "vitess.io/vitess/go/vt/proto/query" vschemapb "vitess.io/vitess/go/vt/proto/vschema" ) From 7db87f7715bfde939c8c23f1d67ae7d686a8f16b Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 17 Jan 2024 08:14:19 +0530 Subject: [PATCH 5/6] tests: add 100% coverage to go/json2 Signed-off-by: Manik Rana --- go/json2/unmarshal_test.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/go/json2/unmarshal_test.go b/go/json2/unmarshal_test.go index 453f492b54a..ff18a29def8 100644 --- a/go/json2/unmarshal_test.go +++ b/go/json2/unmarshal_test.go @@ -20,8 +20,11 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "gotest.tools/assert" + + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/types/known/emptypb" ) func TestUnmarshal(t *testing.T) { @@ -52,6 +55,25 @@ func TestUnmarshal(t *testing.T) { } } +func TestUnmarshalProto(t *testing.T) { + protoData := &emptypb.Empty{} + protoJSONData, err := protojson.Marshal(protoData) + assert.Nil(t, err, "protojson.Marshal error") + + tcase := struct { + in string + out *emptypb.Empty + }{ + in: string(protoJSONData), + out: &emptypb.Empty{}, + } + + err = Unmarshal([]byte(tcase.in), tcase.out) + + assert.Nil(t, err, "Unmarshal(%v) protobuf message", tcase.in) + assert.Equal(t, protoData, tcase.out, "Unmarshal(%v) protobuf message result", tcase.in) +} + func TestAnnotate(t *testing.T) { tcases := []struct { data []byte From 4b1bb2b278f8c2cb5832b1c5bd79acdebab36e98 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 17 Jan 2024 20:48:32 +0530 Subject: [PATCH 6/6] refactor: include actual arguments in tests Signed-off-by: Manik Rana --- go/json2/marshal_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go/json2/marshal_test.go b/go/json2/marshal_test.go index 067a6545954..b155126fb17 100644 --- a/go/json2/marshal_test.go +++ b/go/json2/marshal_test.go @@ -33,9 +33,9 @@ func TestMarshalPB(t *testing.T) { } b, err := MarshalPB(col) - require.NoError(t, err, "MarshalPB(col) error") + require.NoErrorf(t, err, "MarshalPB(%+v) error", col) want := "{\"name\":\"c1\",\"type\":\"VARCHAR\"}" - assert.Equal(t, want, string(b), "MarshalPB(col)") + assert.Equalf(t, want, string(b), "MarshalPB(%+v)", col) } func TestMarshalIndentPB(t *testing.T) { @@ -46,7 +46,7 @@ func TestMarshalIndentPB(t *testing.T) { indent := " " b, err := MarshalIndentPB(col, indent) - require.NoError(t, err, "MarshalIndentPB(col, indent) error") + require.NoErrorf(t, err, "MarshalIndentPB(%+v, %q) error", col, indent) want := "{\n \"name\": \"c1\",\n \"type\": \"VARCHAR\"\n}" - assert.Equal(t, want, string(b), "MarshalIndentPB(col, indent)") + assert.Equal(t, want, string(b), "MarshalIndentPB(%+v, %q)", col, indent) }