Skip to content

refactor: Use testJSONBody helper for request body assertions in tests#4183

Merged
gmlewis merged 4 commits intogoogle:masterfrom
Not-Dhananjay-Mishra:alt
May 1, 2026
Merged

refactor: Use testJSONBody helper for request body assertions in tests#4183
gmlewis merged 4 commits intogoogle:masterfrom
Not-Dhananjay-Mishra:alt

Conversation

@Not-Dhananjay-Mishra
Copy link
Copy Markdown
Contributor

An alternate implementation of #4170 that uses a generic testJSONBody helper.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.69%. Comparing base (a75818a) to head (adefbee).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4183   +/-   ##
=======================================
  Coverage   93.69%   93.69%           
=======================================
  Files         210      210           
  Lines       19763    19763           
=======================================
  Hits        18517    18517           
  Misses       1049     1049           
  Partials      197      197           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Not-Dhananjay-Mishra
Copy link
Copy Markdown
Contributor Author

Not-Dhananjay-Mishra commented Apr 30, 2026

This approach works well when a struct is passed directly to a method then to API without any modifications. However, if a method modifies the struct internally, a separate want struct would need to be created inside the test for comparison otherwise we have to use testBody (string).

@Not-Dhananjay-Mishra Not-Dhananjay-Mishra marked this pull request as ready for review April 30, 2026 15:07
Copy link
Copy Markdown
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

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

Thank you, @Not-Dhananjay-Mishra!

Overall, I really prefer the outcome of this PR over #4170.

I see that both PRs were a great deal of work (thank you both for your time and effort to clean up all the cases so that we can thoroughly investigate this!) and the final state of this one looks much cleaner to me and in my opinion seems to create a better developer experience overall.

LGTM.

Thoughts, @alexandear ?

@gmlewis gmlewis added the NeedsReview PR is awaiting a review before merging. label Apr 30, 2026
Copy link
Copy Markdown
Contributor

@alexandear alexandear left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Thank you for the tremendous amount of work.

I've left a few minor comments.

Comment thread github/credentials_test.go Outdated
Comment thread github/orgs_immutable_releases_test.go Outdated
@alexandear
Copy link
Copy Markdown
Contributor

alexandear commented May 1, 2026

Please also update these testBody:

❯ rg testBody
github/copilot_test.go
909:            testBody(t, r, `{"selected_teams":["team1","team2"]}`+"\n")
947:            testBody(t, r, `{"selected_teams":["team1","team2"]}`+"\n")
985:            testBody(t, r, `{"selected_usernames":["user1","user2"]}`+"\n")
1023:           testBody(t, r, `{"selected_usernames":["user1","user2"]}`+"\n")

github/repos_releases_test.go
740:                    testBody(t, r, "Upload me !\n")
945:            testBody(t, r, "Upload me !\n")
1066:           testBody(t, r, "Upload me !\n")

github/github_test.go
192:func testBody(t *testing.T, r *http.Request, want string) {

github/pulls_test.go
489:                    testBody(t, r, tt.wantUpdate+"\n")
807:                    testBody(t, r, test.wantBody+"\n")
826:            testBody(t, r, expectedBody+"\n")

github/repos_test.go
2939:                           testBody(t, r, `{"dismissal_restrictions":{}}`+"\n")
3496:           testBody(t, r, `{"names":[]}`+"\n")
3519:           testBody(t, r, `{"names":[]}`+"\n")

github/activity_notifications_test.go
102:            testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n")
126:            testBody(t, r, `{}`+"\n")
145:            testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n")
174:            testBody(t, r, `{}`+"\n")

github/authorizations_test.go
22:             testBody(t, r, `{"access_token":"a"}`+"\n")
59:             testBody(t, r, `{"access_token":"a"}`+"\n")
96:             testBody(t, r, `{"access_token":"a"}`+"\n")
124:            testBody(t, r, `{"access_token":"a"}`+"\n")

github/codespaces_orgs_test.go
93:             testBody(t, r, `{"selected_usernames":["u1"]}`+"\n")
123:            testBody(t, r, `{"selected_usernames":["u1"]}`+"\n")

github/dependabot_secrets_test.go
176:            testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n")
358:            testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv=","visibility":"selected","selected_repository_ids":["1296269","1269280"]}`+"\n")
438:            testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n")

Only testBody from github/repos_releases_test.go should be untouched. I think we can rename it to testPlainBody.

@Not-Dhananjay-Mishra
Copy link
Copy Markdown
Contributor Author

Thanks @alexandear, for detailed review!
Changed testBody to testPlainBody and updated the remaining tests.

Copy link
Copy Markdown
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

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

Thank you, @Not-Dhananjay-Mishra and @alexandear!
LGTM.
Merging.

@gmlewis gmlewis removed the NeedsReview PR is awaiting a review before merging. label May 1, 2026
@gmlewis gmlewis merged commit 0a16e22 into google:master May 1, 2026
10 checks passed
@Not-Dhananjay-Mishra Not-Dhananjay-Mishra deleted the alt branch May 1, 2026 12:38
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.

3 participants