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

Hung add test ProcessBandOraclePrices #24

Merged
Changes from 7 commits
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
120 changes: 120 additions & 0 deletions x/oracle/keeper/band_oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/onomyprotocol/reserve/app"
"github.com/onomyprotocol/reserve/x/oracle/types"
"github.com/onomyprotocol/reserve/x/oracle/utils"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -274,3 +275,122 @@ func TestGetPrice(t *testing.T) {
})
}
}

func TestProcessBandOraclePrices(t *testing.T) {
// Set up the application and context
app := app.Setup(t, false)
ctx := app.BaseApp.NewContextLegacy(false, tmproto.Header{Height: 1, ChainID: "3", Time: time.Unix(1618997040, 0)})

// Define table-driven test cases
tests := []struct {
name string
clientID string
calldata *types.CalldataRecord
oracleOutput interface{}
expectedError bool
expectedRate int64
}{
{
name: "Fail when ClientID is not a valid integer",
clientID: "invalid-id",
calldata: nil,
oracleOutput: nil,
expectedError: true,
},
{
name: "Return nil when no CallDataRecord found",
clientID: "1",
calldata: nil,
oracleOutput: nil,
expectedError: false,
},
{
name: "Fail when decoding OracleInput",
clientID: "1",
calldata: &types.CalldataRecord{
ClientId: 1,
Calldata: []byte{0xFF, 0xFF},
},
oracleOutput: nil,
expectedError: true,
},
{
name: "Fail when decoding OracleOutput",
clientID: "1",
calldata: &types.CalldataRecord{
ClientId: 1,
Calldata: utils.MustEncode(types.Input{
Symbols: []string{"ATOM", "BTC"},
Multiplier: types.BandPriceMultiplier,
}),
},
oracleOutput: []byte{0xFF, 0xFF},
expectedError: true,
},
{
name: "Success with valid OracleResponsePacketData",
clientID: "1",
calldata: &types.CalldataRecord{
ClientId: 1,
Calldata: utils.MustEncode(types.Input{
Symbols: []string{"ATOM", "BTC"},
Multiplier: types.BandPriceMultiplier,
}),
},
oracleOutput: types.BandOutput{
Responses: []types.Response{
{Symbol: "ATOM", ResponseCode: 0, Rate: 100},
{Symbol: "BTC", ResponseCode: 0, Rate: 50000},
},
},
expectedError: false,
expectedRate: 100,
},
}

// Iterate over each test case
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.calldata != nil {
err := app.OracleKeeper.SetBandCallDataRecord(ctx, tt.calldata)
require.NoError(t, err)
}

var result []byte
if tt.oracleOutput != nil {
switch v := tt.oracleOutput.(type) {
case types.BandOutput:
result = utils.MustEncode(v)
case []byte:
result = v
}
}

oraclePacketData := types.OracleResponsePacketData{
ClientID: tt.clientID,
RequestID: 1,
Result: result,
ResolveTime: time.Now().Unix(),
}

relayer := sdk.AccAddress("mock-relayer-address")

err := app.OracleKeeper.ProcessBandOraclePrices(ctx, relayer, oraclePacketData)

if tt.expectedError {
require.Error(t, err)
} else {
require.NoError(t, err)

record := app.OracleKeeper.GetBandCallDataRecord(ctx, 1)
require.Nil(t, record)

if tt.expectedRate != 0 {
priceState := app.OracleKeeper.GetBandPriceState(ctx, "ATOM")
require.NotNil(t, priceState)
require.Equal(t, tt.expectedRate, priceState.Rate.Int64())
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should get the PriceState.Price and compare instead of rate here

Copy link
Collaborator

Choose a reason for hiding this comment

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

We also need to check whether BandCallDataRecord was deleted after this line k.DeleteBandCallDataRecord(ctx, uint64(clientID))

}
}
})
}
}
Loading