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

refactor: improve unit test #2914

Merged
merged 3 commits into from
Mar 18, 2024
Merged

refactor: improve unit test #2914

merged 3 commits into from
Mar 18, 2024

Conversation

dreamer-zq
Copy link
Contributor

@dreamer-zq dreamer-zq commented Mar 13, 2024

Summary by CodeRabbit

  • Refactor
    • Updated integration and unit tests across various modules to use a new testing utility, improving consistency and maintainability.
    • Streamlined the process for setting up test environments and executing test transactions and commands.
  • Chores
    • Introduced new utility functions for creating and configuring ABCI applications for testing, enhancing test infrastructure.
    • Simplified application state export for genesis files, aiding in test setup and execution.

Copy link

coderabbitai bot commented Mar 13, 2024

Walkthrough

The overarching change involves transitioning from the simapp package to apptestutil and testutil for setting up and executing integration and unit tests across various modules. This shift aims to streamline testing by using more specialized utilities, enhancing the testing framework's efficiency and clarity.

Changes

Files Change Summary
.../guardian/client/cli/cli_test.go Replaced simapp with apptestutil for test setup and command execution.
.../guardian/client/testutil/test_helpers.go Updated functions for entity management to use apptestutil.
.../guardian/genesis_test.go, .../guardian/keeper/keeper_test.go Changed simapp import to testutil; updated setup functions.
.../mint/abci_test.go, .../mint/client/cli/cli_test.go, .../mint/client/rest/grpc_query_test.go, .../mint/keeper/keeper_test.go, .../mint/simulation/decoder_test.go Replaced simapp with testutil or apptestutil; updated setup and encoding functions.
testutil/app.go, testutil/export.go, testutil/genesis.go, testutil/test_helpers.go Introduced and updated testutil package functionalities for ABCI app creation, state export, and test setup.

"In the garden of code, where the green tests grow,
🐰 A rabbit hopped in, with a mission in tow.
From simapp to testutil, a leap so bold,
Streamlining the tests, a story retold.
With every hop and a skip, the garden did change,
Making testing more fun, and far less strange."
🌱🚀

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

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository 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 and a 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. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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.

@dreamer-zq dreamer-zq requested a review from a team March 13, 2024 06:42
@dreamer-zq dreamer-zq self-assigned this Mar 13, 2024
Copy link

@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 65dccb0 and aee2a7d.
Files ignored due to path filters (1)
  • go.mod is excluded by: !**/*.mod
Files selected for processing (13)
  • modules/guardian/client/cli/cli_test.go (3 hunks)
  • modules/guardian/client/testutil/test_helpers.go (3 hunks)
  • modules/guardian/genesis_test.go (2 hunks)
  • modules/guardian/keeper/keeper_test.go (2 hunks)
  • modules/mint/abci_test.go (2 hunks)
  • modules/mint/client/cli/cli_test.go (2 hunks)
  • modules/mint/client/rest/grpc_query_test.go (1 hunks)
  • modules/mint/keeper/keeper_test.go (1 hunks)
  • modules/mint/simulation/decoder_test.go (1 hunks)
  • testutil/app.go (1 hunks)
  • testutil/export.go (1 hunks)
  • testutil/genesis.go (2 hunks)
  • testutil/test_helpers.go (9 hunks)
Check Runs (1)
Test Units completed (1)
Additional comments: 30
testutil/export.go (1)
  • 7-13: The function ExportAppStateAndValidators correctly delegates the export functionality to app.IrisApp.ExportAppStateAndValidators. However, the forZeroHeight parameter is hardcoded to false when passed to IrisApp.ExportAppStateAndValidators. Ensure this is the intended behavior, as it ignores the forZeroHeight argument provided to the function.
testutil/genesis.go (1)
  • 1-7: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-10]

The GenesisState type is well-defined and uses json.RawMessage for flexibility in handling various module genesis states. This approach is consistent with best practices for handling genesis information in a blockchain context.

modules/guardian/genesis_test.go (2)
  • 16-16: The import statement has been correctly updated to use testutil instead of simapp, aligning with the PR's objective to refactor the testing framework.
  • 28-28: The SetupTest method correctly utilizes testutil.Setup for initializing the test environment. This change is consistent with the PR's goal of improving the testing framework's maintainability and scalability.
modules/mint/simulation/decoder_test.go (3)
  • 11-11: The update to use testutil.MakeCodecs() for codec creation aligns with the PR's objective to refactor the testing framework and improve maintainability.
  • 21-22: The change to use ec.Marshaler for decoding store operations is appropriate and follows best practices for handling encoding and decoding in Cosmos SDK-based applications.
  • 26-26: The marshaling of minter using ec.Marshaler.MustMarshal instead of cdc.MustMarshal is a good practice, ensuring consistency with the updated encoding configuration.
modules/mint/abci_test.go (2)
  • 15-15: The import statement has been correctly updated to use apptestutil instead of simapp, aligning with the PR's objective to refactor the testing framework.
  • 32-33: The createTestApp function now correctly returns an *apptestutil.AppBuilder, which is consistent with the updated testing utilities. Ensure that all tests using this function have been updated accordingly.
Verification successful

The verification process, based on the provided script output, confirms that within modules/mint/abci_test.go, the usage of createTestApp has been updated to handle the new return type correctly. However, it's important to note that the verification was limited to this specific file and did not cover potential usages in other parts of the codebase. Therefore, while the review comment is accurate based on the available information, a comprehensive verification across the entire codebase was not performed.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all usages of createTestApp have been updated to handle the new return type.
rg --type go 'createTestApp\('

Length of output: 210

modules/mint/client/cli/cli_test.go (2)
  • 12-12: The import statement has been correctly updated to use testutil instead of simapp, aligning with the PR's objective to refactor the testing framework.
  • 25-25: The update to use testutil.NewConfig() for network configuration setup is appropriate and ensures consistency with the updated testing utilities.
modules/mint/client/rest/grpc_query_test.go (1)
  • 14-14: The commented-out code indicates that this test suite may be temporarily disabled or awaiting updates. The replacement of simapp with testutil in the import path, even in commented-out code, aligns with the PR's objective. However, ensure that this test suite is either updated or re-enabled as appropriate to maintain test coverage.
testutil/app.go (3)
  • 23-48: The AppBuilder type and its build method provide a flexible way to construct an IrisApp instance for testing. This approach enhances modularity and maintainability by allowing tests to customize the app's initialization. Ensure that the appOpts and baseAppOptions parameters are used appropriately in all tests utilizing this builder pattern.
  • 51-54: The MakeCodecs function correctly returns the application's encoding configuration, facilitating consistent encoding and decoding in tests. This utility function is a good practice for ensuring tests use the correct codec configuration.
  • 56-59: The DefaultGenesis function provides a convenient way to obtain a default genesis state for tests. This approach is consistent with best practices for initializing test environments with a known state.
modules/guardian/client/testutil/test_helpers.go (3)
  • 14-23: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [20-33]

The transition from simapp to apptestutil for executing the CreateSuper command is correctly implemented. This aligns with the PR's objective to improve the testing framework by utilizing more specialized utilities.

  • 30-39: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [36-49]

The DeleteSuperExec function correctly uses apptestutil for executing the DeleteSuper command. This change is consistent with the PR's goal of enhancing test maintainability and efficiency.

  • 46-55: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [52-64]

The QuerySupersExec function remains unchanged and correctly utilizes clitestutil.ExecTestCLICmd for querying supers. This is appropriate as the function is querying data rather than executing transactions, which aligns with the use of clitestutil.

modules/guardian/keeper/keeper_test.go (2)
  • 18-18: The import statement correctly replaces simapp with testutil. This change is necessary for the transition to a more modular and maintainable testing setup.
  • 40-44: The SetupTest function now initializes the test environment using testutil.Setup, which is consistent with the PR's objective to enhance the testing framework. This change improves the test setup process by utilizing the newly introduced AppBuilder.
modules/mint/keeper/keeper_test.go (2)
  • 16-16: The import statement correctly replaces simapp with apptestutil. This change aligns with the PR's goal of transitioning to a more efficient and scalable testing setup.
  • 24-28: The SetupTest function now initializes the test environment using apptestutil.Setup, which is consistent with the PR's objective. This change streamlines the test setup process and enhances the overall testing framework.
modules/guardian/client/cli/cli_test.go (3)
  • 21-21: The import statement correctly replaces simapp with apptestutil. This change is part of the PR's objective to utilize more specialized utilities for testing, enhancing the maintainability and efficiency of tests.
  • 35-41: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [38-87]

The SetupSuite function correctly uses apptestutil.NewConfig for setting up the test configuration. This change is consistent with the PR's goal of improving the testing framework by employing more efficient utilities.

  • 93-93: The execution of the bankcli.NewSendTxCmd() command correctly utilizes apptestutil.ExecTxCmdWithResult. This change aligns with the PR's objective to streamline the testing approach by using apptestutil for command execution.
testutil/test_helpers.go (5)
  • 1-1: The package name change from simapp to testutil is correctly implemented. This change reflects the PR's objective to enhance the testing framework by introducing more specialized testing utilities.
  • 38-48: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [45-68]

The Setup function has been correctly updated to return an AppBuilder instead of SimApp. This change is part of the PR's objective to improve the testing framework by utilizing more modular and maintainable testing utilities.

  • 74-87: The NewConfig function has been modified to use app.RegisterEncodingConfig() and app.ModuleBasics.DefaultGenesis() instead of the previous setup methods. This change aligns with the PR's goal of enhancing the testing utilities for a more efficient and scalable testing setup.
  • 97-115: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [100-136]

The SetupWithGenesisValSet function now returns an AppBuilder instead of SimApp, consistent with the PR's objective to improve the testing framework. This change enhances the test setup process by utilizing the newly introduced AppBuilder.

  • 243-260: The NewTestNetworkFixture function has been refactored to use MakeCodecs() and DefaultGenesis(). This change is part of the PR's objective to enhance the testing framework by employing more efficient and modular testing utilities.

Copy link

@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 aee2a7d and ece68a5.
Files selected for processing (9)
  • modules/guardian/client/cli/cli_test.go (3 hunks)
  • modules/guardian/client/testutil/test_helpers.go (3 hunks)
  • modules/guardian/genesis_test.go (2 hunks)
  • modules/guardian/keeper/grpc_query_test.go (1 hunks)
  • modules/guardian/keeper/keeper_test.go (2 hunks)
  • modules/mint/abci_test.go (2 hunks)
  • modules/mint/keeper/keeper_test.go (1 hunks)
  • testutil/app.go (1 hunks)
  • testutil/test_helpers.go (8 hunks)
Check Runs (1)
Test Units completed (1)
Files skipped from review as they are similar to previous changes (7)
  • modules/guardian/client/cli/cli_test.go
  • modules/guardian/client/testutil/test_helpers.go
  • modules/guardian/genesis_test.go
  • modules/guardian/keeper/keeper_test.go
  • modules/mint/abci_test.go
  • modules/mint/keeper/keeper_test.go
  • testutil/app.go
Additional comments: 7
modules/guardian/keeper/grpc_query_test.go (2)
  • 16-17: The transition to using suite.ctx and suite.ifr for setting up the query helper and registering the query server aligns well with the PR objectives of enhancing test maintainability and efficiency. Ensure similar consistency is maintained across other tests.
  • 23-23: Adding a guardian using suite.keeper.AddSuper and then querying to verify the addition demonstrates a clear and effective test of the keeper's functionality. This approach is commendable for its simplicity and directness.
testutil/test_helpers.go (5)
  • 1-1: Changing the package name to testutil is a positive step towards clarifying the purpose of this package as a utility for testing. This aligns with the PR objectives of enhancing the testing framework's efficiency and maintainability.
  • 38-48: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [44-68]

The CreateApp function has been refactored to utilize the new testing utilities, which is a significant improvement. It simplifies the process of creating a test app with a genesis validator set and accounts. Consider adding more inline comments explaining the steps for future maintainability.

  • 74-86: The NewConfig function's use of app.RegisterEncodingConfig() and setting up the application constructor is a clear and efficient way to configure the test network. This approach enhances the test setup's flexibility and maintainability.
  • 134-141: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [137-210]

The genesisStateWithValSet function demonstrates a thorough approach to setting up a genesis state with a validator set, genesis accounts, and balances. This function is crucial for tests that require a specific initial state. Ensure that the error handling is robust and consider adding more detailed comments for complex sections.

  • 237-266: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [262-310]

The ExecCommand and WaitForTx utility functions are well-implemented and provide a convenient way to execute commands and wait for transactions in tests. These functions contribute to the overall goal of making the testing framework more efficient and maintainable.

Copy link

@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 ece68a5 and de969d9.
Files selected for processing (1)
  • app/sim_test.go (12 hunks)
Additional comments: 7
app/sim_test.go (7)
  • 102-102: The introduction of the createApp function to simplify the initialization of the IrisApp instance is a positive change. It consolidates app creation logic, improving code readability and reducing redundancy.
  • 153-153: Reusing the createApp function in TestAppImportExport maintains consistency in app initialization across tests. This is a good practice for maintainability.
  • 199-199: Using createApp for initializing a new app instance in the import/export test scenario is consistent with the refactor's goal. This ensures uniformity in app creation across different test cases.
  • 223-235: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [226-251]

The explicit listing of store keys and prefixes for comparison is a thorough approach to ensure data integrity across app exports and imports. However, ensure that this list is kept up-to-date as new modules or store keys are added to the application.

  • 318-318: The reuse of createApp in TestAppSimulationAfterImport further demonstrates the utility of centralizing app initialization logic, making the test setup more consistent and easier to manage.
  • 369-369: Again, the consistent use of createApp for app initialization in different testing scenarios, including after importing genesis, aligns with the refactor's objectives. This consistency is beneficial for maintainability.
  • 478-497: The createApp function is well-designed, providing flexibility through optional baseAppOptions while ensuring essential configurations like SetChainID are applied. This approach enhances modularity and maintainability.

app/sim_test.go Show resolved Hide resolved
app/sim_test.go Show resolved Hide resolved
@mitch1024 mitch1024 merged commit 90d0be8 into master Mar 18, 2024
3 checks passed
@mitch1024 mitch1024 deleted the refactor-test branch March 18, 2024 01:32
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.

None yet

3 participants