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: increasing unit tests coverage #1962

Merged
merged 19 commits into from
Apr 6, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

- [1959](https://github.com/umee-network/umee/pull/1959) Update IBC to v6.1
- [1962](https://github.com/umee-network/umee/pull/1962) Increasing unit test coverage for `x/leverage`, `x/oracle`
and `x/uibc`
- [1913](https://github.com/umee-network/umee/pull/1913), [1974](https://github.com/umee-network/umee/pull/1974) uibc: quota status check.
- [1973](https://github.com/umee-network/umee/pull/1973) UIBC: handle zero Quota Params.

Expand Down
148 changes: 148 additions & 0 deletions x/leverage/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,154 @@ const (
uDenom = "u/umee"
)

func (s *IntegrationTestSuite) TestKeeper_InitGenesis() {
tcs := []struct {
name string
g types.GenesisState
expectErr bool
errMsg string
}{
{
"invalid token",
types.GenesisState{
Params: types.DefaultParams(),
Registry: []types.Token{
{},
},
},
true,
"invalid denom: ",
},
{
"invalid address for borrow",
types.GenesisState{
Params: types.DefaultParams(),
AdjustedBorrows: []types.AdjustedBorrow{
{
Address: "",
},
},
},
true,
"empty address string is not allowed",
},
{
"invalid coin for borrow",
types.GenesisState{
Params: types.DefaultParams(),
AdjustedBorrows: []types.AdjustedBorrow{
{
Address: testAddr,
Amount: sdk.DecCoin{},
},
},
},
true,
"invalid denom: ",
},
{
"invalid address for collateral",
types.GenesisState{
Params: types.DefaultParams(),
Collateral: []types.Collateral{
{
Address: "",
},
},
},
true,
"empty address string is not allowed",
},
{
"invalid coin for collateral",
types.GenesisState{
Params: types.DefaultParams(),
Collateral: []types.Collateral{
{
Address: testAddr,
Amount: sdk.Coin{},
},
},
},
true,
"invalid denom: ",
},
{
"invalid coin for reserves",
types.GenesisState{
Params: types.DefaultParams(),
Reserves: sdk.Coins{
sdk.Coin{
Denom: "",
},
},
},
true,
"invalid denom: ",
},
{
"invalid address for badDebt",
types.GenesisState{
Params: types.DefaultParams(),
BadDebts: []types.BadDebt{
{
Address: "",
},
},
},
true,
"empty address string is not allowed",
},
{
"invalid coin for badDebt",
types.GenesisState{
Params: types.DefaultParams(),
BadDebts: []types.BadDebt{
{
Address: testAddr,
Denom: "",
},
},
},
true,
"invalid denom: ",
},
{
"invalid coin for interestScalars",
types.GenesisState{
Params: types.DefaultParams(),
InterestScalars: []types.InterestScalar{
{
Denom: "",
},
},
},
true,
"invalid denom: ",
},
{
"valid",
types.GenesisState{
Params: types.DefaultParams(),
},
false,
"",
},
}

for _, tc := range tcs {
s.Run(
tc.name, func() {
if tc.expectErr {
s.Assertions.PanicsWithError(tc.errMsg, func() { s.app.LeverageKeeper.InitGenesis(s.ctx, tc.g) })
} else {
s.Assertions.NotPanics(func() { s.app.LeverageKeeper.InitGenesis(s.ctx, tc.g) })
}
},
)
}
}

func (s *IntegrationTestSuite) TestKeeper_ExportGenesis() {
borrows := []types.AdjustedBorrow{
{
Expand Down
9 changes: 6 additions & 3 deletions x/leverage/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ var (
ErrSupplyNotAllowed = errors.Register(ModuleName, 203, "supplying of Token disabled")
ErrBorrowNotAllowed = errors.Register(ModuleName, 204, "borrowing of Token disabled")
ErrBlacklisted = errors.Register(ModuleName, 205, "blacklisted Token")
ErrCollateralWeightZero = errors.Register(ModuleName, 206,
"collateral weight of Token is zero: can't be used as a collateral")
ErrDuplicateToken = errors.Register(ModuleName, 207, "duplicate token")
ErrCollateralWeightZero = errors.Register(
ModuleName, 206,
"collateral weight of Token is zero: can't be used as a collateral",
)
ErrDuplicateToken = errors.Register(ModuleName, 207, "duplicate token")
ErrEmptyAddAndUpdateTokens = errors.Register(ModuleName, 208, "empty add and update tokens")

// 3XX = User Positions
ErrInsufficientBalance = errors.Register(ModuleName, 300, "insufficient balance")
Expand Down
140 changes: 137 additions & 3 deletions x/leverage/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,146 @@ package types
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"

"gotest.tools/v3/assert"
)

func TestGenesisValidation(t *testing.T) {
genState := DefaultGenesis()
assert.NilError(t, genState.Validate())
testAddr := "umee1s84d29zk3k20xk9f0hvczkax90l9t94g72n6wm"
validDenom := "umee"

tcs := []struct {
name string
q GenesisState
expectErr bool
errMsg string
}{
{"default genesis", *DefaultGenesis(), false, ""},
{
"invalid params",
*NewGenesisState(
Params{
CompleteLiquidationThreshold: sdk.MustNewDecFromStr("-0.4"),
}, nil, nil, nil, nil, 0, nil, nil, nil,
),
true,
"complete liquidation threshold must be positive",
},
{
"invalid token registry", GenesisState{
Params: DefaultParams(),
Registry: []Token{
{},
},
},
true,
"invalid denom",
},
{
"invalid adjusted borrows address", GenesisState{
Params: DefaultParams(),
AdjustedBorrows: []AdjustedBorrow{
NewAdjustedBorrow("", sdk.DecCoin{}),
},
},
true,
"empty address string is not allowed",
},
{
"invalid adjusted borrows amount", GenesisState{
Params: DefaultParams(),
AdjustedBorrows: []AdjustedBorrow{
NewAdjustedBorrow(testAddr, sdk.DecCoin{}),
},
},
true,
"invalid denom",
},
{
"invalid collateral address", GenesisState{
Params: DefaultParams(),
Collateral: []Collateral{
NewCollateral("", sdk.Coin{}),
},
},
true,
"empty address string is not allowed",
},
{
"invalid collateral amount", GenesisState{
Params: DefaultParams(),
Collateral: []Collateral{
NewCollateral(testAddr, sdk.Coin{}),
},
},
true,
"invalid denom",
},
{
"invalid reserves", GenesisState{
Params: DefaultParams(),
Reserves: sdk.Coins{
sdk.Coin{
Denom: "",
},
},
},
true,
"invalid denom",
},
{
"invalid badDebt address", GenesisState{
Params: DefaultParams(),
BadDebts: []BadDebt{
NewBadDebt("", ""),
},
},
true,
"empty address string is not allowed",
},
{
"invalid badDebt denom", GenesisState{
Params: DefaultParams(),
BadDebts: []BadDebt{
NewBadDebt(testAddr, ""),
},
},
true,
"invalid denom",
},
{
"invalid interestScalar denom", GenesisState{
Params: DefaultParams(),
InterestScalars: []InterestScalar{
NewInterestScalar("", sdk.ZeroDec()),
},
},
true,
"invalid denom",
},
{
"invalid interestScalar address", GenesisState{
Params: DefaultParams(),
InterestScalars: []InterestScalar{
NewInterestScalar(validDenom, sdk.ZeroDec()),
},
},
true,
"exchange rate less than one",
},
}

// TODO #484: expand this test to cover failure cases.
kosegor marked this conversation as resolved.
Show resolved Hide resolved
for _, tc := range tcs {
t.Run(
tc.name, func(t *testing.T) {
err := tc.q.Validate()
if tc.expectErr {
assert.ErrorContains(t, err, tc.errMsg)
} else {
assert.NilError(t, err)
}
},
)
}
}
6 changes: 5 additions & 1 deletion x/leverage/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

var _ sdk.Msg = &MsgGovUpdateRegistry{}

// NewMsgUpdateRegistry will creates a new MsgUpdateRegistry instance
// NewMsgUpdateRegistry will create a new MsgUpdateRegistry instance
func NewMsgUpdateRegistry(authority, title, description string, updateTokens, addTokens []Token) *MsgGovUpdateRegistry {
return &MsgGovUpdateRegistry{
Title: title,
Expand All @@ -36,6 +36,10 @@ func (msg MsgGovUpdateRegistry) ValidateBasic() error {
return err
}

if len(msg.AddTokens) == 0 && len(msg.UpdateTokens) == 0 {
return ErrEmptyAddAndUpdateTokens
}

if err := validateRegistryTokenDenoms(msg.AddTokens); err != nil {
return err
}
Expand Down
Loading