Skip to content

Commit 66009f2

Browse files
author
aljo242
committed
readmes
1 parent ae3e0c7 commit 66009f2

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ Utilities for testing Cosmos SDK chains
77
go get github.com/skip-mev/chaintestutil
88
```
99

10+
## Structure
11+
12+
.
13+
├── encoding # Utilities for creating test encoding configurations
14+
├── keeper # Utilities for creating a set of test keepers for integration tests
15+
├── network # Utilities for creating a local test network for integration tests
16+
└── sample # Functions for generating randomized Cosmos SDK types such as Coins, sdk.Ints, etc.

keeper/README.md

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Keeper Testing
2+
3+
The keeper package exposes an easy API for generating a set of "test keepers" and "test message
4+
servers" for performing integration testing on your application at the keeper level. Typical Cosmos
5+
SDK unit tests may use mocked keepers, or not use a full set that is representative of the full application.
6+
7+
This setup allows developers to test all keepers in coordination without having to spin up a local network
8+
or full application.
9+
10+
The framework initializes and returns keepers for the following modules:
11+
- `x/bank`
12+
- `x/auth`
13+
- `x/staking`
14+
- `x/distribution`
15+
- `x/feegrant`
16+
- `x/mint`
17+
18+
and is easily extendable to add any other keepers that you may with to test upon setup.
19+
20+
A typical testing flow that extends adds the Skip FeeMarket module might look like the following:
21+
22+
```go
23+
import (
24+
"testing"
25+
26+
"github.com/cometbft/cometbft/libs/log"
27+
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
28+
storetypes "github.com/cosmos/cosmos-sdk/store/types"
29+
sdk "github.com/cosmos/cosmos-sdk/types"
30+
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
31+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
32+
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
33+
testkeeper "github.com/skip-mev/chaintestutil/keeper"
34+
"github.com/stretchr/testify/require"
35+
36+
feemarketkeeper "github.com/skip-mev/feemarket/x/feemarket/keeper"
37+
feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"
38+
)
39+
40+
// TestKeepers holds all keepers used during keeper tests for all modules
41+
type TestKeepers struct {
42+
testkeeper.TestKeepers
43+
FeeMarketKeeper *feemarketkeeper.Keeper
44+
}
45+
46+
// TestMsgServers holds all message servers used during keeper tests for all modules
47+
type TestMsgServers struct {
48+
testkeeper.TestMsgServers
49+
FeeMarketMsgServer feemarkettypes.MsgServer
50+
}
51+
52+
var additionalMaccPerms = map[string][]string{
53+
feemarkettypes.ModuleName: nil,
54+
feemarkettypes.FeeCollectorName: {authtypes.Burner},
55+
}
56+
57+
// NewTestSetup returns initialized instances of all the keepers and message servers of the modules
58+
func NewTestSetup(t testing.TB, options ...testkeeper.SetupOption) (sdk.Context, TestKeepers, TestMsgServers) {
59+
options = append(options, testkeeper.WithAdditionalModuleAccounts(additionalMaccPerms))
60+
61+
_, tk, tms := testkeeper.NewTestSetup(t, options...)
62+
63+
// initialize extra keeper
64+
feeMarketKeeper := FeeMarket(tk.Initializer, tk.AccountKeeper)
65+
require.NoError(t, tk.Initializer.LoadLatest())
66+
67+
// initialize msg servers
68+
feeMarketMsgSrv := feemarketkeeper.NewMsgServer(*feeMarketKeeper)
69+
70+
ctx := sdk.NewContext(tk.Initializer.StateStore, tmproto.Header{
71+
Time: testkeeper.ExampleTimestamp,
72+
Height: testkeeper.ExampleHeight,
73+
}, false, log.NewNopLogger())
74+
75+
err := feeMarketKeeper.SetState(ctx, feemarkettypes.DefaultState())
76+
require.NoError(t, err)
77+
err = feeMarketKeeper.SetParams(ctx, feemarkettypes.DefaultParams())
78+
require.NoError(t, err)
79+
80+
testKeepers := TestKeepers{
81+
TestKeepers: tk,
82+
FeeMarketKeeper: feeMarketKeeper,
83+
}
84+
85+
testMsgServers := TestMsgServers{
86+
TestMsgServers: tms,
87+
FeeMarketMsgServer: feeMarketMsgSrv,
88+
}
89+
90+
return ctx, testKeepers, testMsgServers
91+
}
92+
93+
// FeeMarket initializes the fee market module using the testkeepers intializer.
94+
func FeeMarket(
95+
initializer *testkeeper.Initializer,
96+
authKeeper authkeeper.AccountKeeper,
97+
) *feemarketkeeper.Keeper {
98+
storeKey := sdk.NewKVStoreKey(feemarkettypes.StoreKey)
99+
initializer.StateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, initializer.DB)
100+
101+
return feemarketkeeper.NewKeeper(
102+
initializer.Codec,
103+
storeKey,
104+
authKeeper,
105+
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
106+
)
107+
}
108+
```

network/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Network Testing
22

3-
4-
Package network implements and exposes a fully operational in-process CometBFT
3+
The network package implements and exposes a fully operational in-process CometBFT
54
test network that consists of at least one or potentially many validators. This
65
test network can be used primarily for integration tests or unit test suites.
76

@@ -43,7 +42,7 @@ A typical testing flow that extends the bank genesis state might look like the f
4342
"github.com/stretchr/testify/require"
4443
"github.com/stretchr/testify/suite"
4544

46-
"github.com/test-repo/app" // example test repository
45+
"github.com/test-repo/app" // example test repository. Replace with your own
4746
)
4847

4948
var (

0 commit comments

Comments
 (0)