From aff174b085a10acc601f6359e1ef2d9d9ea35764 Mon Sep 17 00:00:00 2001 From: Danis Kirasirov <dbgbbu@gmail.com> Date: Thu, 21 Dec 2023 17:13:48 +0300 Subject: [PATCH] add tests --- go.mod | 1 + go.sum | 2 + pkg/extension/action.go | 9 ++++- pkg/extension/action_test.go | 77 ++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 pkg/extension/action_test.go diff --git a/go.mod b/go.mod index 63de9afb..fcffea8f 100644 --- a/go.mod +++ b/go.mod @@ -32,6 +32,7 @@ require ( require ( cloud.google.com/go/compute v1.23.3 // indirect cloud.google.com/go/compute/metadata v0.2.3 // indirect + github.com/brianvoe/gofakeit/v6 v6.26.3 github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-kit/log v0.2.1 // indirect github.com/go-logfmt/logfmt v0.6.0 // indirect diff --git a/go.sum b/go.sum index 3273d5c5..fcb538d7 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/avast/retry-go/v4 v4.5.1 h1:AxIx0HGi4VZ3I02jr78j5lZ3M6x1E0Ivxa6b0pUUh github.com/avast/retry-go/v4 v4.5.1/go.mod h1:/sipNsvNB3RRuT5iNcb6h73nw3IBmXJ/H3XrCQYSOpc= github.com/bep/gowebp v0.2.0 h1:ZVfK8i9PpZqKHEmthQSt3qCnnHycbLzBPEsVtk2ch2Q= github.com/bep/gowebp v0.2.0/go.mod h1:ZhFodwdiFp8ehGJpF4LdPl6unxZm9lLFjxD3z2h2AgI= +github.com/brianvoe/gofakeit/v6 v6.26.3 h1:3ljYrjPwsUNAUFdUIr2jVg5EhKdcke/ZLop7uVg1Er8= +github.com/brianvoe/gofakeit/v6 v6.26.3/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/pkg/extension/action.go b/pkg/extension/action.go index b81df1fb..a8062d3d 100644 --- a/pkg/extension/action.go +++ b/pkg/extension/action.go @@ -113,8 +113,15 @@ type Action struct { } func ActionToMap(action *Action) map[string]interface{} { + if action == nil { + return nil + } + res := make(map[string]interface{}) - _ = mapstructure.Decode(action, &res) + if err := mapstructure.Decode(action, &res); err != nil { + return nil + } + res["kind"] = int64(action.Kind.Number()) res["target"] = int64(action.Target.Number()) res["view"] = int64(action.View.Number()) diff --git a/pkg/extension/action_test.go b/pkg/extension/action_test.go new file mode 100644 index 00000000..3741b5af --- /dev/null +++ b/pkg/extension/action_test.go @@ -0,0 +1,77 @@ +package extension + +import ( + "testing" + + pb "git.perx.ru/perxis/perxis-go/proto/extensions" + "github.com/brianvoe/gofakeit/v6" + "github.com/stretchr/testify/require" +) + +func TestAction(t *testing.T) { + t.Run("Protobuf", func(t *testing.T) { + var pbAction *pb.Action + gofakeit.Struct(&pbAction) + action := ActionFromPB(pbAction) + result := ActionToPB(action) + require.Equal(t, pbAction, result) + }) + + t.Run("Nil Protobuf", func(t *testing.T) { + var pbAction *pb.Action + action := ActionFromPB(pbAction) + result := ActionToPB(action) + require.Equal(t, pbAction, result) + }) + + t.Run("Map", func(t *testing.T) { + var action *Action + gofakeit.Struct(&action) + dict := ActionToMap(action) + result, err := ActionFromMap(dict) + require.NoError(t, err) + require.Equal(t, action, result) + }) + + t.Run("Nil Map", func(t *testing.T) { + var action *Action + dict := ActionToMap(action) + result, err := ActionFromMap(dict) + require.NoError(t, err) + require.Equal(t, &Action{}, result) + }) +} + +func TestActionRequest(t *testing.T) { + t.Run("Protobuf", func(t *testing.T) { + var pbAction *pb.ActionRequest + gofakeit.Struct(&pbAction) + action := ActionRequestFromPB(pbAction) + result := ActionRequestToPB(action) + require.Equal(t, pbAction, result) + }) + + t.Run("Nil", func(t *testing.T) { + var pbAction *pb.ActionRequest + action := ActionRequestFromPB(pbAction) + result := ActionRequestToPB(action) + require.Equal(t, pbAction, result) + }) +} + +func TestActionResponse(t *testing.T) { + t.Run("Protobuf", func(t *testing.T) { + var pbAction *pb.ActionResponse + gofakeit.Struct(&pbAction) + action := ActionResponseFromPB(pbAction) + result := ActionResponseToPB(action) + require.Equal(t, pbAction, result) + }) + + t.Run("Nil", func(t *testing.T) { + var pbAction *pb.ActionResponse + action := ActionResponseFromPB(pbAction) + result := ActionResponseToPB(action) + require.Equal(t, pbAction, result) + }) +} -- GitLab