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(ecocredit): GetParamSet -> Get #1064

Merged
merged 14 commits into from
May 11, 2022
Merged

Conversation

technicallyty
Copy link
Contributor

@technicallyty technicallyty commented Apr 27, 2022

Description

Closes: #996

  • refactors GetParamSet calls to use the single Get

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

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • 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 ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@technicallyty technicallyty marked this pull request as ready for review April 28, 2022 20:50
@codecov
Copy link

codecov bot commented Apr 28, 2022

Codecov Report

Merging #1064 (cc99100) into master (f653b63) will decrease coverage by 0.09%.
The diff coverage is 100.00%.

❗ Current head cc99100 differs from pull request most recent head 7527dda. Consider uploading reports for the commit 7527dda to get more accurate results

@@            Coverage Diff             @@
##           master    #1064      +/-   ##
==========================================
- Coverage   68.10%   68.00%   -0.10%     
==========================================
  Files         210      207       -3     
  Lines       20968    20855     -113     
==========================================
- Hits        14280    14183      -97     
+ Misses       5375     5367       -8     
+ Partials     1313     1305       -8     
Flag Coverage Δ
experimental-codecov 67.93% <100.00%> (-0.03%) ⬇️
stable-codecov 61.72% <100.00%> (-0.15%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Copy link
Member

@ryanchristo ryanchristo left a comment

Choose a reason for hiding this comment

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

A couple concerns, one with using panic and another with abstracting the expect calls.

x/ecocredit/expected_keepers.go Show resolved Hide resolved
Comment on lines -25 to -30
minFee := sdk.NewCoins(sdk.NewCoin("regen", sdk.NewInt(100)))

// no fee specified should fail
gmAny := gomock.Any()
s.paramsKeeper.EXPECT().GetParamSet(gmAny, gmAny).Do(func(any interface{}, p *core.Params) {
p.BasketFee = minFee
}).Times(2)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we should abstract the expect call. If anything, we should be more specific with the values passed and avoid using gomock.Any() whenever possible. This looks like a nice improvement but I think we lose some of the value of using mocks. I realize the abstracted method currently provides the same assurance as what was before but if we want to be more specific, we should use the expect call directly.

Comment on lines 11 to 43
// ExpectParamGet is a helper function that sets up an expected mock call for the provided type.
// Once we switch to Go 1.18+ we can switch this impl to be generic:
// func ExpectParamGet[T any](obj *T, paramKeeper *mocks.MockParamKeeper, times int) {
// gmAny := gomock.Any()
// paramKeeper.EXPECT().Get(gmAny, gmAny, gmAny).Do(func(_, _ any, param *T) {
// *param = *obj
// }).Times(times)
// }
func ExpectParamGet(obj interface{}, paramKeeper *mocks.MockParamKeeper, times int) {
gmAny := gomock.Any()
switch obj.(type) {
case *[]string:
s := obj.(*[]string)
paramKeeper.EXPECT().Get(gmAny, gmAny, gmAny).Do(func(_, _ interface{}, param *[]string) {
*param = *s
}).Times(times)
case *sdk.Coins:
coins := obj.(*sdk.Coins)
paramKeeper.EXPECT().Get(gmAny, gmAny, gmAny).Do(func(_, _ interface{}, param *sdk.Coins) {
*param = *coins
}).Times(times)
case *bool:
b := obj.(*bool)
paramKeeper.EXPECT().Get(gmAny, gmAny, gmAny).Do(func(_, _ interface{}, param *bool) {
*param = *b
}).Times(times)
case *[]*core.AskDenom:
askDenoms := obj.(*[]*core.AskDenom)
paramKeeper.EXPECT().Get(gmAny, gmAny, gmAny).Do(func(_, _ interface{}, param *[]*core.AskDenom) {
*param = *askDenoms
}).Times(times)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should abstract the expect call. We should try to avoid using gomock.Any() and be more specific with the values passed whenever possible. This may be convenient but gomock.Any() should be used sparingly and we should be as specific as we can when we can when writing tests.

For example, I started using specific values with expect calls in the acceptance tests I'm currently drafting:

s.bankKeeper.EXPECT().
MintCoins(s.sdkCtx, basket.BasketSubModuleName, coins).
Return(nil).AnyTimes()
s.bankKeeper.EXPECT().
SendCoinsFromModuleToAccount(s.sdkCtx, basket.BasketSubModuleName, s.addr, coins).
Return(nil).AnyTimes()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this method is specific to params (ExpectParamGet). the only more specific i can really get is the param key ( and maybe the context, but incorrect contexts would be caught by compiler anyway). i can go ahead and add key []byte as an argument

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ryanchristo i think you may have missed this reply 👋🏻

Copy link
Member

Choose a reason for hiding this comment

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

the only more specific i can really get is the param key

Nope. I saw it. We don't need to use gomock.Any(). See #1064 (comment).

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should be adding more parameters. Might as well use the expect call directly if we add any more.

Copy link
Contributor Author

@technicallyty technicallyty May 4, 2022

Choose a reason for hiding this comment

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

Might as well use the expect call directly if we add any more.

it's only 4 params, i'd much prefer that over the 4+ lines from direct expect call. the only thing thats different at this point is the expected context, but i dont really see that as necessary or valuable, as its not used, and the app wont even compile if we pass anything other than sdk.Context to the acutal params.Get call in the keeper methods.

@technicallyty technicallyty requested a review from aleem1314 May 3, 2022 19:33
@technicallyty
Copy link
Contributor Author

@aleem1314 could i get a review here 🙏🏻

Copy link
Member

@ryanchristo ryanchristo left a comment

Choose a reason for hiding this comment

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

I'm still not sure about abstracting the expect call and think we should be as specific as possible when writing tests. I included a working example without using gomock.Any().

x/ecocredit/server/core/create_class.go Outdated Show resolved Hide resolved
x/ecocredit/server/utils/test_utils.go Outdated Show resolved Hide resolved
s.paramsKeeper.EXPECT().GetParamSet(gmAny, gmAny).Do(func(any interface{}, p *core.Params) {
p.BasketFee = fee
}).Times(1)
utils.ExpectParamGet(&basketFees, s.paramsKeeper, core.KeyBasketCreationFee, 1)
Copy link
Member

@ryanchristo ryanchristo May 4, 2022

Choose a reason for hiding this comment

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

Here's a working example without using gomock.Any() and using a specific time:

Suggested change
utils.ExpectParamGet(&basketFees, s.paramsKeeper, core.KeyBasketCreationFee, 1)
var coins sdk.Coins
s.paramsKeeper.EXPECT().Get(s.sdkCtx, core.KeyBasketCreationFee, &coins).
Do(func(ctx sdk.Context, key []byte, ptr *sdk.Coins) {
ptr = &basketFees
}).
Times(1)

Copy link
Member

@ryanchristo ryanchristo May 4, 2022

Choose a reason for hiding this comment

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

Not sure if setting the ptr is necessary because it works without it and regardless of what it gets set too, in which case it could be:

Suggested change
utils.ExpectParamGet(&basketFees, s.paramsKeeper, core.KeyBasketCreationFee, 1)
var coins sdk.Coins
s.paramsKeeper.EXPECT().Get(s.sdkCtx, core.KeyBasketCreationFee, &coins).
Do(func(sdk.Context, []byte, *sdk.Coins) {}).
Times(1)

Copy link
Contributor

@aleem1314 aleem1314 left a comment

Choose a reason for hiding this comment

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

lgtm

@technicallyty technicallyty enabled auto-merge (squash) May 11, 2022 16:22
@technicallyty technicallyty merged commit 3f66709 into master May 11, 2022
@technicallyty technicallyty deleted the ty/996-param_get branch May 11, 2022 17:00
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.

Use Get instead of GetParamSet
3 participants