2020-01-14 15:04:47 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-04-30 14:23:41 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-01-14 15:04:47 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2020-04-30 14:13:31 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2020-01-14 15:04:47 +00:00
|
|
|
"github.com/kava-labs/kava/x/auction/keeper"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/kava-labs/kava/x/auction/testutil"
|
2020-01-14 15:04:47 +00:00
|
|
|
"github.com/kava-labs/kava/x/auction/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
custom = "custom"
|
|
|
|
TestAuctionCount = 10
|
|
|
|
)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
type querierTestSuite struct {
|
|
|
|
testutil.Suite
|
2020-01-14 15:04:47 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
auctions []types.Auction
|
|
|
|
legacyAmino *codec.LegacyAmino
|
|
|
|
querier sdk.Querier
|
2020-01-14 15:04:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *querierTestSuite) SetupTest() {
|
|
|
|
suite.Suite.SetupTest(10)
|
2020-01-14 15:04:47 +00:00
|
|
|
// Populate with auctions
|
|
|
|
for j := 0; j < TestAuctionCount; j++ {
|
2020-09-17 00:45:10 +00:00
|
|
|
var id uint64
|
|
|
|
var err error
|
2022-01-08 00:39:27 +00:00
|
|
|
lotAmount := int64(rand.Intn(100-10) + 10)
|
|
|
|
|
|
|
|
// Add coins required for auction creation to module account
|
|
|
|
suite.AddCoinsToNamedModule(suite.ModAcc.Name, cs(c("token1", lotAmount), c("usdx", 20), c("debt", 10)))
|
|
|
|
|
|
|
|
ownerAddrIndex := rand.Intn(9-1) + 1
|
2020-09-17 00:45:10 +00:00
|
|
|
if ownerAddrIndex%2 == 0 {
|
2022-01-08 00:39:27 +00:00
|
|
|
id, err = suite.Keeper.StartSurplusAuction(suite.Ctx, suite.ModAcc.Name, c("token1", lotAmount), "token2")
|
2020-09-17 00:45:10 +00:00
|
|
|
} else {
|
2022-01-08 00:39:27 +00:00
|
|
|
id, err = suite.Keeper.StartCollateralAuction(suite.Ctx, suite.ModAcc.Name, c("token1", lotAmount), c("usdx", int64(20)),
|
|
|
|
[]sdk.AccAddress{suite.Addrs[ownerAddrIndex]}, []sdk.Int{sdk.NewInt(lotAmount)}, c("debt", int64(10)))
|
2020-09-17 00:45:10 +00:00
|
|
|
}
|
2020-01-21 17:41:37 +00:00
|
|
|
suite.NoError(err)
|
2020-01-14 15:04:47 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
auc, found := suite.Keeper.GetAuction(suite.Ctx, id)
|
2020-01-14 15:04:47 +00:00
|
|
|
suite.True(found)
|
|
|
|
suite.auctions = append(suite.auctions, auc)
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.legacyAmino = suite.App.LegacyAmino()
|
|
|
|
suite.querier = keeper.NewQuerier(suite.Keeper, suite.legacyAmino)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuerierTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(querierTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *querierTestSuite) assertQuerierResponse(expected interface{}, actual []byte) {
|
|
|
|
expectedJson, err := suite.legacyAmino.MarshalJSONIndent(expected, "", " ")
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(string(expectedJson), string(actual))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *querierTestSuite) TestQueryParams() {
|
|
|
|
bz, err := suite.querier(suite.Ctx, []string{types.QueryGetParams}, abci.RequestQuery{})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(bz)
|
2020-01-14 15:04:47 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
var params types.Params
|
|
|
|
suite.Require().NoError(suite.legacyAmino.UnmarshalJSON(bz, ¶ms))
|
|
|
|
|
|
|
|
expectedParams := suite.Keeper.GetParams(suite.Ctx)
|
|
|
|
suite.Require().Equal(expectedParams, params)
|
2020-01-14 15:04:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *querierTestSuite) TestQueryAuction() {
|
|
|
|
ctx := suite.Ctx.WithIsCheckTx(false)
|
|
|
|
|
2020-01-14 15:04:47 +00:00
|
|
|
// Set up request query
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetAuction}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryAuctionParams(suite.auctions[0].GetID())),
|
2020-01-14 15:04:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute query and check the []byte result
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetAuction}, query)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(bz)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.assertQuerierResponse(suite.auctions[0], bz)
|
2020-01-21 17:41:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *querierTestSuite) TestQueryAuctions() {
|
|
|
|
ctx := suite.Ctx.WithIsCheckTx(false)
|
|
|
|
|
2020-01-21 17:41:37 +00:00
|
|
|
// Set up request query
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryGetAuctions}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(
|
|
|
|
types.NewQueryAllAuctionParams(1, TestAuctionCount, "", "", "", nil),
|
2020-05-25 02:27:11 +00:00
|
|
|
),
|
2020-01-21 17:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute query and check the []byte result
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryGetAuctions}, query)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(bz)
|
2020-01-14 15:04:47 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.assertQuerierResponse(suite.Keeper.GetAllAuctions(suite.Ctx), bz)
|
2020-01-14 15:04:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *querierTestSuite) TestQueryNextAuctionID() {
|
|
|
|
bz, err := suite.querier(suite.Ctx, []string{types.QueryNextAuctionID}, abci.RequestQuery{})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(bz)
|
|
|
|
|
|
|
|
var nextAuctionID uint64
|
|
|
|
suite.Require().NoError(suite.legacyAmino.UnmarshalJSON(bz, &nextAuctionID))
|
|
|
|
|
|
|
|
expectedID, _ := suite.Keeper.GetNextAuctionID(suite.Ctx)
|
|
|
|
suite.Require().Equal(expectedID, nextAuctionID)
|
2020-01-14 15:04:47 +00:00
|
|
|
}
|