-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_fixtures.go
134 lines (123 loc) · 3.95 KB
/
test_fixtures.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package types
import (
"bytes"
"testing"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/tendermint/tendermint/libs/rand"
)
func PromoteProposalFixture(mutators ...func(*PromoteToPrivilegedContractProposal)) *PromoteToPrivilegedContractProposal {
const anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du"
p := &PromoteToPrivilegedContractProposal{
Title: "Foo",
Description: "Bar",
Contract: anyAddress,
}
for _, m := range mutators {
m(p)
}
return p
}
func DemoteProposalFixture(mutators ...func(proposal *DemotePrivilegedContractProposal)) *DemotePrivilegedContractProposal {
const anyAddress = "cosmos1qyqszqgpqyqszqgpqyqszqgpqyqszqgpjnp7du"
p := &DemotePrivilegedContractProposal{
Title: "Foo",
Description: "Bar",
Contract: anyAddress,
}
for _, m := range mutators {
m(p)
}
return p
}
// DeterministicGenesisStateFixture is the same as GenesisStateFixture but with deterministic addresses and codes
func DeterministicGenesisStateFixture(t *testing.T, mutators ...func(*GenesisState)) GenesisState {
genesisState := GenesisStateFixture(t)
for i := range genesisState.Contracts {
genesisState.Contracts[i].ContractAddress = wasmkeeper.BuildContractAddress(uint64(i), uint64(i)).String()
}
for i := range genesisState.Codes {
wasmCode := bytes.Repeat([]byte{byte(i)}, 20)
genesisState.Codes[i].CodeInfo = wasmtypes.CodeInfoFixture(wasmtypes.WithSHA256CodeHash(wasmCode))
genesisState.Codes[i].CodeBytes = wasmCode
}
for _, m := range mutators {
m(&genesisState)
}
return genesisState
}
// GenesisStateFixture test data fixture
func GenesisStateFixture(t *testing.T, mutators ...func(*GenesisState)) GenesisState {
t.Helper()
anyContractAddr := RandomBech32Address(t)
wasmState := wasmtypes.GenesisFixture(func(state *wasmtypes.GenesisState) {
state.Codes[1] = wasmtypes.CodeFixture(func(code *wasmtypes.Code) {
code.CodeID = 2
code.Pinned = true
})
state.Contracts[1] = wasmtypes.ContractFixture(func(contract *wasmtypes.Contract) {
contract.ContractAddress = anyContractAddr
contract.ContractInfo.CodeID = 2
})
state.Sequences = []wasmtypes.Sequence{
{IDKey: wasmtypes.KeyLastCodeID, Value: 10},
{IDKey: wasmtypes.KeyLastInstanceID, Value: 11},
}
state.GenMsgs = nil
})
contracts := make([]Contract, len(wasmState.Contracts))
for i, v := range wasmState.Contracts {
contracts[i] = Contract{
ContractAddress: v.ContractAddress,
ContractInfo: v.ContractInfo,
ContractState: &Contract_KvModel{&KVModel{v.ContractState}},
}
}
genesisState := GenesisState{
Params: wasmState.Params,
Codes: wasmState.Codes,
Contracts: contracts,
Sequences: wasmState.Sequences,
GenMsgs: wasmState.GenMsgs,
PrivilegedContractAddresses: []string{anyContractAddr},
}
for _, m := range mutators {
m(&genesisState)
}
return genesisState
}
// ContractFixture test data factory
func ContractFixture(t *testing.T, mutators ...func(contract *Contract)) Contract {
t.Helper()
wasmContract := wasmtypes.ContractFixture()
c := Contract{
ContractAddress: wasmContract.ContractAddress,
ContractInfo: wasmContract.ContractInfo,
ContractState: &Contract_KvModel{&KVModel{wasmContract.ContractState}},
}
for _, m := range mutators {
m(&c)
}
return c
}
func TgradeContractDetailsFixture(t *testing.T, mutators ...func(d *TgradeContractDetails)) TgradeContractDetails {
t.Helper()
d := TgradeContractDetails{
RegisteredPrivileges: []RegisteredPrivilege{{
Position: 1,
PrivilegeType: "begin_blocker",
}},
}
for _, m := range mutators {
m(&d)
}
return d
}
func RandomAddress(_ *testing.T) sdk.AccAddress {
return rand.Bytes(address.Len)
}
func RandomBech32Address(t *testing.T) string {
return RandomAddress(t).String()
}