From 28829edde465f2e93248fdd04734c4cfa0757e81 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Sun, 13 Oct 2024 23:36:02 -0300 Subject: [PATCH] genesis. test --- x/participation/types/genesis_test.go | 128 +++++++++++++++++++--- x/participation/types/types_test.go | 11 ++ x/profile/types/message_validator_test.go | 6 - 3 files changed, 121 insertions(+), 24 deletions(-) create mode 100644 x/participation/types/types_test.go diff --git a/x/participation/types/genesis_test.go b/x/participation/types/genesis_test.go index 85547b15..73d9b68a 100644 --- a/x/participation/types/genesis_test.go +++ b/x/participation/types/genesis_test.go @@ -3,77 +3,169 @@ package types_test import ( "testing" + sdkmath "cosmossdk.io/math" "github.com/stretchr/testify/require" + "github.com/ignite/network/testutil/sample" "github.com/ignite/network/x/participation/types" ) func TestGenesisState_Validate(t *testing.T) { - tests := []struct { - desc string + var ( + addr1 = sample.Address(r) + addr2 = sample.Address(r) + auctionID1 = uint64(0) + auctionID2 = uint64(1) + ) + + for _, tc := range []struct { + name string genState *types.GenesisState valid bool }{ { - desc: "default is valid", + name: "should validate default genesis state", genState: types.DefaultGenesis(), valid: true, }, { - desc: "valid genesis state", + name: "should validate valid genesis state", genState: &types.GenesisState{ + Params: types.DefaultParams(), + UsedAllocationsList: []types.UsedAllocations{ + { + Address: addr1, + NumAllocations: sdkmath.ZeroInt(), + }, + { + Address: addr2, + NumAllocations: sdkmath.ZeroInt(), + }, + }, AuctionUsedAllocationsList: []types.AuctionUsedAllocations{ { - Address: "0", + Address: addr1, + AuctionID: auctionID1, + NumAllocations: sdkmath.ZeroInt(), }, { - Address: "1", + Address: addr2, + AuctionID: auctionID2, + NumAllocations: sdkmath.ZeroInt(), }, }, + // this line is used by starport scaffolding # types/genesis/validField + }, + valid: true, + }, + { + name: "should validate with matching usedAllocations and auctionUsedAllocations", + genState: &types.GenesisState{ + Params: types.DefaultParams(), UsedAllocationsList: []types.UsedAllocations{ { - Address: "0", + Address: addr1, + NumAllocations: sdkmath.NewInt(5), + }, + }, + AuctionUsedAllocationsList: []types.AuctionUsedAllocations{ + { + Address: addr1, + AuctionID: auctionID1, + NumAllocations: sdkmath.NewInt(2), + Withdrawn: false, }, { - Address: "1", + Address: addr1, + AuctionID: auctionID2, + NumAllocations: sdkmath.NewInt(3), + Withdrawn: false, }, }, - // this line is used by starport scaffolding # types/genesis/validField }, valid: true, }, { - desc: "duplicated auctionUsedAllocations", + name: "should prevent duplicated usedAllocations", genState: &types.GenesisState{ + UsedAllocationsList: []types.UsedAllocations{ + { + Address: addr1, + NumAllocations: sdkmath.ZeroInt(), + }, + { + Address: addr1, + NumAllocations: sdkmath.ZeroInt(), + }, + }, + }, + valid: false, + }, + { + name: "should prevent duplicated auctionUsedAllocations", + genState: &types.GenesisState{ + UsedAllocationsList: []types.UsedAllocations{ + { + Address: addr1, + NumAllocations: sdkmath.ZeroInt(), + }, + }, AuctionUsedAllocationsList: []types.AuctionUsedAllocations{ { - Address: "0", + Address: addr1, + AuctionID: auctionID1, + NumAllocations: sdkmath.ZeroInt(), }, { - Address: "0", + Address: addr1, + AuctionID: auctionID1, + NumAllocations: sdkmath.ZeroInt(), }, }, }, valid: false, }, { - desc: "duplicated usedAllocations", + name: "should prevent invalid address in auctionUsedAllocations", + genState: &types.GenesisState{ + AuctionUsedAllocationsList: []types.AuctionUsedAllocations{ + { + Address: addr1, + AuctionID: auctionID1, + }, + }, + }, + valid: false, + }, + { + name: "should prevent mismatch between usedAllocations and auctionUsedAllocations", genState: &types.GenesisState{ UsedAllocationsList: []types.UsedAllocations{ { - Address: "0", + Address: addr1, + NumAllocations: sdkmath.NewInt(10), }, + }, + AuctionUsedAllocationsList: []types.AuctionUsedAllocations{ { - Address: "0", + Address: addr1, + AuctionID: auctionID1, + NumAllocations: sdkmath.NewInt(2), + Withdrawn: false, + }, + { + Address: addr1, + AuctionID: auctionID2, + NumAllocations: sdkmath.NewInt(8), + Withdrawn: true, }, }, }, valid: false, }, // this line is used by starport scaffolding # types/genesis/testcase - } - for _, tc := range tests { - t.Run(tc.desc, func(t *testing.T) { + } { + t.Run(tc.name, func(t *testing.T) { err := tc.genState.Validate() if tc.valid { require.NoError(t, err) diff --git a/x/participation/types/types_test.go b/x/participation/types/types_test.go new file mode 100644 index 00000000..a796ba8a --- /dev/null +++ b/x/participation/types/types_test.go @@ -0,0 +1,11 @@ +package types_test + +import "math/rand" + +var r *rand.Rand + +// initialize random generator +func init() { + s := rand.NewSource(1) + r = rand.New(s) +} diff --git a/x/profile/types/message_validator_test.go b/x/profile/types/message_validator_test.go index 850e801d..55433fc2 100644 --- a/x/profile/types/message_validator_test.go +++ b/x/profile/types/message_validator_test.go @@ -53,12 +53,6 @@ func TestMsgUpdateValidatorDescription_ValidateBasic(t *testing.T) { err error }{ { - name: "should prevent validate invalid validator address", - msg: types.MsgUpdateValidatorDescription{ - Address: "invalid address", - }, - err: types.ErrInvalidValidatorAddress, - }, { name: "should prevent validate emtpy description", msg: types.MsgUpdateValidatorDescription{ Address: addr,