github.com/Finschia/finschia-sdk@v0.49.1/client/keys/codec_test.go (about)

     1  package keys_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/Finschia/finschia-sdk/client/keys"
    10  	"github.com/Finschia/finschia-sdk/crypto/keyring"
    11  )
    12  
    13  type testCases struct {
    14  	Keys    []keyring.KeyOutput
    15  	Answers []keyring.KeyOutput
    16  	JSON    [][]byte
    17  }
    18  
    19  func getTestCases() testCases {
    20  	return testCases{
    21  		// nolint:govet
    22  		[]keyring.KeyOutput{
    23  			{"A", "B", "C", "D", "E"},
    24  			{"A", "B", "C", "D", ""},
    25  			{"", "B", "C", "D", ""},
    26  			{"", "", "", "", ""},
    27  		},
    28  		make([]keyring.KeyOutput, 4),
    29  		[][]byte{
    30  			[]byte(`{"name":"A","type":"B","address":"C","pubkey":"D","mnemonic":"E"}`),
    31  			[]byte(`{"name":"A","type":"B","address":"C","pubkey":"D"}`),
    32  			[]byte(`{"name":"","type":"B","address":"C","pubkey":"D"}`),
    33  			[]byte(`{"name":"","type":"","address":"","pubkey":""}`),
    34  		},
    35  	}
    36  }
    37  
    38  func TestMarshalJSON(t *testing.T) {
    39  	type args struct {
    40  		o keyring.KeyOutput
    41  	}
    42  
    43  	data := getTestCases()
    44  
    45  	tests := []struct {
    46  		name    string
    47  		args    args
    48  		want    []byte
    49  		wantErr bool
    50  	}{
    51  		{"basic", args{data.Keys[0]}, data.JSON[0], false},
    52  		{"mnemonic is optional", args{data.Keys[1]}, data.JSON[1], false},
    53  
    54  		// REVIEW: Are the next results expected??
    55  		{"empty name", args{data.Keys[2]}, data.JSON[2], false},
    56  		{"empty object", args{data.Keys[3]}, data.JSON[3], false},
    57  	}
    58  	for _, tt := range tests {
    59  		t.Run(tt.name, func(t *testing.T) {
    60  			got, err := keys.MarshalJSON(tt.args.o)
    61  			require.Equal(t, tt.wantErr, err != nil)
    62  			require.True(t, bytes.Equal(got, tt.want))
    63  		})
    64  	}
    65  }
    66  
    67  func TestUnmarshalJSON(t *testing.T) {
    68  	type args struct {
    69  		bz  []byte
    70  		ptr interface{}
    71  	}
    72  
    73  	data := getTestCases()
    74  
    75  	tests := []struct {
    76  		name    string
    77  		args    args
    78  		wantErr bool
    79  	}{
    80  		{"basic", args{data.JSON[0], &data.Answers[0]}, false},
    81  		{"mnemonic is optional", args{data.JSON[1], &data.Answers[1]}, false},
    82  
    83  		// REVIEW: Are the next results expected??
    84  		{"empty name", args{data.JSON[2], &data.Answers[2]}, false},
    85  		{"empty object", args{data.JSON[3], &data.Answers[3]}, false},
    86  	}
    87  	for idx, tt := range tests {
    88  		t.Run(tt.name, func(t *testing.T) {
    89  			err := keys.UnmarshalJSON(tt.args.bz, tt.args.ptr)
    90  			require.Equal(t, tt.wantErr, err != nil)
    91  			// Confirm deserialized objects are the same
    92  			require.Equal(t, data.Keys[idx], data.Answers[idx])
    93  		})
    94  	}
    95  }