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

test(devgas-cli): CLI tests for devgas txs #1728

Merged
merged 3 commits into from
Dec 28, 2023
Merged

Conversation

Unique-Divine
Copy link
Member

@Unique-Divine Unique-Divine commented Dec 19, 2023

Purpose / Abstract

Summary by CodeRabbit

  • Tests
    • Introduced CLI tests for transaction commands in the devgas module, enhancing reliability and functionality.

@Unique-Divine Unique-Divine requested a review from a team as a code owner December 19, 2023 06:45
Copy link
Contributor

coderabbitai bot commented Dec 19, 2023

Warning

Rate Limit Exceeded

@Unique-Divine has exceeded the limit for the number of files or commits that can be reviewed per hour. Please wait 6 minutes and 57 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between c4c24ca and a5ca911.

Walkthrough

The latest update introduces a suite of CLI tests for the devgas module, focusing on transaction commands. It provides a structured testing framework, including setup and execution of tests for registering, canceling, and updating fee shares within the devgas-cli module.

Changes

File Path Change Summary
CHANGELOG.md Added entry for pull request #1728, introducing CLI tests.
.../v1/client/cli/cli_test.go Added a test suite with functions to test CLI commands for the devgas module.

Assessment against linked issues

Objective Addressed Explanation
Add CLI tests for devgas module (#1545)

🐇✨
In the code's burrow, tests now leap and bound,
Commands of devgas, in CLI tests are found.
Register, cancel, update, all in the suite,
A rabbit’s work, now thorough and complete. 🌟🚀

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • You can directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • You can tag CodeRabbit on specific lines of code or entire files in the PR by tagging @coderabbitai in a comment. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • You can tag @coderabbitai in a PR comment and ask questions about the PR and the codebase. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid.
    • @coderabbitai read the files in the src/scheduler package and generate README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d33dfe9 and 48bf7c6.
Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • x/devgas/v1/client/cli/cli_test.go (1 hunks)
Additional comments: 8
x/devgas/v1/client/cli/cli_test.go (8)
  • 1-26: The import section includes packages from both the Cosmos SDK and the CometBFT, which suggests that the project is using a fork of the Tendermint consensus engine. It's important to ensure that the CometBFT fork is compatible with the Cosmos SDK version used in the project.

  • 28-42: The CLITestSuite is well-structured, providing a clear setup for CLI tests. It includes a SetupSuite method to initialize the necessary components for the tests, which is a good practice for maintainability and reusability.

  • 71-78: The commonTxArgs function provides a centralized way to define common flags for CLI commands, which is a good practice for consistency and reduces the risk of errors when constructing CLI commands in tests.

  • 81-90: The TestCase struct and its method NewCtx are well-designed, encapsulating the data needed for a test case and providing a method to generate a new context. This design promotes test readability and maintainability.

  • 92-110: The Run method in the TestCase struct is a good example of encapsulating the execution logic of a test case. It uses the s.Run method from the suite package to run the subtests, which is a standard practice in Go for structuring table-driven tests.

  • 112-152: The TestCmdRegisterFeeShare function tests the register command with various scenarios, including both happy and sad paths. The use of table-driven tests to cover different cases is a good practice for thorough testing.

  • 154-185: The TestCmdCancelFeeShare function follows the same pattern as TestCmdRegisterFeeShare, testing the cancel command with different inputs. The consistency in test structure across different commands is good for maintainability.

  • 187-227: The TestCmdUpdateFeeShare function tests the update command, ensuring that the command behaves correctly for various inputs. The structure and approach are consistent with the other test functions, which is good for code readability.

Copy link

codecov bot commented Dec 19, 2023

Codecov Report

Attention: 23 lines in your changes are missing coverage. Please review.

Comparison is base (d33dfe9) 73.89% compared to head (a5ca911) 73.45%.
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1728      +/-   ##
==========================================
- Coverage   73.89%   73.45%   -0.44%     
==========================================
  Files         192      195       +3     
  Lines       15142    15451     +309     
==========================================
+ Hits        11189    11350     +161     
- Misses       3297     3439     +142     
- Partials      656      662       +6     
Files Coverage Δ
cmd/nibid/cmd/root.go 80.51% <100.00%> (+0.10%) ⬆️
cmd/nibid/cmd/decode_base64.go 72.28% <72.28%> (ø)

... and 2 files with indirect coverage changes

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 48bf7c6 and c4c24ca.
Files selected for processing (1)
  • x/devgas/v1/client/cli/cli_test.go (1 hunks)
Additional comments: 5
x/devgas/v1/client/cli/cli_test.go (5)
  • 28-38: The CLITestSuite struct is well-defined with necessary fields for testing. It includes a keyring for account management, encoding configurations, and contexts for client and base operations, as well as a test account.

  • 71-78: The commonTxArgs function provides a clear and concise way to define common transaction arguments. This is a good use of a helper function to avoid repetition in test cases.

  • 113-153: The TestCmdRegisterFeeShare method defines test cases for the register command. The test cases are well-structured, and the use of table-driven tests is a good practice. Ensure that the error messages are specific enough to catch the intended errors.

Verification successful

The shell scripts intended to verify the specificity and accuracy of the error messages in the test cases did not produce any output. This means that the error messages such as 'decoding bech32 failed', 'invalid contract address', and 'invalid withdraw address' were not found elsewhere in the codebase, or the pattern used for ast-grep did not match any code.

Given this result, it's not possible to confirm whether the error messages used in the test cases are consistent with the actual error messages produced by the code under test. To proceed, we can attempt a different strategy to verify the error messages by looking for the functions that might produce these errors and checking if the error messages align with the test expectations.

Let's generate a new script to search for functions that could be related to the error messages mentioned in the test cases.


The new context from the shell scripts provides instances where the error messages 'decoding bech32 failed', 'invalid contract address', and 'invalid withdraw address' are used in the codebase. These instances are found in test files and in the actual implementation code, which suggests that the error messages in the TestCmdRegisterFeeShare method are consistent with the error handling in the rest of the codebase.

Here are the findings:

  • The error message "decoding bech32 failed" is used in various test cases across the codebase, indicating that it is a standard error message for bech32 decoding issues.
  • The error message "invalid contract address" is used in the msg_server.go and msg.go files when there is an error parsing the contract address from a bech32 string, which aligns with the test case expectation.
  • The error message "invalid withdraw address" is used in the query.go and msg.go files when there is an error parsing the withdraw address from a bech32 string, which also aligns with the test case expectation.

Based on this information, we can conclude that the error messages used in the test cases are specific and accurate, reflecting the actual error messages that would be produced by the code under test.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the error messages used in the test cases are specific and accurate.
rg --type go --files-with-matches 'decoding bech32 failed' | xargs -L1 ast-grep --lang go --pattern '$_("$ERROR_MESSAGE")'
rg --type go --files-with-matches 'invalid contract address' | xargs -L1 ast-grep --lang go --pattern '$_("$ERROR_MESSAGE")'
rg --type go --files-with-matches 'invalid withdraw address' | xargs -L1 ast-grep --lang go --pattern '$_("$ERROR_MESSAGE")'

Length of output: 378



Script:

#!/bin/bash
# Search for functions that might produce the error messages mentioned in the test cases.
rg --type go 'decoding bech32 failed' -C 3
rg --type go 'invalid contract address' -C 3
rg --type go 'invalid withdraw address' -C 3

Length of output: 12962

* 155-186: The `TestCmdCancelFeeShare` method is similar to `TestCmdRegisterFeeShare` and follows the same pattern for defining test cases. The structure and approach are consistent with the previous test method.
  • 188-228: The TestCmdUpdateFeeShare method also uses table-driven tests for the update command. The test cases are consistent with the other test methods, and the approach is correct. As with the other test methods, ensure that the error messages are specific and meaningful.

Comment on lines +44 to +69
// Runs once before the entire test suite.
func (s *CLITestSuite) SetupSuite() {
s.encCfg = testutilmod.MakeTestEncodingConfig(devgas.AppModuleBasic{})
s.keyring = keyring.NewInMemory(s.encCfg.Codec)
s.baseCtx = sdkclient.Context{}.
WithKeyring(s.keyring).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(sdktestutilcli.MockTendermintRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(sdkclient.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")

var outBuf bytes.Buffer
ctxGen := func() sdkclient.Context {
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
c := sdktestutilcli.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
}
s.clientCtx = ctxGen().WithOutput(&outBuf)

testAccs := sdktestutil.CreateKeyringAccounts(s.T(), s.keyring, 1)
s.testAcc = testAccs[0]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The SetupSuite method correctly sets up the encoding configuration, keyring, base context, and client context. It also initializes a test account. However, the error from Marshal is ignored, which could lead to silent failures if marshaling fails.

- bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
+ bz, err := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
+ s.Require().NoError(err, "marshaling TxResponse should not fail")

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// Runs once before the entire test suite.
func (s *CLITestSuite) SetupSuite() {
s.encCfg = testutilmod.MakeTestEncodingConfig(devgas.AppModuleBasic{})
s.keyring = keyring.NewInMemory(s.encCfg.Codec)
s.baseCtx = sdkclient.Context{}.
WithKeyring(s.keyring).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(sdktestutilcli.MockTendermintRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(sdkclient.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")
var outBuf bytes.Buffer
ctxGen := func() sdkclient.Context {
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
c := sdktestutilcli.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
}
s.clientCtx = ctxGen().WithOutput(&outBuf)
testAccs := sdktestutil.CreateKeyringAccounts(s.T(), s.keyring, 1)
s.testAcc = testAccs[0]
}
// Runs once before the entire test suite.
func (s *CLITestSuite) SetupSuite() {
s.encCfg = testutilmod.MakeTestEncodingConfig(devgas.AppModuleBasic{})
s.keyring = keyring.NewInMemory(s.encCfg.Codec)
s.baseCtx = sdkclient.Context{}.
WithKeyring(s.keyring).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(sdktestutilcli.MockTendermintRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(sdkclient.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")
var outBuf bytes.Buffer
ctxGen := func() sdkclient.Context {
bz, err := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
s.Require().NoError(err, "marshaling TxResponse should not fail")
c := sdktestutilcli.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
}
s.clientCtx = ctxGen().WithOutput(&outBuf)
testAccs := sdktestutil.CreateKeyringAccounts(s.T(), s.keyring, 1)
s.testAcc = testAccs[0]
}

Comment on lines +81 to +111
type TestCase struct {
name string
args []string
extraArgs []string
wantErr string
}

func (tc TestCase) NewCtx(s *CLITestSuite) sdkclient.Context {
return s.baseCtx
}

func (tc TestCase) Run(s *CLITestSuite) {
s.Run(tc.name, func() {
ctx := svrcmd.CreateExecuteContext(context.Background())

cmd := cli.NewTxCmd()
cmd.SetContext(ctx)
args := append(tc.args, commonTxArgs()...)
cmd.SetArgs(append(args, tc.extraArgs...))

s.Require().NoError(sdkclient.SetCmdClientContextHandler(tc.NewCtx(s), cmd))

err := cmd.Execute()
if tc.wantErr != "" {
s.Require().Error(err)
s.ErrorContains(err, tc.wantErr)
return
}
s.Require().NoError(err)
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The TestCase struct and its associated methods are well-structured for defining and running test cases. The Run method properly encapsulates the test logic within a named subtest. However, the error message check in line 106 should be refined to ensure that it is not too broad and accurately captures the expected error.

- s.ErrorContains(err, tc.wantErr)
+ s.Require().ErrorContains(err, tc.wantErr, "the error message should contain the expected text")

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
type TestCase struct {
name string
args []string
extraArgs []string
wantErr string
}
func (tc TestCase) NewCtx(s *CLITestSuite) sdkclient.Context {
return s.baseCtx
}
func (tc TestCase) Run(s *CLITestSuite) {
s.Run(tc.name, func() {
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd := cli.NewTxCmd()
cmd.SetContext(ctx)
args := append(tc.args, commonTxArgs()...)
cmd.SetArgs(append(args, tc.extraArgs...))
s.Require().NoError(sdkclient.SetCmdClientContextHandler(tc.NewCtx(s), cmd))
err := cmd.Execute()
if tc.wantErr != "" {
s.Require().Error(err)
s.ErrorContains(err, tc.wantErr)
return
}
s.Require().NoError(err)
})
}
type TestCase struct {
name string
args []string
extraArgs []string
wantErr string
}
func (tc TestCase) NewCtx(s *CLITestSuite) sdkclient.Context {
return s.baseCtx
}
func (tc TestCase) Run(s *CLITestSuite) {
s.Run(tc.name, func() {
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd := cli.NewTxCmd()
cmd.SetContext(ctx)
args := append(tc.args, commonTxArgs()...)
cmd.SetArgs(append(args, tc.extraArgs...))
s.Require().NoError(sdkclient.SetCmdClientContextHandler(tc.NewCtx(s), cmd))
err := cmd.Execute()
if tc.wantErr != "" {
s.Require().Error(err)
s.Require().ErrorContains(err, tc.wantErr, "the error message should contain the expected text")
return
}
s.Require().NoError(err)
})
}

@Unique-Divine Unique-Divine merged commit 022600b into main Dec 28, 2023
15 checks passed
@Unique-Divine Unique-Divine deleted the realu/devgas-cli branch December 28, 2023 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

test(devgas): Add CLI tests
2 participants