forked from lino-network/lino
/
test_util.go
133 lines (113 loc) · 4.73 KB
/
test_util.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
package proposal
import (
"testing"
"github.com/cosmos/cosmos-sdk/store"
"github.com/lino-network/lino/param"
"github.com/lino-network/lino/types"
"github.com/lino-network/lino/x/global"
"github.com/lino-network/lino/x/post"
"github.com/lino-network/lino/x/vote"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/libs/log"
sdk "github.com/cosmos/cosmos-sdk/types"
acc "github.com/lino-network/lino/x/account"
val "github.com/lino-network/lino/x/validator"
abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tendermint/libs/db"
)
// Construct some global addrs and txs for tests.
var (
testAccountKVStoreKey = sdk.NewKVStoreKey("account")
testGlobalKVStoreKey = sdk.NewKVStoreKey("global")
testProposalKVStoreKey = sdk.NewKVStoreKey("proposal")
testVoteKVStoreKey = sdk.NewKVStoreKey("vote")
testParamKVStoreKey = sdk.NewKVStoreKey("param")
testValidatorKVStoreKey = sdk.NewKVStoreKey("validator")
testPostKVStoreKey = sdk.NewKVStoreKey("post")
)
func initGlobalManager(ctx sdk.Context, gm global.GlobalManager) error {
return gm.InitGlobalManager(ctx, types.NewCoinFromInt64(10000*types.Decimals))
}
func setupTest(t *testing.T, height int64) (
sdk.Context, acc.AccountManager, ProposalManager, post.PostManager, vote.VoteManager,
val.ValidatorManager, global.GlobalManager) {
ctx := getContext(height)
ph := param.NewParamHolder(testParamKVStoreKey)
ph.InitParam(ctx)
accManager := acc.NewAccountManager(testAccountKVStoreKey, ph)
proposalManager := NewProposalManager(testProposalKVStoreKey, ph)
globalManager := global.NewGlobalManager(testGlobalKVStoreKey, ph)
voteManager := vote.NewVoteManager(testGlobalKVStoreKey, ph)
valManager := val.NewValidatorManager(testValidatorKVStoreKey, ph)
postManager := post.NewPostManager(testPostKVStoreKey, ph)
cdc := globalManager.WireCodec()
cdc.RegisterInterface((*types.Event)(nil), nil)
cdc.RegisterConcrete(acc.ReturnCoinEvent{}, "1", nil)
cdc.RegisterConcrete(param.ChangeParamEvent{}, "2", nil)
cdc.RegisterConcrete(DecideProposalEvent{}, "3", nil)
err := initGlobalManager(ctx, globalManager)
assert.Nil(t, err)
return ctx, accManager, proposalManager, postManager, voteManager, valManager, globalManager
}
func getContext(height int64) sdk.Context {
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(testAccountKVStoreKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(testProposalKVStoreKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(testGlobalKVStoreKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(testParamKVStoreKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(testVoteKVStoreKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(testValidatorKVStoreKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(testPostKVStoreKey, sdk.StoreTypeIAVL, db)
ms.LoadLatestVersion()
return sdk.NewContext(ms, abci.Header{Height: height}, false, log.NewNopLogger())
}
// helper function to create an account for testing purpose
func createTestAccount(
ctx sdk.Context, am acc.AccountManager, username string, initCoin types.Coin) types.AccountKey {
am.CreateAccount(ctx, "referrer", types.AccountKey(username),
secp256k1.GenPrivKey().PubKey(), secp256k1.GenPrivKey().PubKey(),
secp256k1.GenPrivKey().PubKey(), initCoin)
return types.AccountKey(username)
}
func createTestPost(
t *testing.T, ctx sdk.Context, username, postID string, initCoin types.Coin,
am acc.AccountManager, pm post.PostManager, redistributionRate string) (types.AccountKey, string) {
user := createTestAccount(ctx, am, username, initCoin)
msg := &post.CreatePostMsg{
PostID: postID,
Title: string(make([]byte, 50)),
Content: string(make([]byte, 1000)),
Author: user,
ParentAuthor: "",
ParentPostID: "",
SourceAuthor: "",
SourcePostID: "",
Links: []types.IDToURLMapping{},
RedistributionSplitRate: redistributionRate,
}
splitRate, err := sdk.NewRatFromDecimal(redistributionRate, types.NewRatFromDecimalPrecision)
assert.Nil(t, err)
err = pm.CreatePost(
ctx, msg.Author, msg.PostID, msg.SourceAuthor, msg.SourcePostID,
msg.ParentAuthor, msg.ParentPostID, msg.Content,
msg.Title, splitRate, msg.Links)
assert.Nil(t, err)
return user, postID
}
func addProposalInfo(ctx sdk.Context, pm ProposalManager, proposalID types.ProposalKey,
agreeVotes, disagreeVotes types.Coin) sdk.Error {
proposal, err := pm.storage.GetOngoingProposal(ctx, proposalID)
if err != nil {
return err
}
proposalInfo := proposal.GetProposalInfo()
proposalInfo.AgreeVotes = agreeVotes
proposalInfo.DisagreeVotes = disagreeVotes
proposal.SetProposalInfo(proposalInfo)
if err := pm.storage.SetOngoingProposal(ctx, proposalID, proposal); err != nil {
return err
}
return nil
}