Skip to content

Commit

Permalink
add oracle e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-zq committed Jun 28, 2024
1 parent 60e371c commit 4cb043e
Show file tree
Hide file tree
Showing 7 changed files with 652 additions and 51 deletions.
15 changes: 15 additions & 0 deletions e2e/oracle/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package oracle

import (
"testing"

"github.com/stretchr/testify/suite"
)

func TestTxTestSuite(t *testing.T) {
suite.Run(t, new(TxTestSuite))
}

func TestQueryTestSuite(t *testing.T) {
suite.Run(t, new(QueryTestSuite))
}
172 changes: 172 additions & 0 deletions e2e/oracle/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package oracle

import (
"fmt"

"github.com/cosmos/gogoproto/proto"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"

"mods.irisnet.org/e2e"
oraclecli "mods.irisnet.org/modules/oracle/client/cli"
oracletypes "mods.irisnet.org/modules/oracle/types"
servicecli "mods.irisnet.org/modules/service/client/cli"
servicetypes "mods.irisnet.org/modules/service/types"

"mods.irisnet.org/e2e/service"
)

// QueryTestSuite is a suite of end-to-end tests for the nft module
type QueryTestSuite struct {
e2e.TestSuite
}

// TestQueryCmd tests all query command in the nft module
func (s *QueryTestSuite) TestQueryCmd() {
val := s.Network.Validators[0]
clientCtx := val.ClientCtx
expectedCode := uint32(0)

// ---------------------------------------------------------------------------
serviceName := "test-service"
serviceDesc := "test-description"
serviceAuthorDesc := "test-author-description"
serviceTags := "tags3,tags4"
serviceSchemas := `{"input":{"type":"object"},"output":{"type":"object"},"error":{"type":"object"}}`
serviceDenom := sdk.DefaultBondDenom

serviceDeposit := fmt.Sprintf("50000%s", serviceDenom)
servicePrices := fmt.Sprintf(`{"price": "50%s"}`, serviceDenom)
qos := int64(3)
options := "{}"

author := val.Address
provider := author
creator := author

feedName := "test-feed"
aggregateFunc := "avg"
valueJSONPath := "price"
latestHistory := 10
description := "description"
input := `{"header":{},"body":{}}`
providers := provider
timeout := 2
serviceFeeCap := fmt.Sprintf("50%s", serviceDenom)
threshold := 1
frequency := 12
baseURL := val.APIAddress

//------Define && Bind Service-------------
args := []string{
fmt.Sprintf("--%s=%s", servicecli.FlagName, serviceName),
fmt.Sprintf("--%s=%s", servicecli.FlagDescription, serviceDesc),
fmt.Sprintf("--%s=%s", servicecli.FlagTags, serviceTags),
fmt.Sprintf("--%s=%s", servicecli.FlagAuthorDescription, serviceAuthorDesc),
fmt.Sprintf("--%s=%s", servicecli.FlagSchemas, serviceSchemas),

fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf(
"--%s=%s",
flags.FlagFees,
sdk.NewCoins(sdk.NewCoin(s.Network.BondDenom, sdk.NewInt(10))).String(),
),
}

txResult := service.DefineServiceExec(
s.T(),
s.Network,
clientCtx,
author.String(),
args...)
s.Require().Equal(expectedCode, txResult.Code)

//------test GetCmdBindService()-------------
args = []string{
fmt.Sprintf("--%s=%s", servicecli.FlagServiceName, serviceName),
fmt.Sprintf("--%s=%s", servicecli.FlagDeposit, serviceDeposit),
fmt.Sprintf("--%s=%s", servicecli.FlagPricing, servicePrices),
fmt.Sprintf("--%s=%d", servicecli.FlagQoS, qos),
fmt.Sprintf("--%s=%s", servicecli.FlagOptions, options),
fmt.Sprintf("--%s=%s", servicecli.FlagProvider, provider),

fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf(
"--%s=%s",
flags.FlagFees,
sdk.NewCoins(sdk.NewCoin(s.Network.BondDenom, sdk.NewInt(10))).String(),
),
}

txResult = service.BindServiceExec(
s.T(),
s.Network,
clientCtx,
provider.String(),
args...)
s.Require().Equal(expectedCode, txResult.Code)

//------test GetCmdCreateFeed()-------------
args = []string{
fmt.Sprintf("--%s=%s", oraclecli.FlagFeedName, feedName),
fmt.Sprintf("--%s=%s", oraclecli.FlagAggregateFunc, aggregateFunc),
fmt.Sprintf("--%s=%s", oraclecli.FlagValueJsonPath, valueJSONPath),
fmt.Sprintf("--%s=%d", oraclecli.FlagLatestHistory, latestHistory),
fmt.Sprintf("--%s=%s", oraclecli.FlagDescription, description),
fmt.Sprintf("--%s=%s", oraclecli.FlagServiceFeeCap, serviceFeeCap),
fmt.Sprintf("--%s=%s", oraclecli.FlagServiceName, serviceName),
fmt.Sprintf("--%s=%s", oraclecli.FlagInput, input),
fmt.Sprintf("--%s=%s", oraclecli.FlagProviders, providers),
fmt.Sprintf("--%s=%d", oraclecli.FlagTimeout, timeout),
fmt.Sprintf("--%s=%d", oraclecli.FlagThreshold, threshold),
fmt.Sprintf("--%s=%d", oraclecli.FlagFrequency, frequency),
fmt.Sprintf("--%s=%s", oraclecli.FlagCreator, creator),

fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf(
"--%s=%s",
flags.FlagFees,
sdk.NewCoins(sdk.NewCoin(s.Network.BondDenom, sdk.NewInt(10))).String(),
),
}

txResult = CreateFeedExec(s.T(), s.Network, clientCtx, creator.String(), args...)
s.Require().Equal(expectedCode, txResult.Code)

// ------test GetCmdQueryFeed()-------------
url := fmt.Sprintf("%s/irismod/oracle/feeds/%s", baseURL, feedName)
resp, err := testutil.GetRequest(url)
s.Require().NoError(err)
respType := proto.Message(&oracletypes.QueryFeedResponse{})
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(resp, respType))
feedResp := respType.(*oracletypes.QueryFeedResponse)
s.Require().NoError(err)
s.Require().Equal(feedName, feedResp.Feed.Feed.FeedName)
s.Require().Equal(servicetypes.PAUSED, feedResp.Feed.State)

// ------test GetCmdQueryFeeds()-------------
url = fmt.Sprintf("%s/irismod/oracle/feeds", baseURL)
resp, err = testutil.GetRequest(url)
s.Require().NoError(err)
respType = proto.Message(&oracletypes.QueryFeedsResponse{})
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(resp, respType))
feedsResp := respType.(*oracletypes.QueryFeedsResponse)
s.Require().NoError(err)
s.Require().Len(feedsResp.Feeds, 1)
s.Require().Equal(feedResp.Feed, feedsResp.Feeds[0])

// ------test GetCmdQueryFeedValue()-------------
url = fmt.Sprintf("%s/irismod/oracle/feeds/%s/values", baseURL, feedName)
resp, err = testutil.GetRequest(url)
respType = proto.Message(&oracletypes.QueryFeedValueResponse{})
s.Require().NoError(err)
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(resp, respType))
feedValueResp := respType.(*oracletypes.QueryFeedValueResponse)
s.Require().NoError(err)
s.Require().Len(feedValueResp.FeedValues, 0)
}
189 changes: 189 additions & 0 deletions e2e/oracle/test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package oracle

import (
"fmt"
"testing"

"github.com/cometbft/cometbft/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"

oraclecli "mods.irisnet.org/modules/oracle/client/cli"
oracletypes "mods.irisnet.org/modules/oracle/types"
"mods.irisnet.org/simapp"
)

// CreateFeedExec creates a feed execution message.
//
// Parameters:
// - t: The testing context.
// - network: The simulation network.
// - clientCtx: The client context.
// - from: The sender address.
// - extraArgs: Additional arguments.
// Returns a response transaction.
func CreateFeedExec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
from string,
extraArgs ...string) *simapp.ResponseTx {
args := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, from),
}
args = append(args, extraArgs...)

return network.ExecTxCmdWithResult(t, clientCtx, oraclecli.GetCmdCreateFeed(), args)
}

// EditFeedExec creates a feed execution message.
//
// Parameters:
// - t: The testing context.
// - network: The simulation network.
// - clientCtx: The client context.
// - from: The sender address.
// - feedName: The name of the feed.
// - extraArgs: Additional arguments.
//
// Returns:
// - A pointer to a simapp.ResponseTx.
func EditFeedExec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
from string,
feedName string,
extraArgs ...string) *simapp.ResponseTx {
args := []string{
feedName,
fmt.Sprintf("--%s=%s", flags.FlagFrom, from),
}
args = append(args, extraArgs...)

return network.ExecTxCmdWithResult(t, clientCtx, oraclecli.GetCmdEditFeed(), args)
}

// StartFeedExec starts a feed execution message.
//
// Parameters:
// - t: The testing context.
// - network: The simulation network.
// - clientCtx: The client context.
// - from: The sender address.
// - feedName: The name of the feed.
// - extraArgs: Additional arguments.
//
// Returns:
// - A pointer to a simapp.ResponseTx.
func StartFeedExec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
from string,
feedName string,
extraArgs ...string) *simapp.ResponseTx {
args := []string{
feedName,
fmt.Sprintf("--%s=%s", flags.FlagFrom, from),
}
args = append(args, extraArgs...)

return network.ExecTxCmdWithResult(t, clientCtx, oraclecli.GetCmdStartFeed(), args)
}

// PauseFeedExec creates a transaction to pause a feed.
//
// t: The testing context.
// network: The simulation network.
// clientCtx: The client context.
// from: The sender address.
// feedName: The name of the feed.
// extraArgs: Additional arguments.
// Returns a pointer to a simapp.ResponseTx.
func PauseFeedExec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
from string,
feedName string,
extraArgs ...string) *simapp.ResponseTx {
args := []string{
feedName,
fmt.Sprintf("--%s=%s", flags.FlagFrom, from),
}
args = append(args, extraArgs...)

return network.ExecTxCmdWithResult(t, clientCtx, oraclecli.GetCmdPauseFeed(), args)
}

// QueryFeedExec creates a transaction to query a feed.
//
// Parameters:
// - t: The testing context.
// - network: The simulation network.
// - clientCtx: The client context.
// - feedName: The name of the feed.
// - extraArgs: Additional arguments.
// Returns a pointer to an oracletypes.FeedContext.
func QueryFeedExec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
feedName string,
extraArgs ...string) *oracletypes.FeedContext {
args := []string{
feedName,
fmt.Sprintf("--%s=json", cli.OutputFlag),
}
args = append(args, extraArgs...)

response := &oracletypes.FeedContext{}
network.ExecQueryCmd(t, clientCtx, oraclecli.GetCmdQueryFeed(), args, response)
return response
}

// QueryFeedsExec queries the feeds using the provided network, client context, and optional extra arguments.
//
// Parameters:
// - t: The testing context.
// - network: The simulation network.
// - clientCtx: The client context.
// - extraArgs: Optional extra arguments.
//
// Returns:
// - *oracletypes.QueryFeedsResponse: The response containing the queried feeds.
func QueryFeedsExec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
extraArgs ...string) *oracletypes.QueryFeedsResponse {
args := []string{
fmt.Sprintf("--%s=json", cli.OutputFlag),
}
args = append(args, extraArgs...)

response := &oracletypes.QueryFeedsResponse{}
network.ExecQueryCmd(t, clientCtx, oraclecli.GetCmdQueryFeeds(), args, response)
return response
}

// QueryFeedValueExec creates a transaction to query a feed value.
//
// Parameters:
// - t: The testing context.
// - network: The simulation network.
// - clientCtx: The client context.
// - feedName: The name of the feed.
// - extraArgs: Additional arguments.
// Returns a pointer to an oracletypes.QueryFeedValueResponse.
func QueryFeedValueExec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
feedName string,
extraArgs ...string) *oracletypes.QueryFeedValueResponse {
args := []string{
feedName,
fmt.Sprintf("--%s=json", cli.OutputFlag),
}
args = append(args, extraArgs...)

response := &oracletypes.QueryFeedValueResponse{}
network.ExecQueryCmd(t, clientCtx, oraclecli.GetCmdQueryFeedValue(), args, response)
return response
}
Loading

0 comments on commit 4cb043e

Please sign in to comment.