[PoC] Use builders to make table tests more readable#1762
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: dprotaso If they are not already assigned, you can assign the PR to them by writing The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Looking at it while walking we could have higher level builder functions to remove more boilerplate ie. AddStatusCondition Edit: Pushed those changes |
The table test having WantCreate be a type of metav1.Object actually results in more boilerplate - hence some of the calls to `Build`
fe5d3d6 to
3f95d25
Compare
|
The following is the coverage report on pkg/.
|
mattmoor
left a comment
There was a problem hiding this comment.
Generally 👍 I wonder about applicability beyond the table tests, or even outside of the controllers.
We have a lot of little bits and pieces that have been thrown together to produce test data (I'm super guilty of this), it'd be good to consolidate it all around this pattern, if we can. WDYT?
| case builder.ConfigBuilder: | ||
| ls.Configuration.Items = append(ls.Configuration.Items, o.Configuration) | ||
| case builder.RevisionBuilder: | ||
| ls.Revision.Items = append(ls.Revision.Items, o.Revision) |
There was a problem hiding this comment.
I don't understand why we take these instead of just calling Build()?
| } | ||
|
|
||
| func (bldr RevisionBuilder) WithCondition(c v1alpha1.RevisionCondition) RevisionBuilder { | ||
| bldr.Revision.Status.Conditions = append(bldr.Revision.Status.Conditions, c) |
There was a problem hiding this comment.
This could end up with duplicates
| } | ||
|
|
||
| rev := resources.MakeRevision(config, buildName) | ||
| rev := resources.MakeRevision(config) |
There was a problem hiding this comment.
I think this part of the change is a separable distraction from the test change. Want to send a separate PR?
| @@ -0,0 +1,94 @@ | |||
| package builder | |||
|
|
||
| type ConfigBuilder struct { | ||
| *v1alpha1.Configuration | ||
| } |
There was a problem hiding this comment.
This might better below under pkg/apis/serving/v1alpha1/testing?
| func makeRevStatus(rev *v1alpha1.Revision, status v1alpha1.RevisionStatus) *v1alpha1.Revision { | ||
| rev.Status = status | ||
| return rev | ||
| } |
| WithCondition(v1alpha1.ConfigurationCondition{ | ||
| Type: v1alpha1.ConfigurationConditionReady, | ||
| Status: corev1.ConditionTrue, | ||
| }).AsUpdateAction(), |
There was a problem hiding this comment.
I like shedding the UpdateActionImpl wrapper :)
| ls := NewListers(r.Objects) | ||
|
|
||
| for i, action := range r.WantUpdates { | ||
| if builder, ok := action.Object.(builder.ConfigBuilder); ok { |
There was a problem hiding this comment.
Seems like this could avoid the reference to ConfigBuilder by defining an interface:
type Builder interface{
Build() runtime.Object
}but I'm not sure about that return value. Would it work with cmp.Diff or semantic equality or whatever is being used to compare objects?
| }, | ||
| WantCreates: []metav1.Object{ | ||
| setRevConcurrencyModel(resources.MakeRevision(cfg("validation-failure", "foo", 1234), noBuildName), "Bogus"), | ||
| setRevConcurrencyModel(resources.MakeRevision(cfg("validation-failure", "foo", 1234)), "Bogus"), |
There was a problem hiding this comment.
I definitely like the builders better than this style (setRevConcurrencyModel).
| @@ -0,0 +1,62 @@ | |||
| package builder | |||
There was a problem hiding this comment.
Haven't thought about it too carefully, but would it make sense for these methods to be defined in the api package (or a subpackage of that) rather than in the controller testing package?
There was a problem hiding this comment.
That was @mattmoor's suggestion here:
#1762 (comment)
|
/assign grantr mattmoor |
|
LGTM as well. I wanted to use this but then I realized it wasn't merged yet 😆 |
|
Closing - moving work to #1924 |
This is just a proof-of-concept to source feedback on whether folks like this pattern, and whether they feel this improves readability. This secondly helps reduce the combinatorial method names.