github.com/Finschia/finschia-sdk@v0.48.1/client/context_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/spf13/viper"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/Finschia/finschia-sdk/client"
    13  	"github.com/Finschia/finschia-sdk/client/flags"
    14  	"github.com/Finschia/finschia-sdk/codec"
    15  	"github.com/Finschia/finschia-sdk/codec/types"
    16  	"github.com/Finschia/finschia-sdk/crypto/keyring"
    17  	"github.com/Finschia/finschia-sdk/testutil/network"
    18  	"github.com/Finschia/finschia-sdk/testutil/testdata"
    19  )
    20  
    21  func TestMain(m *testing.M) {
    22  	viper.Set(flags.FlagKeyringBackend, keyring.BackendMemory)
    23  	os.Exit(m.Run())
    24  }
    25  
    26  func TestContext_PrintObject(t *testing.T) {
    27  	ctx := client.Context{}
    28  
    29  	animal := &testdata.Dog{
    30  		Size_: "big",
    31  		Name:  "Spot",
    32  	}
    33  	any, err := types.NewAnyWithValue(animal)
    34  	require.NoError(t, err)
    35  	hasAnimal := &testdata.HasAnimal{
    36  		Animal: any,
    37  		X:      10,
    38  	}
    39  
    40  	//
    41  	// proto
    42  	//
    43  	registry := testdata.NewTestInterfaceRegistry()
    44  	ctx = ctx.WithCodec(codec.NewProtoCodec(registry))
    45  
    46  	// json
    47  	buf := &bytes.Buffer{}
    48  	ctx = ctx.WithOutput(buf)
    49  	ctx.OutputFormat = "json"
    50  	err = ctx.PrintProto(hasAnimal)
    51  	require.NoError(t, err)
    52  	require.Equal(t,
    53  		`{"animal":{"@type":"/testdata.Dog","size":"big","name":"Spot"},"x":"10"}
    54  `, buf.String())
    55  
    56  	// yaml
    57  	buf = &bytes.Buffer{}
    58  	ctx = ctx.WithOutput(buf)
    59  	ctx.OutputFormat = "text"
    60  	err = ctx.PrintProto(hasAnimal)
    61  	require.NoError(t, err)
    62  	require.Equal(t,
    63  		`animal:
    64    '@type': /testdata.Dog
    65    name: Spot
    66    size: big
    67  x: "10"
    68  `, buf.String())
    69  
    70  	//
    71  	// amino
    72  	//
    73  	amino := testdata.NewTestAmino()
    74  	ctx = ctx.WithLegacyAmino(&codec.LegacyAmino{Amino: amino})
    75  
    76  	// json
    77  	buf = &bytes.Buffer{}
    78  	ctx = ctx.WithOutput(buf)
    79  	ctx.OutputFormat = "json"
    80  	err = ctx.PrintObjectLegacy(hasAnimal)
    81  	require.NoError(t, err)
    82  	require.Equal(t,
    83  		`{"type":"testdata/HasAnimal","value":{"animal":{"type":"testdata/Dog","value":{"size":"big","name":"Spot"}},"x":"10"}}
    84  `, buf.String())
    85  
    86  	// yaml
    87  	buf = &bytes.Buffer{}
    88  	ctx = ctx.WithOutput(buf)
    89  	ctx.OutputFormat = "text"
    90  	err = ctx.PrintObjectLegacy(hasAnimal)
    91  	require.NoError(t, err)
    92  	require.Equal(t,
    93  		`type: testdata/HasAnimal
    94  value:
    95    animal:
    96      type: testdata/Dog
    97      value:
    98        name: Spot
    99        size: big
   100    x: "10"
   101  `, buf.String())
   102  }
   103  
   104  func TestCLIQueryConn(t *testing.T) {
   105  	cfg := network.DefaultConfig()
   106  	cfg.NumValidators = 1
   107  
   108  	n := network.New(t, cfg)
   109  	defer n.Cleanup()
   110  
   111  	testClient := testdata.NewQueryClient(n.Validators[0].ClientCtx)
   112  	res, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
   113  	require.NoError(t, err)
   114  	require.Equal(t, "hello", res.Message)
   115  }