Skip to content

Commit

Permalink
unit test for UpdateParams, AddRequiredDenoms, RemoveRequiredDenoms
Browse files Browse the repository at this point in the history
  • Loading branch information
antstalepresh committed Jul 12, 2024
1 parent 20e491d commit 5a9194a
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
119 changes: 116 additions & 3 deletions x/oracle/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"testing"

"cosmossdk.io/math"
"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -14,8 +13,7 @@ import (
)

var (
stakingAmt = sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)
randomExchangeRate = math.LegacyNewDec(1700)
stakingAmt = sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)
)

func setup(t *testing.T) (TestInput, types.MsgServer) {
Expand All @@ -40,3 +38,118 @@ func setup(t *testing.T) (TestInput, types.MsgServer) {

return input, msgServer
}

func TestMsgUpdateParams(t *testing.T) {
input, msgServer := setup(t)

// Test default params setting
input.OracleKeeper.SetParams(input.Ctx, types.DefaultParams())
params := input.OracleKeeper.GetParams(input.Ctx)
require.NotNil(t, params)

// Test updating with invalid authority
_, err := msgServer.UpdateParams(input.Ctx, &types.MsgUpdateParams{
Authority: "invalid_authority",
Params: &params,
})
require.Error(t, err)

// Test updating with correct authority
_, err = msgServer.UpdateParams(input.Ctx, &types.MsgUpdateParams{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Params: &params,
})
require.NoError(t, err)

// Test updating required denoms
params.RequiredDenoms = []types.Denom{{Denom: "BTC", Id: 1}}
_, err = msgServer.UpdateParams(input.Ctx, &types.MsgUpdateParams{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Params: &params,
})
require.Error(t, err)
}

func TestMsgAddRequiredDenoms(t *testing.T) {
input, msgServer := setup(t)

params := input.OracleKeeper.GetParams(input.Ctx)
require.Len(t, params.RequiredDenoms, 3)
require.Equal(t, params.LastDenomId, uint32(3))

// Test with invalid authority
_, err := msgServer.AddRequiredDenoms(input.Ctx, &types.MsgAddRequiredDenoms{
Authority: "invalid_authority",
Symbols: []string{"ATOM"},
})
require.Error(t, err)

// Adding a single denom
_, err = msgServer.AddRequiredDenoms(input.Ctx, &types.MsgAddRequiredDenoms{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Symbols: []string{"ATOM"},
})
require.NoError(t, err)
params = input.OracleKeeper.GetParams(input.Ctx)
require.Len(t, params.RequiredDenoms, 4)
require.Equal(t, params.LastDenomId, uint32(4))

// Adding two denoms
_, err = msgServer.AddRequiredDenoms(input.Ctx, &types.MsgAddRequiredDenoms{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Symbols: []string{"AKT", "ARB"},
})
require.NoError(t, err)
params = input.OracleKeeper.GetParams(input.Ctx)
require.Len(t, params.RequiredDenoms, 6)
require.Equal(t, params.LastDenomId, uint32(6))

// Adding already existing denom
_, err = msgServer.AddRequiredDenoms(input.Ctx, &types.MsgAddRequiredDenoms{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Symbols: []string{"ATOM"},
})
require.Error(t, err)
}

func TestMsgRemoveRequiredDenoms(t *testing.T) {
input, msgServer := setup(t)

params := input.OracleKeeper.GetParams(input.Ctx)
require.Len(t, params.RequiredDenoms, 3)
require.Equal(t, params.LastDenomId, uint32(3))

// Test with invalid authority
_, err := msgServer.RemoveRequiredDenoms(input.Ctx, &types.MsgRemoveRequiredDenoms{
Authority: "invalid_authority",
Symbols: []string{"BTC"},
})
require.Error(t, err)

// Removing a single denom
_, err = msgServer.RemoveRequiredDenoms(input.Ctx, &types.MsgRemoveRequiredDenoms{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Symbols: []string{"BTC"},
})
require.NoError(t, err)
params = input.OracleKeeper.GetParams(input.Ctx)
require.Len(t, params.RequiredDenoms, 2)
require.Equal(t, params.LastDenomId, uint32(3))

// Removing two denoms
_, err = msgServer.RemoveRequiredDenoms(input.Ctx, &types.MsgRemoveRequiredDenoms{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Symbols: []string{"ETH", "USDT"},
})
require.NoError(t, err)
params = input.OracleKeeper.GetParams(input.Ctx)
require.Len(t, params.RequiredDenoms, 0)
require.Equal(t, params.LastDenomId, uint32(3))

// Removing not existing denom
_, err = msgServer.RemoveRequiredDenoms(input.Ctx, &types.MsgRemoveRequiredDenoms{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Symbols: []string{"BTC"},
})
require.NoError(t, err)
}
1 change: 1 addition & 0 deletions x/oracle/keeper/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func CreateTestInput(t *testing.T) TestInput {
{Denom: "ETH", Id: 2},
{Denom: "USDT", Id: 3},
}
defaults.LastDenomId = 3
keeper.SetParams(ctx, defaults)

return TestInput{ctx, legacyAmino, accountKeeper, bankKeeper, keeper, *stakingKeeper, distrKeeper}
Expand Down

0 comments on commit 5a9194a

Please sign in to comment.