cosmos-sdk/client/context_test.go
Robert Zaremba 7bca49e2d2
refactor(client): add client/Context.Codec and deprecate JSONCodec (#9498)
* chore(types): add sdk.Context.Codec and deprecate JSONCodec

* Use clientContext.Codec rather than JSONCodec everywhere

* update tests to use clientContext.Codec

* added a note that EncodingConfig.Marshaler will be renamed to Codec

* update changelog

* fix tests to use clientCtx.WithCodec instead of WithJSONCodec

* fix genutil build

* Update simapp/params/encoding.go

Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>

Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
2021-06-11 11:49:39 +00:00

116 lines
2.5 KiB
Go

package client_test
import (
"bytes"
"context"
"os"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)
func TestMain(m *testing.M) {
viper.Set(flags.FlagKeyringBackend, keyring.BackendMemory)
os.Exit(m.Run())
}
func TestContext_PrintObject(t *testing.T) {
ctx := client.Context{}
animal := &testdata.Dog{
Size_: "big",
Name: "Spot",
}
any, err := types.NewAnyWithValue(animal)
require.NoError(t, err)
hasAnimal := &testdata.HasAnimal{
Animal: any,
X: 10,
}
//
// proto
//
registry := testdata.NewTestInterfaceRegistry()
ctx = ctx.WithCodec(codec.NewProtoCodec(registry))
// json
buf := &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "json"
err = ctx.PrintProto(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`{"animal":{"@type":"/testdata.Dog","size":"big","name":"Spot"},"x":"10"}
`, buf.String())
// yaml
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "text"
err = ctx.PrintProto(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`animal:
'@type': /testdata.Dog
name: Spot
size: big
x: "10"
`, buf.String())
//
// amino
//
amino := testdata.NewTestAmino()
ctx = ctx.WithLegacyAmino(&codec.LegacyAmino{Amino: amino})
// json
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "json"
err = ctx.PrintObjectLegacy(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`{"type":"testdata/HasAnimal","value":{"animal":{"type":"testdata/Dog","value":{"size":"big","name":"Spot"}},"x":"10"}}
`, buf.String())
// yaml
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "text"
err = ctx.PrintObjectLegacy(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`type: testdata/HasAnimal
value:
animal:
type: testdata/Dog
value:
name: Spot
size: big
x: "10"
`, buf.String())
}
func TestCLIQueryConn(t *testing.T) {
cfg := network.DefaultConfig()
cfg.NumValidators = 1
n := network.New(t, cfg)
defer n.Cleanup()
testClient := testdata.NewQueryClient(n.Validators[0].ClientCtx)
res, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
require.NoError(t, err)
require.Equal(t, "hello", res.Message)
}