Refactor for DRY state getters_validator_test and getters_test#10117
Conversation
401c82b to
0240ce0
Compare
| @@ -0,0 +1,22 @@ | |||
| package testtmpl | |||
There was a problem hiding this comment.
| package testtmpl | |
| package testing |
or test
There was a problem hiding this comment.
@terencechain my idea is to create a package not general for testing, but for the specific goal of having all the test code that are repeated in many subpackages of state
There was a problem hiding this comment.
testing here refers to the test template too; it’s more idiomatic and follows the rest of the repo
| "github.com/prysmaticlabs/prysm/testing/require" | ||
| ) | ||
|
|
||
| type testTmplBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice_factory func() (state.BeaconState, error) |
There was a problem hiding this comment.
| type testTmplBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice_factory func() (state.BeaconState, error) | |
| type getState func() (state.BeaconState, error) |
There was a problem hiding this comment.
@terencechain getState would be global for the package, and it clash with other factories necessary for other tests templates
There was a problem hiding this comment.
It's local within the pkg, and it is better given the function signatures. We can make it more explicit if there really is a need (which I don't believe) later
|
|
||
| type testTmplBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice_factory func() (state.BeaconState, error) | ||
|
|
||
| func TestTmplBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice( |
There was a problem hiding this comment.
| func TestTmplBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice( | |
| func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice( |
There was a problem hiding this comment.
@terencechain there would be no way of disguising a test from the test template by the function name
|
|
||
| func TestTmplBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice( | ||
| t *testing.T, | ||
| fact testTmplBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice_factory, |
There was a problem hiding this comment.
| fact testTmplBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice_factory, | |
| st getState, |
|
So my general idea is to show here an example for a specific pattern that will have to be repeated many times. As there are v1, v2, v3, state-static/v[1..3] we will have to do this with most of the testing code. My idea is:
This idea can be modified of course, but I would like to try to keep these principles of having a mechanical relationship between the names of the repeated tests and the test templates, and have it in its own package separated from other generic test helpers. |
Sorry. Naming is hard, but I'd love to be a little more idiomatic towards Go and around what Prysm has done so far.
|
terencechain
left a comment
There was a problem hiding this comment.
Could you also please run gazelle and create the BUILD.bazel file?
1.) bazel run //:gazelle
2.) add testonly = True
3.) edit visibility to beacon chain subpkg or even beacon state state subpkg?
|
@terencechain so if we are already doing something similar? Could you send me a few links to the code so I see how it is done in other places and I do the same? |
Sure. There should be helpers in the below links that do the same |
0240ce0 to
f01388d
Compare
|
@terencechain please, check again |
|
@terencechain @prestonvanloon @rkapka @nisdas please have a look, I also added BUILD.bazel that I forgot previously |
This is done |
| require.Equal(t, false, beaconState.MatchCurrentJustifiedCheckpoint(c1)) | ||
| } | ||
|
|
||
| func VerifyBeaconState_MatchCurrentJustifiedCheckptNative(t *testing.T, factory getStateWithCurrentJustifiedCheckpoint) { |
There was a problem hiding this comment.
You do not need to separate out for our native state. They all follow the same interface, you can use the above test for the native state. This applies to all the below following tests.
There was a problem hiding this comment.
@nidas The Native one does not have clear method, that is why I indroduced this
There was a problem hiding this comment.
You can just have the native state clear method to do nothing so as to reduce test duplication here.
There was a problem hiding this comment.
If I am not understanding something from your comment, let's fix it in another PR, so this does not get in more conflicts
There was a problem hiding this comment.
Maybe it will be clearer with an example:
func TestBeaconState_MatchCurrentJustifiedCheckpt(t *testing.T) {
testtmpl.VerifyBeaconState_MatchCurrentJustifiedCheckpt(
t,
func(cp *ethpb.Checkpoint) (state.BeaconState, error) {
return InitializeFromProto(ðpb.BeaconState{CurrentJustifiedCheckpoint: cp})
},
func(i state.BeaconState) {
}
)
}Basically the clear function is no-op .
|
@rkapka it seems it is waiting for your review |
Either mine or Nishant's. Please resolve his comment and I'm sure he will approve. |
nisdas
left a comment
There was a problem hiding this comment.
Approving it, as you cant abstract out the native state tests without it failing. We can look at this again later
|
@leolara We need |
Ok |
Done |
|
@rkapka waiting for your approval |

What type of PR is this?
Refactor
What does this PR do? Why is it needed?
There are many code repetition on state unit tests, this is an example of a way of fixing this
Which issues(s) does this PR fix?
Partly #10047
Other notes for review
If this approach is seen as good, I will do the same with all the tests in
statepackage while extending coverage