cosmos-sdk/client/context_test.go
Alexander Bezobchuk 0ccc48d2a3
CLI/Tests: Remove Fixtures (#6799)
* remove fixtures

* setup tests

* update x/mint

* cli: update x/staking commands

* tests: convert x/staking CLI tests

* tests: fix x/auth CLI tests

* cli updates

* fix buiild

* fix build

* Update x/gov/client/cli/cli_test.go

Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>

* remove GenerateOrBroadcastTx

* move TestCLIQueryConn

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
2020-07-21 13:54:07 +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_PrintOutput(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.WithJSONMarshaler(codec.NewProtoCodec(registry))
// json
buf := &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "json"
err = ctx.PrintOutput(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`{"animal":{"@type":"/testdata.Dog","size":"big","name":"Spot"},"x":"10"}
`, string(buf.Bytes()))
// yaml
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "text"
err = ctx.PrintOutput(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`animal:
'@type': /testdata.Dog
name: Spot
size: big
x: "10"
`, string(buf.Bytes()))
//
// amino
//
amino := testdata.NewTestAmino()
ctx = ctx.WithJSONMarshaler(codec.NewAminoCodec(&codec.Codec{Amino: amino}))
// json
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "json"
err = ctx.PrintOutput(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`{"type":"testdata/HasAnimal","value":{"animal":{"type":"testdata/Dog","value":{"size":"big","name":"Spot"}},"x":"10"}}
`, string(buf.Bytes()))
// yaml
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "text"
err = ctx.PrintOutput(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`type: testdata/HasAnimal
value:
animal:
type: testdata/Dog
value:
name: Spot
size: big
x: "10"
`, string(buf.Bytes()))
}
func TestCLIQueryConn(t *testing.T) {
cfg := network.DefaultConfig()
cfg.NumValidators = 1
n := network.New(t, cfg)
defer n.Cleanup()
testClient := testdata.NewTestServiceClient(n.Validators[0].ClientCtx)
res, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
require.NoError(t, err)
require.Equal(t, "hello", res.Message)
}