Skip to content

Commit

Permalink
fix: add tests for x/liquidity/types and fix bugs
Browse files Browse the repository at this point in the history
Merge pull request #111 from cosmosquad-labs/testing-1
  • Loading branch information
hallazzang authored Feb 4, 2022
2 parents 5143d47 + 2b3470c commit 9318023
Show file tree
Hide file tree
Showing 19 changed files with 1,169 additions and 298 deletions.
16 changes: 8 additions & 8 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/params"
store "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil/network"
simappparams "github.com/cosmosquad-labs/squad/app/params"

"github.com/cosmos/cosmos-sdk/std"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
Expand All @@ -32,13 +24,21 @@ import (
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/std"
store "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

simappparams "github.com/cosmosquad-labs/squad/app/params"
minttypes "github.com/cosmosquad-labs/squad/x/mint/types"
)

Expand Down
14 changes: 14 additions & 0 deletions x/liquidity/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build norace
// +build norace

package testutil

import (
"testing"

"github.com/stretchr/testify/suite"
)

func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
40 changes: 40 additions & 0 deletions x/liquidity/client/testutil/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package testutil

import (
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmosquad-labs/squad/x/liquidity/client/cli"
)

var commonArgs = []string{
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)).String()),
}

func MsgCreatePair(clientCtx client.Context, from, baseCoinDenom, quoteCoinDenom string, extraArgs ...string) (testutil.BufferWriter, error) {
args := append(append([]string{
baseCoinDenom,
quoteCoinDenom,
fmt.Sprintf("--%s=%s", flags.FlagFrom, from),
}, commonArgs...), extraArgs...)

return clitestutil.ExecTestCLICmd(clientCtx, cli.NewCreatePairCmd(), args)
}

func MsgCreatePool(clientCtx client.Context, from string, pairId uint64, depositCoins sdk.Coins, extraArgs ...string) (testutil.BufferWriter, error) {
args := append(append([]string{
strconv.FormatUint(pairId, 10),
depositCoins.String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, from),
}, commonArgs...), extraArgs...)

return clitestutil.ExecTestCLICmd(clientCtx, cli.NewCreatePoolCmd(), args)
}
107 changes: 107 additions & 0 deletions x/liquidity/client/testutil/suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package testutil

import (
"testing"

"github.com/stretchr/testify/suite"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/simapp"
store "github.com/cosmos/cosmos-sdk/store/types"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"

squadapp "github.com/cosmosquad-labs/squad/app"
squadparams "github.com/cosmosquad-labs/squad/app/params"
"github.com/cosmosquad-labs/squad/x/liquidity/client/cli"
"github.com/cosmosquad-labs/squad/x/liquidity/types"
)

type IntegrationTestSuite struct {
suite.Suite

cfg network.Config
network *network.Network
val *network.Validator
clientCtx client.Context
}

func NewAppConstructor(encodingCfg squadparams.EncodingConfig) network.AppConstructor {
return func(val network.Validator) servertypes.Application {
return squadapp.NewSquadApp(
val.Ctx.Logger, dbm.NewMemDB(), nil, true, make(map[int64]bool), val.Ctx.Config.RootDir, 0,
encodingCfg,
simapp.EmptyAppOptions{},
baseapp.SetPruning(store.NewPruningOptionsFromString(val.AppConfig.Pruning)),
baseapp.SetMinGasPrices(val.AppConfig.MinGasPrices),
)
}
}

func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

if testing.Short() {
s.T().Skip("skipping test in unit-tests mode.")
}

encCfg := squadapp.MakeTestEncodingConfig()

cfg := network.DefaultConfig()
cfg.AppConstructor = NewAppConstructor(encCfg)
cfg.GenesisState = squadapp.ModuleBasics.DefaultGenesis(cfg.Codec)
cfg.NumValidators = 1

s.cfg = cfg
s.network = network.New(s.T(), cfg)

s.val = s.network.Validators[0]
s.clientCtx = s.val.ClientCtx

_, err := s.network.WaitForHeight(1)
s.Require().NoError(err)

s.createPair("node0token", s.cfg.BondDenom)

err = s.network.WaitForNextBlock()
s.Require().NoError(err)
}

func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}

func (s *IntegrationTestSuite) createPair(baseCoinDenom, quoteCoinDenom string) {
_, err := MsgCreatePair(s.clientCtx, s.val.Address.String(), baseCoinDenom, quoteCoinDenom)
s.Require().NoError(err)

err = s.network.WaitForNextBlock()
s.Require().NoError(err)
}

//nolint
func (s *IntegrationTestSuite) createPool(pairId uint64, depositCoins sdk.Coins) {
_, err := MsgCreatePool(s.clientCtx, s.val.Address.String(), pairId, depositCoins)
s.Require().NoError(err)

err = s.network.WaitForNextBlock()
s.Require().NoError(err)
}

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

cmd := cli.QueryPairs()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, []string{"--output=json"})
s.Require().NoError(err)

var resp types.QueryPairsResponse
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &resp), out.String())
s.Require().NotNil(resp.Pairs)
}
1 change: 1 addition & 0 deletions x/liquidity/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"

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

"github.com/cosmosquad-labs/squad/x/liquidity/types"
)

Expand Down
79 changes: 79 additions & 0 deletions x/liquidity/types/common_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package types_test

import (
"time"

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

"github.com/cosmosquad-labs/squad/x/liquidity/types"
)

var testAddr = sdk.AccAddress(crypto.AddressHash([]byte("test")))

func newBuyOrder(price sdk.Dec, amt sdk.Int) *types.BaseOrder {
return types.NewBaseOrder(types.SwapDirectionBuy, price, amt, price.MulInt(amt).TruncateInt())
}
Expand All @@ -14,6 +19,72 @@ func newSellOrder(price sdk.Dec, amt sdk.Int) *types.BaseOrder {
return types.NewBaseOrder(types.SwapDirectionSell, price, amt, amt)
}

func newBuyUserOrder(reqId uint64, price sdk.Dec, amt sdk.Int) *types.UserOrder {
return &types.UserOrder{
BaseOrder: types.BaseOrder{
Direction: types.SwapDirectionBuy,
Price: price,
Amount: amt,
OpenAmount: amt,
OfferCoinAmount: price.MulInt(amt).Ceil().TruncateInt(),
RemainingOfferCoinAmount: price.MulInt(amt).Ceil().TruncateInt(),
ReceivedAmount: sdk.ZeroInt(),
},
RequestId: reqId,
Orderer: testAddr,
}
}

//nolint
func newSellUserOrder(reqId uint64, price sdk.Dec, amt sdk.Int) *types.UserOrder {
return &types.UserOrder{
BaseOrder: types.BaseOrder{
Direction: types.SwapDirectionSell,
Price: price,
Amount: amt,
OpenAmount: amt,
OfferCoinAmount: amt,
RemainingOfferCoinAmount: amt,
ReceivedAmount: sdk.ZeroInt(),
},
RequestId: reqId,
Orderer: testAddr,
}
}

func newBuyPoolOrder(poolId uint64, price sdk.Dec, amt sdk.Int) *types.PoolOrder {
return &types.PoolOrder{
BaseOrder: types.BaseOrder{
Direction: types.SwapDirectionBuy,
Price: price,
Amount: amt,
OpenAmount: amt,
OfferCoinAmount: price.MulInt(amt).Ceil().TruncateInt(),
RemainingOfferCoinAmount: price.MulInt(amt).Ceil().TruncateInt(),
ReceivedAmount: sdk.ZeroInt(),
},
PoolId: poolId,
ReserveAddress: testAddr,
}
}

//nolint
func newSellPoolOrder(poolId uint64, price sdk.Dec, amt sdk.Int) *types.PoolOrder {
return &types.PoolOrder{
BaseOrder: types.BaseOrder{
Direction: types.SwapDirectionSell,
Price: price,
Amount: amt,
OpenAmount: amt,
OfferCoinAmount: amt,
RemainingOfferCoinAmount: amt,
ReceivedAmount: sdk.ZeroInt(),
},
PoolId: poolId,
ReserveAddress: testAddr,
}
}

func newInt(i int64) sdk.Int {
return sdk.NewInt(i)
}
Expand All @@ -37,3 +108,11 @@ func parseCoins(s string) sdk.Coins {
}
return coins
}

func parseTime(s string) time.Time {
t, err := time.Parse(time.RFC3339, s)
if err != nil {
panic(err)
}
return t
}
Loading

0 comments on commit 9318023

Please sign in to comment.