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

refactor: improve leverage keeper tests and errors #1300

Merged
merged 45 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
ca8df09
supply and withdraw tests
toteki Aug 25, 2022
d17672e
err--
toteki Aug 25, 2022
3f6e746
make proto-all
toteki Aug 25, 2022
18ad3f9
error improvements and fix cascading client test error DEVX
toteki Aug 29, 2022
01a0d9d
TestCollateralize
toteki Aug 29, 2022
55e0242
++
toteki Aug 29, 2022
3ac25d8
TestDe+Collateralize
toteki Aug 29, 2022
fb2465d
case++
toteki Aug 29, 2022
196a2dd
borrow test + more cases
toteki Aug 29, 2022
0c89068
add invariant checks to every msg test case
toteki Aug 29, 2022
a7c28ad
move files
toteki Aug 29, 2022
c24c19b
++
toteki Aug 29, 2022
7bf90ef
TestRepay
toteki Aug 29, 2022
da7a29d
--
toteki Aug 29, 2022
8bdbc1f
Liquidate single-denom test cases
toteki Aug 29, 2022
29075d0
liquidate cases
toteki Aug 30, 2022
f4d3fbf
--
toteki Aug 30, 2022
b6c065b
remove setupAccount and initBorrowScenario
toteki Aug 30, 2022
e0f12e0
Merge branch 'main' into adam/tests
toteki Aug 30, 2022
c317fae
imports
toteki Aug 30, 2022
84b454b
lint++
toteki Aug 30, 2022
137b60b
fix test
toteki Aug 30, 2022
16f6087
++
toteki Aug 30, 2022
c951541
cl++
toteki Aug 30, 2022
abc1520
remove unnecessary interface
toteki Aug 30, 2022
58d3dab
move function
toteki Aug 30, 2022
c38f47f
Merge branch 'main' into adam/tests
toteki Aug 30, 2022
357b4c3
apply s.app, s.ctx, and s.Require() shorthands
toteki Aug 30, 2022
b0058d0
Merge branch 'adam/tests' of github.com:umee-network/umee into adam/t…
toteki Aug 30, 2022
2d7c253
apply coin shorthand in keeper_test package
toteki Aug 30, 2022
104e8f2
use setReserves helper
toteki Aug 30, 2022
f19ad73
improve big number readability
toteki Aug 30, 2022
d69a209
Merge branch 'main' into adam/tests
toteki Aug 30, 2022
29464d2
ensure espected value is on the left for all require.Equal
toteki Aug 30, 2022
95aa22b
use borrow and repay helpers
toteki Aug 30, 2022
28c4707
lint++
toteki Aug 30, 2022
2031109
always require.ErrorIs
toteki Aug 30, 2022
e4c6f67
use forceBorrow helper rather than SetTokenSettings in high utilizati…
toteki Aug 30, 2022
09daf91
Merge branch 'main' into adam/tests
toteki Aug 30, 2022
ed7b666
Merge branch 'main' into adam/tests
toteki Aug 30, 2022
187c0fe
fix #1310
toteki Aug 31, 2022
ace8889
Merge branch 'main' into adam/tests
toteki Aug 31, 2022
b3379b6
fix sim tests
toteki Sep 1, 2022
e526216
Merge branch 'main' into adam/tests
toteki Sep 1, 2022
ccec6f0
Merge branch 'main' into adam/tests
toteki Sep 1, 2022
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 @@ -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

toteki marked this conversation as resolved.
Show resolved Hide resolved
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