-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add more tests for oracle module (#389)
1. Overview Add more tests in x/types. Change v.BaseDenom to v.SymbolDenom in tally.go. Since we are using symbol everywhere.
- Loading branch information
1 parent
7e65fb8
commit 48cd887
Showing
10 changed files
with
279 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/persistenceOne/persistence-sdk/v2/x/oracle/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDenomString(t *testing.T) { | ||
testCases := []struct { | ||
denom types.Denom | ||
expectedStr string | ||
}{ | ||
{ | ||
denom: types.DenomPersistence, | ||
expectedStr: "base_denom: uxprt\nsymbol_denom: XPRT\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.DenomPersistence, | ||
denomCompared: types.DenomPersistence, | ||
equal: true, | ||
}, | ||
{ | ||
denom: types.DenomPersistence, | ||
denomCompared: types.DenomAtom, | ||
equal: false, | ||
}, | ||
{ | ||
denom: types.DenomAtom, | ||
denomCompared: types.DenomAtom, | ||
equal: true, | ||
}, | ||
{ | ||
denom: types.DenomAtom, | ||
denomCompared: types.DenomPersistence, | ||
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.DenomPersistence}, | ||
expectedStr: "base_denom: uxprt\nsymbol_denom: XPRT\nexponent: 6", | ||
}, | ||
{ | ||
denomList: types.DenomList{types.DenomPersistence, types.DenomAtom}, | ||
expectedStr: "base_denom: uxprt\nsymbol_denom: XPRT\nexponent: 6\n\nbase_denom: ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2\nsymbol_denom: ATOM\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.DenomPersistence}, | ||
denomSymbol: types.DenomPersistence.SymbolDenom, | ||
symbolInList: true, | ||
}, | ||
{ | ||
denomList: types.DenomList{types.DenomPersistence}, | ||
denomSymbol: types.DenomAtom.SymbolDenom, | ||
symbolInList: false, | ||
}, | ||
{ | ||
denomList: types.DenomList{types.DenomPersistence, types.DenomAtom}, | ||
denomSymbol: types.DenomAtom.SymbolDenom, | ||
symbolInList: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
require.Equal(t, testCase.symbolInList, testCase.denomList.Contains(testCase.denomSymbol)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGenesisValidation(t *testing.T) { | ||
// Valid state | ||
genState := DefaultGenesisState() | ||
require.NoError(t, ValidateGenesis(genState)) | ||
|
||
// Invalid Vote Period | ||
genState.Params.VotePeriod = 0 | ||
require.Error(t, ValidateGenesis(genState)) | ||
|
||
// Invalid VoteThreshold | ||
genState = DefaultGenesisState() | ||
genState.Params.VoteThreshold = sdk.NewDecWithPrec(33, 2) | ||
require.Error(t, ValidateGenesis(genState)) | ||
|
||
// Invalid Rewardband | ||
genState = DefaultGenesisState() | ||
genState.Params.RewardBand = sdk.NewDec(2) | ||
require.Error(t, ValidateGenesis(genState)) | ||
genState.Params.RewardBand = sdk.NewDec(-1) | ||
require.Error(t, ValidateGenesis(genState)) | ||
|
||
// Invalid RewardDistributionWindow | ||
genState = DefaultGenesisState() | ||
genState.Params.RewardDistributionWindow = genState.Params.VotePeriod - 1 | ||
require.Error(t, ValidateGenesis(genState)) | ||
|
||
// Invalid SlashFraction | ||
genState = DefaultGenesisState() | ||
genState.Params.SlashFraction = sdk.NewDec(2) | ||
require.Error(t, ValidateGenesis(genState)) | ||
genState.Params.SlashFraction = sdk.NewDec(-1) | ||
require.Error(t, ValidateGenesis(genState)) | ||
|
||
// Invalid SlashWindow | ||
genState = DefaultGenesisState() | ||
genState.Params.SlashWindow = genState.Params.VotePeriod - 1 | ||
require.Error(t, ValidateGenesis(genState)) | ||
|
||
// Invalid MinValidPerWindow | ||
genState = DefaultGenesisState() | ||
genState.Params.MinValidPerWindow = sdk.NewDec(2) | ||
require.Error(t, ValidateGenesis(genState)) | ||
genState.Params.MinValidPerWindow = sdk.NewDec(-1) | ||
require.Error(t, ValidateGenesis(genState)) | ||
|
||
// Invalid AcceptList | ||
genState = DefaultGenesisState() | ||
genState.Params.AcceptList = DenomList{Denom{}} | ||
require.Error(t, ValidateGenesis(genState)) | ||
} | ||
|
||
func TestGetGenesisStateFromAppState(t *testing.T) { | ||
emptyGenesis := GenesisState{ | ||
Params: Params{}, | ||
ExchangeRates: []ExchangeRateTuple{}, | ||
FeederDelegations: []FeederDelegation{}, | ||
MissCounters: []MissCounter{}, | ||
AggregateExchangeRatePrevotes: []AggregateExchangeRatePrevote{}, | ||
AggregateExchangeRateVotes: []AggregateExchangeRateVote{}, | ||
} | ||
|
||
bz, err := json.Marshal(emptyGenesis) | ||
require.Nil(t, err) | ||
|
||
require.NotNil(t, GetGenesisStateFromAppState(ModuleCdc, map[string]json.RawMessage{ | ||
ModuleName: bz, | ||
})) | ||
require.NotNil(t, GetGenesisStateFromAppState(ModuleCdc, map[string]json.RawMessage{})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/hex" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestAggregateVoteHash(t *testing.T) { | ||
addrs := []sdk.AccAddress{ | ||
sdk.AccAddress([]byte("addr1_______________")), | ||
} | ||
|
||
aggregateVoteHash := GetAggregateVoteHash("salt", "XPRT:100,ATOM:100", sdk.ValAddress(addrs[0])) | ||
hexStr := hex.EncodeToString(aggregateVoteHash) | ||
aggregateVoteHashRes, err := AggregateVoteHashFromHexString(hexStr) | ||
require.NoError(t, err) | ||
require.Equal(t, true, aggregateVoteHash.Equal(aggregateVoteHashRes)) | ||
require.Equal(t, true, AggregateVoteHash([]byte{}).Empty()) | ||
|
||
got, _ := yaml.Marshal(&aggregateVoteHash) | ||
require.Equal(t, aggregateVoteHash.String()+"\n", string(got)) | ||
|
||
res := AggregateVoteHash{} | ||
testMarshal(t, &aggregateVoteHash, &res, aggregateVoteHash.MarshalJSON, (&res).UnmarshalJSON) | ||
testMarshal(t, &aggregateVoteHash, &res, aggregateVoteHash.Marshal, (&res).Unmarshal) | ||
} | ||
|
||
func testMarshal(t *testing.T, original, res interface{}, marshal func() ([]byte, error), unmarshal func([]byte) error) { | ||
bz, err := marshal() | ||
require.NoError(t, err) | ||
err = unmarshal(bz) | ||
require.NoError(t, err) | ||
require.EqualValues(t, original, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters