cosmos-sdk/x/accounts/query_server_test.go
Hwangjae Lee 06c9c543ab
feat(x/accounts): Add schema caching feature and corresponding test case (#20055)
Signed-off-by: Hwangjae Lee <meetrick@gmail.com>
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: guoguangwu <guoguangwug@gmail.com>
Signed-off-by: cuithon <dscs@outlook.com>
Signed-off-by: ipangpang <arronipangpang@gmail.com>
Co-authored-by: Marko <marko@baricevic.me>
Co-authored-by: Reece Williams <31943163+Reecepbcups@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: guangwu <guoguangwu@magic-shield.com>
Co-authored-by: Tien Nguyen <htiennv@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
Co-authored-by: son trinh <trinhleson2000@gmail.com>
Co-authored-by: Erlangshen219 <104747507+Erlangshen219@users.noreply.github.com>
Co-authored-by: cuithon <65674308+cuithon@users.noreply.github.com>
Co-authored-by: Andi <36215014+ChengenH@users.noreply.github.com>
Co-authored-by: ipangpang <167595720+ipangpang@users.noreply.github.com>
Co-authored-by: goofylfg <165781272+goofylfg@users.noreply.github.com>
Co-authored-by: Alexander Peters <alpe@users.noreply.github.com>
Co-authored-by: Leon <156270887+leonz789@users.noreply.github.com>
Co-authored-by: qinglin89 <316032931@qq.com>
Co-authored-by: Facundo <facundomedica@gmail.com>
Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
Co-authored-by: Matt Kocubinski <mkocubinski@gmail.com>
Co-authored-by: Hieu Vu <72878483+hieuvubk@users.noreply.github.com>
Co-authored-by: Cosmos SDK <113218068+github-prbot@users.noreply.github.com>
Co-authored-by: github-merge-queue <118344674+github-merge-queue@users.noreply.github.com>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>
Co-authored-by: Kien Trinh <51135161+kien6034@users.noreply.github.com>
Co-authored-by: Tuan Tran <tuantran@notional.ventures>
Co-authored-by: Khanh Hoa <khanhoait.bka@gmail.com>
Co-authored-by: Emil Georgiev <emil.georgiev@mail.schwarz>
Co-authored-by: Khanh Hoa <49144992+hoanguyenkh@users.noreply.github.com>
Co-authored-by: Hoang Do <hoangdv2429@gmail.com>
Co-authored-by: Duong Minh Ngoc <153509244+minhngoc274@users.noreply.github.com>
Co-authored-by: Khanh Hoa <hoa@notional.ventures>
Co-authored-by: kienn6034 <kien@notional.ventures>
Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com>
Co-authored-by: Qt <golang.chen@gmail.com>
Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
2024-05-17 05:11:45 +00:00

78 lines
2.1 KiB
Go

package accounts
import (
"testing"
"github.com/cosmos/gogoproto/types"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/wrapperspb"
"cosmossdk.io/x/accounts/accountstd"
"cosmossdk.io/x/accounts/internal/implementation"
v1 "cosmossdk.io/x/accounts/v1"
)
func TestQueryServer(t *testing.T) {
k, ctx := newKeeper(t, accountstd.AddAccount("test", NewTestAccount))
ms := NewMsgServer(k)
qs := NewQueryServer(k)
// create account
initMsg, err := implementation.PackAny(&emptypb.Empty{})
require.NoError(t, err)
initResp, err := ms.Init(ctx, &v1.MsgInit{
Sender: "sender",
AccountType: "test",
Message: initMsg,
})
require.NoError(t, err)
t.Run("account query", func(t *testing.T) {
// query
req := &wrapperspb.UInt64Value{Value: 10}
anypbReq, err := implementation.PackAny(req)
require.NoError(t, err)
queryResp, err := qs.AccountQuery(ctx, &v1.AccountQueryRequest{
Target: initResp.AccountAddress,
Request: anypbReq,
})
require.NoError(t, err)
resp, err := implementation.UnpackAnyRaw(queryResp.Response)
require.NoError(t, err)
require.Equal(t, "10", resp.(*types.StringValue).Value)
})
t.Run("account number", func(t *testing.T) {
numResp, err := qs.AccountNumber(ctx, &v1.AccountNumberRequest{Address: initResp.AccountAddress})
require.NoError(t, err)
require.Equal(t, 0, int(numResp.Number))
})
t.Run("account type", func(t *testing.T) {
typ, err := qs.AccountType(ctx, &v1.AccountTypeRequest{Address: initResp.AccountAddress})
require.NoError(t, err)
require.Equal(t, "test", typ.AccountType)
})
t.Run("schema caching", func(t *testing.T) {
// Request schema once
schemaReq := &v1.SchemaRequest{AccountType: "test"}
schemaResp1, err := qs.Schema(ctx, schemaReq)
require.NoError(t, err)
require.NotNil(t, schemaResp1)
// Request schema again
schemaResp2, err := qs.Schema(ctx, schemaReq)
require.NoError(t, err)
require.NotNil(t, schemaResp2)
// Check if both responses are the same (cached)
require.Equal(t, schemaResp1, schemaResp2)
})
}