Skip to content

Change testJSONMarshal - #2708

Closed
exageraldo wants to merge 13 commits into
google:masterfrom
exageraldo:test-json-marshal
Closed

Change testJSONMarshal#2708
exageraldo wants to merge 13 commits into
google:masterfrom
exageraldo:test-json-marshal

Conversation

@exageraldo

Copy link
Copy Markdown
Contributor

Fixes: #2699

@codecov

codecovBot commented Mar 15, 2023

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.16%. Comparing base (77b5b3d) to head (d29f932).
Report is 346 commits behind head on master.

Additional details and impacted files
@@ Coverage Diff @@## master #2708 +/- ##
==========================================
+ Coverage 98.13% 98.16% +0.03% 
==========================================
Files 148 148 Lines 12783 12783 ==========================================
+ Hits 12544 12548 +4 + Misses 164 160 -4 
Partials 75 75 

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

@gmlewis

Copy link
Copy Markdown
Collaborator

Thanks, @exageraldo ! Please remove "Draft" status when you would like me to review this PR.

Comment threadgithub/actions_artifacts_test.go Outdated
Comment threadgithub/activity_notifications.go Outdated
Comment threadgithub/github.go
…ces before comparison (inside testJSONMarshal)
…itError_Marshal and TestAbuseRateLimitError_Marshal
@exageraldo

Copy link
Copy Markdown
ContributorAuthor

Some tests are no longer _Marshal but _addOtions, because the structures are responsible for mounting query strings, not a json.

TestGetAuditLogOptions_addOptions
TestTrafficBreakdownOptions_addOptions
TestSearchOptions_addOptions
TestTeamListTeamMembersOptions_addOptions
TestListExternalGroupsOptions_addOptions
TestUserListOptions_addOptions
TestHovercardOptions_addOptions

Since we do a tag validation in the testAddURLOptions function, I had to make some small changes to the following structures for the tests to pass.

GetAuditLogOptions ~> ListCursorOptions `url:",omitempty"`
SearchOptions ~> ListOptions `url:",omitempty"`
TeamListTeamMembersOptions ~> ListOptions `url:",omitempty"`
ListExternalGroupsOptions ~> ListOptions `url:",omitempty"`
UserListOptions ~> ListOptions `url:",omitempty"`

Theoretically it is just an explicit way of leaving the structure as it already was (did that make any sense?)

@exageraldo

exageraldo commented Mar 22, 2023

Copy link
Copy Markdown
ContributorAuthor

In some cases, I had to use json.Marshal to mount part of the expected string.

github/event_types_test.go

part, _:=json.Marshal(">= 2.0.0, < 2.0.2")

github/teams_discussions_test.go

bodyHTML, _:=json.Marshal(`<p>test</p>`)

In both cases, the idea is just to convert ">" into "\u003e" and "<" into "\u003c".

Do you think we can keep it that way, or can we add two other strings.Replace inside the testJSONMarshal function?

want=strings.Replace(want, ">", "\u003e", -1)
want=strings.Replace(want, "<", "\u003c", -1)

I could not find any other simple way to handle this.


And now a new case:

github/repos_contents_test.go

contentValue, _:=json.Marshal([]byte{1})

@gmlewis

Copy link
Copy Markdown
Collaborator

Yeah, those cases look a bit odd.
Go ahead with what you think is best, and then when I take the time to review the whole PR, I might come up with other ideas. 😄

@exageraldoexageraldo changed the title [WIP] Change testJSONMarshalChange testJSONMarshalMar 24, 2023
@exageraldo
exageraldo marked this pull request as ready for review March 24, 2023 00:22
@gmlewis

Copy link
Copy Markdown
Collaborator

We'll give this PR a couple weeks to get a reply and have the conflicts resolved, then if we haven't got any updates, it will be closed as abandoned.

@exageraldo

Copy link
Copy Markdown
ContributorAuthor

I'll fix these things by this weekend 😄

@gmlewis

Copy link
Copy Markdown
Collaborator

Awesome - @exageraldo - thank you for the update!
No rush, actually - I just need to clean out old, abandoned PRs periodically.

@exageraldo

Copy link
Copy Markdown
ContributorAuthor

hey, sorry for the delay. i had some unforeseen circumstances here, but I'm already finalizing it! by monday I'll be pushing up the changes.

@gmlewis

Copy link
Copy Markdown
Collaborator

Why are you changing GitHub workflows?

@exageraldo

Copy link
Copy Markdown
ContributorAuthor

I didn't change anything in the workflows. I ended up opting for a "merge" instead of a "rebase" because some changes were disappearing and I didn't know how to solve it. If you look in the "Files Changed" tab, you'll see that all the changes are related to tests only.

@gmlewis

Copy link
Copy Markdown
Collaborator

I'll look again when I'm not on my android phone later today.

@gmlewis

Copy link
Copy Markdown
Collaborator

OK, I must have been looking only at a merge commit... looking now and it is looking much better, thanks.
Performing code review...

@gmlewisgmlewis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wow, thank you, @exageraldo !
A have a few questions...

Comment threadgithub/github_test.go

if diff := cmp.Diff(string(w), string(got)); diff != "" {
t.Errorf("json.Marshal returned:\n%s\nwant:\n%s\ndiff:\n%v", got, w, diff)
// Remove all spaces and new lines from the JSON strings.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Remove all spaces and new lines from the JSON strings.
// Remove all tabs and newlines from the JSON strings.

Comment threadgithub/github_test.go
want = strings.Replace(want, "\t", "", -1)
want = strings.Replace(want, "\n", "", -1)

// Replace the "<" and ">" characters with their unicode escape sequences.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This seems odd - why was this necessary?

Comment threadgithub/github_test.go
Comment on lines 2715 to 2751
want := `{
"reason": "reason",
"created_at": ` + referenceTimeStr + `
}`

testJSONMarshal(t, u, want)
}

func TestRateLimitError_Marshal(t *testing.T) {
testJSONMarshal(t, &RateLimitError{}, "{}")

u := &RateLimitError{
Rate: Rate{
Limit: 1,
Remaining: 1,
Reset: Timestamp{referenceTime},
},
Message: "msg",
}

want := `{
"Rate": {
"limit": 1,
"remaining": 1,
"reset": ` + referenceTimeStr + `
},
"message": "msg"
}`

testJSONMarshal(t, u, want)
}

func TestAbuseRateLimitError_Marshal(t *testing.T) {
testJSONMarshal(t, &AbuseRateLimitError{}, "{}")

u := &AbuseRateLimitError{
Message: "msg",
}

want := `{
"message": "msg"
"reason":"reason",
"created_at":` + referenceTimeStr + `
}`

testJSONMarshal(t, u, want)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why were these removed?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

wow. It wasn't supposed to happen.
I think it was when I sync the branch. I'll add it again!

Order *string `url:"order,omitempty"` // The order of audit log events. Can be one of "asc" or "desc". Default: "desc". (Optional.)

ListCursorOptions
ListCursorOptions `url:",omitempty"`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This seems odd - why is this necessary for an embedded struct?

Comment threadgithub/search.go
TextMatch bool `url:"-"`

ListOptions
ListOptions `url:",omitempty"`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This also seems odd - why is this needed?

Role string `url:"role,omitempty"`

ListOptions
ListOptions `url:",omitempty"`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same thing

Comment threadgithub/users.go
// ListOptions.Page has no effect.
// ListOptions.PerPage controls an undocumented GitHub API parameter.
ListOptions
ListOptions `url:",omitempty"`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ditto

@WillAbides

WillAbides commented Oct 11, 2023

Copy link
Copy Markdown
Contributor

We could simplify this by changing testJSONMarshal to this:

// Test whether the marshaling of v produces JSON that corresponds// to the want string.functestJSONMarshal(t*testing.T, vinterface{}, wantstring) {
t.Helper()
got, err:=json.Marshal(v)
iferr!=nil {
t.Errorf("Unable to marshal JSON for %#v", v)
}
got=normalizeJSON(t, got)
wantBytes:=normalizeJSON(t, []byte(want))
diff:=cmp.Diff(string(wantBytes), string(got))
ifdiff!="" {
t.Errorf("json.Marshal returned:\n%s\nwant:\n%s\ndiff:\n%v", string(got), string(wantBytes), diff)
}
}
// normalizeJSON normalizes the JSON in b by unmarshaling and marshaling it// again.funcnormalizeJSON(t*testing.T, b []byte) []byte {
t.Helper()
varvinterface{}
err:=json.Unmarshal(b, &v)
iferr!=nil {
t.Errorf("Unable to unmarshal JSON for %v: %v", string(b), err)
}
w, err:=json.MarshalIndent(v, "", " ")
iferr!=nil {
t.Errorf("Unable to marshal JSON for %#v", v)
}
returnw
}

That way differences like casing are failures but it isn't required to have the same whitespace or order of fields. This is similar to what testify does with assert.JSONEq().

@gmlewis

Copy link
Copy Markdown
Collaborator

I will close this PR around the end of January, 2025 as "abandoned" if there is no further response.

@exageraldo

Copy link
Copy Markdown
ContributorAuthor

I'll organize the branch to push up something like @WillAbides suggested!

@exageraldo

Copy link
Copy Markdown
ContributorAuthor

@WillAbides 's solution worked very well! Thanks a lot!
I "messed up" this branch a lot and ended up creating another one here #3519
Now I'm working on fixing all the tests that broke.

Maybe we can close this PR.

@gmlewis

Copy link
Copy Markdown
Collaborator

@WillAbides 's solution worked very well! Thanks a lot! I "messed up" this branch a lot and ended up creating another one here #3519 Now I'm working on fixing all the tests that broke.

Maybe we can close this PR.

SGTM.
Closing as obsolete.

@gmlewisgmlewis closed this Mar 17, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

about testJSONMarshal's behavior

3 participants

@exageraldo@gmlewis@WillAbides