Skip to content

Commit

Permalink
[perp] add: msg open position tests (#462)
Browse files Browse the repository at this point in the history
* add: msg open position tests

* chore: lint

* Update x/perp/types/msgs.go

Co-authored-by: Walter White <[email protected]>

* fix: revet back suggestion

Co-authored-by: Walter White <[email protected]>
  • Loading branch information
testinginprod and NibiruHeisenberg authored May 23, 2022
1 parent efccccc commit 76ff91d
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion x/perp/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (m *MsgOpenPosition) ValidateBasic() error {
if !m.BaseAssetAmountLimit.GT(sdk.ZeroInt()) {
return fmt.Errorf("base asset amount limit must always be greater than zero")
}
if m.QuoteAssetAmount.GT(sdk.ZeroInt()) {
if !m.QuoteAssetAmount.GT(sdk.ZeroInt()) {
return fmt.Errorf("quote asset amount must be always greater than zero")
}

Expand Down
111 changes: 111 additions & 0 deletions x/perp/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package types

import (
"testing"

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

"github.com/NibiruChain/nibiru/x/testutil/sample"
)

func TestMsgOpenPosition_ValidateBasic(t *testing.T) {
type test struct {
msg *MsgOpenPosition
wantErr bool
}

cases := map[string]test{
"ok": {
msg: &MsgOpenPosition{
Sender: sample.AccAddress().String(),
TokenPair: "NIBI:USDN",
Side: Side_BUY,
QuoteAssetAmount: sdk.NewInt(100),
Leverage: sdk.NewDec(10),
BaseAssetAmountLimit: sdk.NewInt(100),
},
wantErr: false,
},

"invalid side": {
msg: &MsgOpenPosition{
Sender: sample.AccAddress().String(),
TokenPair: "NIBI:USDN",
Side: 2,
QuoteAssetAmount: sdk.NewInt(100),
Leverage: sdk.NewDec(10),
BaseAssetAmountLimit: sdk.NewInt(100),
},
wantErr: true,
},
"invalid address": {
msg: &MsgOpenPosition{
Sender: "",
TokenPair: "NIBI:USDN",
Side: Side_SELL,
QuoteAssetAmount: sdk.NewInt(100),
Leverage: sdk.NewDec(10),
BaseAssetAmountLimit: sdk.NewInt(100),
},
wantErr: true,
},
"invalid leverage": {
msg: &MsgOpenPosition{
Sender: sample.AccAddress().String(),
TokenPair: "NIBI:USDN",
Side: Side_BUY,
QuoteAssetAmount: sdk.NewInt(100),
Leverage: sdk.ZeroDec(),
BaseAssetAmountLimit: sdk.NewInt(100),
},
wantErr: true,
},
"invalid quote asset amount": {
msg: &MsgOpenPosition{
Sender: sample.AccAddress().String(),
TokenPair: "NIBI:USDN",
Side: Side_BUY,
QuoteAssetAmount: sdk.NewInt(0),
Leverage: sdk.NewDec(10),
BaseAssetAmountLimit: sdk.NewInt(100),
},
wantErr: true,
},
"invalid token pair": {
msg: &MsgOpenPosition{
Sender: sample.AccAddress().String(),
TokenPair: "NIBI-USDN",
Side: Side_BUY,
QuoteAssetAmount: sdk.NewInt(0),
Leverage: sdk.NewDec(10),
BaseAssetAmountLimit: sdk.NewInt(100),
},
wantErr: true,
},
"invalid base asset amount limit": {
msg: &MsgOpenPosition{
Sender: sample.AccAddress().String(),
TokenPair: "NIBI:USDN",
Side: Side_BUY,
QuoteAssetAmount: sdk.NewInt(0),
Leverage: sdk.NewDec(10),
BaseAssetAmountLimit: sdk.ZeroInt(),
},
wantErr: true,
},
}

for name, tc := range cases {
tc := tc
name := name
t.Run(name, func(t *testing.T) {
err := tc.msg.ValidateBasic()
if err != nil && tc.wantErr == false {
t.Fatalf("unexpected error: %s", err)
}
if err == nil && tc.wantErr == true {
t.Fatalf("expected error: %s", err)
}
})
}
}

0 comments on commit 76ff91d

Please sign in to comment.