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

switch to use the helper method to create positions #1787

Merged
merged 1 commit into from
Jun 27, 2024
Merged

Conversation

jayy04
Copy link
Contributor

@jayy04 jayy04 commented Jun 26, 2024

Changelist

[Describe or list the changes made in this PR]

Test Plan

[Describe how this PR was tested (if applicable)]

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • Refactor
    • Improved test readability and centralized position creation logic by utilizing helper functions for creating asset and perpetual positions across various test files.

Copy link
Contributor

coderabbitai bot commented Jun 26, 2024

Walkthrough

The updates across multiple test files in this refactor primarily involve standardizing the creation of asset and perpetual positions using helper functions from a utility package (testutil). This modification not only simplifies the test setup code but also enhances readability and maintenance by centralizing test data structure creation.

Changes

Files/Paths Change Summary
protocol/daemons/liquidation/client/sub_task_runner_test.go Replaced direct assignments for positions with calls to testutil.CreateSingleAssetPosition and testutil.CreateSinglePerpetualPosition.
protocol/x/clob/client/cli/liquidations_cli_test.go Updated import statements; utilized new parameters in testutil.CreateSingleAssetPosition and testutil.CreateSinglePerpetualPosition.
protocol/x/clob/e2e/ -> app_test.go, conditional_orders_test.go, isolated_subaccount_orders_test.go, liquidation_deleveraging_test.go, long_term_orders_test.go, reduce_only_orders_test.go, short_term_orders_test.go, withdrawal_gating_test.go Unified position creation using testutil functions across multiple e2e test files.
protocol/x/clob/keeper/ -> deleveraging_test.go, liquidations_test.go, msg_server_place_order_test.go, process_operations_liquidations_test.go, process_operations_long_term_test.go, process_operations_test.go Refactored position initialization via testutil functions to streamline asset and perpetual position setups.
protocol/x/sending/app_test.go Introduced testutil for creating asset positions using CreateSingleAssetPosition.
protocol/x/subaccounts/ -> keeper/isolated_subaccount_test.go, lib/oimf_test.go, lib/updates_test.go, types/genesis_test.go, types/subaccount_test.go Replaced custom position creation logic with testutil functions to handle position creation more efficiently.
protocol/x/vault/keeper/ -> grpc_query_vault_test.go, msg_server_deposit_to_vault_test.go, orders_test.go Simplified position creation in tests by importing testutil and using appropriate helper functions.

Poem

To structure tests, we made a call,
With testutil, positions sprawl.
From clob to subaccounts, a unified way,
Refactoring makes the code obey.
Tests now simpler, clear, precise,
Our codebase earns a fresh device.
Centralized logic, oh what a gift,
Now test with ease, give code a lift!


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 testing code 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 testing code 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 testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @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.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f320246 and 3542530.

Files selected for processing (28)
  • protocol/daemons/liquidation/client/sub_task_runner_test.go (5 hunks)
  • protocol/x/clob/client/cli/liquidations_cli_test.go (4 hunks)
  • protocol/x/clob/e2e/app_test.go (4 hunks)
  • protocol/x/clob/e2e/conditional_orders_test.go (7 hunks)
  • protocol/x/clob/e2e/isolated_subaccount_orders_test.go (6 hunks)
  • protocol/x/clob/e2e/liquidation_deleveraging_test.go (19 hunks)
  • protocol/x/clob/e2e/long_term_orders_test.go (12 hunks)
  • protocol/x/clob/e2e/reduce_only_orders_test.go (5 hunks)
  • protocol/x/clob/e2e/short_term_orders_test.go (8 hunks)
  • protocol/x/clob/e2e/withdrawal_gating_test.go (4 hunks)
  • protocol/x/clob/keeper/deleveraging_test.go (10 hunks)
  • protocol/x/clob/keeper/liquidations_test.go (25 hunks)
  • protocol/x/clob/keeper/msg_server_place_order_test.go (3 hunks)
  • protocol/x/clob/keeper/process_operations_liquidations_test.go (7 hunks)
  • protocol/x/clob/keeper/process_operations_long_term_test.go (4 hunks)
  • protocol/x/clob/keeper/process_operations_test.go (29 hunks)
  • protocol/x/sending/app_test.go (3 hunks)
  • protocol/x/subaccounts/keeper/isolated_subaccount_test.go (6 hunks)
  • protocol/x/subaccounts/keeper/subaccount_test.go (73 hunks)
  • protocol/x/subaccounts/lib/oimf_test.go (7 hunks)
  • protocol/x/subaccounts/lib/updates_test.go (5 hunks)
  • protocol/x/subaccounts/types/genesis_test.go (6 hunks)
  • protocol/x/subaccounts/types/subaccount_test.go (6 hunks)
  • protocol/x/vault/keeper/grpc_query_vault_test.go (3 hunks)
  • protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go (2 hunks)
  • protocol/x/vault/keeper/orders_test.go (4 hunks)
  • protocol/x/vault/keeper/shares_test.go (2 hunks)
  • protocol/x/vault/keeper/vault_test.go (2 hunks)
Files not summarized due to errors (1)
  • protocol/x/subaccounts/keeper/subaccount_test.go: Error: Message exceeds token limit
Files not reviewed due to errors (4)
  • protocol/x/sending/app_test.go (no review received)
  • protocol/x/clob/e2e/liquidation_deleveraging_test.go (no review received)
  • protocol/x/clob/keeper/deleveraging_test.go (no review received)
  • protocol/x/clob/keeper/process_operations_test.go (no review received)
Files skipped from review due to trivial changes (6)
  • protocol/x/clob/e2e/app_test.go
  • protocol/x/clob/keeper/process_operations_liquidations_test.go
  • protocol/x/subaccounts/keeper/isolated_subaccount_test.go
  • protocol/x/subaccounts/lib/updates_test.go
  • protocol/x/subaccounts/types/subaccount_test.go
  • protocol/x/vault/keeper/grpc_query_vault_test.go
Additional comments not posted (52)
protocol/x/vault/keeper/vault_test.go (1)

102-105: Refactoring Approved: Use of centralized asset position creation method.

The replacement of direct asset position instantiation with testutil.CreateSingleAssetPosition centralizes the creation logic, enhancing maintainability and consistency.

protocol/x/subaccounts/types/genesis_test.go (1)

131-132: Refactor Approved: Centralized creation methods for asset and perpetual positions.

Using testutil.CreateSingleAssetPosition and testutil.CreateSinglePerpetualPosition improves the consistency and maintainability of position creation in tests. These changes are beneficial for handling complex data structures like big.Int more effectively.

Also applies to: 149-152, 168-171, 191-200, 216-220

protocol/x/subaccounts/lib/oimf_test.go (1)

202-206: Refactoring Approved: Centralized perpetual position creation in tests.

The use of testutil.CreateSinglePerpetualPosition in test cases standardizes the creation of perpetual positions, enhancing the clarity and maintainability of the tests.

Also applies to: 244-248, 280-284, 298-302, 325-329, 343-347

protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go (1)

271-274: Refactoring Approved: Centralized asset position creation in depositor setup.

Utilizing testutil.CreateSingleAssetPosition for initial depositor balances in the genesis state setup enhances the consistency and maintainability of the test setup.

protocol/x/clob/client/cli/liquidations_cli_test.go (4)

12-12: Ensure consistency in import aliasing.

The import alias appflags is introduced for clarity. This is a good practice as it helps in distinguishing similar imports and enhances code readability.


19-19: Good use of centralized utility functions.

Switching to testutil for common test functionalities is a positive change. It centralizes common logic, thereby reducing redundancy and potential errors.


151-161: Refactor: Use helper functions for position creation.

Using testutil.CreateSingleAssetPosition and testutil.CreateSinglePerpetualPosition to create positions is an excellent refactor. It ensures that position creation is consistent and potentially reduces the chance of errors.


216-216: Check integration of new order placement function.

The function MsgPlaceOrderExec from clob_testutil is used here. Ensure it integrates well with the existing system and that all its dependencies (like error handling and transaction finality) are properly managed.

protocol/x/vault/keeper/shares_test.go (4)

324-327: Optimize asset position creation using test utility functions.

The CreateSingleAssetPosition function is used correctly to create asset positions with specific values. This centralizes the creation logic and ensures consistency across tests.


Line range hint 25-56: Thorough testing of total shares management.

The function TestGetSetTotalShares effectively tests various scenarios for managing total shares, including edge cases such as setting shares to zero and negative values. The use of require statements is appropriate for unit tests.


Line range hint 58-103: Comprehensive testing of owner shares functionality.

The function TestGetSetOwnerShares is well-structured and thoroughly tests the functionality for setting and getting owner shares, including handling of negative values. The use of assertions is correctly implemented.


Line range hint 105-123: Effectively tests retrieval of all owner shares.

The TestGetAllOwnerShares function correctly tests the retrieval of all owner shares from a vault, using require.ElementsMatch to assert the correctness of the returned slice of shares.

protocol/x/clob/e2e/reduce_only_orders_test.go (2)

65-91: Consistent use of test utility functions for position creation.

The use of CreateSingleAssetPosition and CreateSinglePerpetualPosition functions ensures consistent and centralized logic for creating positions in test cases, which enhances maintainability and readability.


Line range hint 6-283: Thorough testing of reduce-only orders across multiple scenarios.

The TestReduceOnlyOrders function effectively tests various scenarios for reduce-only orders, including partial and full matches across different blocks. The use of testutil functions for setting up test cases is appropriate and enhances test reliability.

protocol/x/clob/keeper/msg_server_place_order_test.go (3)

6-6: Approved addition of math/big import.

The import of math/big is necessary for handling large integers, which aligns with the use of big.NewInt in the subsequent code.


20-20: Approved addition of testutil import.

The import of testutil is necessary for accessing helper functions like CreateSingleAssetPosition, which is used to create asset positions in a standardized way throughout the tests.


509-512: Refactor to use helper method for asset position creation.

The use of testutil.CreateSingleAssetPosition is a good practice as it standardizes the creation of asset positions across tests, ensuring consistency and reducing redundancy.

protocol/x/clob/e2e/withdrawal_gating_test.go (4)

4-4: Approved addition of math/big import.

The import of math/big is necessary for handling large integers, which aligns with the use of big.NewInt in the subsequent code.


21-21: Approved addition of testutil import.

The import of testutil is necessary for accessing helper functions like CreateSingleAssetPosition, which is used to create asset positions in a standardized way throughout the tests.


36-39: Refactor to use helper method for asset position creation.

The use of testutil.CreateSingleAssetPosition is a good practice as it standardizes the creation of asset positions across tests, ensuring consistency and reducing redundancy.


264-274: Refactor to use helper methods for asset and perpetual position creation.

Using testutil.CreateSingleAssetPosition and testutil.CreateSinglePerpetualPosition standardizes the creation of positions, which is beneficial for maintainability and readability.

protocol/daemons/liquidation/client/sub_task_runner_test.go (2)

5-5: Approved use of math/big:

The import of the math/big package is appropriate given the use of big.NewInt for handling large integer values in the test setup. This aligns well with the PR's objective to standardize the handling of large numbers.


16-16: Approved updated import for testutil:

The change from a more general import to a specific utility import (testutil "github.com/dydxprotocol/v4-chain/protocol/testutil/util") is a good practice. It makes the import more explicit and potentially avoids conflicts or ambiguity in large projects.

protocol/x/clob/e2e/isolated_subaccount_orders_test.go (3)

19-19: Good use of utility imports for enhanced test modularity.

The addition of testutil helps centralize the creation of test entities, improving readability and maintainability.


73-78: Refactored position creation using utility functions.

The use of testutil.CreateSingleAssetPosition and testutil.CreateSinglePerpetualPosition ensures consistent and error-free creation of positions. This change enhances the readability and maintainability of the test code.

Also applies to: 82-86, 95-100, 104-108, 117-122, 126-131, 140-145


Line range hint 160-652: Comprehensive test coverage for various trading scenarios.

The test cases are well-structured and cover a wide range of scenarios, including edge cases and error handling. This thorough testing ensures that the system behaves as expected under different conditions.

protocol/x/vault/keeper/orders_test.go (4)

133-136: Correct use of the helper method for position creation.
The refactoring to use testutil.CreateSingleAssetPosition is correctly implemented and improves code readability by abstracting the position creation logic.


279-282: Proper implementation of the helper method for asset position creation.
The use of testutil.CreateSingleAssetPosition here is consistent with other test functions, ensuring uniformity across the test suite.


653-657: Effective use of helper methods for both asset and perpetual position creation.
The refactoring to use testutil.CreateSinglePerpetualPosition alongside testutil.CreateSingleAssetPosition is correctly implemented, enhancing modularity and maintainability of the test code.


Line range hint 1-1: Logic review for client ID generation.
The function TestGetVaultClobOrderClientId correctly implements the logic for generating client IDs based on various parameters. The test cases are comprehensive and cover a wide range of scenarios.

protocol/x/clob/keeper/process_operations_long_term_test.go (6)

456-460: Refactor to use utility function for position creation is appropriate.

Using testutil.CreateSinglePerpetualPosition centralizes position creation logic, which is a good practice as it enhances maintainability and ensures consistency across tests. This change is in line with the PR's objective to use helper methods for creating positions.


463-467: Refactor to use utility function for position creation is appropriate.

Using testutil.CreateSinglePerpetualPosition centralizes position creation logic, which is a good practice as it enhances maintainability and ensures consistency across tests. This change is in line with the PR's objective to use helper methods for creating positions.


810-814: Refactor to use utility function for position creation is appropriate.

Using testutil.CreateSinglePerpetualPosition centralizes position creation logic, which is a good practice as it enhances maintainability and ensures consistency across tests. This change is in line with the PR's objective to use helper methods for creating positions.


817-821: Refactor to use utility function for position creation is appropriate.

Using testutil.CreateSinglePerpetualPosition centralizes position creation logic, which is a good practice as it enhances maintainability and ensures consistency across tests. This change is in line with the PR's objective to use helper methods for creating positions.


902-906: Refactor to use utility function for position creation is appropriate.

Using testutil.CreateSinglePerpetualPosition centralizes position creation logic, which is a good practice as it enhances maintainability and ensures consistency across tests. This change is in line with the PR's objective to use helper methods for creating positions.


909-913: Refactor to use utility function for position creation is appropriate.

Using testutil.CreateSinglePerpetualPosition centralizes position creation logic, which is a good practice as it enhances maintainability and ensures consistency across tests. This change is in line with the PR's objective to use helper methods for creating positions.

protocol/x/clob/e2e/short_term_orders_test.go (5)

23-23: Approved import addition for utility functions.

The addition of the testutil import is consistent with the PR's objectives and is used appropriately throughout the test file.


157-162: Refactor using utility function for perpetual position creation approved.

The use of testutil.CreateSinglePerpetualPosition enhances code maintainability by centralizing the position creation logic. The parameters and their types are correctly applied.


166-169: Refactor using utility function for asset position creation approved.

The use of testutil.CreateSingleAssetPosition is consistent with the PR's objectives to centralize and standardize position creation. The implementation and parameter usage are correct.


184-196: Consistent use of utility functions for position creation in different scenarios approved.

The repeated use of testutil.CreateSinglePerpetualPosition and testutil.CreateSingleAssetPosition across various test cases demonstrates a consistent approach to refactoring, which aligns with the PR's goals.


Line range hint 334-373: Continued approval for the use of utility functions in complex test scenarios.

The consistent application of testutil.CreateSinglePerpetualPosition and testutil.CreateSingleAssetPosition in complex test scenarios further demonstrates the benefits of this refactoring approach.

protocol/x/clob/e2e/long_term_orders_test.go (4)

26-26: Approved import changes.

The switch from dtypes to testutil/util aligns with the PR's aim to use helper methods for position creation, enhancing maintainability.


622-661: Refactor approved for position creation using helper functions.

The use of testutil.CreateSinglePerpetualPosition and testutil.CreateSingleAssetPosition improves code readability and reduces redundancy by centralizing position creation logic.


622-661: Consistency in using helper functions for position creation.

The consistent use of helper functions across different test cases ensures uniformity and maintains the integrity of test setups.


Line range hint 1542-1698: Correct application of helper methods in regression tests.

Using helper methods for position creation in regression tests ensures that the tests are robust and maintainable. The tests correctly handle orders with invalid time-in-force settings.

protocol/x/clob/e2e/conditional_orders_test.go (1)

562-572: Refactoring to use testutil helper functions for position creation

The changes correctly utilize the testutil.CreateSingleAssetPosition and testutil.CreateSinglePerpetualPosition functions to create asset and perpetual positions. This centralizes the creation logic, which enhances maintainability and consistency across tests.

Ensure that the parameters passed to these functions match the expected types and values, particularly the use of big.NewInt for quantities, to maintain precision in financial calculations.

Also applies to: 604-614, 684-694

protocol/x/clob/keeper/liquidations_test.go (4)

488-503: Refactoring to use helper methods is implemented correctly.


573-588: Consistent use of helper methods enhances maintainability.


703-718: Ensure that the modified values in the test scenarios are as intended.


1103-1118: New test scenario added. Verify the correctness of specific values used.

protocol/x/subaccounts/keeper/subaccount_test.go (2)

140-144: Refactored Perpetual Position Creation Approved

The replacement of direct instantiation with testutil.CreateSinglePerpetualPosition correctly centralizes the creation logic. The parameters passed are appropriate for the context of IsoUsd_IsolatedMarket.


411-436: Review of Perpetual and Asset Position Creation

Refactoring to use testutil.CreateSinglePerpetualPosition and testutil.CreateSingleAssetPosition is consistent with the PR's goal. Ensure that the parameters, especially the negative values, are intended as they significantly impact the business logic.

assettypes "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/client/testutil"
clob_testutil "github.com/dydxprotocol/v4-chain/protocol/x/clob/client/testutil"
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Clarify the specific utility of clob_testutil.

The review comment is valid. There are no comments or documentation explaining the specific utility of clob_testutil in the liquidations_cli_test.go file. Adding such explanations would help maintain clarity and prevent confusion.

  • File: protocol/x/clob/client/cli/liquidations_cli_test.go
  • Lines: 21
Analysis chain

Clarify the specific utility of clob_testutil.

It's important to maintain clear documentation or comments explaining the specific utilities provided by clob_testutil as opposed to testutil, especially since both are used in this file. This helps maintain clarity and prevents confusion.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Check for documentation or comments regarding `clob_testutil` usage.
rg --type python --files-with-matches 'clob_testutil' | xargs rg 'clob_testutil' --context 5

Length of output: 3231



Script:

#!/bin/bash
# Description: Check for documentation or comments regarding `clob_testutil` usage in Go files.
rg --type go --files-with-matches 'clob_testutil' | xargs rg 'clob_testutil' --context 5

Length of output: 1480

Comment on lines +288 to +298
testutil.CreateSingleAssetPosition(
0,
big.NewInt(55_000_000_000), // $55,000
),
},
PerpetualPositions: []*satypes.PerpetualPosition{
{
PerpetualId: 0,
Quantums: dtypes.NewInt(-100_000_000), // -1 BTC
FundingIndex: dtypes.NewInt(10_000),
},
testutil.CreateSinglePerpetualPosition(
0,
big.NewInt(-100_000_000), // -1 BTC
big.NewInt(10_000),
),
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor using testutil functions for position creation:

The replacement of direct instantiation with testutil.CreateSingleAssetPosition and testutil.CreateSinglePerpetualPosition is a significant improvement. This change not only reduces redundancy but also enhances the maintainability and readability of the test code by centralizing the creation logic.

Consider adding a brief comment above these utility function calls to explain what each parameter represents, especially for magic numbers like 0 or large integers like 55_000_000_000, to enhance code readability.

+ // Create a single asset position with specified parameters
  testutil.CreateSingleAssetPosition(
      0, // Asset ID
      big.NewInt(55_000_000_000), // Position value in USD
  ),

+ // Create a single perpetual position with specified parameters
  testutil.CreateSinglePerpetualPosition(
      0, // Perpetual ID
      big.NewInt(-100_000_000), // Position size in BTC
      big.NewInt(10_000), // Funding index
  ),

Also applies to: 368-378, 448-458, 526-536

@jayy04 jayy04 merged commit 2d2d0f3 into main Jun 27, 2024
19 checks passed
@jayy04 jayy04 deleted the jy/test-cleanup-3 branch June 27, 2024 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants