From 199fb9a2789b3f3d2f0c2a57f9f4e5efd6412608 Mon Sep 17 00:00:00 2001 From: evgeniy-scherbina Date: Wed, 18 Oct 2023 15:05:45 -0400 Subject: [PATCH] Improve tests --- service/cachemdw/response_test.go | 59 ++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/service/cachemdw/response_test.go b/service/cachemdw/response_test.go index 63d1b43..978be5a 100644 --- a/service/cachemdw/response_test.go +++ b/service/cachemdw/response_test.go @@ -9,7 +9,7 @@ import ( "github.com/kava-labs/kava-proxy-service/service/cachemdw" ) -func TestUnitTestJsonRpcResponse_IsEmpty(t *testing.T) { +func TestUnitTestJsonRpcResponse_IsResultEmpty(t *testing.T) { toJSON := func(t *testing.T, result any) []byte { resultInJSON, err := json.Marshal(result) require.NoError(t, err) @@ -107,3 +107,60 @@ func TestUnitTestJsonRpcResponse_IsEmpty(t *testing.T) { }) } } + +func TestUnitTestJsonRpcResponse_IsCacheable(t *testing.T) { + toJSON := func(t *testing.T, result any) []byte { + resultInJSON, err := json.Marshal(result) + require.NoError(t, err) + + return resultInJSON + } + + tests := []struct { + name string + resp *cachemdw.JsonRpcResponse + isCacheable bool + }{ + { + name: "empty result", + resp: &cachemdw.JsonRpcResponse{ + Version: "2.0", + ID: []byte("1"), + Result: []byte{}, + }, + isCacheable: false, + }, + { + name: "non-empty error", + resp: &cachemdw.JsonRpcResponse{ + Version: "2.0", + ID: []byte("1"), + Result: toJSON(t, "0x1234"), + JsonRpcError: &cachemdw.JsonRpcError{ + Code: 1, + Message: "error", + }, + }, + isCacheable: false, + }, + { + name: "valid response", + resp: &cachemdw.JsonRpcResponse{ + Version: "2.0", + ID: []byte("1"), + Result: toJSON(t, "0x1234"), + }, + isCacheable: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + require.Equal( + t, + tc.isCacheable, + tc.resp.IsCacheable(), + ) + }) + } +}