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

test: add tests for x/liquidity/types #111

Merged
merged 29 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
88b9425
test: add test for Matchable()
hallazzang Jan 25, 2022
9a68968
fix: do not multiply price to sell amount
hallazzang Jan 25, 2022
13dd3e6
test: add test for EstimatedPriceDirection()
hallazzang Jan 25, 2022
f489f32
style: sort imports
hallazzang Jan 26, 2022
f463ec8
chore: Merge branch 'main' of github.com:cosmosquad-labs/squad into t…
hallazzang Jan 26, 2022
587612e
chore: Merge branch '93-addr-naming' of github.com:cosmosquad-labs/sq…
hallazzang Jan 26, 2022
ac881bb
chore: Merge branch 'main' of github.com:cosmosquad-labs/squad into t…
hallazzang Jan 26, 2022
3d8b4e8
chore: Merge branch 'main' of github.com:cosmosquad-labs/squad into t…
hallazzang Jan 26, 2022
36d343e
chore: Merge branch 'testing-1' of github.com:cosmosquad-labs/squad i…
hallazzang Jan 26, 2022
7b25173
test: add and update keys tests
hallazzang Jan 26, 2022
06cb758
chore: Merge branch '114-market-order' of github.com:cosmosquad-labs/…
hallazzang Jan 26, 2022
7e1ef51
chore: Merge branch 'main' of github.com:cosmosquad-labs/squad into t…
hallazzang Jan 26, 2022
19760f6
test: update params test
hallazzang Jan 26, 2022
37fde95
test: update tests for requests
hallazzang Jan 26, 2022
70e83df
test: update tests
hallazzang Jan 27, 2022
5312cbb
chore: Merge branch 'main' of github.com:cosmosquad-labs/squad into t…
hallazzang Jan 27, 2022
29cc4b0
chore: Merge branch 'main' of github.com:cosmosquad-labs/squad into t…
hallazzang Jan 28, 2022
65f476f
test: remove outdated test
hallazzang Jan 28, 2022
c4033cc
chore: Merge branch 'main' of github.com:cosmosquad-labs/squad into t…
hallazzang Jan 28, 2022
4ec2813
style: sort imports
hallazzang Jan 29, 2022
42a5840
test: add integration test skeleton
hallazzang Jan 29, 2022
cfb6621
fix: rename PairEscrowAddr to PairEscrowAddress
hallazzang Feb 3, 2022
0d2d572
test: add tests for pair.go
hallazzang Feb 3, 2022
3de6ccb
test: update msg tests
hallazzang Feb 3, 2022
9fce5b3
fix: fix bug in UserOrder and PoolOrder
hallazzang Feb 4, 2022
221f5c1
fix: fix bug in Orders.Sort and add tests
hallazzang Feb 4, 2022
cd15eaf
chore: rename test file
hallazzang Feb 4, 2022
915b319
chore: add nolint flag
hallazzang Feb 4, 2022
2b3470c
chore: Merge branch 'main' of github.com:cosmosquad-labs/squad into t…
hallazzang Feb 4, 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
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