github.com/Finschia/finschia-sdk@v0.48.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  		tc := tc // copy to local variable for scopelint
    53  		t.Run(name, func(t *testing.T) {
    54  			assert.Equal(t, tc.title, tc.p.GetTitle())
    55  			assert.Equal(t, tc.desc, tc.p.GetDescription())
    56  			assert.Equal(t, tc.typ, tc.p.ProposalType())
    57  			assert.Equal(t, "upgrade", tc.p.ProposalRoute())
    58  			assert.Equal(t, tc.str, tc.p.String())
    59  
    60  			// try to encode and decode type to ensure codec works
    61  			wrap := ProposalWrapper{tc.p}
    62  			bz, err := cdc.Marshal(&wrap)
    63  			require.NoError(t, err)
    64  			unwrap := ProposalWrapper{}
    65  			err = cdc.Unmarshal(bz, &unwrap)
    66  			require.NoError(t, err)
    67  
    68  			// all methods should look the same
    69  			assert.Equal(t, tc.title, unwrap.Prop.GetTitle())
    70  			assert.Equal(t, tc.desc, unwrap.Prop.GetDescription())
    71  			assert.Equal(t, tc.typ, unwrap.Prop.ProposalType())
    72  			assert.Equal(t, "upgrade", unwrap.Prop.ProposalRoute())
    73  			assert.Equal(t, tc.str, unwrap.Prop.String())
    74  		})
    75  
    76  	}
    77  }
    78  
    79  // tests a software update proposal can be marshaled and unmarshaled
    80  func TestMarshalSoftwareUpdateProposal(t *testing.T) {
    81  	// create proposal
    82  	plan := types.Plan{
    83  		Name:   "upgrade",
    84  		Height: 1000,
    85  	}
    86  	content := types.NewSoftwareUpgradeProposal("title", "description", plan)
    87  	sup, ok := content.(*types.SoftwareUpgradeProposal)
    88  	require.True(t, ok)
    89  
    90  	// create codec
    91  	ir := codectypes.NewInterfaceRegistry()
    92  	types.RegisterInterfaces(ir)
    93  	gov.RegisterInterfaces(ir)
    94  	cdc := codec.NewProtoCodec(ir)
    95  
    96  	// marshal message
    97  	bz, err := cdc.MarshalJSON(sup)
    98  	require.NoError(t, err)
    99  
   100  	// unmarshal proposal
   101  	newSup := &types.SoftwareUpgradeProposal{}
   102  	err = cdc.UnmarshalJSON(bz, newSup)
   103  	require.NoError(t, err)
   104  }