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(types/tx/msgs.go): add unit tests #20199

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

EmilGeorgiev
Copy link
Contributor

@EmilGeorgiev EmilGeorgiev commented Apr 27, 2024

Description

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • [ x] included the correct type prefix in the PR title
  • [x ] confirmed ! in the type prefix if API or client breaking change
  • [x ] targeted the correct branch (see PR Targeting)
  • [ x] provided a link to the relevant issue or specification
  • [ x] reviewed "Files changed" and left comments if necessary
  • [ x] included the necessary unit and integration tests
  • [x ] added a changelog entry to CHANGELOG.md
  • [x ] updated the relevant documentation or specification, including comments for documenting Go code
  • [x ] confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • Tests
    • Enhanced testing for transaction message handling and validation to ensure robustness and reliability.
    • Renamed package and updated references for improved consistency and clarity.
    • Added new methods for JSON marshaling in DummyProtoMessage1 and DummyProtoMessage2.
    • Introduced test functions in types/tx/msgs_test.go for message manipulation within transaction context.

@EmilGeorgiev EmilGeorgiev requested a review from a team as a code owner April 27, 2024 04:24
Copy link
Contributor

coderabbitai bot commented Apr 27, 2024

Walkthrough

The changes involve renaming the Go package from tx to tx_test and adjusting references within the test functions to align with this new package structure. This modification suggests a shift towards using an external test package to possibly enhance test isolation and reduce dependency on internal package structures.

Changes

File Summary of Changes
types/tx/types_test.go Renamed package from tx to tx_test. Updated references from Tx to tx.Tx in test functions. Added JSON marshaling methods to DummyProtoMessage1 and DummyProtoMessage2.
types/tx/msgs_test.go Introduced test functions for setting, getting, and unpacking messages in a transaction context.

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.

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.
  • 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.

Comment on lines 10 to 11
"github.com/cosmos/cosmos-sdk/codec/types"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"github.com/cosmos/cosmos-sdk/codec/types"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/codec/types"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@alpe alpe left a comment

Choose a reason for hiding this comment

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

Good start 💪

return
}

expected := mustAny(tc.msg)
Copy link
Contributor

Choose a reason for hiding this comment

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

This calls the same types.NewAnyWithValue method as tx.SetMsg. If you want to ensure that the Any type has the expected data then you can verify cache, url and bytes. Or Unmarshal with and without cache

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree. It will be better to test cache, url and bytes. Thank you for the suggestion

Fixed. The function "mustAny" is removed


expected := mustAny(tc.msg)
require.Equal(t, expected, actual)
require.Nil(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

let's check this first to fail fast.
nit: here and others: require.NoError() should be preferred for better output

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}

require.Equal(t, expected, actual)
require.Nil(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comments as above

btw, personal preference: tx.SetMsg is called from one place only. maybe it can be refactored to use tx.SetMsgs(txs...sdk.Msg) ([]*types.Any, error) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your idea is to remove tx.SetMsg and use tx.SetMsgs instead? tx.SetMsg is used only in one place in Cosmos-SDK, but it is exported and we don't know how many users use it their projects.

I think we should keep tx.SetMsg but we can simplified it from:

func SetMsg(msg sdk.Msg) (*types.Any, error) {
	any, err := types.NewAnyWithValue(msg)
	if err != nil {
		return nil, err
	}

	return any, nil
}

to:

func SetMsg(msg sdk.Msg) (*types.Any, error) {
	return types.NewAnyWithValue(msg)
}

err = tx.UnpackInterfaces(unpacker, tc.msgs)
require.Equal(t, tc.expErr, err != nil)
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

good test cases

require.Nil(t, err)
})
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I find the map type easier to read and you can get rid of the name field. For example:

	specs := map[string]struct{
        	msg    sdk.Msg
		expErr bool
	}{
	"Set nil Msg": {
		msg:    nil,
		expErr: true,
         },
	}
	for name, spec := range specs {
	t.Run(name, func(t *testing.T) {
	            
	})
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comment. I really like how it looks with a map. I will start using map[string]struct{} instead struct{}.

Fixed

@@ -1,9 +1,10 @@
package tx
package tx_test
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

@@ -0,0 +1,195 @@
package tx_test
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this? Unless you have to avoid cicular dependencies or want to call from the "outside", the _test package mostly just adds complexity, IMHO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like this practice because it improving package design since using the _test package forces you to test the package as if you are an external user, it encourages you to think more carefully about the design of your package’s API. You'll need to ensure that all necessary functionality is properly exposed through exported types and functions, which can lead to better, more user-friendly design. If you have problems with testing and using package probably the clients of the package will have the same problems.

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

Comment on lines +61 to +62
transaction := tx.Tx{
Body: &tx.TxBody{
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using require.PanicsWithValue to check for specific panic messages, enhancing the clarity and robustness of your panic-related tests.

Comment on lines +179 to +183
transaction := &tx.Tx{
Body: &tx.TxBody{},
AuthInfo: &tx.AuthInfo{
Fee: &tx.Fee{
GasLimit: tx.MaxGasWanted,
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding more test cases to cover scenarios with multiple signers and invalid signer addresses to ensure comprehensive coverage of the GetSigners method.

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