Skip to content

Commit

Permalink
fix: logic for appending api returned validators and adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drohit-cb committed Aug 27, 2024
1 parent d21d705 commit 8a20d7a
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ jobs:
- name: Run linter
uses: golangci/golangci-lint-action@v6
with:
version: latest
version: v1.55.2
args: --timeout 3m
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Coinbase Go SDK Changelog

## [0.0.2] - 2024-08-27

### Fixed

- Fixed a bug where we weren't handling the api returned validators properly resulting in an index out of range error.

## [0.0.1] - 2024-08-23

### Added
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ test-coverage:

.PHONY: mocks
mocks:
mockery --disable-version-string --name StakeAPI --keeptree --dir gen/client --output pkg/mocks
mockery --disable-version-string --name AssetsAPI --keeptree --dir gen/client --output pkg/mocks
mockery --disable-version-string --name StakeAPI --keeptree --dir gen/client --output pkg/mocks
mockery --disable-version-string --name ValidatorsAPI --keeptree --dir gen/client --output pkg/mocks

.PHONY: docs
docs:
Expand Down
10 changes: 5 additions & 5 deletions pkg/coinbase/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import (
)

type Validator struct {
validator client.Validator
model client.Validator
}

func NewValidator(validator client.Validator) Validator {
return Validator{
validator: validator,
model: validator,
}
}

func (v Validator) ID() string {
return v.validator.ValidatorId
return v.model.ValidatorId
}

func (v Validator) Status() client.ValidatorStatus {
return v.validator.Status
return v.model.Status
}

func (v Validator) ToString() string {
Expand All @@ -43,7 +43,7 @@ func (c *Client) ListValidators(ctx context.Context, networkId string, assetId s
return nil, err
}

validators := make([]Validator, 0, len(validatorList.GetData()))
validators := make([]Validator, len(validatorList.GetData()))
for i, validator := range validatorList.GetData() {
validators[i] = NewValidator(validator)
}
Expand Down
146 changes: 146 additions & 0 deletions pkg/coinbase/validators_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package coinbase

import (
"context"
"fmt"
"testing"

api "github.com/coinbase/coinbase-sdk-go/gen/client"
"github.com/coinbase/coinbase-sdk-go/pkg/mocks"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)

type ValidatorSuite struct {
suite.Suite

mockValidatorsAPI *mocks.ValidatorsAPI
client *Client
}

func TestValidatorTestSuite(t *testing.T) {
suite.Run(t, new(ValidatorSuite))
}

func (s *ValidatorSuite) SetupTest() {
s.mockValidatorsAPI = mocks.NewValidatorsAPI(s.T())
s.client = &Client{client: &api.APIClient{
ValidatorsAPI: s.mockValidatorsAPI,
}}
}

func (s *ValidatorSuite) TearDownTest() {
s.mockValidatorsAPI.AssertExpectations(s.T())
}

func (s *ValidatorSuite) TestListValidators_Success() {
ctx := context.Background()
networkId := "test-network"
assetId := "test-asset"

mockValidators := &api.ValidatorList{
Data: []api.Validator{
{
ValidatorId: "validator-1",
Status: api.VALIDATORSTATUS_ACTIVE,
},
},
}

s.mockSuccessfulListValidators(ctx, networkId, assetId, mockValidators)

validators, err := s.client.ListValidators(ctx, networkId, assetId)

s.Assert().NoError(err)
s.Len(validators, 1)
s.Equal("validator-1", validators[0].ID())
s.Equal(api.VALIDATORSTATUS_ACTIVE, validators[0].Status())
}

func (s *ValidatorSuite) TestListValidators_Failure() {
ctx := context.Background()
networkId := "test-network"
assetId := "test-asset"
errorMessage := "some error calling api"

s.mockFailedListValidators(ctx, networkId, assetId, fmt.Errorf(errorMessage))

validators, err := s.client.ListValidators(ctx, networkId, assetId)

s.Assert().Nil(validators)
s.EqualError(err, errorMessage)
}

func (s *ValidatorSuite) TestGetValidator_Success() {
ctx := context.Background()
networkId := "test-network"
assetId := "test-asset"
validatorId := "validator-1"

s.mockSuccessfulGetValidator(ctx, networkId, assetId, validatorId, &api.Validator{
ValidatorId: validatorId,
Status: api.VALIDATORSTATUS_ACTIVE,
})

validator, err := s.client.GetValidator(ctx, networkId, assetId, validatorId)

s.Assert().NoError(err)
s.Equal(validatorId, validator.ID())
s.Equal(api.VALIDATORSTATUS_ACTIVE, validator.Status())
s.Equal("Validator { Id: 'validator-1' Status: 'active' }", validator.ToString())
}

func (s *ValidatorSuite) TestGetValidator_Failure() {
ctx := context.Background()
networkId := "test-network"
assetId := "test-asset"
validatorId := "validator-1"
errorMessage := "some error calling api"

s.mockFailedGetValidator(ctx, networkId, assetId, validatorId, fmt.Errorf(errorMessage))

validator, err := s.client.GetValidator(ctx, networkId, assetId, validatorId)

s.Assert().Empty(validator)
s.EqualError(err, errorMessage)
}

func (s *ValidatorSuite) mockSuccessfulListValidators(ctx context.Context, networkId string, assetId string, mockValidators *api.ValidatorList) {
s.T().Helper()

s.mockValidatorsAPI.On("ListValidators", ctx, networkId, assetId).Return(api.ApiListValidatorsRequest{
ApiService: s.mockValidatorsAPI,
}, nil, nil).Once()

s.mockValidatorsAPI.On("ListValidatorsExecute", mock.Anything).Return(mockValidators, nil, nil).Once()
}

func (s *ValidatorSuite) mockFailedListValidators(ctx context.Context, networkId string, assetId string, err error) {
s.T().Helper()

s.mockValidatorsAPI.On("ListValidators", ctx, networkId, assetId).Return(api.ApiListValidatorsRequest{
ApiService: s.mockValidatorsAPI,
}, nil, nil).Once()

s.mockValidatorsAPI.On("ListValidatorsExecute", mock.Anything).Return(nil, nil, err).Once()
}

func (s *ValidatorSuite) mockSuccessfulGetValidator(ctx context.Context, networkId string, assetId string, validatorId string, validator *api.Validator) {
s.T().Helper()

s.mockValidatorsAPI.On("GetValidator", ctx, networkId, assetId, validatorId).Return(api.ApiGetValidatorRequest{
ApiService: s.mockValidatorsAPI,
}, nil, nil)

s.mockValidatorsAPI.On("GetValidatorExecute", mock.Anything).Return(validator, nil, nil)
}

func (s *ValidatorSuite) mockFailedGetValidator(ctx context.Context, networkId string, assetId string, validatorId string, err error) {
s.T().Helper()

s.mockValidatorsAPI.On("GetValidator", ctx, networkId, assetId, validatorId).Return(api.ApiGetValidatorRequest{
ApiService: s.mockValidatorsAPI,
}, nil, nil)

s.mockValidatorsAPI.On("GetValidatorExecute", mock.Anything).Return(nil, nil, err)
}
146 changes: 146 additions & 0 deletions pkg/mocks/ValidatorsAPI.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8a20d7a

Please sign in to comment.