Skip to content

Commit

Permalink
chore: improve test suite testutil/keeper by adding utility methods f…
Browse files Browse the repository at this point in the history
…or state initialization (#1019)

* adding helper utils to shorten boilerplate code

* extending

* cleaning up more code and adding optional args

* semantics

* semantics

* fix comments and code format

---------

Co-authored-by: Pantani <Pantani>
  • Loading branch information
muku314115 authored Dec 12, 2023
1 parent a26d7c2 commit df001ee
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 80 deletions.
18 changes: 18 additions & 0 deletions testutil/keeper/launch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package keeper

import (
"context"
"math/rand"

"github.com/stretchr/testify/require"

"github.com/tendermint/spn/testutil/sample"
)

// CreateChain creates a chain in the store and returns launch ID.
func (tm TestMsgServers) CreateChain(ctx context.Context, r *rand.Rand, coordAddress string, genesisURL string, hasProject bool, projectID uint64) uint64 {
msgCreateChain := sample.MsgCreateChain(r, coordAddress, genesisURL, hasProject, projectID)
res, err := tm.LaunchSrv.CreateChain(ctx, &msgCreateChain)
require.NoError(tm.T, err)
return res.LaunchID
}
10 changes: 7 additions & 3 deletions testutil/keeper/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/tendermint/spn/testutil/sample"
profiletypes "github.com/tendermint/spn/x/profile/types"
)

// CreateCoordinator creates a coordinator in the store and returns ID with associated address
func (tm TestMsgServers) CreateCoordinator(ctx context.Context, r *rand.Rand) (id uint64, address sdk.AccAddress) {
addr := sample.AccAddress(r)
return tm.CreateCoordinatorWithAddr(ctx, r, sample.Address(r))
}

// CreateCoordinatorWithAddr creates a coordinator in the store and returns ID with associated address
func (tm TestMsgServers) CreateCoordinatorWithAddr(ctx context.Context, r *rand.Rand, address string) (uint64, sdk.AccAddress) {
addr := sdk.MustAccAddressFromBech32(address)
res, err := tm.ProfileSrv.CreateCoordinator(ctx, &profiletypes.MsgCreateCoordinator{
Address: addr.String(),
Address: address,
Description: sample.CoordinatorDescription(r),
})
require.NoError(tm.T, err)
Expand Down
17 changes: 17 additions & 0 deletions testutil/keeper/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper

import (
"context"
"math/rand"

"github.com/stretchr/testify/require"
"github.com/tendermint/spn/testutil/sample"
)

// CreateProject creates a coordinator in the store and returns ID with associated address
func (tm TestMsgServers) CreateProject(ctx context.Context, r *rand.Rand, coordAddress string) (id uint64) {
msgCreateProject := sample.MsgCreateProject(r, coordAddress)
resProject, err := tm.ProjectSrv.CreateProject(ctx, &msgCreateProject)
require.NoError(tm.T, err)
return resProject.ProjectID
}
18 changes: 3 additions & 15 deletions x/launch/keeper/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,13 @@ import (
func TestKeeper_CreateNewChain(t *testing.T) {
sdkCtx, tk, ts := testkeeper.NewTestSetup(t)
ctx := sdk.WrapSDKContext(sdkCtx)
coordAddress := sample.Address(r)
coordNoProjectAddress := sample.Address(r)

// Create coordinators
msgCreateCoordinator := sample.MsgCreateCoordinator(coordAddress)
res, err := ts.ProfileSrv.CreateCoordinator(ctx, &msgCreateCoordinator)
require.NoError(t, err)
coordID := res.CoordinatorID

msgCreateCoordinator = sample.MsgCreateCoordinator(coordNoProjectAddress)
res, err = ts.ProfileSrv.CreateCoordinator(ctx, &msgCreateCoordinator)
require.NoError(t, err)
coordNoProjectID := res.CoordinatorID
coordID, coordAddress := ts.CreateCoordinator(ctx, r)
coordNoProjectID, _ := ts.CreateCoordinator(ctx, r)

// Create a project
msgCreateProject := sample.MsgCreateProject(r, coordAddress)
resProject, err := ts.ProjectSrv.CreateProject(ctx, &msgCreateProject)
require.NoError(t, err)
projectID := resProject.ProjectID
projectID := ts.CreateProject(ctx, r, coordAddress.String())

for _, tc := range []struct {
name string
Expand Down
16 changes: 4 additions & 12 deletions x/launch/keeper/msg_create_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,20 @@ func TestMsgCreateChain(t *testing.T) {
)

// Create an invalid coordinator
invalidCoordAddress := sample.Address(r)
msgCreateInvalidCoordinator := sample.MsgCreateCoordinator(invalidCoordAddress)
_, err := ts.ProfileSrv.CreateCoordinator(ctx, &msgCreateInvalidCoordinator)
require.NoError(t, err)
_, invalidCoordAddr := ts.CreateCoordinator(ctx, r)
invalidCoordAddress := invalidCoordAddr.String()

// Create coordinators
for i := range coordAddrs {
addr := sample.Address(r)
coordAddrs[i] = addr
msgCreateCoordinator := sample.MsgCreateCoordinator(addr)
resCoord, err := ts.ProfileSrv.CreateCoordinator(ctx, &msgCreateCoordinator)
require.NoError(t, err)
coordMap[addr] = resCoord.CoordinatorID
coordMap[addr], _ = ts.CreateCoordinatorWithAddr(ctx, r, addr)
}

// Create a project for each valid coordinator
for i := range coordAddrs {
addr := coordAddrs[i]
msgCreateProject := sample.MsgCreateProject(r, addr)
resProject, err := ts.ProjectSrv.CreateProject(ctx, &msgCreateProject)
require.NoError(t, err)
prjtMap[addr] = resProject.ProjectID
prjtMap[addr] = ts.CreateProject(ctx, r, addr)
}

// assign random sdk.Coins to `chainCreationFee` param and provide balance to coordinators
Expand Down
48 changes: 12 additions & 36 deletions x/launch/keeper/msg_edit_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,41 @@ import (

func TestMsgEditChain(t *testing.T) {
var (
coordAddress = sample.Address(r)
coordAddress2 = sample.Address(r)
coordNoExist = sample.Address(r)
launchIDNoExist = uint64(1000)
sdkCtx, tk, ts = testkeeper.NewTestSetup(t)
ctx = sdk.WrapSDKContext(sdkCtx)
)

// Create coordinators
msgCreateCoordinator := sample.MsgCreateCoordinator(coordAddress)
_, err := ts.ProfileSrv.CreateCoordinator(ctx, &msgCreateCoordinator)
require.NoError(t, err)
_, coordAddr := ts.CreateCoordinator(ctx, r)
_, coordAddr2 := ts.CreateCoordinator(ctx, r)

msgCreateCoordinator = sample.MsgCreateCoordinator(coordAddress2)
_, err = ts.ProfileSrv.CreateCoordinator(ctx, &msgCreateCoordinator)
require.NoError(t, err)
coordAddress := coordAddr.String()
coordAddress2 := coordAddr2.String()

// Create a chain
msgCreateChain := sample.MsgCreateChain(r, coordAddress, "", false, 0)
res, err := ts.LaunchSrv.CreateChain(ctx, &msgCreateChain)
require.NoError(t, err)
launchID := res.LaunchID
launchID := ts.CreateChain(ctx, r, coordAddress, "", false, 0)

// create a project
msgCreateProject := sample.MsgCreateProject(r, coordAddress)
resProject, err := ts.ProjectSrv.CreateProject(ctx, &msgCreateProject)
require.NoError(t, err)
projectID := ts.CreateProject(ctx, r, coordAddress)

// create a chain with an existing project
msgCreateChain = sample.MsgCreateChain(r, coordAddress, "", true, resProject.ProjectID)
res, err = ts.LaunchSrv.CreateChain(ctx, &msgCreateChain)
require.NoError(t, err)
launchIDHasProject := res.LaunchID
launchIDHasProject := ts.CreateChain(ctx, r, coordAddress, "", true, projectID)

// create a project
msgCreateProject = sample.MsgCreateProject(r, coordAddress)
resProject, err = ts.ProjectSrv.CreateProject(ctx, &msgCreateProject)
require.NoError(t, err)
validProjectID := resProject.ProjectID
validProjectID := ts.CreateProject(ctx, r, coordAddress)

// create a project from a different address
msgCreateProject = sample.MsgCreateProject(r, coordAddress2)
resProject, err = ts.ProjectSrv.CreateProject(ctx, &msgCreateProject)
require.NoError(t, err)
projectDifferentCoordinator := resProject.ProjectID
projectDifferentCoordinator := ts.CreateProject(ctx, r, coordAddress2)

// Create a new chain for more tests
msgCreateChain = sample.MsgCreateChain(r, coordAddress, "", false, 0)
res, err = ts.LaunchSrv.CreateChain(ctx, &msgCreateChain)
require.NoError(t, err)
launchID2 := res.LaunchID
launchID2 := ts.CreateChain(ctx, r, coordAddress, "", false, 0)

// create a new project and add a chainProjects entry to it
msgCreateProject = sample.MsgCreateProject(r, coordAddress)
resProject, err = ts.ProjectSrv.CreateProject(ctx, &msgCreateProject)
require.NoError(t, err)
projectDuplicateChain := resProject.ProjectID
projectDuplicateChain := ts.CreateProject(ctx, r, coordAddress)

err = tk.ProjectKeeper.AddChainToProject(sdkCtx, projectDuplicateChain, launchID2)
err := tk.ProjectKeeper.AddChainToProject(sdkCtx, projectDuplicateChain, launchID2)
require.NoError(t, err)

// create message with an invalid metadata length
Expand Down
14 changes: 4 additions & 10 deletions x/launch/keeper/msg_update_launch_information_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@ import (
func TestMsgUpdateLaunchInformation(t *testing.T) {
sdkCtx, tk, ts := testkeeper.NewTestSetup(t)
ctx := sdk.WrapSDKContext(sdkCtx)
coordAddress := sample.Address(r)
coordAddress2 := sample.Address(r)
coordNoExist := sample.Address(r)
launchIDNoExist := uint64(1000)

// Create coordinators
msgCreateCoordinator := sample.MsgCreateCoordinator(coordAddress)
resCoord, err := ts.ProfileSrv.CreateCoordinator(ctx, &msgCreateCoordinator)
require.NoError(t, err)
coordID, coordAddr := ts.CreateCoordinator(ctx, r)
coordAddress := coordAddr.String()

coordID := resCoord.CoordinatorID

msgCreateCoordinator = sample.MsgCreateCoordinator(coordAddress2)
_, err = ts.ProfileSrv.CreateCoordinator(ctx, &msgCreateCoordinator)
require.NoError(t, err)
_, coordAddr2 := ts.CreateCoordinator(ctx, r)
coordAddress2 := coordAddr2.String()

// Create a chain
launchID := uint64(1)
Expand Down
5 changes: 1 addition & 4 deletions x/project/keeper/msg_create_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ func TestMsgCreateProject(t *testing.T) {
for i := range coordAddrs {
addr := sample.Address(r)
coordAddrs[i] = addr
msgCreateCoordinator := sample.MsgCreateCoordinator(addr)
resCoord, err := ts.ProfileSrv.CreateCoordinator(ctx, &msgCreateCoordinator)
require.NoError(t, err)
coordMap[addr] = resCoord.CoordinatorID
coordMap[addr], _ = ts.CreateCoordinatorWithAddr(ctx, r, addr)
}
})

Expand Down

0 comments on commit df001ee

Please sign in to comment.