Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase x/oracle unit test coverage #782

Merged
merged 7 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions x/oracle/types/denom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package types_test

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/umee-network/umee/v2/app"
"github.com/umee-network/umee/v2/x/oracle/types"
)

var (
umeeDenom = types.Denom{
BaseDenom: app.BondDenom,
SymbolDenom: app.DisplayDenom,
Exponent: 6,
}
lunaDenom = types.Denom{
BaseDenom: denomLunaIBC,
SymbolDenom: "ULUNA",
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
Exponent: 6,
}
atomDenom = types.Denom{
BaseDenom: denomAtomIBC,
SymbolDenom: "UATOM",
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
Exponent: 6,
}
)

func TestDenomString(t *testing.T) {
testCases := []struct {
denom types.Denom
expectedStr string
}{
{
denom: umeeDenom,
expectedStr: "base_denom: uumee\nsymbol_denom: UMEE\nexponent: 6\n",
},
{
denom: lunaDenom,
expectedStr: "base_denom: ibc/0EF15DF2F02480ADE0BB6E85D9EBB5DAEA2836D3860E9F97F9AADE4F57A31AA0\nsymbol_denom: ULUNA\nexponent: 6\n",
},
{
denom: atomDenom,
expectedStr: "base_denom: ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2\nsymbol_denom: UATOM\nexponent: 6\n",
},
}

for _, testCase := range testCases {
require.Equal(t, testCase.expectedStr, testCase.denom.String())
}
}

func TestDenomEqual(t *testing.T) {
testCases := []struct {
denom types.Denom
denomCompared types.Denom
equal bool
}{
{
denom: umeeDenom,
denomCompared: umeeDenom,
equal: true,
},
{
denom: umeeDenom,
denomCompared: lunaDenom,
equal: false,
},
{
denom: lunaDenom,
denomCompared: lunaDenom,
equal: true,
},
{
denom: atomDenom,
denomCompared: atomDenom,
equal: true,
},
{
denom: atomDenom,
denomCompared: lunaDenom,
equal: false,
},
}

for _, testCase := range testCases {
require.Equal(t, testCase.equal, testCase.denom.Equal(&testCase.denomCompared))
}
}

func TestDenomListString(t *testing.T) {
testCases := []struct {
denomList types.DenomList
expectedStr string
}{
{
denomList: types.DenomList{umeeDenom},
expectedStr: "base_denom: uumee\nsymbol_denom: UMEE\nexponent: 6",
},
{
denomList: types.DenomList{atomDenom, lunaDenom},
expectedStr: "base_denom: ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2\nsymbol_denom: UATOM\nexponent: 6\n\nbase_denom: ibc/0EF15DF2F02480ADE0BB6E85D9EBB5DAEA2836D3860E9F97F9AADE4F57A31AA0\nsymbol_denom: ULUNA\nexponent: 6",
},
}

for _, testCase := range testCases {
require.Equal(t, testCase.expectedStr, testCase.denomList.String())
}
}

func TestDenomListContains(t *testing.T) {
testCases := []struct {
denomList types.DenomList
denomSymbol string
symbolInList bool
}{
{
denomList: types.DenomList{umeeDenom},
denomSymbol: umeeDenom.SymbolDenom,
symbolInList: true,
},
{
denomList: types.DenomList{umeeDenom},
denomSymbol: lunaDenom.SymbolDenom,
symbolInList: false,
},
{
denomList: types.DenomList{umeeDenom, atomDenom},
denomSymbol: lunaDenom.SymbolDenom,
symbolInList: false,
},
{
denomList: types.DenomList{umeeDenom, atomDenom},
denomSymbol: atomDenom.SymbolDenom,
symbolInList: true,
},
{
denomList: types.DenomList{umeeDenom, atomDenom, lunaDenom},
denomSymbol: lunaDenom.SymbolDenom,
symbolInList: true,
},
}

for _, testCase := range testCases {
require.Equal(t, testCase.symbolInList, testCase.denomList.Contains(testCase.denomSymbol))
}
}
108 changes: 108 additions & 0 deletions x/oracle/types/keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package types_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/umee-network/umee/v2/app"
"github.com/umee-network/umee/v2/x/oracle/types"
)

const (
denomLunaIBC = "ibc/0EF15DF2F02480ADE0BB6E85D9EBB5DAEA2836D3860E9F97F9AADE4F57A31AA0"
denomAtomIBC = "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
)

func TestGetExchangeRateKey(t *testing.T) {
testCases := []struct {
denom string
expectedKey []byte
}{
{
denom: app.BondDenom,
expectedKey: []byte{0x1, 0x75, 0x75, 0x6d, 0x65, 0x65, 0x0},
},
{
denom: denomLunaIBC,
expectedKey: []byte{0x1, 0x69, 0x62, 0x63, 0x2f, 0x30, 0x45, 0x46, 0x31, 0x35, 0x44, 0x46, 0x32, 0x46, 0x30, 0x32, 0x34, 0x38, 0x30, 0x41, 0x44, 0x45, 0x30, 0x42, 0x42, 0x36, 0x45, 0x38, 0x35, 0x44, 0x39, 0x45, 0x42, 0x42, 0x35, 0x44, 0x41, 0x45, 0x41, 0x32, 0x38, 0x33, 0x36, 0x44, 0x33, 0x38, 0x36, 0x30, 0x45, 0x39, 0x46, 0x39, 0x37, 0x46, 0x39, 0x41, 0x41, 0x44, 0x45, 0x34, 0x46, 0x35, 0x37, 0x41, 0x33, 0x31, 0x41, 0x41, 0x30, 0x0},
},
{
denom: denomAtomIBC,
expectedKey: []byte{0x1, 0x69, 0x62, 0x63, 0x2f, 0x32, 0x37, 0x33, 0x39, 0x34, 0x46, 0x42, 0x30, 0x39, 0x32, 0x44, 0x32, 0x45, 0x43, 0x43, 0x44, 0x35, 0x36, 0x31, 0x32, 0x33, 0x43, 0x37, 0x34, 0x46, 0x33, 0x36, 0x45, 0x34, 0x43, 0x31, 0x46, 0x39, 0x32, 0x36, 0x30, 0x30, 0x31, 0x43, 0x45, 0x41, 0x44, 0x41, 0x39, 0x43, 0x41, 0x39, 0x37, 0x45, 0x41, 0x36, 0x32, 0x32, 0x42, 0x32, 0x35, 0x46, 0x34, 0x31, 0x45, 0x35, 0x45, 0x42, 0x32, 0x0},
},
}

for i, testCase := range testCases {
actualKey := types.GetExchangeRateKey(testCase.denom)
require.Equalf(t, testCase.expectedKey, actualKey, "test %d - expected key: %s should be the same as actual key: %s", i, testCase.expectedKey, actualKey)
}
}

func TestGetFeederDelegationKey(t *testing.T) {
testCases := []struct {
val sdk.ValAddress
expectedKey []byte
}{
{
val: []byte("addr________________"),
expectedKey: []byte{0x2, 0x14, 0x61, 0x64, 0x64, 0x72, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f},
},
}

for i, testCase := range testCases {
actualKey := types.GetFeederDelegationKey(testCase.val)
require.Equalf(t, testCase.expectedKey, actualKey, "test %d - expected key: %s should be the same as actual key: %s", i, testCase.expectedKey, actualKey)
}
}

func TestGetMissCounterKey(t *testing.T) {
testCases := []struct {
val sdk.ValAddress
expectedKey []byte
}{
{
val: []byte("addr________________"),
expectedKey: []byte{0x3, 0x14, 0x61, 0x64, 0x64, 0x72, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f},
},
}

for i, testCase := range testCases {
actualKey := types.GetMissCounterKey(testCase.val)
require.Equalf(t, testCase.expectedKey, actualKey, "test %d - expected key: %s should be the same as actual key: %s", i, testCase.expectedKey, actualKey)
}
}

func TestGetAggregateExchangeRatePrevoteKey(t *testing.T) {
testCases := []struct {
val sdk.ValAddress
expectedKey []byte
}{
{
val: []byte("addr________________"),
expectedKey: []byte{0x4, 0x14, 0x61, 0x64, 0x64, 0x72, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add a comment as to the origin of these bytes - that would also clarify what the meaning would be if the test ever failed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example:

// keyPrefixAggregateExchangeRatePrevote | lengthPrefixed(addr)

},
}

for i, testCase := range testCases {
actualKey := types.GetAggregateExchangeRatePrevoteKey(testCase.val)
require.Equalf(t, testCase.expectedKey, actualKey, "test %d - expected key: %s should be the same as actual key: %s", i, testCase.expectedKey, actualKey)
}
}

func TestGetAggregateExchangeRateVoteKey(t *testing.T) {
testCases := []struct {
val sdk.ValAddress
expectedKey []byte
}{
{
val: []byte("addr________________"),
expectedKey: []byte{0x5, 0x14, 0x61, 0x64, 0x64, 0x72, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f},
},
}

for i, testCase := range testCases {
actualKey := types.GetAggregateExchangeRateVoteKey(testCase.val)
require.Equalf(t, testCase.expectedKey, actualKey, "test %d - expected key: %s should be the same as actual key: %s", i, testCase.expectedKey, actualKey)
}
}