2019-08-19 06:29:17 -07:00
|
|
|
package keeper_test
|
2019-06-18 06:44:09 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2019-08-19 06:29:17 -07:00
|
|
|
keep "github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
|
2019-06-18 06:44:09 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBalances(t *testing.T) {
|
2019-08-19 06:29:17 -07:00
|
|
|
app, ctx := createTestApp(false)
|
2019-06-18 06:44:09 -07:00
|
|
|
req := abci.RequestQuery{
|
2019-08-19 06:29:17 -07:00
|
|
|
Path: fmt.Sprintf("custom/bank/%s", keep.QueryBalance),
|
2019-06-18 06:44:09 -07:00
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2019-08-19 06:29:17 -07:00
|
|
|
querier := keep.NewQuerier(app.BankKeeper)
|
2019-06-18 06:44:09 -07:00
|
|
|
|
2019-08-19 06:29:17 -07:00
|
|
|
res, err := querier(ctx, []string{"balances"}, req)
|
2019-06-18 06:44:09 -07:00
|
|
|
require.NotNil(t, err)
|
|
|
|
require.Nil(t, res)
|
|
|
|
|
|
|
|
_, _, addr := authtypes.KeyTestPubAddr()
|
2019-08-19 06:29:17 -07:00
|
|
|
req.Data = app.Codec().MustMarshalJSON(types.NewQueryBalanceParams(addr))
|
|
|
|
res, err = querier(ctx, []string{"balances"}, req)
|
2019-06-18 06:44:09 -07:00
|
|
|
require.Nil(t, err) // the account does not exist, no error returned anyway
|
|
|
|
require.NotNil(t, res)
|
|
|
|
|
|
|
|
var coins sdk.Coins
|
2019-08-19 06:29:17 -07:00
|
|
|
require.NoError(t, app.Codec().UnmarshalJSON(res, &coins))
|
2019-06-18 06:44:09 -07:00
|
|
|
require.True(t, coins.IsZero())
|
|
|
|
|
2019-08-19 06:29:17 -07:00
|
|
|
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
|
2019-06-18 06:44:09 -07:00
|
|
|
acc.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("foo", 10)))
|
2019-08-19 06:29:17 -07:00
|
|
|
app.AccountKeeper.SetAccount(ctx, acc)
|
|
|
|
res, err = querier(ctx, []string{"balances"}, req)
|
2019-06-18 06:44:09 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, res)
|
2019-08-19 06:29:17 -07:00
|
|
|
require.NoError(t, app.Codec().UnmarshalJSON(res, &coins))
|
2019-06-18 06:44:09 -07:00
|
|
|
require.True(t, coins.AmountOf("foo").Equal(sdk.NewInt(10)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuerierRouteNotFound(t *testing.T) {
|
2019-08-19 06:29:17 -07:00
|
|
|
app, ctx := createTestApp(false)
|
2019-06-18 06:44:09 -07:00
|
|
|
req := abci.RequestQuery{
|
|
|
|
Path: "custom/bank/notfound",
|
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2019-08-19 06:29:17 -07:00
|
|
|
querier := keep.NewQuerier(app.BankKeeper)
|
|
|
|
_, err := querier(ctx, []string{"notfound"}, req)
|
2019-06-18 06:44:09 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|