cosmos-sdk/x/gov/simulation/decoder_test.go
Callum Waters c7e2305286
refactor: move legacy gov to v1beta1 (#10748)
Ref: #9810

Moves all legacy gov code to `v1beta1`. This preserves all existing behavior (i.e. everything still uses v1beta1). It's merely moving things around to get everything in the right place logistically (hence the large diff still)

---

### 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...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] 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)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] 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-12-13 18:48:44 +00:00

95 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"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
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 := v1beta1.ContentFromProposalType("test", "test", v1beta1.ProposalTypeText)
proposalA, err := v1beta1.NewProposal(content, 1, endTime, endTime.Add(24*time.Hour))
require.NoError(t, err)
proposalB, err := v1beta1.NewProposal(content, 2, endTime, endTime.Add(24*time.Hour))
require.NoError(t, err)
proposalIDBz := make([]byte, 8)
binary.LittleEndian.PutUint64(proposalIDBz, 1)
deposit := v1beta1.NewDeposit(1, delAddr1, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())))
vote := v1beta1.NewVote(1, delAddr1, v1beta1.NewNonSplitVoteOption(v1beta1.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)
}
})
}
}