Skip to content

Commit

Permalink
Add err handling tests for encode/decode versioned bytes functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 committed Nov 1, 2023
1 parent 60ac98b commit 49ff074
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/loop/internal/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func encodeVersionedBytes(data any, version int32) (*pb.VersionedBytes, error) {
return nil, err
}
default:
return nil, fmt.Errorf("Unsupported encoding version %d for data %v", version, data)
return nil, fmt.Errorf("unsupported encoding version %d for data %v", version, data)
}

return &pb.VersionedBytes{Version: SimpleJsonEncodingVersion, Data: jsonData}, nil
Expand All @@ -51,7 +51,7 @@ func decodeVersionedBytes(res any, vData *pb.VersionedBytes) error {
return err
}
default:
return fmt.Errorf("Unsupported encoding version %d for versionedData %v", vData.Version, vData.Data)
return fmt.Errorf("unsupported encoding version %d for versionedData %v", vData.Version, vData.Data)
}

return nil
Expand Down
46 changes: 46 additions & 0 deletions pkg/loop/internal/chain_reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package internal

import (
"errors"
"testing"

"github.com/smartcontractkit/chainlink-relay/pkg/loop/internal/pb"
)

func TestVersionedBytesFunctionsBadPaths(t *testing.T) {
t.Run("EncodeVersionedBytes unsupported type", func(t *testing.T) {
expected := errors.New("json: unsupported type: chan int")
invalidData := make(chan int)

_, err := encodeVersionedBytes(invalidData, SimpleJsonEncodingVersion)
if err == nil || err.Error() != expected.Error() {
t.Errorf("expected error: %s, but got: %v", expected, err)
}
})

t.Run("EncodeVersionedBytes unsupported encoding version", func(t *testing.T) {
expected := errors.New("unsupported encoding version 2 for data map[key:value]")
data := map[string]interface{}{
"key": "value",
}

_, err := encodeVersionedBytes(data, 2)
if err == nil || err.Error() != expected.Error() {
t.Errorf("expected error: %s, but got: %v", expected, err)
}
})

t.Run("DecodeVersionedBytes", func(t *testing.T) {
var decodedData map[string]interface{}
expected := errors.New("unsupported encoding version 2 for versionedData [97 98 99 100 102]")
versionedBytes := &pb.VersionedBytes{
Version: 2, // Unsupported version
Data: []byte("abcdf"),
}

err := decodeVersionedBytes(&decodedData, versionedBytes)
if err == nil || err.Error() != expected.Error() {
t.Errorf("expected error: %s, but got: %v", expected, err)
}
})
}

0 comments on commit 49ff074

Please sign in to comment.