github.com/cosmos/cosmos-sdk@v0.50.10/x/gov/migrations/v4/json_test.go (about)

     1  package v4_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/cosmos/cosmos-sdk/client"
    10  	moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
    11  	"github.com/cosmos/cosmos-sdk/x/gov"
    12  	v4 "github.com/cosmos/cosmos-sdk/x/gov/migrations/v4"
    13  	v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
    14  )
    15  
    16  func TestMigrateJSON(t *testing.T) {
    17  	encodingConfig := moduletestutil.MakeTestEncodingConfig(gov.AppModuleBasic{})
    18  	clientCtx := client.Context{}.
    19  		WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
    20  		WithTxConfig(encodingConfig.TxConfig).
    21  		WithCodec(encodingConfig.Codec)
    22  
    23  	govGenState := v1.DefaultGenesisState()
    24  	oldGovState := &v1.GenesisState{
    25  		StartingProposalId: govGenState.StartingProposalId,
    26  		Deposits:           govGenState.Deposits,
    27  		Votes:              govGenState.Votes,
    28  		Proposals:          govGenState.Proposals,
    29  		DepositParams: &v1.DepositParams{
    30  			MinDeposit:       govGenState.Params.MinDeposit,
    31  			MaxDepositPeriod: govGenState.Params.MaxDepositPeriod,
    32  		},
    33  		VotingParams: &v1.VotingParams{
    34  			VotingPeriod: govGenState.Params.VotingPeriod,
    35  		},
    36  		TallyParams: &v1.TallyParams{
    37  			Quorum:        govGenState.Params.Quorum,
    38  			Threshold:     govGenState.Params.Threshold,
    39  			VetoThreshold: govGenState.Params.VetoThreshold,
    40  		},
    41  	}
    42  
    43  	migrated, err := v4.MigrateJSON(oldGovState)
    44  	require.NoError(t, err)
    45  	require.Equal(t, migrated, govGenState)
    46  
    47  	bz, err := clientCtx.Codec.MarshalJSON(migrated)
    48  	require.NoError(t, err)
    49  
    50  	// Indent the JSON bz correctly.
    51  	var jsonObj map[string]interface{}
    52  	err = json.Unmarshal(bz, &jsonObj)
    53  	require.NoError(t, err)
    54  	indentedBz, err := json.MarshalIndent(jsonObj, "", "\t")
    55  	require.NoError(t, err)
    56  
    57  	// Make sure about:
    58  	// - Proposals use MsgExecLegacyContent
    59  	expected := `{
    60  	"constitution": "",
    61  	"deposit_params": null,
    62  	"deposits": [],
    63  	"params": {
    64  		"burn_proposal_deposit_prevote": false,
    65  		"burn_vote_quorum": false,
    66  		"burn_vote_veto": true,
    67  		"expedited_min_deposit": [
    68  			{
    69  				"amount": "50000000",
    70  				"denom": "stake"
    71  			}
    72  		],
    73  		"expedited_threshold": "0.667000000000000000",
    74  		"expedited_voting_period": "86400s",
    75  		"max_deposit_period": "172800s",
    76  		"min_deposit": [
    77  			{
    78  				"amount": "10000000",
    79  				"denom": "stake"
    80  			}
    81  		],
    82  		"min_deposit_ratio": "0.010000000000000000",
    83  		"min_initial_deposit_ratio": "0.000000000000000000",
    84  		"proposal_cancel_dest": "",
    85  		"proposal_cancel_ratio": "0.500000000000000000",
    86  		"quorum": "0.334000000000000000",
    87  		"threshold": "0.500000000000000000",
    88  		"veto_threshold": "0.334000000000000000",
    89  		"voting_period": "172800s"
    90  	},
    91  	"proposals": [],
    92  	"starting_proposal_id": "1",
    93  	"tally_params": null,
    94  	"votes": [],
    95  	"voting_params": null
    96  }`
    97  
    98  	require.Equal(t, expected, string(indentedBz))
    99  }