Skip to content

Commit

Permalink
refactor: improve leverage keeper tests and errors (#1300)
Browse files Browse the repository at this point in the history
* supply and withdraw tests

* err--

* make proto-all

* error improvements and fix cascading client test error DEVX

* TestCollateralize

* ++

* TestDe+Collateralize

* case++

* borrow test + more cases

* add invariant checks to every msg test case

* move files

* ++

* TestRepay

* --

* Liquidate single-denom test cases

* liquidate cases

* --

* remove setupAccount and initBorrowScenario

* imports

* lint++

* fix test

* ++

* cl++

* remove unnecessary interface

* move function

* apply s.app, s.ctx, and s.Require() shorthands

* apply coin shorthand in keeper_test package

* use setReserves helper

* improve big number readability

* ensure espected value is on the left for all require.Equal

* use borrow and repay helpers

* lint++

* always require.ErrorIs

* use forceBorrow helper rather than SetTokenSettings in high utilization test

* fix #1310

* fix sim tests
  • Loading branch information
toteki authored Sep 1, 2022
1 parent 52fe2e3 commit 9ec13b9
Show file tree
Hide file tree
Showing 29 changed files with 2,088 additions and 1,551 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- [1012](https://github.com/umee-network/umee/pull/1012) Improve negative time elapsed error message
- [1236](https://github.com/umee-network/umee/pull/1236) Improve leverage event fields.
- [1294](https://github.com/umee-network/umee/pull/1294) Simplify window progress query math.
- [1300](https://github.com/umee-network/umee/pull/1300) Improve leverage test suite and error specificity.

### Bug Fixes

Expand Down
1 change: 0 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,6 @@ func (app *UmeeApp) setAnteHandler(txConfig client.TxConfig) {
OracleKeeper: app.OracleKeeper,
},
)

if err != nil {
panic(err)
}
Expand Down
105 changes: 36 additions & 69 deletions x/leverage/client/tests/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
type IntegrationTestSuite struct {
suite.Suite

abort bool // stop interdependent tests on the first error for clarity

cfg network.Config
network *network.Network
}
Expand All @@ -44,38 +42,37 @@ func (s *IntegrationTestSuite) TearDownSuite() {
s.network.Cleanup()
}

// TestCases are queries and transactions that can be run, and return a boolean
// which indicates to abort the test suite if true
type TestCase interface {
Run(s *IntegrationTestSuite) bool
// runTestQuery
func (s *IntegrationTestSuite) runTestQueries(tqs ...testQuery) {
for _, t := range tqs {
t.Run(s)
}
}

// runTestCases runs test transactions or queries, stopping early if an error occurs
func (s *IntegrationTestSuite) runTestCases(tcs ...TestCase) {
for _, t := range tcs {
if !s.abort {
s.abort = t.Run(s)
}
func (s *IntegrationTestSuite) runTestTransactions(txs ...testTransaction) {
for _, t := range txs {
t.Run(s)
}
}

type testTransaction struct {
name string
msg string
command *cobra.Command
args []string
expectedErr *errors.Error
}

type testQuery struct {
name string
msg string
command *cobra.Command
args []string
expectErr bool
responseType proto.Message
expectedResponse proto.Message
}

func (t testTransaction) Run(s *IntegrationTestSuite) (abort bool) {
func (t testTransaction) Run(s *IntegrationTestSuite) {
clientCtx := s.network.Validators[0].ClientCtx

txFlags := []string{
Expand All @@ -87,36 +84,21 @@ func (t testTransaction) Run(s *IntegrationTestSuite) (abort bool) {

t.args = append(t.args, txFlags...)

s.Run(t.name, func() {
out, err := clitestutil.ExecTestCLICmd(clientCtx, t.command, t.args)
s.Require().NoError(err)
if err != nil {
abort = true
}

resp := &sdk.TxResponse{}
err = clientCtx.Codec.UnmarshalJSON(out.Bytes(), resp)
s.Require().NoError(err, out.String())
if err != nil {
abort = true
}

if t.expectedErr == nil {
s.Require().Equal(0, int(resp.Code), "events %v", resp.Events)
if int(resp.Code) != 0 {
abort = true
}
} else {
s.Require().Equal(int(t.expectedErr.ABCICode()), int(resp.Code))
if int(resp.Code) != int(t.expectedErr.ABCICode()) {
abort = true
}
}
})
return abort
out, err := clitestutil.ExecTestCLICmd(clientCtx, t.command, t.args)
s.Require().NoError(err, t.msg)

resp := &sdk.TxResponse{}
err = clientCtx.Codec.UnmarshalJSON(out.Bytes(), resp)
s.Require().NoError(err, t.msg)

if t.expectedErr == nil {
s.Require().Equal(0, int(resp.Code), t.msg)
} else {
s.Require().Equal(int(t.expectedErr.ABCICode()), int(resp.Code), t.msg)
}
}

func (t testQuery) Run(s *IntegrationTestSuite) (abort bool) {
func (t testQuery) Run(s *IntegrationTestSuite) {
clientCtx := s.network.Validators[0].ClientCtx

queryFlags := []string{
Expand All @@ -125,31 +107,16 @@ func (t testQuery) Run(s *IntegrationTestSuite) (abort bool) {

t.args = append(t.args, queryFlags...)

s.Run(t.name, func() {
out, err := clitestutil.ExecTestCLICmd(clientCtx, t.command, t.args)

if t.expectErr {
s.Require().Error(err)
if err == nil {
abort = true
}
} else {
s.Require().NoError(err)
if err != nil {
abort = true
}

err = clientCtx.Codec.UnmarshalJSON(out.Bytes(), t.responseType)
s.Require().NoError(err, out.String())
if err != nil {
abort = true
}

s.Require().Equal(t.expectedResponse, t.responseType)
if !s.Assert().Equal(t.expectedResponse, t.responseType) {
abort = true
}
}
})
return abort
out, err := clitestutil.ExecTestCLICmd(clientCtx, t.command, t.args)

if t.expectErr {
s.Require().Error(err, t.msg)
} else {
s.Require().NoError(err, t.msg)

err = clientCtx.Codec.UnmarshalJSON(out.Bytes(), t.responseType)
s.Require().NoError(err, t.msg)

s.Require().Equal(t.expectedResponse, t.responseType)
}
}
32 changes: 16 additions & 16 deletions x/leverage/client/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func (s *IntegrationTestSuite) TestInvalidQueries() {
invalidQueries := []TestCase{
testQuery{
invalidQueries := []testQuery{
{
"query market summary - invalid denom",
cli.GetCmdQueryMarketSummary(),
[]string{
Expand All @@ -20,7 +20,7 @@ func (s *IntegrationTestSuite) TestInvalidQueries() {
nil,
nil,
},
testQuery{
{
"query account balances - invalid address",
cli.GetCmdQueryAccountBalances(),
[]string{
Expand All @@ -30,7 +30,7 @@ func (s *IntegrationTestSuite) TestInvalidQueries() {
nil,
nil,
},
testQuery{
{
"query account summary - invalid address",
cli.GetCmdQueryAccountSummary(),
[]string{
Expand All @@ -43,16 +43,16 @@ func (s *IntegrationTestSuite) TestInvalidQueries() {
}

// These queries do not require any borrower setup because they contain invalid arguments
s.runTestCases(invalidQueries...)
s.runTestQueries(invalidQueries...)
}

func (s *IntegrationTestSuite) TestLeverageScenario() {
val := s.network.Validators[0]

oraclePrice := sdk.MustNewDecFromStr("0.00003421")

initialQueries := []TestCase{
testQuery{
initialQueries := []testQuery{
{
"query params",
cli.GetCmdQueryParams(),
[]string{},
Expand All @@ -62,7 +62,7 @@ func (s *IntegrationTestSuite) TestLeverageScenario() {
Params: types.DefaultParams(),
},
},
testQuery{
{
"query registered tokens",
cli.GetCmdQueryRegisteredTokens(),
[]string{},
Expand Down Expand Up @@ -94,7 +94,7 @@ func (s *IntegrationTestSuite) TestLeverageScenario() {
},
},
},
testQuery{
{
"query market summary - zero supply",
cli.GetCmdQueryMarketSummary(),
[]string{
Expand Down Expand Up @@ -202,8 +202,8 @@ func (s *IntegrationTestSuite) TestLeverageScenario() {
nil,
}

nonzeroQueries := []TestCase{
testQuery{
nonzeroQueries := []testQuery{
{
"query account balances",
cli.GetCmdQueryAccountBalances(),
[]string{
Expand All @@ -223,7 +223,7 @@ func (s *IntegrationTestSuite) TestLeverageScenario() {
),
},
},
testQuery{
{
"query account summary",
cli.GetCmdQueryAccountSummary(),
[]string{
Expand All @@ -250,20 +250,20 @@ func (s *IntegrationTestSuite) TestLeverageScenario() {
}

// These queries do not require any borrower setup
s.runTestCases(initialQueries...)
s.runTestQueries(initialQueries...)

// These transactions will set up nonzero leverage positions and allow nonzero query results
s.runTestCases(
s.runTestTransactions(
supply,
addCollateral,
borrow,
)

// These queries run while the supplying and borrowing is active to produce nonzero output
s.runTestCases(nonzeroQueries...)
s.runTestQueries(nonzeroQueries...)

// These transactions run after nonzero queries are finished
s.runTestCases(
s.runTestTransactions(
liquidate,
repay,
removeCollateral,
Expand Down
5 changes: 5 additions & 0 deletions x/leverage/fixtures/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"github.com/umee-network/umee/v3/x/leverage/types"
)

const (
// AtomDenom is an ibc denom to be used as ATOM's BaseDenom during testing
AtomDenom = "ibc/CDC4587874B85BEA4FCEC3CEA5A1195139799A1FEE711A07D972537E18FDA39D"
)

// Token returns a valid token
func Token(base, symbol string) types.Token {
return types.Token{
Expand Down
Loading

0 comments on commit 9ec13b9

Please sign in to comment.