From b6e92d22da3c0a1dec68d4c1975093150bfefbe5 Mon Sep 17 00:00:00 2001 From: Calvin Lobo Date: Fri, 5 Jan 2024 11:24:04 -0500 Subject: [PATCH] Increased test coverage --- ecosystem/grpc/client_interceptor_test.go | 25 +++++++++++++++++++++-- ecosystem/grpc/registry_test.go | 20 ++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 ecosystem/grpc/registry_test.go diff --git a/ecosystem/grpc/client_interceptor_test.go b/ecosystem/grpc/client_interceptor_test.go index 9e19aff..4161335 100644 --- a/ecosystem/grpc/client_interceptor_test.go +++ b/ecosystem/grpc/client_interceptor_test.go @@ -21,7 +21,10 @@ type PingService struct { func (s *PingService) Ping(_ context.Context, _ *ping.PingRequest) (*ping.PingResponse, error) { // Your implementation of the Ping method goes here fmt.Println("Received Ping request") - return nil, s.err + if s.err != nil { + return nil, s.err + } + return &ping.PingResponse{}, nil } func setupServerAndClient(port int) (*PingService, ping.PingServiceClient) { @@ -42,6 +45,10 @@ func setupServerAndClient(port int) (*PingService, ping.PingServiceClient) { } }() + defaultInverseMapping := DefaultInverseMapping() + defaultInverseMapping[codes.DataLoss] = simplerr.CodeResourceExhausted + GetDefaultRegistry().SetInverseMapping(defaultInverseMapping) + interceptor := ReturnSimpleErrors(nil) conn, err := grpc.Dial(fmt.Sprintf(":%d", port), @@ -58,7 +65,7 @@ func setupServerAndClient(port int) (*PingService, ping.PingServiceClient) { func TestClientInterceptor(t *testing.T) { - _, client := setupServerAndClient(50051) + server, client := setupServerAndClient(50051) _, err := client.Ping(context.Background(), &ping.PingRequest{}) require.True(t, simplerr.HasErrorCode(err, simplerr.CodeNotFound), "simplerror code can be detected") @@ -71,6 +78,12 @@ func TestClientInterceptor(t *testing.T) { method, ok := simplerr.GetAttribute(err, AttrGRPCMethod) require.True(t, ok) require.Equal(t, "/ping.PingService/Ping", method, "can get the grpc method which errored") + + // Test the custom added mapping + server.err = status.Error(codes.DataLoss, "test error") + _, err = client.Ping(context.Background(), &ping.PingRequest{}) + require.True(t, simplerr.HasErrorCode(err, simplerr.CodeResourceExhausted), "simplerror code can be detected") + } // When a non grpc error is returned, the client still returns a grpc error with code Unknown @@ -94,3 +107,11 @@ func TestClientInterceptorNotGPRCError(t *testing.T) { require.Equal(t, "/ping.PingService/Ping", method, "can get the grpc method which errored") } + +func TestClientInterceptorNoError(t *testing.T) { + server, client := setupServerAndClient(50053) + server.err = nil + + _, err := client.Ping(context.Background(), &ping.PingRequest{}) + require.Nil(t, err) +} diff --git a/ecosystem/grpc/registry_test.go b/ecosystem/grpc/registry_test.go new file mode 100644 index 0000000..a9f8af9 --- /dev/null +++ b/ecosystem/grpc/registry_test.go @@ -0,0 +1,20 @@ +package simplegrpc + +import ( + "github.com/lobocv/simplerr" + "github.com/stretchr/testify/require" + "google.golang.org/grpc/codes" + "testing" +) + +func TestGetGPRCCode(t *testing.T) { + + reg := NewRegistry() + simplerrCode, found := reg.getGRPCCode(codes.NotFound) + require.True(t, found) + require.Equal(t, simplerr.CodeNotFound, simplerrCode) + + simplerrCode, found = reg.getGRPCCode(codes.Code(100000)) + require.False(t, found) + require.Equal(t, simplerr.CodeUnknown, simplerrCode) +}