forked from neutron-org/neutron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_suite.go
127 lines (108 loc) · 3.91 KB
/
test_suite.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
package apptesting
import (
"context"
"crypto/rand"
"fmt"
"time"
dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/libs/log"
tmtypes "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
"github.com/MonikaCat/neutron/v2/app"
"github.com/MonikaCat/neutron/v2/testutil"
dexmoduletypes "github.com/MonikaCat/neutron/v2/x/dex/types"
)
type KeeperTestHelper struct {
suite.Suite
App *app.App
Ctx sdk.Context
GoCtx context.Context
// Used for testing queries end to end.
// You can wrap this in a module-specific QueryClient()
// and then make calls as you would a normal GRPC client.
QueryHelper *baseapp.QueryServiceTestHelper
}
// Setup sets up basic environment for suite (App, Ctx, and test accounts)
func (s *KeeperTestHelper) Setup() {
s.App = testutil.Setup(s.T())
ctx := s.App.BaseApp.NewContext(
false,
tmtypes.Header{Height: 1, ChainID: "neutron-1", Time: time.Now().UTC()},
)
s.Ctx = ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())
s.GoCtx = sdk.WrapSDKContext(s.Ctx)
s.QueryHelper = &baseapp.QueryServiceTestHelper{
GRPCQueryRouter: s.App.GRPCQueryRouter(),
Ctx: s.Ctx,
}
}
// setupAddr takes a balance, prefix, and address number. Then returns the respective account address byte array.
// If prefix is left blank, it will be replaced with a random prefix.
func SetupAddr(index int) sdk.AccAddress {
prefixBz := make([]byte, 8)
_, _ = rand.Read(prefixBz)
prefix := string(prefixBz)
addr := sdk.AccAddress([]byte(fmt.Sprintf("addr%s%8d", prefix, index)))
return addr
}
func (s *KeeperTestHelper) SetupAddr(index int) sdk.AccAddress {
return SetupAddr(index)
}
func SetupAddrs(numAddrs int) []sdk.AccAddress {
addrs := make([]sdk.AccAddress, numAddrs)
for i := 0; i < numAddrs; i++ {
addrs[i] = SetupAddr(i)
}
return addrs
}
func (s *KeeperTestHelper) SetupAddrs(numAddrs int) []sdk.AccAddress {
return SetupAddrs(numAddrs)
}
// These are for testing msg.ValidateBasic() functions
// which need to validate for valid/invalid addresses.
// Should not be used for anything else because these addresses
// are totally uninterpretable (100% random).
func GenerateTestAddrs() (string, string) {
pk1 := ed25519.GenPrivKey().PubKey()
validAddr := sdk.AccAddress(pk1.Address()).String()
invalidAddr := sdk.AccAddress("").String()
return validAddr, invalidAddr
}
// CreateTestContext creates a test context.
func (s *KeeperTestHelper) CreateTestContext() sdk.Context {
ctx, _ := s.CreateTestContextWithMultiStore()
return ctx
}
// CreateTestContextWithMultiStore creates a test context and returns it together with multi store.
func (s *KeeperTestHelper) CreateTestContextWithMultiStore() (sdk.Context, sdk.CommitMultiStore) {
db := dbm.NewMemDB()
logger := log.NewNopLogger()
ms := rootmulti.NewStore(db, logger)
return sdk.NewContext(ms, tmtypes.Header{}, false, logger), ms
}
// CreateTestContext creates a test context.
func (s *KeeperTestHelper) Commit() {
s.App.EndBlock(abci.RequestEndBlock{Height: s.Ctx.BlockHeight()})
oldHeight := s.Ctx.BlockHeight()
oldHeader := s.Ctx.BlockHeader()
s.App.Commit()
newHeader := tmtypes.Header{
Height: oldHeight + 1,
ChainID: oldHeader.ChainID,
Time: oldHeader.Time.Add(time.Minute),
}
s.App.BeginBlock(abci.RequestBeginBlock{Header: newHeader})
s.Ctx = s.App.GetBaseApp().NewContext(false, newHeader)
}
// FundAcc funds target address with specified amount.
func (s *KeeperTestHelper) FundAcc(acc sdk.AccAddress, amounts sdk.Coins) {
err := s.App.BankKeeper.MintCoins(s.Ctx, dexmoduletypes.ModuleName, amounts)
s.Require().NoError(err)
err = s.App.BankKeeper.SendCoinsFromModuleToAccount(s.Ctx, dexmoduletypes.ModuleName, acc, amounts)
s.Require().NoError(err)
}