github.com/Finschia/finschia-sdk@v0.49.1/x/upgrade/types/proposal_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/Finschia/finschia-sdk/codec"
    10  	codectypes "github.com/Finschia/finschia-sdk/codec/types"
    11  	gov "github.com/Finschia/finschia-sdk/x/gov/types"
    12  	"github.com/Finschia/finschia-sdk/x/upgrade/types"
    13  )
    14  
    15  type ProposalWrapper struct {
    16  	Prop gov.Content
    17  }
    18  
    19  func TestContentAccessors(t *testing.T) {
    20  	cases := map[string]struct {
    21  		p     gov.Content
    22  		title string
    23  		desc  string
    24  		typ   string
    25  		str   string
    26  	}{
    27  		"upgrade": {
    28  			p: types.NewSoftwareUpgradeProposal("Title", "desc", types.Plan{
    29  				Name:   "due_height",
    30  				Info:   "https://foo.bar",
    31  				Height: 99999999999,
    32  			}),
    33  			title: "Title",
    34  			desc:  "desc",
    35  			typ:   "SoftwareUpgrade",
    36  			str:   "Software Upgrade Proposal:\n  Title:       Title\n  Description: desc\n",
    37  		},
    38  		"cancel": {
    39  			p:     types.NewCancelSoftwareUpgradeProposal("Cancel", "bad idea"),
    40  			title: "Cancel",
    41  			desc:  "bad idea",
    42  			typ:   "CancelSoftwareUpgrade",
    43  			str:   "Cancel Software Upgrade Proposal:\n  Title:       Cancel\n  Description: bad idea\n",
    44  		},
    45  	}
    46  
    47  	cdc := codec.NewLegacyAmino()
    48  	gov.RegisterLegacyAminoCodec(cdc)
    49  	types.RegisterLegacyAminoCodec(cdc)
    50  
    51  	for name, tc := range cases {
    52  		t.Run(name, func(t *testing.T) {
    53  			assert.Equal(t, tc.title, tc.p.GetTitle())
    54  			assert.Equal(t, tc.desc, tc.p.GetDescription())
    55  			assert.Equal(t, tc.typ, tc.p.ProposalType())
    56  			assert.Equal(t, "upgrade", tc.p.ProposalRoute())
    57  			assert.Equal(t, tc.str, tc.p.String())
    58  
    59  			// try to encode and decode type to ensure codec works
    60  			wrap := ProposalWrapper{tc.p}
    61  			bz, err := cdc.Marshal(&wrap)
    62  			require.NoError(t, err)
    63  			unwrap := ProposalWrapper{}
    64  			err = cdc.Unmarshal(bz, &unwrap)
    65  			require.NoError(t, err)
    66  
    67  			// all methods should look the same
    68  			assert.Equal(t, tc.title, unwrap.Prop.GetTitle())
    69  			assert.Equal(t, tc.desc, unwrap.Prop.GetDescription())
    70  			assert.Equal(t, tc.typ, unwrap.Prop.ProposalType())
    71  			assert.Equal(t, "upgrade", unwrap.Prop.ProposalRoute())
    72  			assert.Equal(t, tc.str, unwrap.Prop.String())
    73  		})
    74  	}
    75  }
    76  
    77  // tests a software update proposal can be marshaled and unmarshaled
    78  func TestMarshalSoftwareUpdateProposal(t *testing.T) {
    79  	// create proposal
    80  	plan := types.Plan{
    81  		Name:   "upgrade",
    82  		Height: 1000,
    83  	}
    84  	content := types.NewSoftwareUpgradeProposal("title", "description", plan)
    85  	sup, ok := content.(*types.SoftwareUpgradeProposal)
    86  	require.True(t, ok)
    87  
    88  	// create codec
    89  	ir := codectypes.NewInterfaceRegistry()
    90  	types.RegisterInterfaces(ir)
    91  	gov.RegisterInterfaces(ir)
    92  	cdc := codec.NewProtoCodec(ir)
    93  
    94  	// marshal message
    95  	bz, err := cdc.MarshalJSON(sup)
    96  	require.NoError(t, err)
    97  
    98  	// unmarshal proposal
    99  	newSup := &types.SoftwareUpgradeProposal{}
   100  	err = cdc.UnmarshalJSON(bz, newSup)
   101  	require.NoError(t, err)
   102  }