-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
chore(systemtests): Remove testutil dependency #21995
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,15 +4,13 @@ package systemtests | |||||
|
||||||
import ( | ||||||
"fmt" | ||||||
"net/http" | ||||||
"testing" | ||||||
|
||||||
"github.com/stretchr/testify/assert" | ||||||
"github.com/stretchr/testify/require" | ||||||
"github.com/tidwall/gjson" | ||||||
"github.com/tidwall/sjson" | ||||||
|
||||||
"github.com/cosmos/cosmos-sdk/testutil" | ||||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||||||
) | ||||||
|
||||||
func TestBankSendTxCmd(t *testing.T) { | ||||||
|
@@ -53,7 +51,7 @@ func TestBankSendTxCmd(t *testing.T) { | |||||
insufficientCmdArgs = append(insufficientCmdArgs, fmt.Sprintf("%d%s", valBalance, denom), "--fees=10stake") | ||||||
rsp = cli.Run(insufficientCmdArgs...) | ||||||
RequireTxFailure(t, rsp) | ||||||
require.Contains(t, rsp, sdkerrors.ErrInsufficientFunds.Error()) | ||||||
require.Contains(t, rsp, "insufficient funds") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use error constants instead of hardcoded strings Instead of hardcoding the error message Apply this diff to fix the issue: - require.Contains(t, rsp, "insufficient funds")
+ require.Contains(t, rsp, sdkerrors.ErrInsufficientFunds.Error()) 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Prefer checking error codes over error messages Directly asserting that the response contains "insufficient funds" is fragile because error messages can change. Instead, consider parsing the response to check the error code, which makes the test more robust and less dependent on the exact error message. You can create an assertion function similar to assertInsufficientFundsErr := func(_ assert.TestingT, gotErr error, gotOutputs ...interface{}) bool {
require.Len(t, gotOutputs, 1)
code := gjson.Get(gotOutputs[0].(string), "code")
require.True(t, code.Exists())
// Replace <expected_error_code> with the actual error code for insufficient funds
require.Equal(t, <expected_error_code>, code.Int())
return false
} Then modify your test to use this assertion: - require.Contains(t, rsp, "insufficient funds")
+ rsp = cli.WithRunErrorMatcher(assertInsufficientFundsErr).Run(insufficientCmdArgs...) |
||||||
|
||||||
// test tx bank send with unauthorized signature | ||||||
assertUnauthorizedErr := func(_ assert.TestingT, gotErr error, gotOutputs ...interface{}) bool { | ||||||
|
@@ -234,23 +232,26 @@ func TestBankGRPCQueries(t *testing.T) { | |||||
blockHeight := sut.CurrentHeight() | ||||||
|
||||||
supplyTestCases := []struct { | ||||||
name string | ||||||
url string | ||||||
headers map[string]string | ||||||
expOut string | ||||||
name string | ||||||
url string | ||||||
headers map[string]string | ||||||
expHttpCode int | ||||||
expOut string | ||||||
}{ | ||||||
{ | ||||||
"test GRPC total supply", | ||||||
supplyUrl, | ||||||
map[string]string{ | ||||||
blockHeightHeader: fmt.Sprintf("%d", blockHeight), | ||||||
}, | ||||||
http.StatusOK, | ||||||
expTotalSupplyOutput, | ||||||
}, | ||||||
{ | ||||||
"test GRPC total supply of a specific denom", | ||||||
supplyUrl + "/by_denom?denom=" + newDenom, | ||||||
map[string]string{}, | ||||||
http.StatusOK, | ||||||
specificDenomOutput, | ||||||
}, | ||||||
{ | ||||||
|
@@ -259,67 +260,75 @@ func TestBankGRPCQueries(t *testing.T) { | |||||
map[string]string{ | ||||||
blockHeightHeader: fmt.Sprintf("%d", blockHeight+5), | ||||||
}, | ||||||
http.StatusInternalServerError, | ||||||
"invalid height", | ||||||
}, | ||||||
{ | ||||||
"test GRPC total supply of a bogus denom", | ||||||
supplyUrl + "/by_denom?denom=foobar", | ||||||
map[string]string{}, | ||||||
http.StatusOK, | ||||||
// http.StatusNotFound, | ||||||
bogusDenomOutput, | ||||||
}, | ||||||
} | ||||||
|
||||||
for _, tc := range supplyTestCases { | ||||||
t.Run(tc.name, func(t *testing.T) { | ||||||
resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers) | ||||||
require.NoError(t, err) | ||||||
resp := GetRequestWithHeaders(t, tc.url, tc.headers, tc.expHttpCode) | ||||||
require.Contains(t, string(resp), tc.expOut) | ||||||
}) | ||||||
} | ||||||
|
||||||
// test denom metadata endpoint | ||||||
denomMetadataUrl := baseurl + "/cosmos/bank/v1beta1/denoms_metadata" | ||||||
dmTestCases := []GRPCTestCase{ | ||||||
dmTestCases := []RestTestCase{ | ||||||
{ | ||||||
"test GRPC client metadata", | ||||||
denomMetadataUrl, | ||||||
http.StatusOK, | ||||||
fmt.Sprintf(`{"metadatas":%s,"pagination":{"next_key":null,"total":"2"}}`, bankDenomMetadata), | ||||||
}, | ||||||
{ | ||||||
"test GRPC client metadata of a specific denom", | ||||||
denomMetadataUrl + "/uatom", | ||||||
http.StatusOK, | ||||||
fmt.Sprintf(`{"metadata":%s}`, atomDenomMetadata), | ||||||
}, | ||||||
{ | ||||||
"test GRPC client metadata of a bogus denom", | ||||||
denomMetadataUrl + "/foobar", | ||||||
http.StatusNotFound, | ||||||
`{"code":5, "message":"client metadata for denom foobar", "details":[]}`, | ||||||
}, | ||||||
} | ||||||
|
||||||
RunGRPCQueries(t, dmTestCases) | ||||||
RunRestQueries(t, dmTestCases) | ||||||
|
||||||
// test bank balances endpoint | ||||||
balanceUrl := baseurl + "/cosmos/bank/v1beta1/balances/" | ||||||
allBalancesOutput := `{"balances":[` + specificDenomOutput + `,{"denom":"stake","amount":"10000000"}],"pagination":{"next_key":null,"total":"2"}}` | ||||||
|
||||||
balanceTestCases := []GRPCTestCase{ | ||||||
balanceTestCases := []RestTestCase{ | ||||||
{ | ||||||
"test GRPC total account balance", | ||||||
balanceUrl + account1Addr, | ||||||
http.StatusOK, | ||||||
allBalancesOutput, | ||||||
}, | ||||||
{ | ||||||
"test GRPC account balance of a specific denom", | ||||||
fmt.Sprintf("%s%s/by_denom?denom=%s", balanceUrl, account1Addr, newDenom), | ||||||
http.StatusOK, | ||||||
fmt.Sprintf(`{"balance":%s}`, specificDenomOutput), | ||||||
}, | ||||||
{ | ||||||
"test GRPC account balance of a bogus denom", | ||||||
fmt.Sprintf("%s%s/by_denom?denom=foobar", balanceUrl, account1Addr), | ||||||
http.StatusOK, | ||||||
fmt.Sprintf(`{"balance":%s}`, bogusDenomOutput), | ||||||
}, | ||||||
} | ||||||
|
||||||
RunGRPCQueries(t, balanceTestCases) | ||||||
RunRestQueries(t, balanceTestCases) | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,10 +50,9 @@ func TestBankV2SendTxCmd(t *testing.T) { | |
valBalanceAfer := gjson.Get(valRaw, "balance.amount").Int() | ||
|
||
// TODO: Make DeductFee ante handler work with bank/v2 | ||
require.Equal(t, valBalanceAfer, valBalance - transferAmount) | ||
require.Equal(t, valBalanceAfer, valBalance-transferAmount) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting improvement approved, but fee deduction testing needed. The removal of the space before the subtraction operator improves code formatting. However, the TODO comment above this line indicates that fee deduction is not being properly handled or tested for bank/v2. Consider the following actions:
Would you like assistance in drafting the additional test case for fee deduction? |
||
|
||
receiverRaw := cli.CustomQuery("q", "bankv2", "balance", receiverAddr, denom) | ||
receiverBalance := gjson.Get(receiverRaw, "balance.amount").Int() | ||
require.Equal(t, receiverBalance, transferAmount) | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Use
http.StatusBadRequest
instead ofhttp.StatusInternalServerError
for client errorsIn your test cases, you're using
http.StatusInternalServerError
(500) when the client provides invalid input, such as invalid addresses or empty parameters. Since these are client-side errors due to bad requests, it's more appropriate to usehttp.StatusBadRequest
(400) to indicate the nature of the error.Apply this diff to update the status codes:
Also applies to: 684-684, 690-690, 696-696, 702-702, 764-764, 791-791