Skip to content

Commit

Permalink
refactor(tests): improve x/reports tests using gomock (#1184)
Browse files Browse the repository at this point in the history
## Description

This PR improves the x/reports tests using gomock.

<!-- Add a description of the changes that this PR introduces and the
files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is
not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type
prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json)
in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR
Targeting](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building
modules](https://docs.cosmos.network/v0.44/building-modules/intro.html)
- [ ] included the necessary unit and integration
[tests](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go
code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable
and please add
your handle next to the items reviewed if you only reviewed selected
items.*

I have...

- [ ] confirmed the correct [type
prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json)
in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
dadamu committed Jun 26, 2023
1 parent 28e295c commit b9e80e9
Show file tree
Hide file tree
Showing 8 changed files with 794 additions and 695 deletions.
56 changes: 19 additions & 37 deletions x/reports/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ package keeper_test
import (
"testing"

authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

profileskeeper "github.com/desmos-labs/desmos/v5/x/profiles/keeper"
profilestypes "github.com/desmos-labs/desmos/v5/x/profiles/types"

postskeeper "github.com/desmos-labs/desmos/v5/x/posts/keeper"
poststypes "github.com/desmos-labs/desmos/v5/x/posts/types"
"github.com/golang/mock/gomock"

db "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
Expand All @@ -19,43 +13,32 @@ import (
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/stretchr/testify/suite"

"github.com/desmos-labs/desmos/v5/app"
relationshipskeeper "github.com/desmos-labs/desmos/v5/x/relationships/keeper"
relationshipstypes "github.com/desmos-labs/desmos/v5/x/relationships/types"
"github.com/desmos-labs/desmos/v5/x/reports/keeper"
"github.com/desmos-labs/desmos/v5/x/reports/testutil"
"github.com/desmos-labs/desmos/v5/x/reports/types"
subspaceskeeper "github.com/desmos-labs/desmos/v5/x/subspaces/keeper"
subspacestypes "github.com/desmos-labs/desmos/v5/x/subspaces/types"
)

type KeeperTestSuite struct {
suite.Suite

cdc codec.Codec
legacyAminoCdc *codec.LegacyAmino
ctx sdk.Context
cdc codec.Codec
ctx sdk.Context

storeKey storetypes.StoreKey
k keeper.Keeper

ak profileskeeper.Keeper
sk subspaceskeeper.Keeper
rk relationshipskeeper.Keeper
pk postskeeper.Keeper
ak *testutil.MockProfilesKeeper
sk *testutil.MockSubspacesKeeper
rk *testutil.MockRelationshipsKeeper
pk *testutil.MockPostsKeeper
}

func (suite *KeeperTestSuite) SetupTest() {
// Define store keys
keys := sdk.NewMemoryStoreKeys(
paramstypes.StoreKey, authtypes.StoreKey,
profilestypes.StoreKey, relationshipstypes.StoreKey,
subspacestypes.StoreKey, poststypes.StoreKey,
types.StoreKey,
)
tKeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
keys := sdk.NewMemoryStoreKeys(types.StoreKey)
suite.storeKey = keys[types.StoreKey]

// Create an in-memory db
Expand All @@ -64,23 +47,22 @@ func (suite *KeeperTestSuite) SetupTest() {
for _, key := range keys {
ms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, memDB)
}
for _, tKey := range tKeys {
ms.MountStoreWithDB(tKey, storetypes.StoreTypeTransient, memDB)
}

if err := ms.LoadLatestVersion(); err != nil {
panic(err)
}

suite.ctx = sdk.NewContext(ms, tmproto.Header{ChainID: "test-chain"}, false, log.NewNopLogger())
suite.cdc, suite.legacyAminoCdc = app.MakeCodecs()
suite.cdc, _ = app.MakeCodecs()

// Mocks initializations
ctrl := gomock.NewController(suite.T())
defer ctrl.Finish()

// Define keeper
suite.sk = subspaceskeeper.NewKeeper(suite.cdc, keys[subspacestypes.StoreKey], nil, nil, authtypes.NewModuleAddress("gov").String())
suite.rk = relationshipskeeper.NewKeeper(suite.cdc, keys[relationshipstypes.StoreKey], suite.sk)
authKeeper := authkeeper.NewAccountKeeper(suite.cdc, keys[authtypes.StoreKey], authtypes.ProtoBaseAccount, app.GetMaccPerms(), "cosmos", authtypes.NewModuleAddress("gov").String())
suite.ak = profileskeeper.NewKeeper(suite.cdc, suite.legacyAminoCdc, keys[profilestypes.StoreKey], authKeeper, suite.rk, nil, nil, nil, authtypes.NewModuleAddress("gov").String())
suite.pk = postskeeper.NewKeeper(suite.cdc, keys[poststypes.StoreKey], suite.ak, suite.sk, suite.rk, authtypes.NewModuleAddress("gov").String())
// Setup keepers
suite.sk = testutil.NewMockSubspacesKeeper(ctrl)
suite.rk = testutil.NewMockRelationshipsKeeper(ctrl)
suite.ak = testutil.NewMockProfilesKeeper(ctrl)
suite.pk = testutil.NewMockPostsKeeper(ctrl)
suite.k = keeper.NewKeeper(
suite.cdc,
suite.storeKey,
Expand Down
18 changes: 9 additions & 9 deletions x/reports/keeper/external_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ func (h Hooks) AfterSubspaceSaved(ctx sdk.Context, subspaceID uint64) {

// AfterSubspaceDeleted implements subspacestypes.Hooks
func (h Hooks) AfterSubspaceDeleted(ctx sdk.Context, subspaceID uint64) {
// Delete the reason id key
h.k.DeleteNextReasonID(ctx, subspaceID)

// Delete all the reasons related to this subspace
h.k.IterateSubspaceReasons(ctx, subspaceID, func(reason types.Reason) (stop bool) {
h.k.DeleteReason(ctx, reason.SubspaceID, reason.ID)
return false
})

// Delete the report id key
h.k.DeleteNextReportID(ctx, subspaceID)

Expand All @@ -53,6 +44,15 @@ func (h Hooks) AfterSubspaceDeleted(ctx sdk.Context, subspaceID uint64) {
h.k.DeleteReport(ctx, report.SubspaceID, report.ID)
return false
})

// Delete the reason id key
h.k.DeleteNextReasonID(ctx, subspaceID)

// Delete all the reasons related to this subspace
h.k.IterateSubspaceReasons(ctx, subspaceID, func(reason types.Reason) (stop bool) {
h.k.DeleteReason(ctx, reason.SubspaceID, reason.ID)
return false
})
}

// AfterSubspaceGroupSaved implements subspacestypes.Hooks
Expand Down
77 changes: 10 additions & 67 deletions x/reports/keeper/external_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,21 @@ package keeper_test
import (
"time"

poststypes "github.com/desmos-labs/desmos/v5/x/posts/types"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/desmos-labs/desmos/v5/x/reports/types"
subspacestypes "github.com/desmos-labs/desmos/v5/x/subspaces/types"
)

func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceSaved() {
testCases := []struct {
name string
store func(ctx sdk.Context)
subspace subspacestypes.Subspace
check func(ctx sdk.Context)
name string
store func(ctx sdk.Context)
subspaceID uint64
check func(ctx sdk.Context)
}{
{
name: "saving a subspaces adds the correct keys",
subspace: subspacestypes.NewSubspace(
1,
"Test subspace",
"This is a test subspace",
"cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm",
"cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm",
"cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm",
time.Date(2020, 1, 2, 12, 00, 00, 000, time.UTC),
nil,
),
name: "saving a subspaces adds the correct keys",
subspaceID: 1,
check: func(ctx sdk.Context) {
storedReasonID, err := suite.k.GetNextReasonID(ctx, 1)
suite.Require().NoError(err)
Expand All @@ -43,29 +31,10 @@ func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceSaved() {
{
name: "reason and report ids are not overwritten",
store: func(ctx sdk.Context) {
suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace(
1,
"Test subspace",
"This is a test subspace",
"cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm",
"cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm",
"cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm",
time.Date(2020, 1, 2, 12, 00, 00, 000, time.UTC),
nil,
))
suite.k.SetNextReportID(ctx, 1, 2)
suite.k.SetNextReasonID(ctx, 1, 2)
},
subspace: subspacestypes.NewSubspace(
1,
"Test subspace",
"This is a test subspace",
"cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm",
"cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm",
"cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm",
time.Date(2020, 1, 2, 12, 00, 00, 000, time.UTC),
nil,
),
subspaceID: 1,
check: func(ctx sdk.Context) {
storedReasonID, err := suite.k.GetNextReasonID(ctx, 1)
suite.Require().NoError(err)
Expand All @@ -78,9 +47,6 @@ func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceSaved() {
},
}

// Set the hooks
suite.sk.SetHooks(suite.k.Hooks())

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
Expand All @@ -89,7 +55,7 @@ func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceSaved() {
tc.store(ctx)
}

suite.sk.SaveSubspace(ctx, tc.subspace)
suite.k.Hooks().AfterSubspaceSaved(ctx, tc.subspaceID)
if tc.check != nil {
tc.check(ctx)
}
Expand Down Expand Up @@ -141,9 +107,6 @@ func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceDeleted() {
},
}

// Set the hooks
suite.sk.SetHooks(suite.k.Hooks())

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
Expand All @@ -152,7 +115,7 @@ func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceDeleted() {
tc.store(ctx)
}

suite.sk.DeleteSubspace(ctx, tc.subspaceID)
suite.k.Hooks().AfterSubspaceDeleted(ctx, tc.subspaceID)
if tc.check != nil {
tc.check(ctx)
}
Expand All @@ -171,23 +134,6 @@ func (suite *KeeperTestSuite) TestKeeper_AfterPostDeleted() {
{
name: "deleting a post removes all the associated reports",
store: func(ctx sdk.Context) {
suite.pk.SavePost(ctx, poststypes.NewPost(
1,
0,
1,
"External ID",
"This is a text",
"cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd",
1,
nil,
nil,
nil,
poststypes.REPLY_SETTING_EVERYONE,
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC),
nil,
"cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd",
))

suite.k.SaveReport(ctx, types.NewReport(
1,
1,
Expand All @@ -209,9 +155,6 @@ func (suite *KeeperTestSuite) TestKeeper_AfterPostDeleted() {
},
}

// Set the hooks
suite.pk.SetHooks(suite.k.Hooks())

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
Expand All @@ -220,7 +163,7 @@ func (suite *KeeperTestSuite) TestKeeper_AfterPostDeleted() {
tc.store(ctx)
}

suite.pk.DeletePost(ctx, tc.subspaceID, tc.postID)
suite.k.Hooks().AfterPostDeleted(ctx, tc.subspaceID, tc.postID)
if tc.check != nil {
tc.check(ctx)
}
Expand Down
Loading

0 comments on commit b9e80e9

Please sign in to comment.