-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d081713
commit da5032a
Showing
10 changed files
with
675 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package random | ||
|
||
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)) | ||
} |
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,197 @@ | ||
package random | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/cosmos/gogoproto/proto" | ||
"github.com/tidwall/gjson" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
"github.com/cosmos/cosmos-sdk/testutil/network" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"mods.irisnet.org/e2e" | ||
"mods.irisnet.org/e2e/service" | ||
randomcli "mods.irisnet.org/modules/random/client/cli" | ||
randomtypes "mods.irisnet.org/modules/random/types" | ||
servicecli "mods.irisnet.org/modules/service/client/cli" | ||
servicetypes "mods.irisnet.org/modules/service/types" | ||
) | ||
|
||
// QueryTestSuite is a suite of end-to-end tests for the nft module | ||
type QueryTestSuite struct { | ||
e2e.TestSuite | ||
} | ||
|
||
// SetupSuite sets up test suite | ||
func (s *QueryTestSuite) SetupSuite() { | ||
s.SetModifyConfigFn(func(cfg *network.Config) { | ||
var serviceGenState servicetypes.GenesisState | ||
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[servicetypes.ModuleName], &serviceGenState) | ||
|
||
serviceGenState.Definitions = append( | ||
serviceGenState.Definitions, | ||
servicetypes.GenOraclePriceSvcDefinition(), | ||
servicetypes.GetRandomSvcDefinition(), | ||
) | ||
serviceGenState.Bindings = append( | ||
serviceGenState.Bindings, | ||
servicetypes.GenOraclePriceSvcBinding(sdk.DefaultBondDenom), | ||
) | ||
cfg.GenesisState[servicetypes.ModuleName] = cfg.Codec.MustMarshalJSON(&serviceGenState) | ||
}) | ||
s.TestSuite.SetupSuite() | ||
} | ||
|
||
// TestQueryCmd tests all query command in the nft module | ||
func (s *QueryTestSuite) TestQueryCmd() { | ||
val := s.Validators[0] | ||
clientCtx := val.ClientCtx | ||
expectedCode := uint32(0) | ||
|
||
// --------------------------------------------------------------------------- | ||
serviceDeposit := fmt.Sprintf("50000%s", s.BondDenom) | ||
servicePrices := fmt.Sprintf(`{"price": "50%s"}`, s.BondDenom) | ||
qos := int64(3) | ||
options := "{}" | ||
provider := val.Address | ||
baseURL := val.APIAddress | ||
|
||
from := val.Address | ||
blockInterval := 4 | ||
oracle := true | ||
serviceFeeCap := fmt.Sprintf("50%s", s.BondDenom) | ||
|
||
respResult := `{"code":200,"message":""}` | ||
seedStr := "ABCDEF12ABCDEF12ABCDEF12ABCDEF12ABCDEF12ABCDEF12ABCDEF12ABCDEF12" | ||
respOutput := fmt.Sprintf(`{"header":{},"body":{"seed":"%s"}}`, seedStr) | ||
|
||
// ------bind random service------------- | ||
args := []string{ | ||
fmt.Sprintf("--%s=%s", servicecli.FlagServiceName, randomtypes.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 GetCmdRequestRandom()------------- | ||
args = []string{ | ||
fmt.Sprintf("--%s=%s", randomcli.FlagServiceFeeCap, serviceFeeCap), | ||
fmt.Sprintf("--%s=%t", randomcli.FlagOracle, oracle), | ||
fmt.Sprintf("--%s=%d", randomcli.FlagBlockInterval, blockInterval), | ||
|
||
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 = RequestRandomExec(s.T(), s.Network, clientCtx, from.String(), args...) | ||
s.Require().Equal(expectedCode, txResult.Code) | ||
|
||
requestID := gjson.Get(txResult.Log, "0.events.1.attributes.0.value").String() | ||
requestHeight := gjson.Get(txResult.Log, "0.events.1.attributes.2.value").Int() | ||
|
||
// ------test GetCmdQueryRandomRequestQueue()------------- | ||
url := fmt.Sprintf("%s/irismod/random/queue", baseURL) | ||
resp, err := testutil.GetRequest(url) | ||
respType := proto.Message(&randomtypes.QueryRandomRequestQueueResponse{}) | ||
s.Require().NoError(err) | ||
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(resp, respType)) | ||
qrrResp := respType.(*randomtypes.QueryRandomRequestQueueResponse) | ||
s.Require().NoError(err) | ||
s.Require().Len(qrrResp.Requests, 1) | ||
|
||
// ------get service request------------- | ||
requestHeight = requestHeight + 1 | ||
_, err = s.Network.WaitForHeightWithTimeout( | ||
requestHeight, | ||
time.Duration(int64(blockInterval+2)*int64(s.Network.TimeoutCommit)), | ||
) | ||
if err != nil { | ||
s.Network.WaitForNBlock(2) | ||
} | ||
|
||
blockResult, err := val.RPCClient.BlockResults(context.Background(), &requestHeight) | ||
s.Require().NoError(err) | ||
var requestId string | ||
for _, event := range blockResult.EndBlockEvents { | ||
if event.Type == servicetypes.EventTypeNewBatchRequestProvider { | ||
var found bool | ||
var requestIds []string | ||
var requestsBz []byte | ||
for _, attribute := range event.Attributes { | ||
if string(attribute.Key) == servicetypes.AttributeKeyRequests { | ||
requestsBz = []byte(attribute.Value) | ||
found = true | ||
} | ||
} | ||
s.Require().True(found) | ||
if found { | ||
err := json.Unmarshal(requestsBz, &requestIds) | ||
s.Require().NoError(err) | ||
} | ||
s.Require().Len(requestIds, 1) | ||
requestId = requestIds[0] | ||
} | ||
} | ||
s.Require().NotEmpty(requestId) | ||
|
||
// ------respond service request------------- | ||
args = []string{ | ||
fmt.Sprintf("--%s=%s", servicecli.FlagRequestID, requestId), | ||
fmt.Sprintf("--%s=%s", servicecli.FlagResult, respResult), | ||
fmt.Sprintf("--%s=%s", servicecli.FlagData, respOutput), | ||
|
||
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.RespondServiceExec( | ||
s.T(), | ||
s.Network, | ||
clientCtx, | ||
provider.String(), | ||
args...) | ||
s.Require().Equal(expectedCode, txResult.Code) | ||
|
||
// ------test GetCmdQueryRandom()------------- | ||
url = fmt.Sprintf("%s/irismod/random/randoms/%s", baseURL, requestID) | ||
resp, err = testutil.GetRequest(url) | ||
respType = proto.Message(&randomtypes.QueryRandomResponse{}) | ||
s.Require().NoError(err) | ||
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(resp, respType)) | ||
randomResp := respType.(*randomtypes.QueryRandomResponse) | ||
s.Require().NoError(err) | ||
s.Require().NotNil(randomResp.Random.Value) | ||
} |
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,62 @@ | ||
package random | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cometbft/cometbft/libs/cli" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
|
||
randomcli "mods.irisnet.org/modules/random/client/cli" | ||
randomtypes "mods.irisnet.org/modules/random/types" | ||
"mods.irisnet.org/simapp" | ||
) | ||
|
||
// MsgRedelegateExec creates a redelegate message. | ||
func RequestRandomExec(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, randomcli.GetCmdRequestRandom(), args) | ||
} | ||
|
||
func QueryRandomExec(t *testing.T, | ||
network simapp.Network, | ||
clientCtx client.Context, | ||
requestID string, | ||
extraArgs ...string) *randomtypes.Random { | ||
args := []string{ | ||
requestID, | ||
fmt.Sprintf("--%s=json", cli.OutputFlag), | ||
} | ||
args = append(args, extraArgs...) | ||
|
||
response := &randomtypes.Random{} | ||
network.ExecQueryCmd(t, clientCtx, randomcli.GetCmdQueryRandom(), args, response) | ||
return response | ||
} | ||
|
||
func QueryRandomRequestQueueExec(t *testing.T, | ||
network simapp.Network, | ||
clientCtx client.Context, | ||
genHeight string, | ||
extraArgs ...string) *randomtypes.QueryRandomRequestQueueResponse { | ||
args := []string{ | ||
genHeight, | ||
fmt.Sprintf("--%s=json", cli.OutputFlag), | ||
} | ||
args = append(args, extraArgs...) | ||
|
||
response := &randomtypes.QueryRandomRequestQueueResponse{} | ||
network.ExecQueryCmd(t, clientCtx, randomcli.GetCmdQueryRandomRequestQueue(), args, response) | ||
return response | ||
} |
Oops, something went wrong.