-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[perp] add: msg open position tests (#462)
* 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
1 parent
efccccc
commit 76ff91d
Showing
2 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |