cosmos-sdk/x/gov/simulation/decoder_test.go
MD Aleem d9fb4cf34d
refactor!: Remove clientCtx.JSONCodec and rename EncodingConfig.Marshaler to Codec (#9521)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Closes: #9499 

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2021-07-01 08:52:38 +00:00

94 lines
2.7 KiB
Go

package simulation_test
import (
"encoding/binary"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/x/gov/simulation"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
var (
delPk1 = ed25519.GenPrivKey().PubKey()
delAddr1 = sdk.AccAddress(delPk1.Address())
)
func TestDecodeStore(t *testing.T) {
cdc := simapp.MakeTestEncodingConfig().Codec
dec := simulation.NewDecodeStore(cdc)
endTime := time.Now().UTC()
content := types.ContentFromProposalType("test", "test", types.ProposalTypeText)
proposalA, err := types.NewProposal(content, 1, endTime, endTime.Add(24*time.Hour))
require.NoError(t, err)
proposalB, err := types.NewProposal(content, 2, endTime, endTime.Add(24*time.Hour))
require.NoError(t, err)
proposalIDBz := make([]byte, 8)
binary.LittleEndian.PutUint64(proposalIDBz, 1)
deposit := types.NewDeposit(1, delAddr1, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())))
vote := types.NewVote(1, delAddr1, types.NewNonSplitVoteOption(types.OptionYes))
proposalBzA, err := cdc.Marshal(&proposalA)
require.NoError(t, err)
proposalBzB, err := cdc.Marshal(&proposalB)
require.NoError(t, err)
tests := []struct {
name string
kvA, kvB kv.Pair
expectedLog string
wantPanic bool
}{
{
"proposals",
kv.Pair{Key: types.ProposalKey(1), Value: proposalBzA},
kv.Pair{Key: types.ProposalKey(2), Value: proposalBzB},
fmt.Sprintf("%v\n%v", proposalA, proposalB), false,
},
{
"proposal IDs",
kv.Pair{Key: types.InactiveProposalQueueKey(1, endTime), Value: proposalIDBz},
kv.Pair{Key: types.InactiveProposalQueueKey(1, endTime), Value: proposalIDBz},
"proposalIDA: 1\nProposalIDB: 1", false,
},
{
"deposits",
kv.Pair{Key: types.DepositKey(1, delAddr1), Value: cdc.MustMarshal(&deposit)},
kv.Pair{Key: types.DepositKey(1, delAddr1), Value: cdc.MustMarshal(&deposit)},
fmt.Sprintf("%v\n%v", deposit, deposit), false,
},
{
"votes",
kv.Pair{Key: types.VoteKey(1, delAddr1), Value: cdc.MustMarshal(&vote)},
kv.Pair{Key: types.VoteKey(1, delAddr1), Value: cdc.MustMarshal(&vote)},
fmt.Sprintf("%v\n%v", vote, vote), false,
},
{
"other",
kv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
kv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
"", true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if tt.wantPanic {
require.Panics(t, func() { dec(tt.kvA, tt.kvB) }, tt.name)
} else {
require.Equal(t, tt.expectedLog, dec(tt.kvA, tt.kvB), tt.name)
}
})
}
}