Skip to content

Commit

Permalink
Added tests for subscription messages
Browse files Browse the repository at this point in the history
Signed-off-by: jay-dee7 <[email protected]>
  • Loading branch information
jay-dee7 committed Apr 29, 2021
1 parent b62e979 commit 70e1982
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 27 deletions.
19 changes: 11 additions & 8 deletions x/subscription/alias.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// nolint
// autogenerated code using github.com/rigelrozanski/multitool
// aliases generated for the following subdirectories:
// ALIASGEN: github.com/sentinel-official/hub/x/subscription/types
// ALIASGEN: github.com/sentinel-official/hub/x/subscription/keeper
// ALIASGEN: github.com/sentinel-official/hub/x/subscription/types
package subscription

import (
Expand All @@ -18,6 +16,9 @@ const (

var (
// functions aliases
NewKeeper = keeper.NewKeeper
NewMsgServiceServer = keeper.NewMsgServiceServer
NewQueryServiceServer = keeper.NewQueryServiceServer
RegisterLegacyAminoCodec = types.RegisterLegacyAminoCodec
RegisterInterfaces = types.RegisterInterfaces
NewGenesisState = types.NewGenesisState
Expand Down Expand Up @@ -62,17 +63,14 @@ var (
RegisterQueryServiceHandlerFromEndpoint = types.RegisterQueryServiceHandlerFromEndpoint
RegisterQueryServiceHandler = types.RegisterQueryServiceHandler
RegisterQueryServiceHandlerClient = types.RegisterQueryServiceHandlerClient
NewKeeper = keeper.NewKeeper
NewMsgServiceServer = keeper.NewMsgServiceServer
NewQueryServiceServer = keeper.NewQueryServiceServer

// variable aliases
ModuleCdc = types.ModuleCdc
ErrorMarshal = types.ErrorMarshal
ErrorUnmarshal = types.ErrorUnmarshal
ErrorUnknownMsgType = types.ErrorUnknownMsgType
ErrorUnknownQueryType = types.ErrorUnknownQueryType
ErrorInvalidField = types.ErrorInvalidField
ErrorInvalidFieldFrom = types.ErrorInvalidFieldFrom
ErrorPlanDoesNotExist = types.ErrorPlanDoesNotExist
ErrorNodeDoesNotExist = types.ErrorNodeDoesNotExist
ErrorUnauthorized = types.ErrorUnauthorized
Expand All @@ -86,6 +84,11 @@ var (
ErrorDuplicateQuota = types.ErrorDuplicateQuota
ErrorQuotaDoesNotExist = types.ErrorQuotaDoesNotExist
ErrorCanNotAddQuota = types.ErrorCanNotAddQuota
ErrorInvalidFieldId = types.ErrorInvalidFieldId
ErrorInvalidFieldAddress = types.ErrorInvalidFieldAddress
ErrorInvalidFieldBytes = types.ErrorInvalidFieldBytes
ErrorInvalidFieldDenom = types.ErrorInvalidFieldDenom
ErrorInvalidFieldDeposit = types.ErrorInvalidFieldDeposit
ErrInvalidLengthEvents = types.ErrInvalidLengthEvents
ErrIntOverflowEvents = types.ErrIntOverflowEvents
ErrUnexpectedEndOfGroupEvents = types.ErrUnexpectedEndOfGroupEvents
Expand Down Expand Up @@ -123,6 +126,7 @@ var (
)

type (
Keeper = keeper.Keeper
EventModule = types.EventModule
EventSetSubscriptionCount = types.EventSetSubscriptionCount
EventSubscribeToNode = types.EventSubscribeToNode
Expand Down Expand Up @@ -168,5 +172,4 @@ type (
Quota = types.Quota
Subscriptions = types.Subscriptions
Subscription = types.Subscription
Keeper = keeper.Keeper
)
7 changes: 6 additions & 1 deletion x/subscription/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var (
ErrorUnmarshal = errors.Register(ModuleName, 102, "error occurred while unmarshalling")
ErrorUnknownMsgType = errors.Register(ModuleName, 103, "unknown message type")
ErrorUnknownQueryType = errors.Register(ModuleName, 104, "unknown query type")
ErrorInvalidField = errors.Register(ModuleName, 105, "invalid field")
ErrorInvalidFieldFrom = errors.Register(ModuleName, 105, "invalid value for field from; expected a valid address")
ErrorPlanDoesNotExist = errors.Register(ModuleName, 106, "plan does not exist")
ErrorNodeDoesNotExist = errors.Register(ModuleName, 107, "node does not exist")
ErrorUnauthorized = errors.Register(ModuleName, 108, "unauthorized")
Expand All @@ -23,4 +23,9 @@ var (
ErrorDuplicateQuota = errors.Register(ModuleName, 116, "duplicate quota")
ErrorQuotaDoesNotExist = errors.Register(ModuleName, 117, "quota does not exist")
ErrorCanNotAddQuota = errors.Register(ModuleName, 118, "can not add quota")
ErrorInvalidFieldId = errors.Register(ModuleName, 119, "invalid value for field id; expected a value greater than 0")
ErrorInvalidFieldAddress = errors.Register(ModuleName, 120, "invalid value for field address; expected a valid address")
ErrorInvalidFieldBytes = errors.Register(ModuleName, 121, "invalid value for field bytes; expected a positive integer value")
ErrorInvalidFieldDenom = errors.Register(ModuleName, 122, "invalid value for field denom; expected a valid denom")
ErrorInvalidFieldDeposit = errors.Register(ModuleName, 123, "invalid value for field deposit; expected a positive integer value")
)
36 changes: 18 additions & 18 deletions x/subscription/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

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

hubtypes "github.com/sentinel-official/hub/types"
)
Expand Down Expand Up @@ -36,17 +35,17 @@ func (m *MsgSubscribeToNodeRequest) Type() string {

func (m *MsgSubscribeToNodeRequest) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return errors.Wrapf(ErrorInvalidField, "%s", "from")
return ErrorInvalidFieldFrom
}

// Address should be valid
if _, err := hubtypes.NodeAddressFromBech32(m.Address); err != nil {
return errors.Wrapf(ErrorInvalidField, "%s", "address")
return ErrorInvalidFieldAddress
}

// Deposit should be valid and positive
if !m.Deposit.IsValid() || !m.Deposit.IsPositive() {
return errors.Wrapf(ErrorInvalidField, "%s", "deposit")
return ErrorInvalidFieldDeposit
}

return nil
Expand Down Expand Up @@ -83,17 +82,18 @@ func (m *MsgSubscribeToPlanRequest) Type() string {

func (m *MsgSubscribeToPlanRequest) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return errors.Wrapf(ErrorInvalidField, "%s", "from")
return ErrorInvalidFieldFrom
}

// Id shouldn't be zero
if m.Id == 0 {
return errors.Wrapf(ErrorInvalidField, "%s", "id")
return ErrorInvalidFieldId
}

// Denom should be valid
if err := sdk.ValidateDenom(m.Denom); err != nil {
return errors.Wrapf(ErrorInvalidField, "%s", "denom")
err := sdk.ValidateDenom(m.Denom)
if err != nil {
return ErrorInvalidFieldDenom
}

return nil
Expand Down Expand Up @@ -129,12 +129,12 @@ func (m *MsgCancelRequest) Type() string {

func (m *MsgCancelRequest) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return errors.Wrapf(ErrorInvalidField, "%s", "from")
return ErrorInvalidFieldFrom
}

// Id shouldn't be zero
if m.Id == 0 {
return errors.Wrapf(ErrorInvalidField, "%s", "id")
return ErrorInvalidFieldId
}

return nil
Expand Down Expand Up @@ -172,22 +172,22 @@ func (m *MsgAddQuotaRequest) Type() string {

func (m *MsgAddQuotaRequest) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return errors.Wrapf(ErrorInvalidField, "%s", "from")
return ErrorInvalidFieldFrom
}

// Id shouldn't be zero
if m.Id == 0 {
return errors.Wrapf(ErrorInvalidField, "%s", "id")
return ErrorInvalidFieldId
}

// Address should be valid
if _, err := sdk.AccAddressFromBech32(m.Address); err != nil {
return errors.Wrapf(ErrorInvalidField, "%s", "address")
return ErrorInvalidFieldAddress
}

// Bytes should be positive
if !m.Bytes.IsPositive() {
return errors.Wrapf(ErrorInvalidField, "%s", "bytes")
return ErrorInvalidFieldBytes
}

return nil
Expand Down Expand Up @@ -225,22 +225,22 @@ func (m *MsgUpdateQuotaRequest) Type() string {

func (m *MsgUpdateQuotaRequest) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return errors.Wrapf(ErrorInvalidField, "%s", "from")
return ErrorInvalidFieldFrom
}

// Id shouldn't be zero
if m.Id == 0 {
return errors.Wrapf(ErrorInvalidField, "%s", "id")
return ErrorInvalidFieldId
}

// Address shouldn be valid
if _, err := sdk.AccAddressFromBech32(m.Address); err != nil {
return errors.Wrapf(ErrorInvalidField, "%s", "address")
return ErrorInvalidFieldAddress
}

// Bytes should be positive
if !m.Bytes.IsPositive() {
return errors.Wrapf(ErrorInvalidField, "%s", "bytes")
return ErrorInvalidFieldBytes
}

return nil
Expand Down
170 changes: 170 additions & 0 deletions x/subscription/types/msg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package types

import (
"errors"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
hubtypes "github.com/sentinel-official/hub/types"
)

func TestMsgAddQuotaRequest_ValidateBasic(t *testing.T) {
correctAddress, err := sdk.AccAddressFromBech32("sent1grdunxx5jxd0ja75wt508sn6v39p70hhw53zs8")
if err != nil {
t.Errorf("invalid address: %s", err)
}

tests := []struct {
name string
m *MsgAddQuotaRequest
want error
}{
{"nil from address", NewMsgAddQuotaRequest(nil, 0, nil, sdk.NewInt(0)), ErrorInvalidFieldFrom},
{"empty from address", NewMsgAddQuotaRequest(sdk.AccAddress{}, 0, nil, sdk.NewInt(0)), ErrorInvalidFieldFrom},
{"zero id", NewMsgAddQuotaRequest(correctAddress, 0, nil, sdk.NewInt(0)), ErrorInvalidFieldId},
{"nil address", NewMsgAddQuotaRequest(correctAddress, 10, nil, sdk.NewInt(0)), ErrorInvalidFieldAddress},
{"empty address", NewMsgAddQuotaRequest(correctAddress, 10, sdk.AccAddress{}, sdk.NewInt(0)), ErrorInvalidFieldAddress},
{"empty bytes", NewMsgAddQuotaRequest(correctAddress, 10, sdk.AccAddress{}, sdk.Int{}), ErrorInvalidFieldAddress},
{"zero bytes", NewMsgAddQuotaRequest(correctAddress, 10, correctAddress, sdk.NewInt(0)), ErrorInvalidFieldBytes},
{"valid", NewMsgAddQuotaRequest(correctAddress, 10, correctAddress, sdk.NewInt(1000)), nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.m.ValidateBasic(); !errors.Is(err, tt.want) {
t.Errorf("ValidateBasic() = %s, want %s", err, tt.want)
}
})
}
}

func TestMsgUpdateQuotaRequest_ValidateBasic(t *testing.T) {
correctAddress, err := sdk.AccAddressFromBech32("sent1grdunxx5jxd0ja75wt508sn6v39p70hhw53zs8")
if err != nil {
t.Errorf("invalid address: %s", err)
}

tests := []struct {
name string
m *MsgUpdateQuotaRequest
want error
}{
{"nil from address", NewMsgUpdateQuotaRequest(nil, 0, nil, sdk.NewInt(0)), ErrorInvalidFieldFrom},
{"empty from address", NewMsgUpdateQuotaRequest(sdk.AccAddress{}, 0, nil, sdk.NewInt(0)), ErrorInvalidFieldFrom},
{"zero id", NewMsgUpdateQuotaRequest(correctAddress, 0, nil, sdk.NewInt(0)), ErrorInvalidFieldId},
{"nil address", NewMsgUpdateQuotaRequest(correctAddress, 10, nil, sdk.NewInt(0)), ErrorInvalidFieldAddress},
{"empty address", NewMsgUpdateQuotaRequest(correctAddress, 10, sdk.AccAddress{}, sdk.NewInt(0)), ErrorInvalidFieldAddress},
{"empty bytes", NewMsgUpdateQuotaRequest(correctAddress, 10, sdk.AccAddress{}, sdk.Int{}), ErrorInvalidFieldAddress},
{"zero bytes", NewMsgUpdateQuotaRequest(correctAddress, 10, correctAddress, sdk.NewInt(0)), ErrorInvalidFieldBytes},
{"valid", NewMsgUpdateQuotaRequest(correctAddress, 10, correctAddress, sdk.NewInt(1000)), nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.m.ValidateBasic(); !errors.Is(err, tt.want) {
t.Errorf("ValidateBasic() = %s, want %s", err, tt.want)
}
})
}
}

func TestMsgCancelRequest_ValidateBasic(t *testing.T) {
correctAddress, err := sdk.AccAddressFromBech32("sent1grdunxx5jxd0ja75wt508sn6v39p70hhw53zs8")
if err != nil {
t.Errorf("invalid address: %s", err)
}

tests := []struct {
name string
m *MsgCancelRequest
want error
}{
{"nil from address", NewMsgCancelRequest(nil, 0), ErrorInvalidFieldFrom},
{"empty from address", NewMsgCancelRequest(sdk.AccAddress{}, 0), ErrorInvalidFieldFrom},
{"zero id", NewMsgCancelRequest(correctAddress, 0), ErrorInvalidFieldId},
{"valid", NewMsgCancelRequest(correctAddress, 10), nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.m.ValidateBasic(); !errors.Is(err, tt.want) {
t.Errorf("ValidateBasic() = %s, want %s", err, tt.want)
}
})
}
}

func TestMsgSubscribeToNodeRequest_ValidateBasic(t *testing.T) {
correctAddress, err := sdk.AccAddressFromBech32("sent1grdunxx5jxd0ja75wt508sn6v39p70hhw53zs8")
if err != nil {
t.Errorf("invalid address: %s", err)
}

nodeAddress, err := hubtypes.NodeAddressFromBech32("sentnode1grdunxx5jxd0ja75wt508sn6v39p70hhczsm43")
if err != nil {
t.Errorf("invalid node address: %s", err)
}

coinWithWrongDenom := sdk.Coin{
Denom: "wrongdenom",
Amount: sdk.NewInt(0),
}
coinWithWrongAmount := sdk.Coin{
Denom: "sent",
Amount: sdk.NewInt(-10),
}
validCoin := sdk.Coin{
Denom: "sent",
Amount: sdk.NewInt(1000),
}

tests := []struct {
name string
m *MsgSubscribeToNodeRequest
want error
}{
{"nil from address", NewMsgSubscribeToNodeRequest(nil, nil, sdk.Coin{}), ErrorInvalidFieldFrom},
{"empty from address", NewMsgSubscribeToNodeRequest(sdk.AccAddress{}, nil, sdk.Coin{}), ErrorInvalidFieldFrom},
{"nil address", NewMsgSubscribeToNodeRequest(correctAddress, nil, sdk.Coin{}), ErrorInvalidFieldAddress},
{"empty address", NewMsgSubscribeToNodeRequest(correctAddress, hubtypes.NodeAddress{}, sdk.Coin{}), ErrorInvalidFieldAddress},
{"empty coin", NewMsgSubscribeToNodeRequest(correctAddress, nodeAddress, sdk.Coin{}), ErrorInvalidFieldDeposit},
{"wrong coin denom", NewMsgSubscribeToNodeRequest(correctAddress, nodeAddress, coinWithWrongDenom), ErrorInvalidFieldDeposit},
{"wrong coin amount", NewMsgSubscribeToNodeRequest(correctAddress, nodeAddress, coinWithWrongAmount), ErrorInvalidFieldDeposit},
{"valid", NewMsgSubscribeToNodeRequest(correctAddress, nodeAddress, validCoin), nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.m.ValidateBasic(); !errors.Is(err, tt.want) {
t.Errorf("ValidateBasic() = %s, want %s", err, tt.want)
}
})
}
}

func TestMsgSubscribeToPlanRequest_ValidateBasic(t *testing.T) {
correctAddress, err := sdk.AccAddressFromBech32("sent1grdunxx5jxd0ja75wt508sn6v39p70hhw53zs8")
if err != nil {
t.Errorf("invalid address: %s", err)
}

tests := []struct {
name string
m *MsgSubscribeToPlanRequest
want error
}{
{"nil from address", NewMsgSubscribeToPlanRequest(nil, 0, ""), ErrorInvalidFieldFrom},
{"empty from address", NewMsgSubscribeToPlanRequest(sdk.AccAddress{}, 0, ""), ErrorInvalidFieldFrom},
{"zero id", NewMsgSubscribeToPlanRequest(correctAddress, 0, ""), ErrorInvalidFieldId},
{"wrong denom", NewMsgSubscribeToPlanRequest(correctAddress, 10, "!!wrongdenom"), ErrorInvalidFieldDenom},
{"valid", NewMsgSubscribeToPlanRequest(correctAddress, 10, "wrongdenom"), nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.m.ValidateBasic(); !errors.Is(err, tt.want) {
t.Errorf("ValidateBasic() = %s, want %s", err, tt.want)
}
})
}
}

0 comments on commit 70e1982

Please sign in to comment.