forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 35
/
context_test.go
116 lines (101 loc) · 2.54 KB
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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, err := network.New(t, t.TempDir(), cfg)
require.NoError(t, err)
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)
}