Skip to content

Commit

Permalink
Get error tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Dec 4, 2023
1 parent 72b4b59 commit c672a55
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions pkg/loop/internal/chain_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,29 @@ func TestChainReaderClient(t *testing.T) {
client := &chainReaderClient{grpc: pb.NewChainReaderClient(conn)}
ctx := context.Background()

errorTypes := []error{
types.ErrChainReaderConfigMissing,
types.InvalidArgumentError(fmt.Errorf("%w: ChainReader config doesn't match abi", types.ErrInvalidConfig).Error()),
types.ErrInvalidType,
type testCase struct {
errType error
errMsg string
}

for _, errorType := range errorTypes {
es.err = errorType
t.Run("GetLatestValue unwraps errors from server "+errorType.Error(), func(t *testing.T) {
testCases := []testCase{
{types.ErrChainReaderConfigMissing, ""},
{types.ErrInvalidConfig, "ChainReader config doesn't match abi"},
{types.ErrInvalidType, "wrong length"},
}

for _, tc := range testCases {
_, ok := tc.errType.(error)
require.True(t, ok)
if tc.errMsg != "" {
es.err = fmt.Errorf("%w: %s", tc.errType, tc.errMsg)
} else {
es.err = tc.errType
}

t.Run("GetLatestValue unwraps errors from server "+tc.errType.Error(), func(t *testing.T) {
err := client.GetLatestValue(ctx, types.BoundContract{}, "method", "anything", "anything")
assert.ErrorIs(t, err, errorType)
assert.ErrorIs(t, tc.errType, err) // Note: our custom error type must be first arg, otherwise gRPC's Is() is called instead of ours
})
}

Expand All @@ -100,7 +112,7 @@ func TestChainReaderClient(t *testing.T) {
invalidTypeErr := types.ErrInvalidType
t.Run("GetLatestValue returns error if type cannot be encoded in the wire format", func(t *testing.T) {
err := client.GetLatestValue(ctx, types.BoundContract{}, "method", &cannotEncode{}, &TestStruct{})
assert.NotNil(t, errors.As(err, &invalidTypeErr))
assert.ErrorIs(t, err, &invalidTypeErr)
})
}

Expand Down

0 comments on commit c672a55

Please sign in to comment.