Skip to content

Commit

Permalink
test: increase x/oracle unit test coverage (#782)
Browse files Browse the repository at this point in the history
* chore: increasce test coverage

* rename variables

Co-authored-by: Adam Wozniak <29418299+adamewozniak@users.noreply.github.com>

* moved test vars and const to test_utils

* add comments to keys

* fix string unit test

* add changelog

Co-authored-by: Adam Moser <63419657+toteki@users.noreply.github.com>
Co-authored-by: Adam Wozniak <29418299+adamewozniak@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 8, 2022
1 parent 8090261 commit 2343d70
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

- [781](https://github.com/umee-network/umee/pull/781) Oracle module unit test cleanup.
- [782](https://github.com/umee-network/umee/pull/782) Add unit test to `x/oracle/types/denom.go` and `x/oracle/types/keys.go`.

## [v2.0.0](https://github.com/umee-network/umee/releases/tag/v2.0.0) - 2022-04-06

Expand Down
128 changes: 128 additions & 0 deletions x/oracle/types/denom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package types_test

import (
"testing"

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

func TestDenomString(t *testing.T) {
testCases := []struct {
denom types.Denom
expectedStr string
}{
{
denom: types.DenomUmee,
expectedStr: "base_denom: uumee\nsymbol_denom: umee\nexponent: 6\n",
},
{
denom: types.DenomLuna,
expectedStr: "base_denom: ibc/0EF15DF2F02480ADE0BB6E85D9EBB5DAEA2836D3860E9F97F9AADE4F57A31AA0\nsymbol_denom: LUNA\nexponent: 6\n",
},
{
denom: types.DenomAtom,
expectedStr: "base_denom: ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2\nsymbol_denom: ATOM\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: types.DenomUmee,
denomCompared: types.DenomUmee,
equal: true,
},
{
denom: types.DenomUmee,
denomCompared: types.DenomLuna,
equal: false,
},
{
denom: types.DenomLuna,
denomCompared: types.DenomLuna,
equal: true,
},
{
denom: types.DenomAtom,
denomCompared: types.DenomAtom,
equal: true,
},
{
denom: types.DenomAtom,
denomCompared: types.DenomLuna,
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{types.DenomUmee},
expectedStr: "base_denom: uumee\nsymbol_denom: umee\nexponent: 6",
},
{
denomList: types.DenomList{types.DenomAtom, types.DenomLuna},
expectedStr: "base_denom: ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2\nsymbol_denom: ATOM\nexponent: 6\n\nbase_denom: ibc/0EF15DF2F02480ADE0BB6E85D9EBB5DAEA2836D3860E9F97F9AADE4F57A31AA0\nsymbol_denom: LUNA\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{types.DenomUmee},
denomSymbol: types.DenomUmee.SymbolDenom,
symbolInList: true,
},
{
denomList: types.DenomList{types.DenomUmee},
denomSymbol: types.DenomLuna.SymbolDenom,
symbolInList: false,
},
{
denomList: types.DenomList{types.DenomUmee, types.DenomAtom},
denomSymbol: types.DenomLuna.SymbolDenom,
symbolInList: false,
},
{
denomList: types.DenomList{types.DenomUmee, types.DenomAtom},
denomSymbol: types.DenomAtom.SymbolDenom,
symbolInList: true,
},
{
denomList: types.DenomList{types.DenomUmee, types.DenomAtom, types.DenomLuna},
denomSymbol: types.DenomLuna.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"
)

func TestGetExchangeRateKey(t *testing.T) {
testCases := []struct {
denom string
// KeyPrefixExchangeRate | []byte(denom) | 0
expectedKey []byte
}{
{
denom: app.BondDenom,
expectedKey: []byte{0x1, 0x75, 0x75, 0x6d, 0x65, 0x65, 0x0},
},
{
denom: types.IbcDenomLuna,
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: types.IbcDenomAtom,
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
// KeyPrefixFeederDelegation | lengthPrefixed(addr)
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
// KeyPrefixMissCounter | lengthPrefixed(addr)
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
// KeyPrefixAggregateExchangeRatePrevote | lengthPrefixed(addr)
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},
},
}

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
// KeyPrefixAggregateExchangeRateVote | lengthPrefixed(addr)
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)
}
}
21 changes: 21 additions & 0 deletions x/oracle/types/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,30 @@ import (
tmprotocrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
)

const (
IbcDenomLuna = "ibc/0EF15DF2F02480ADE0BB6E85D9EBB5DAEA2836D3860E9F97F9AADE4F57A31AA0"
IbcDenomAtom = "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"
)

var (
_ StakingKeeper = MockStakingKeeper{}
_ stakingtypes.ValidatorI = MockValidator{}

DenomUmee = Denom{
BaseDenom: UmeeDenom,
SymbolDenom: UmeeSymbol,
Exponent: 6,
}
DenomLuna = Denom{
BaseDenom: IbcDenomLuna,
SymbolDenom: "LUNA",
Exponent: 6,
}
DenomAtom = Denom{
BaseDenom: IbcDenomAtom,
SymbolDenom: "ATOM",
Exponent: 6,
}
)

// GenerateRandomTestCase
Expand Down

0 comments on commit 2343d70

Please sign in to comment.