Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase x/oracle unit test coverage (backport #786) #795

Merged
merged 1 commit into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

- [781](https://github.com/umee-network/umee/pull/781) Oracle module unit test cleanup.
- [782](https://github.com/umee-network/umee/pull/782) Add unit test to `x/oracle/types/denom.go` and `x/oracle/types/keys.go`.
- [786](https://github.com/umee-network/umee/pull/786) Add unit test to `x/oracle/...`.

## [v2.0.0](https://github.com/umee-network/umee/releases/tag/v2.0.0) - 2022-04-06

Expand Down
141 changes: 128 additions & 13 deletions x/oracle/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package keeper_test

import (
"context"
"math/rand"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto/secp256k1"

umeeapp "github.com/umee-network/umee/v2/app"
"github.com/umee-network/umee/v2/x/oracle/keeper"
"github.com/umee-network/umee/v2/x/oracle/types"
)

func (s *IntegrationTestSuite) TestQuerier_ActiveExchangeRates() {
s.app.OracleKeeper.SetExchangeRate(s.ctx, exchangeRate, sdk.OneDec())
res, err := s.queryClient.ActiveExchangeRates(context.Background(), &types.QueryActiveExchangeRatesRequest{})
res, err := s.queryClient.ActiveExchangeRates(s.ctx.Context(), &types.QueryActiveExchangeRatesRequest{})
s.Require().NoError(err)
s.Require().Equal([]string{exchangeRate}, res.ActiveRates)
}

func (s *IntegrationTestSuite) TestQuerier_ExchangeRates() {
s.app.OracleKeeper.SetExchangeRate(s.ctx, exchangeRate, sdk.OneDec())
res, err := s.queryClient.ExchangeRates(context.Background(), &types.QueryExchangeRatesRequest{})
res, err := s.queryClient.ExchangeRates(s.ctx.Context(), &types.QueryExchangeRatesRequest{})
s.Require().NoError(err)
s.Require().Equal(sdk.DecCoins{
sdk.NewDecCoinFromDec(exchangeRate, sdk.OneDec()),
}, res.ExchangeRates)

res, err = s.queryClient.ExchangeRates(context.Background(), &types.QueryExchangeRatesRequest{
res, err = s.queryClient.ExchangeRates(s.ctx.Context(), &types.QueryExchangeRatesRequest{
Denom: exchangeRate,
})
s.Require().NoError(err)
Expand All @@ -44,7 +44,7 @@ func (s *IntegrationTestSuite) TestQuerier_FeeederDelegation() {
err := s.app.OracleKeeper.ValidateFeeder(s.ctx, feederAddr, valAddr)
s.Require().Error(err)

_, err = s.queryClient.FeederDelegation(context.Background(), &types.QueryFeederDelegationRequest{
_, err = s.queryClient.FeederDelegation(s.ctx.Context(), &types.QueryFeederDelegationRequest{
ValidatorAddr: inactiveValidator,
})
s.Require().Error(err)
Expand All @@ -54,7 +54,7 @@ func (s *IntegrationTestSuite) TestQuerier_FeeederDelegation() {
err = s.app.OracleKeeper.ValidateFeeder(s.ctx, feederAddr, valAddr)
s.Require().NoError(err)

res, err := s.queryClient.FeederDelegation(context.Background(), &types.QueryFeederDelegationRequest{
res, err := s.queryClient.FeederDelegation(s.ctx.Context(), &types.QueryFeederDelegationRequest{
ValidatorAddr: valAddr.String(),
})
s.Require().NoError(err)
Expand All @@ -64,15 +64,15 @@ func (s *IntegrationTestSuite) TestQuerier_FeeederDelegation() {
func (s *IntegrationTestSuite) TestQuerier_MissCounter() {
missCounter := uint64(rand.Intn(100))

res, err := s.queryClient.MissCounter(context.Background(), &types.QueryMissCounterRequest{
res, err := s.queryClient.MissCounter(s.ctx.Context(), &types.QueryMissCounterRequest{
ValidatorAddr: valAddr.String(),
})
s.Require().NoError(err)
s.Require().Equal(res.MissCounter, uint64(0))

s.app.OracleKeeper.SetMissCounter(s.ctx, valAddr, missCounter)

res, err = s.queryClient.MissCounter(context.Background(), &types.QueryMissCounterRequest{
res, err = s.queryClient.MissCounter(s.ctx.Context(), &types.QueryMissCounterRequest{
ValidatorAddr: valAddr.String(),
})
s.Require().NoError(err)
Expand All @@ -91,7 +91,7 @@ func (s *IntegrationTestSuite) TestQuerier_AggregatePrevote() {
s.Require().NoError(err)
s.Require().Equal(prevote, res)

queryRes, err := s.queryClient.AggregatePrevote(context.Background(), &types.QueryAggregatePrevoteRequest{
queryRes, err := s.queryClient.AggregatePrevote(s.ctx.Context(), &types.QueryAggregatePrevoteRequest{
ValidatorAddr: valAddr.String(),
})
s.Require().NoError(err)
Expand All @@ -103,7 +103,7 @@ func (s *IntegrationTestSuite) TestQuerier_AggregatePrevote() {
}

func (s *IntegrationTestSuite) TestQuerier_AggregatePrevotes() {
res, err := s.queryClient.AggregatePrevotes(context.Background(), &types.QueryAggregatePrevotesRequest{})
res, err := s.queryClient.AggregatePrevotes(s.ctx.Context(), &types.QueryAggregatePrevotesRequest{})
s.Require().Equal([]types.AggregateExchangeRatePrevote(nil), res.AggregatePrevotes)
s.Require().NoError(err)
}
Expand All @@ -121,7 +121,7 @@ func (s *IntegrationTestSuite) TestQuerier_AggregateVote() {
}
s.app.OracleKeeper.SetAggregateExchangeRateVote(s.ctx, valAddr, vote)

res, err := s.queryClient.AggregateVote(context.Background(), &types.QueryAggregateVoteRequest{
res, err := s.queryClient.AggregateVote(s.ctx.Context(), &types.QueryAggregateVoteRequest{
ValidatorAddr: valAddr.String(),
})
s.Require().NoError(err)
Expand All @@ -132,13 +132,128 @@ func (s *IntegrationTestSuite) TestQuerier_AggregateVote() {
}

func (s *IntegrationTestSuite) TestQuerier_AggregateVotes() {
res, err := s.queryClient.AggregateVotes(context.Background(), &types.QueryAggregateVotesRequest{})
res, err := s.queryClient.AggregateVotes(s.ctx.Context(), &types.QueryAggregateVotesRequest{})
s.Require().NoError(err)
s.Require().Equal([]types.AggregateExchangeRateVote(nil), res.AggregateVotes)
}

func (s *IntegrationTestSuite) TestQuerier_AggregateVoteInvalidExchangeRate() {
res, err := s.queryClient.AggregateVote(s.ctx.Context(), &types.QueryAggregateVoteRequest{
ValidatorAddr: valAddr.String(),
})
s.Require().Nil(res)
s.Require().ErrorContains(err, "no aggregate vote")
}

func (s *IntegrationTestSuite) TestQuerier_AggregatePrevoteInvalidExchangeRate() {
res, err := s.queryClient.AggregatePrevote(s.ctx.Context(), &types.QueryAggregatePrevoteRequest{
ValidatorAddr: valAddr.String(),
})
s.Require().Nil(res)
s.Require().ErrorContains(err, "no aggregate prevote")
}

func (s *IntegrationTestSuite) TestQuerier_Params() {
res, err := s.queryClient.Params(context.Background(), &types.QueryParamsRequest{})
res, err := s.queryClient.Params(s.ctx.Context(), &types.QueryParamsRequest{})
s.Require().NoError(err)
s.Require().Equal(types.DefaultGenesisState().Params, res.Params)
}

func (s *IntegrationTestSuite) TestQuerier_ExchangeRatesInvalidExchangeRate() {
resExchangeRate, err := s.queryClient.ExchangeRates(s.ctx.Context(), &types.QueryExchangeRatesRequest{
Denom: " ",
})
s.Require().Nil(resExchangeRate)
s.Require().ErrorContains(err, "unknown denom")
}

func (s *IntegrationTestSuite) TestQuerier_AggregatePrevoteInvalidValAddr() {
resExchangeRate, err := s.queryClient.AggregatePrevote(s.ctx.Context(), &types.QueryAggregatePrevoteRequest{
ValidatorAddr: "valAddrInvalid",
})
s.Require().Nil(resExchangeRate)
s.Require().ErrorContains(err, "decoding bech32 failed")
}

func (s *IntegrationTestSuite) TestQuerier_AggregatePrevotesAppendVotes() {
s.app.OracleKeeper.SetAggregateExchangeRatePrevote(s.ctx, valAddr, types.NewAggregateExchangeRatePrevote(
types.AggregateVoteHash{},
valAddr,
uint64(s.ctx.BlockHeight()),
))

_, err := s.queryClient.AggregatePrevotes(s.ctx.Context(), &types.QueryAggregatePrevotesRequest{})
s.Require().Nil(err)
}

func (s *IntegrationTestSuite) TestQuerier_AggregateVotesAppendVotes() {
s.app.OracleKeeper.SetAggregateExchangeRateVote(s.ctx, valAddr, types.NewAggregateExchangeRateVote(
types.DefaultGenesisState().ExchangeRates,
valAddr,
))

_, err := s.queryClient.AggregateVotes(s.ctx.Context(), &types.QueryAggregateVotesRequest{})
s.Require().Nil(err)
}

func (s *IntegrationTestSuite) TestEmptyRequest() {
q := keeper.NewQuerier(keeper.Keeper{})
const emptyRequestErrorMsg = "empty request"

resParams, err := q.Params(s.ctx.Context(), nil)
s.Require().Nil(resParams)
s.Require().ErrorContains(err, emptyRequestErrorMsg)

resExchangeRate, err := q.ExchangeRates(s.ctx.Context(), nil)
s.Require().Nil(resExchangeRate)
s.Require().ErrorContains(err, emptyRequestErrorMsg)

resActiveExchangeRates, err := q.ActiveExchangeRates(s.ctx.Context(), nil)
s.Require().Nil(resActiveExchangeRates)
s.Require().ErrorContains(err, emptyRequestErrorMsg)

resFeederDelegation, err := q.FeederDelegation(s.ctx.Context(), nil)
s.Require().Nil(resFeederDelegation)
s.Require().ErrorContains(err, emptyRequestErrorMsg)

resMissCounter, err := q.MissCounter(s.ctx.Context(), nil)
s.Require().Nil(resMissCounter)
s.Require().ErrorContains(err, emptyRequestErrorMsg)

resAggregatePrevote, err := q.AggregatePrevote(s.ctx.Context(), nil)
s.Require().Nil(resAggregatePrevote)
s.Require().ErrorContains(err, emptyRequestErrorMsg)

resAggregatePrevotes, err := q.AggregatePrevotes(s.ctx.Context(), nil)
s.Require().Nil(resAggregatePrevotes)
s.Require().ErrorContains(err, emptyRequestErrorMsg)

resAggregateVote, err := q.AggregateVote(s.ctx.Context(), nil)
s.Require().Nil(resAggregateVote)
s.Require().ErrorContains(err, emptyRequestErrorMsg)

resAggregateVotes, err := q.AggregateVotes(s.ctx.Context(), nil)
s.Require().Nil(resAggregateVotes)
s.Require().ErrorContains(err, emptyRequestErrorMsg)
}

func (s *IntegrationTestSuite) TestInvalidBechAddress() {
q := keeper.NewQuerier(keeper.Keeper{})
invalidAddressMsg := "empty address string is not allowed"

resFeederDelegation, err := q.FeederDelegation(s.ctx.Context(), &types.QueryFeederDelegationRequest{})
s.Require().Nil(resFeederDelegation)
s.Require().ErrorContains(err, invalidAddressMsg)

resMissCounter, err := q.MissCounter(s.ctx.Context(), &types.QueryMissCounterRequest{})
s.Require().Nil(resMissCounter)
s.Require().ErrorContains(err, invalidAddressMsg)

resAggregatePrevote, err := q.AggregatePrevote(s.ctx.Context(), &types.QueryAggregatePrevoteRequest{})
s.Require().Nil(resAggregatePrevote)
s.Require().ErrorContains(err, invalidAddressMsg)

resAggregateVote, err := q.AggregateVote(s.ctx.Context(), &types.QueryAggregateVoteRequest{})
s.Require().Nil(resAggregateVote)
s.Require().ErrorContains(err, invalidAddressMsg)
}
113 changes: 113 additions & 0 deletions x/oracle/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,119 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestParamKeyTable(t *testing.T) {
require.NotNil(t, ParamKeyTable())
}

func TestValidateVotePeriod(t *testing.T) {
err := validateVotePeriod("invalidUint64")
require.ErrorContains(t, err, "invalid parameter type: string")

err = validateVotePeriod(uint64(0))
require.ErrorContains(t, err, "vote period must be positive: 0")

err = validateVotePeriod(uint64(10))
require.Nil(t, err)
}

func TestValidateVoteThreshold(t *testing.T) {
err := validateVoteThreshold("invalidSdkType")
require.ErrorContains(t, err, "invalid parameter type: string")

err = validateVoteThreshold(sdk.MustNewDecFromStr("0.31"))
require.ErrorContains(t, err, "vote threshold must be bigger than 33%: 0.310000000000000000")

err = validateVoteThreshold(sdk.MustNewDecFromStr("40.0"))
require.ErrorContains(t, err, "vote threshold too large: 40.000000000000000000")

err = validateVoteThreshold(sdk.MustNewDecFromStr("0.35"))
require.Nil(t, err)
}

func TestValidateRewardBand(t *testing.T) {
err := validateRewardBand("invalidSdkType")
require.ErrorContains(t, err, "invalid parameter type: string")

err = validateRewardBand(sdk.MustNewDecFromStr("-0.31"))
require.ErrorContains(t, err, "reward band must be positive: -0.310000000000000000")

err = validateRewardBand(sdk.MustNewDecFromStr("40.0"))
require.ErrorContains(t, err, "reward band is too large: 40.000000000000000000")

err = validateRewardBand(sdk.OneDec())
require.Nil(t, err)
}

func TestValidateRewardDistributionWindow(t *testing.T) {
err := validateRewardDistributionWindow("invalidUint64")
require.ErrorContains(t, err, "invalid parameter type: string")

err = validateRewardDistributionWindow(uint64(0))
require.ErrorContains(t, err, "reward distribution window must be positive: 0")

err = validateRewardDistributionWindow(uint64(10))
require.Nil(t, err)
}

func TestValidateAcceptList(t *testing.T) {
err := validateAcceptList("invalidUint64")
require.ErrorContains(t, err, "invalid parameter type: string")

err = validateAcceptList(DenomList{
{BaseDenom: ""},
})
require.ErrorContains(t, err, "oracle parameter AcceptList Denom must have BaseDenom")

err = validateAcceptList(DenomList{
{BaseDenom: DenomUmee.BaseDenom, SymbolDenom: ""},
})
require.ErrorContains(t, err, "oracle parameter AcceptList Denom must have SymbolDenom")

err = validateAcceptList(DenomList{
{BaseDenom: DenomUmee.BaseDenom, SymbolDenom: DenomUmee.SymbolDenom},
})
require.Nil(t, err)
}

func TestValidateSlashFraction(t *testing.T) {
err := validateSlashFraction("invalidSdkType")
require.ErrorContains(t, err, "invalid parameter type: string")

err = validateSlashFraction(sdk.MustNewDecFromStr("-0.31"))
require.ErrorContains(t, err, "slash fraction must be positive: -0.310000000000000000")

err = validateSlashFraction(sdk.MustNewDecFromStr("40.0"))
require.ErrorContains(t, err, "slash fraction is too large: 40.000000000000000000")

err = validateSlashFraction(sdk.OneDec())
require.Nil(t, err)
}

func TestValidateSlashWindow(t *testing.T) {
err := validateSlashWindow("invalidUint64")
require.ErrorContains(t, err, "invalid parameter type: string")

err = validateSlashWindow(uint64(0))
require.ErrorContains(t, err, "slash window must be positive: 0")

err = validateSlashWindow(uint64(10))
require.Nil(t, err)
}

func TestValidateMinValidPerWindow(t *testing.T) {
err := validateMinValidPerWindow("invalidSdkType")
require.ErrorContains(t, err, "invalid parameter type: string")

err = validateMinValidPerWindow(sdk.MustNewDecFromStr("-0.31"))
require.ErrorContains(t, err, "min valid per window must be positive: -0.310000000000000000")

err = validateMinValidPerWindow(sdk.MustNewDecFromStr("40.0"))
require.ErrorContains(t, err, "min valid per window is too large: 40.000000000000000000")

err = validateMinValidPerWindow(sdk.OneDec())
require.Nil(t, err)
}

func TestParamsEqual(t *testing.T) {
p1 := DefaultParams()
err := p1.Validate()
Expand Down
Loading