Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Commit 70f4c1c

Browse files
lumtisPantani
andauthored
test: improve coverage (#280)
* Params tests * Fix trigger revert launch * Query params test * coordinator tests * Add request failing cases * Improve add validator case * Improve launch messages test assertion * lint * Apply suggestions from code review Co-authored-by: Danilo Pantani <danpantani@gmail.com> * Update x/launch/keeper/msg_server_edit_chain_test.go Co-authored-by: Danilo Pantani <danpantani@gmail.com> * add test for `types/params.go` and run gofmt Co-authored-by: Danilo Pantani <danpantani@gmail.com>
1 parent eb066f7 commit 70f4c1c

16 files changed

+403
-166
lines changed

testutil/sample/launch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ func GenesisHash() string {
186186
return hex.EncodeToString(hash[:])
187187
}
188188

189-
// Params returns a sample of params for the launch module
190-
func Params() launch.Params {
189+
// LaunchParams returns a sample of params for the launch module
190+
func LaunchParams() launch.Params {
191191
maxLaunchTime := rand.Intn(int(launch.MaxParametrableLaunchTime))
192192
minLaunchTime := rand.Intn(maxLaunchTime)
193193

@@ -237,6 +237,6 @@ func LaunchGenesisState() launch.GenesisState {
237237
Count: 1,
238238
},
239239
},
240-
Params: Params(),
240+
Params: LaunchParams(),
241241
}
242242
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package keeper_test
2+
3+
import (
4+
"testing"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
"github.com/stretchr/testify/require"
8+
testkeeper "github.com/tendermint/spn/testutil/keeper"
9+
"github.com/tendermint/spn/testutil/sample"
10+
"github.com/tendermint/spn/x/launch/types"
11+
)
12+
13+
func TestParamsQuery(t *testing.T) {
14+
keeper, _, ctx, _ := testkeeper.Launch(t)
15+
wctx := sdk.WrapSDKContext(ctx)
16+
params := sample.LaunchParams()
17+
keeper.SetParams(ctx, params)
18+
19+
response, err := keeper.Params(wctx, &types.QueryParamsRequest{})
20+
require.NoError(t, err)
21+
require.Equal(t, &types.QueryParamsResponse{Params: params}, response)
22+
}

x/launch/keeper/msg_server_create_chain_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/stretchr/testify/require"
88
"github.com/tendermint/spn/testutil/sample"
99
"github.com/tendermint/spn/x/launch/types"
10+
profiletypes "github.com/tendermint/spn/x/profile/types"
1011
)
1112

1213
func TestMsgCreateChain(t *testing.T) {
@@ -24,36 +25,33 @@ func TestMsgCreateChain(t *testing.T) {
2425
name string
2526
msg types.MsgCreateChain
2627
wantedChainID uint64
27-
valid bool
28+
err error
2829
}{
2930
{
3031
name: "valid message",
3132
msg: sample.MsgCreateChain(coordAddress, ""),
3233
wantedChainID: 0,
33-
valid: true,
3434
},
3535
{
3636
name: "creates a unique chain ID",
3737
msg: sample.MsgCreateChain(coordAddress, ""),
3838
wantedChainID: 1,
39-
valid: true,
4039
},
4140
{
4241
name: "valid message with genesis url",
4342
msg: sample.MsgCreateChain(coordAddress, "foo.com"),
4443
wantedChainID: 2,
45-
valid: true,
4644
},
4745
{
48-
name: "coordinator doesn't exist for the chain",
49-
msg: sample.MsgCreateChain(sample.AccAddress(), ""),
50-
valid: false,
46+
name: "coordinator doesn't exist for the chain",
47+
msg: sample.MsgCreateChain(sample.AccAddress(), ""),
48+
err: profiletypes.ErrCoordAddressNotFound,
5149
},
5250
} {
5351
t.Run(tc.name, func(t *testing.T) {
5452
got, err := srv.CreateChain(ctx, &tc.msg)
55-
if !tc.valid {
56-
require.Error(t, err)
53+
if tc.err != nil {
54+
require.ErrorIs(t, err, tc.err)
5755
return
5856
}
5957
require.NoError(t, err)

x/launch/keeper/msg_server_edit_chain_test.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/stretchr/testify/require"
88
"github.com/tendermint/spn/testutil/sample"
99
"github.com/tendermint/spn/x/launch/types"
10+
profiletypes "github.com/tendermint/spn/x/profile/types"
1011
)
1112

1213
func TestMsgEditChain(t *testing.T) {
@@ -33,9 +34,9 @@ func TestMsgEditChain(t *testing.T) {
3334
chainID := res.Id
3435

3536
for _, tc := range []struct {
36-
name string
37-
msg types.MsgEditChain
38-
valid bool
37+
name string
38+
msg types.MsgEditChain
39+
err error
3940
}{
4041
{
4142
name: "edit genesis chain ID",
@@ -45,7 +46,6 @@ func TestMsgEditChain(t *testing.T) {
4546
false,
4647
false,
4748
),
48-
valid: true,
4949
},
5050
{
5151
name: "edit source",
@@ -55,7 +55,6 @@ func TestMsgEditChain(t *testing.T) {
5555
false,
5656
false,
5757
),
58-
valid: true,
5958
},
6059
{
6160
name: "edit initial genesis with default genesis",
@@ -65,7 +64,6 @@ func TestMsgEditChain(t *testing.T) {
6564
true,
6665
false,
6766
),
68-
valid: true,
6967
},
7068
{
7169
name: "edit initial genesis with genesis url",
@@ -75,7 +73,6 @@ func TestMsgEditChain(t *testing.T) {
7573
true,
7674
true,
7775
),
78-
valid: true,
7976
},
8077
{
8178
name: "edit source and initial genesis",
@@ -85,7 +82,6 @@ func TestMsgEditChain(t *testing.T) {
8582
true,
8683
true,
8784
),
88-
valid: true,
8985
},
9086
{
9187
name: "non existent chain id",
@@ -95,7 +91,7 @@ func TestMsgEditChain(t *testing.T) {
9591
false,
9692
false,
9793
),
98-
valid: false,
94+
err: types.ErrChainNotFound,
9995
},
10096
{
10197
name: "non existent coordinator",
@@ -105,7 +101,7 @@ func TestMsgEditChain(t *testing.T) {
105101
false,
106102
false,
107103
),
108-
valid: false,
104+
err: profiletypes.ErrCoordAddressNotFound,
109105
},
110106
{
111107
name: "invalid coordinator",
@@ -115,22 +111,22 @@ func TestMsgEditChain(t *testing.T) {
115111
false,
116112
false,
117113
),
118-
valid: false,
114+
err: profiletypes.ErrCoordInvalid,
119115
},
120116
} {
121117
t.Run(tc.name, func(t *testing.T) {
122118
// Fetch the previous state of the chain to perform checks
123119
var previousChain types.Chain
124120
var found bool
125-
if tc.valid {
121+
if tc.err == nil {
126122
previousChain, found = k.GetChain(sdkCtx, tc.msg.ChainID)
127123
require.True(t, found)
128124
}
129125

130126
// Send the message
131127
_, err := srv.EditChain(ctx, &tc.msg)
132-
if !tc.valid {
133-
require.Error(t, err)
128+
if tc.err != nil {
129+
require.ErrorIs(t, err, tc.err)
134130
return
135131
}
136132
require.NoError(t, err)

x/launch/keeper/msg_server_request_add_account_test.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,79 +45,97 @@ func TestMsgRequestAddAccount(t *testing.T) {
4545
Coins: sample.Coins(),
4646
},
4747
err: types.ErrChainNotFound,
48-
}, {
48+
},
49+
{
4950
name: "launch triggered chain",
5051
msg: types.MsgRequestAddAccount{
5152
ChainID: chains[0].Id,
5253
Address: addr1,
5354
Coins: sample.Coins(),
5455
},
5556
err: types.ErrTriggeredLaunch,
56-
}, {
57+
},
58+
{
5759
name: "coordinator not found",
5860
msg: types.MsgRequestAddAccount{
5961
ChainID: chains[1].Id,
6062
Address: addr1,
6163
Coins: sample.Coins(),
6264
},
6365
err: types.ErrChainInactive,
64-
}, {
66+
},
67+
{
6568
name: "add chain 3 request 1",
6669
msg: types.MsgRequestAddAccount{
6770
ChainID: chains[2].Id,
6871
Address: addr1,
6972
Coins: sample.Coins(),
7073
},
7174
wantID: 0,
72-
}, {
75+
},
76+
{
7377
name: "add chain 4 request 1",
7478
msg: types.MsgRequestAddAccount{
7579
ChainID: chains[3].Id,
7680
Address: addr1,
7781
Coins: sample.Coins(),
7882
},
7983
wantID: 0,
80-
}, {
84+
},
85+
{
8186
name: "add chain 4 request 2",
8287
msg: types.MsgRequestAddAccount{
8388
ChainID: chains[3].Id,
8489
Address: addr2,
8590
Coins: sample.Coins(),
8691
},
8792
wantID: 1,
88-
}, {
93+
},
94+
{
8995
name: "add chain 5 request 1",
9096
msg: types.MsgRequestAddAccount{
9197
ChainID: chains[4].Id,
9298
Address: addr1,
9399
Coins: sample.Coins(),
94100
},
95101
wantID: 0,
96-
}, {
102+
},
103+
{
97104
name: "add chain 5 request 2",
98105
msg: types.MsgRequestAddAccount{
99106
ChainID: chains[4].Id,
100107
Address: addr2,
101108
Coins: sample.Coins(),
102109
},
103110
wantID: 1,
104-
}, {
111+
},
112+
{
105113
name: "add chain 5 request 3",
106114
msg: types.MsgRequestAddAccount{
107115
ChainID: chains[4].Id,
108116
Address: addr3,
109117
Coins: sample.Coins(),
110118
},
111119
wantID: 2,
112-
}, {
113-
name: "add coordinator account",
120+
},
121+
{
122+
name: "request from coordinator is pre-approved",
114123
msg: types.MsgRequestAddAccount{
115124
ChainID: chains[4].Id,
116125
Address: coordAddr,
117126
Coins: sample.Coins(),
118127
},
119128
wantApprove: true,
120129
},
130+
{
131+
name: "failing request from coordinator",
132+
msg: types.MsgRequestAddAccount{
133+
ChainID: chains[4].Id,
134+
Address: coordAddr,
135+
Coins: sample.Coins(),
136+
},
137+
err: types.ErrAccountAlreadyExist,
138+
},
121139
}
122140
for _, tt := range tests {
123141
t.Run(tt.name, func(t *testing.T) {

x/launch/keeper/msg_server_request_add_validator_test.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,54 @@ func TestMsgRequestAddValidator(t *testing.T) {
3434
msg types.MsgRequestAddValidator
3535
wantID uint64
3636
wantApprove bool
37-
valid bool
37+
err error
3838
}{
3939
{
40-
name: "invalid chain",
41-
msg: sample.MsgRequestAddValidator(addr1, invalidChain),
42-
valid: false,
43-
}, {
44-
name: "chain with triggered launch",
45-
msg: sample.MsgRequestAddValidator(addr1, chains[0].Id),
46-
valid: false,
47-
}, {
48-
name: "chain without coordinator",
49-
msg: sample.MsgRequestAddValidator(addr1, chains[1].Id),
50-
valid: false,
51-
}, {
40+
name: "invalid chain",
41+
msg: sample.MsgRequestAddValidator(addr1, invalidChain),
42+
err: types.ErrChainNotFound,
43+
},
44+
{
45+
name: "chain with triggered launch",
46+
msg: sample.MsgRequestAddValidator(addr1, chains[0].Id),
47+
err: types.ErrTriggeredLaunch,
48+
},
49+
{
50+
name: "chain without coordinator",
51+
msg: sample.MsgRequestAddValidator(addr1, chains[1].Id),
52+
err: types.ErrChainInactive,
53+
},
54+
{
5255
name: "request to a chain 3",
5356
msg: sample.MsgRequestAddValidator(addr1, chains[2].Id),
54-
valid: true,
5557
wantID: 0,
56-
}, {
58+
},
59+
{
5760
name: "second request to a chain 3",
5861
msg: sample.MsgRequestAddValidator(addr2, chains[2].Id),
59-
valid: true,
6062
wantID: 1,
61-
}, {
63+
},
64+
{
6265
name: "request to a chain 4",
6366
msg: sample.MsgRequestAddValidator(addr1, chains[3].Id),
64-
valid: true,
6567
wantID: 0,
66-
}, {
67-
name: "add coordinator to a chain",
68+
},
69+
{
70+
name: "request from coordinator is pre-approved",
71+
msg: sample.MsgRequestAddValidator(coordAddr, chains[3].Id),
72+
wantApprove: true,
73+
},
74+
{
75+
name: "failing request from coordinator",
6876
msg: sample.MsgRequestAddValidator(coordAddr, chains[3].Id),
69-
valid: true,
77+
err: types.ErrValidatorAlreadyExist,
7078
wantApprove: true,
7179
},
7280
} {
7381
t.Run(tc.name, func(t *testing.T) {
7482
got, err := srv.RequestAddValidator(ctx, &tc.msg)
75-
if !tc.valid {
76-
require.Error(t, err)
83+
if tc.err != nil {
84+
require.ErrorIs(t, tc.err, err)
7785
return
7886
}
7987
require.NoError(t, err)

0 commit comments

Comments
 (0)