Skip to content

Commit

Permalink
chore(tests): improve x/reports tests using gomock
Browse files Browse the repository at this point in the history
  • Loading branch information
dadamu committed Jun 26, 2023
1 parent 61393a0 commit 0660513
Show file tree
Hide file tree
Showing 7 changed files with 784 additions and 688 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
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
86 changes: 66 additions & 20 deletions x/reports/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"

"github.com/desmos-labs/desmos/v5/x/reports/types"
subspacestypes "github.com/desmos-labs/desmos/v5/x/subspaces/types"
Expand All @@ -12,35 +13,47 @@ import (
func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() {
testCases := []struct {
name string
setup func()
store func(ctx sdk.Context)
expGenesis *types.GenesisState
}{
{
name: "subspaces data entries are exported properly",
setup: func() {
subspaces := []subspacestypes.Subspace{
subspacestypes.NewSubspace(
1,
"Test subspace",
"This is a test subspace",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
"cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC),
nil,
),
subspacestypes.NewSubspace(
2,
"Another test subspace",
"This is another test subspace",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
"cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC),
nil,
),
}
suite.sk.EXPECT().
IterateSubspaces(gomock.Any(), gomock.Any()).
Do(func(ctx sdk.Context, fn func(subspace subspacestypes.Subspace) (stop bool)) {
for _, subspace := range subspaces {
fn(subspace)
}
})
},
store: func(ctx sdk.Context) {
suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace(
1,
"Test subspace",
"This is a test subspace",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
"cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC),
nil,
))
suite.k.SetNextReasonID(ctx, 1, 1)
suite.k.SetNextReportID(ctx, 1, 2)

suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace(
2,
"Another test subspace",
"This is another test subspace",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
"cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC),
nil,
))
suite.k.SetNextReasonID(ctx, 2, 3)
suite.k.SetNextReportID(ctx, 2, 4)

Expand All @@ -53,6 +66,16 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() {
},
{
name: "reasons are exported properly",
setup: func() {
subspaces := []subspacestypes.Subspace{}
suite.sk.EXPECT().
IterateSubspaces(gomock.Any(), gomock.Any()).
Do(func(ctx sdk.Context, fn func(subspace subspacestypes.Subspace) (stop bool)) {
for _, subspace := range subspaces {
fn(subspace)
}
})
},
store: func(ctx sdk.Context) {
suite.k.SaveReason(ctx, types.NewReason(
1,
Expand All @@ -74,6 +97,16 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() {
},
{
name: "reports are exported properly",
setup: func() {
subspaces := []subspacestypes.Subspace{}
suite.sk.EXPECT().
IterateSubspaces(gomock.Any(), gomock.Any()).
Do(func(ctx sdk.Context, fn func(subspace subspacestypes.Subspace) (stop bool)) {
for _, subspace := range subspaces {
fn(subspace)
}
})
},
store: func(ctx sdk.Context) {
suite.k.SaveReport(ctx, types.NewReport(
1,
Expand Down Expand Up @@ -101,6 +134,16 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() {
},
{
name: "params are exported properly",
setup: func() {
subspaces := []subspacestypes.Subspace{}
suite.sk.EXPECT().
IterateSubspaces(gomock.Any(), gomock.Any()).
Do(func(ctx sdk.Context, fn func(subspace subspacestypes.Subspace) (stop bool)) {
for _, subspace := range subspaces {
fn(subspace)
}
})
},
store: func(ctx sdk.Context) {
suite.k.SetParams(ctx, types.NewParams([]types.StandardReason{
types.NewStandardReason(1, "Spam", "This content is spam"),
Expand All @@ -116,6 +159,9 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() {
tc := tc
suite.Run(tc.name, func() {
ctx, _ := suite.ctx.CacheContext()
if tc.setup != nil {
tc.setup()
}
if tc.store != nil {
tc.store(ctx)
}
Expand Down
Loading

0 comments on commit 0660513

Please sign in to comment.