From 60f67d94a25462ec625f4aa174218194dec18d0a Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Thu, 27 Jun 2024 22:15:02 +0530 Subject: [PATCH] feat: use mock pkg instead of exposing rueidis mock fxn --- message.go | 5 - rueidiscompat/command_test.go | 379 +++++++++++++++++++++++----------- rueidiscompat/go.mod | 2 + rueidiscompat/go.sum | 4 + 4 files changed, 265 insertions(+), 125 deletions(-) diff --git a/message.go b/message.go index 9a2e6d19..ec49c18a 100644 --- a/message.go +++ b/message.go @@ -1386,8 +1386,3 @@ func (m *prettyRedisMessage) MarshalJSON() ([]byte, error) { } return json.Marshal(obj) } - -// NewMockResult is a mock method to send back RedisResult with err for test coverage -func NewMockResult(val RedisMessage, err error) RedisResult { - return RedisResult{val: val, err: err} -} diff --git a/rueidiscompat/command_test.go b/rueidiscompat/command_test.go index 34ce1146..5ff4ab12 100644 --- a/rueidiscompat/command_test.go +++ b/rueidiscompat/command_test.go @@ -29,13 +29,12 @@ package rueidiscompat import ( "errors" "fmt" - "github.com/redis/rueidis" - "github.com/stretchr/testify/assert" "testing" "time" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "github.com/redis/rueidis/mock" ) var _ = Describe("Commands", func() { @@ -885,122 +884,262 @@ func TestFormatMs(t *testing.T) { } } -func TestInitialErrorInRedisResult(t *testing.T) { - t.Run("Initial error in RedisResult", func(t *testing.T) { - mockRes := rueidis.NewMockResult(rueidis.RedisMessage{}, errors.New("Initial error")) - cmd := newJSONSliceCmd(mockRes) - assert.NotNil(t, cmd.Err()) - assert.EqualError(t, cmd.Err(), "Initial error") - cmd2 := newIntPointerSliceCmd(mockRes) - assert.NotNil(t, cmd2.Err()) - assert.EqualError(t, cmd2.Err(), "Initial error") - cmd3 := newMapStringSliceInterfaceCmd(mockRes) - assert.NotNil(t, cmd3.Err()) - assert.EqualError(t, cmd3.Err(), "Initial error") - cmd4 := newTSTimestampValueSliceCmd(mockRes) - assert.NotNil(t, cmd4.Err()) - assert.EqualError(t, cmd4.Err(), "Initial error") - cmd5 := newMapStringInterfaceCmd(mockRes) - assert.NotNil(t, cmd5.Err()) - assert.EqualError(t, cmd5.Err(), "Initial error") - cmd6 := newTSTimestampValueCmd(mockRes) - assert.NotNil(t, cmd6.Err()) - assert.EqualError(t, cmd6.Err(), "Initial error") - - cmd7 := newTDigestInfoCmd(mockRes) - assert.NotNil(t, cmd7.Err()) - assert.EqualError(t, cmd7.Err(), "Initial error") - - cmd8 := newMapStringIntCmd(mockRes) - assert.NotNil(t, cmd8.Err()) - assert.EqualError(t, cmd8.Err(), "Initial error") - - cmd9 := newTopKInfoCmd(mockRes) - assert.NotNil(t, cmd9.Err()) - assert.EqualError(t, cmd9.Err(), "Initial error") - - cmd10 := newCMSInfoCmd(mockRes) - assert.NotNil(t, cmd10.Err()) - assert.EqualError(t, cmd10.Err(), "Initial error") - - cmd11 := newCFInfoCmd(mockRes) - assert.NotNil(t, cmd11.Err()) - assert.EqualError(t, cmd11.Err(), "Initial error") - - cmd12 := newScanDumpCmd(mockRes) - assert.NotNil(t, cmd12.Err()) - assert.EqualError(t, cmd12.Err(), "Initial error") - - cmd13 := newBFInfoCmd(mockRes) - assert.NotNil(t, cmd13.Err()) - assert.EqualError(t, cmd13.Err(), "Initial error") - - cmd14 := newMapStringInterfaceSliceCmd(mockRes) - assert.NotNil(t, cmd14.Err()) - assert.EqualError(t, cmd14.Err(), "Initial error") - - cmd15 := newFunctionListCmd(mockRes) - assert.NotNil(t, cmd15.Err()) - assert.EqualError(t, cmd15.Err(), "Initial error") - - cmd16 := newCommandsInfoCmd(mockRes) - assert.NotNil(t, cmd16.Err()) - assert.EqualError(t, cmd16.Err(), "Initial error") - - cmd17 := newGeoPosCmd(mockRes) - assert.NotNil(t, cmd17.Err()) - assert.EqualError(t, cmd17.Err(), "Initial error") - - cmd18 := newClusterShardsCmd(mockRes) - assert.NotNil(t, cmd18.Err()) - assert.EqualError(t, cmd18.Err(), "Initial error") - - cmd19 := newClusterSlotsCmd(mockRes) - assert.NotNil(t, cmd19.Err()) - assert.EqualError(t, cmd19.Err(), "Initial error") - - cmd20 := newTimeCmd(mockRes) - assert.NotNil(t, cmd20.Err()) - assert.EqualError(t, cmd20.Err(), "Initial error") - - cmd21 := newXInfoConsumersCmd(mockRes) - assert.NotNil(t, cmd21.Err()) - assert.EqualError(t, cmd21.Err(), "Initial error") - - cmd22 := newXInfoStreamFullCmd(mockRes) - assert.NotNil(t, cmd22.Err()) - assert.EqualError(t, cmd22.Err(), "Initial error") - - cmd23 := newXInfoStreamCmd(mockRes) - assert.NotNil(t, cmd23.Err()) - assert.EqualError(t, cmd23.Err(), "Initial error") - - cmd24 := newXInfoGroupsCmd(mockRes) - assert.NotNil(t, cmd24.Err()) - assert.EqualError(t, cmd24.Err(), "Initial error") - - cmd25 := newXAutoClaimCmd(mockRes) - assert.NotNil(t, cmd25.Err()) - assert.EqualError(t, cmd25.Err(), "Initial error") - - cmd26 := newXPendingExtCmd(mockRes) - assert.NotNil(t, cmd26.Err()) - assert.EqualError(t, cmd26.Err(), "Initial error") - - cmd27 := newXPendingCmd(mockRes) - assert.NotNil(t, cmd27.Err()) - assert.EqualError(t, cmd27.Err(), "Initial error") - - cmd28 := newStringStructMapCmd(mockRes) - assert.NotNil(t, cmd28.Err()) - assert.EqualError(t, cmd28.Err(), "Initial error") - - cmd29 := newZSliceSingleCmd(mockRes) - assert.NotNil(t, cmd29.Err()) - assert.EqualError(t, cmd29.Err(), "Initial error") - - cmd30 := newZSliceCmd(mockRes) - assert.NotNil(t, cmd30.Err()) - assert.EqualError(t, cmd30.Err(), "Initial error") - }) +func TestCommandErrorHandling(t *testing.T) { + mockRes := mock.ErrorResult(errors.New("initial error")) + + tests := []struct { + name string + command func() error + expected string + }{ + { + name: "JSONSliceCmd", + command: func() error { + cmd := newJSONSliceCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "IntPointerSliceCmd", + command: func() error { + cmd := newIntPointerSliceCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "MapStringSliceInterfaceCmd", + command: func() error { + cmd := newMapStringSliceInterfaceCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "TSTimestampValueSliceCmd", + command: func() error { + cmd := newTSTimestampValueSliceCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "MapStringInterfaceCmd", + command: func() error { + cmd := newMapStringInterfaceCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "TSTimestampValueCmd", + command: func() error { + cmd := newTSTimestampValueCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "TDigestInfoCmd", + command: func() error { + cmd := newTDigestInfoCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "MapStringIntCmd", + command: func() error { + cmd := newMapStringIntCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "TopKInfoCmd", + command: func() error { + cmd := newTopKInfoCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "CMSInfoCmd", + command: func() error { + cmd := newCMSInfoCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "CFInfoCmd", + command: func() error { + cmd := newCFInfoCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "ScanDumpCmd", + command: func() error { + cmd := newScanDumpCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "BFInfoCmd", + command: func() error { + cmd := newBFInfoCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "MapStringInterfaceSliceCmd", + command: func() error { + cmd := newMapStringInterfaceSliceCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "FunctionListCmd", + command: func() error { + cmd := newFunctionListCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "CommandsInfoCmd", + command: func() error { + cmd := newCommandsInfoCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "GeoPosCmd", + command: func() error { + cmd := newGeoPosCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "ClusterShardsCmd", + command: func() error { + cmd := newClusterShardsCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "ClusterSlotsCmd", + command: func() error { + cmd := newClusterSlotsCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "TimeCmd", + command: func() error { + cmd := newTimeCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "XInfoConsumersCmd", + command: func() error { + cmd := newXInfoConsumersCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "XInfoStreamFullCmd", + command: func() error { + cmd := newXInfoStreamFullCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "XInfoStreamCmd", + command: func() error { + cmd := newXInfoStreamCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "XInfoGroupsCmd", + command: func() error { + cmd := newXInfoGroupsCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "XAutoClaimCmd", + command: func() error { + cmd := newXAutoClaimCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "XPendingExtCmd", + command: func() error { + cmd := newXPendingExtCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "XPendingCmd", + command: func() error { + cmd := newXPendingCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "StringStructMapCmd", + command: func() error { + cmd := newStringStructMapCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "ZSliceSingleCmd", + command: func() error { + cmd := newZSliceSingleCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + { + name: "ZSliceCmd", + command: func() error { + cmd := newZSliceCmd(mockRes) + return cmd.Err() + }, + expected: "initial error", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := tt.command() + if err == nil || err.Error() != tt.expected { + t.Errorf("Expected error %v, got %v", tt.expected, err) + } + }) + } } diff --git a/rueidiscompat/go.mod b/rueidiscompat/go.mod index c106ea0d..ad2edd78 100644 --- a/rueidiscompat/go.mod +++ b/rueidiscompat/go.mod @@ -18,6 +18,8 @@ require ( github.com/google/go-cmp v0.6.0 // indirect github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/redis/rueidis/mock v1.0.40 // indirect + go.uber.org/mock v0.3.0 // indirect golang.org/x/net v0.24.0 // indirect golang.org/x/sys v0.19.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/rueidiscompat/go.sum b/rueidiscompat/go.sum index fa1bb90c..a37b3b52 100644 --- a/rueidiscompat/go.sum +++ b/rueidiscompat/go.sum @@ -18,9 +18,13 @@ github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo= github.com/onsi/gomega v1.31.1/go.mod h1:y40C95dwAD1Nz36SsEnxvfFe8FFfNxzI5eJ0EYGyAy0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/rueidis/mock v1.0.40 h1:FBRlRxzvxPv7DfmdUGii/o+E3h03QnRy/PYqoWEkJC8= +github.com/redis/rueidis/mock v1.0.40/go.mod h1:ly0qDXZTYQC6ZEPenDL6R0AYM10OmLUCV8LXJrX+8gs= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo= +go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=