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: simplify error messages in test cases #795

Merged

Conversation

panmini
Copy link
Contributor

@panmini panmini commented Oct 28, 2024

Pull Request: Simplify Error Messages in Test Cases

Description:

This pull request aims to improve the readability and maintainability of the error messages in the test cases.

Changes Made:

  • Removed unnecessary transformations in error message strings.
  • Replaced complex string manipulations with direct string literals for clarity.
  • Ensured that all test cases still pass after these modifications.

Related Issues:

Testing:

  • All existing tests were run and passed successfully as much as I can see by using "make format-go & make test-go" to ensure that no functionality was broken by these changes.

Additional Notes:

  • This is my first contribution to a project on Github and try to learn Go, and I'm excited to be a part of it! I look forward to your feedback.

Summary by CodeRabbit

  • Bug Fixes
    • Updated error messages in various test cases for improved clarity and consistency, including handling of invalid inputs across multiple predicates (e.g., bank balances, public keys, DIDs, hex bytes, and URIs).
    • Enhanced readability of error messages by utilizing pre-defined constants instead of inline string manipulations.

These changes ensure that error messages are more straightforward, providing better context for users when issues arise.

Copy link
Contributor

coderabbitai bot commented Oct 28, 2024

Warning

Rate limit exceeded

@panmini has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 33 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 as a PR comment. 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

Reviewing files that changed from the base of the PR and between 796e96d and 5882814.

Walkthrough

This pull request introduces modifications to various test files, focusing on the formatting of expected error messages in multiple test cases. The changes involve replacing the use of strings.Join for constructing error messages with direct string constants, enhancing clarity and consistency. The test cases affected include those in bank_test.go, crypto_test.go, did_test.go, encoding_test.go, and uri_test.go, ensuring that error messages are straightforward and more readable.

Changes

File Path Change Summary
x/logic/predicate/bank_test.go Introduced constant bench32DecodingFail for error messages in TestBank, replacing strings.Join.
x/logic/predicate/crypto_test.go Added constants badPublicKeyLength and failedToParsePublicKey in TestXVerify, replacing dynamic message construction.
x/logic/predicate/did_test.go Added constant invalidDID in TestDID to simplify error message construction.
x/logic/predicate/encoding_test.go Introduced constant hexEncodingErrorFmt in TestHexBytesPredicate, replacing dynamic message generation.
x/logic/predicate/uri_test.go Added constant invalidURLEscape in TestURIEncoded for consistent error message formatting.

Poem

🐰 In the land of code, so bright and fair,
Error messages now breathe fresh air.
No more joins, just strings that flow,
Clarity reigns, as we all know!
Hooray for tests, they shine so bright,
In the realm of logic, all feels right! 🌟


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 107b920 and 1d3421a.

📒 Files selected for processing (5)
  • x/logic/predicate/bank_test.go (3 hunks)
  • x/logic/predicate/crypto_test.go (2 hunks)
  • x/logic/predicate/did_test.go (1 hunks)
  • x/logic/predicate/encoding_test.go (1 hunks)
  • x/logic/predicate/uri_test.go (1 hunks)
🧰 Additional context used
📓 Learnings (1)
x/logic/predicate/bank_test.go (4)
Learnt from: ccamel
PR: axone-protocol/axoned#769
File: x/logic/predicate/bank_test.go:170-172
Timestamp: 2024-10-05T09:28:45.913Z
Learning: In test cases, error messages may involve transformations like `strings.Join(strings.Split(...))` because error messages used to be atoms, but now they are strings (lists of characters). This transformation was initially implemented to update the tests without much hassle.
Learnt from: ccamel
PR: axone-protocol/axoned#769
File: x/logic/predicate/bank_test.go:170-172
Timestamp: 2024-10-08T17:06:13.771Z
Learning: In test cases, error messages may involve transformations like `strings.Join(strings.Split(...))` because error messages used to be atoms, but now they are strings (lists of characters). This transformation was initially implemented to update the tests without much hassle.
Learnt from: ccamel
PR: axone-protocol/axoned#769
File: x/logic/predicate/bank_test.go:465-466
Timestamp: 2024-10-05T08:52:06.607Z
Learning: In the tests in the `x/logic/predicate/bank_test.go` file, setting the Bech32 prefix using `sdk.GetConfig().SetBech32PrefixForAccount` is acceptable, even though it modifies global configuration, because it's part of a third-party library and the team considers this state as shared across all tests.
Learnt from: ccamel
PR: axone-protocol/axoned#769
File: x/logic/predicate/bank_test.go:465-466
Timestamp: 2024-10-08T17:06:13.771Z
Learning: In the tests in the `x/logic/predicate/bank_test.go` file, setting the Bech32 prefix using `sdk.GetConfig().SetBech32PrefixForAccount` is acceptable, even though it modifies global configuration, because it's part of a third-party library and the team considers this state as shared across all tests.
🔇 Additional comments (7)
x/logic/predicate/encoding_test.go (1)

61-61: LGTM! Improved error message readability.

The simplified error message format is clearer and more direct compared to the previous strings.Join approach. This change aligns well with the PR's objective of enhancing test case maintainability.

x/logic/predicate/did_test.go (1)

82-82: LGTM! Error message simplification looks good.

The simplified error message format improves readability while maintaining the necessary error information.

Let's verify the consistency of error message formatting across related files:

✅ Verification successful

Error message format is consistent with codebase patterns

The search results confirm that the error message format in the test case follows the established pattern across the codebase:

  • Domain errors consistently use the format error(domain_error(type, value), [message], predicate)
  • The simplified error message [invalid DID] aligns with similar error messages in other test files
  • Other DID-related errors in the same file follow the same structure
  • The change maintains consistency with error handling patterns in related predicates (uri_encoded, hex_bytes, bank_balances)

The error message simplification is appropriate and maintains the established error formatting conventions throughout the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for similar error message patterns in other test files
# to ensure consistent formatting approach

# Search for error message patterns in test files
rg -g '*_test.go' 'error\(domain_error\([^)]+\),\[[^\]]+\],'

Length of output: 62


Script:

#!/bin/bash
# Let's try a broader search pattern to find error message patterns in test files
# First, let's look for domain_error patterns
rg -g '*_test.go' 'domain_error' -B 1 -A 1

# Also search for wantError patterns in test files to see the general error formatting
rg -g '*_test.go' 'wantError.*fmt\.Errorf' -B 1 -A 1

Length of output: 21822

x/logic/predicate/uri_test.go (2)

Line range hint 1-200: LGTM! Test coverage is comprehensive and well-structured.

The test suite demonstrates thorough coverage of the uri_encoded predicate, including:

  • Various URI component types (path, query_value, segment, fragment)
  • Special character handling
  • Error cases and invalid inputs
  • Type checking and variable bindings

170-170: LGTM! Error message format is now more explicit and informative.

The updated error message format provides clearer context by including the specific invalid URL escape sequence. This change aligns well with the PR's objective of improving error message readability.

Let's verify the consistency of error message formats across similar predicates:

✅ Verification successful

The previous command failed because of incorrect glob pattern. Let's try with specific paths and a broader search to understand the error message patterns in the codebase.


The previous search gave us one instance of the domain_error pattern, but we need to see more context from other test files to verify consistency. Let's first locate all test files and then search for error patterns within them.


Error message format is consistent with codebase patterns

The updated error message in uri_test.go follows the established error pattern used throughout the codebase:

  • It uses the standard error(error_type(...), [details], predicate/arity) format
  • The domain_error pattern is consistently used for encoding errors across different predicates (uri, hex, bech32)
  • The error details are consistently enclosed in square brackets
  • The predicate reference with arity is always included at the end

The change aligns perfectly with similar error messages found in other predicate tests, particularly matching the pattern seen in did_test.go, hex_test.go, and bank_test.go for domain-specific encoding errors.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for similar domain_error patterns in test files
# Expected: Similar error message patterns in other predicate tests

# Search for domain_error patterns in test files
rg -A 2 "domain_error\(" "**/*_test.go"

Length of output: 94


Script:

#!/bin/bash
# Let's try a more precise search for domain_error patterns in test files
rg -A 2 "domain_error" "x/logic/predicate/uri_test.go"

# Also search for similar error patterns in other test files within the predicate directory
rg -A 2 "error\(" "x/logic/predicate/*_test.go"

Length of output: 302


Script:

#!/bin/bash
# Let's find all test files in the x/logic/predicate directory first
fd "_test.go$" x/logic/predicate/

# Then search for error patterns in all test files
fd "_test.go$" x/logic/predicate/ -x rg -A 2 "error\("

Length of output: 7747

x/logic/predicate/crypto_test.go (2)

207-207: LGTM! Improved error message readability.

The simplified error message format is clearer while maintaining the same semantic meaning.


258-258: LGTM! Improved error message readability.

The simplified error message format is clearer while maintaining the same semantic meaning.

x/logic/predicate/bank_test.go (1)

176-176: LGTM! Error messages have been simplified effectively.

The changes successfully simplify the error messages by removing unnecessary string transformations while maintaining consistency across different test cases. This aligns well with the PR's objective of enhancing readability and maintainability.

Also applies to: 310-310, 454-454

x/logic/predicate/encoding_test.go Outdated Show resolved Hide resolved
@panmini panmini marked this pull request as draft October 28, 2024 10:26
@panmini panmini force-pushed the refactor/simplify-error-messages branch from 354a3eb to 602ff84 Compare October 28, 2024 14:27
Copy link

codecov bot commented Oct 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #795   +/-   ##
=======================================
  Coverage   42.73%   42.73%           
=======================================
  Files         111      111           
  Lines        6273     6273           
=======================================
  Hits         2681     2681           
  Misses       3468     3468           
  Partials      124      124           

@panmini panmini force-pushed the refactor/simplify-error-messages branch from 602ff84 to d0830d1 Compare October 28, 2024 14:35
@panmini panmini marked this pull request as ready for review October 28, 2024 17:55
@ccamel ccamel requested review from ccamel and amimart October 29, 2024 08:04
@ccamel ccamel self-assigned this Oct 29, 2024
@ccamel ccamel linked an issue Oct 31, 2024 that may be closed by this pull request
x/logic/predicate/bank_test.go Outdated Show resolved Hide resolved
Copy link
Member

@ccamel ccamel left a comment

Choose a reason for hiding this comment

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

Great! Thanks so much for your contribution 👍

@ccamel
Copy link
Member

ccamel commented Nov 7, 2024

@panmini Looks like your commits aren’t linked to your github identity, and they’ll need to be signed to go into our codebase. Here’s a quick way to squash everything into one signed commit:

# (on your branch)
git reset --soft $(git merge-base main HEAD)
git commit -S -m "test(logic): simplify error messages in test cases"
git push --force

This should do the trick!
Thanks! 🙏

@panmini panmini force-pushed the refactor/simplify-error-messages branch 2 times, most recently from 782c51c to 796e96d Compare November 7, 2024 16:50
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: 0

🧹 Outside diff range and nitpick comments (1)
x/logic/predicate/crypto_test.go (1)

161-164: LGTM! Consider adding documentation.

The introduction of these constants improves code readability by removing inline string manipulations. The naming is clear and descriptive.

Consider adding a brief comment explaining the comma-separated format of these error messages for future maintainers:

 const (
+    // Error messages are stored as comma-separated characters for compatibility with the error formatting system
     badPublicKeyLength     = "e,d,2,5,5,1,9,:, ,b,a,d, ,p,u,b,l,i,c, ,k,e,y, ,l,e,n,g,t,h,:, ,3,3"
     failedToParsePublicKey = "f,a,i,l,e,d, ,t,o, ,p,a,r,s,e, ,c,o,m,p,r,e,s,s,e,d, ,p,u,b,l,i,c, ,k,e,y, ,(,f,i,r,s,t, ,1,0, ,b,y,t,e,s,),:, ,0,2,1,3,c,8,4,2,6,b,e,4,7,1,e,5,5,5,0,6"
 )
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 782c51c and 796e96d.

📒 Files selected for processing (5)
  • x/logic/predicate/bank_test.go (4 hunks)
  • x/logic/predicate/crypto_test.go (3 hunks)
  • x/logic/predicate/did_test.go (2 hunks)
  • x/logic/predicate/encoding_test.go (2 hunks)
  • x/logic/predicate/uri_test.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • x/logic/predicate/bank_test.go
  • x/logic/predicate/did_test.go
  • x/logic/predicate/encoding_test.go
  • x/logic/predicate/uri_test.go
🔇 Additional comments (2)
x/logic/predicate/crypto_test.go (2)

211-211: LGTM! Error message constant is properly used.

The usage of badPublicKeyLength constant in the EdDSA test case improves readability while maintaining the same error validation.


262-262: LGTM! Error message constant is properly used.

The usage of failedToParsePublicKey constant in the ECDSA test case improves readability while maintaining the same error validation.

@panmini panmini force-pushed the refactor/simplify-error-messages branch from 796e96d to 5882814 Compare November 7, 2024 16:58
@ccamel ccamel merged commit 9b72402 into axone-protocol:main Nov 8, 2024
21 checks passed
@ccamel
Copy link
Member

ccamel commented Nov 8, 2024

Thanks @panmini ❤️

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.

🧪 Simplify error message formatting in test cases
2 participants