-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[BEAM-13888] Add unit testing to ioutilx #17058
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
Conversation
* Add basic unit testing to the go ioutilx package
jrmccluskey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good start! Have some style changes for you to make and a few things that leverage Go features.
| data, err := ReadN(r, testLength) | ||
| // err is expected to be nil | ||
| if err != nil { | ||
| t.Errorf("Failed to read data: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go style is to have lowercase first words thanks to how errors can nest. You' also want to use "got" and "want" a lot - see https://github.com/golang/go/wiki/TestComments#got-before-want
| t.Errorf("Failed to read data: %v", err) | |
| t.Errorf("failed to read data: got %v", err) |
| ) | ||
|
|
||
| func TestReadN(t *testing.T) { | ||
| const testString = "hello world!" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to use consts here
| const testString = "hello world!" | |
| testString := "hello world!" |
| t.Errorf("Got %q, wanted %q", string(data), testString) | ||
| } | ||
|
|
||
| // test bad read |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split the bad read case into a separate test case called TestReadN_bad
| t.Errorf("Failed to read data: %v", err) | ||
| } | ||
| // length of data is expected to match testLength | ||
| if (len(data) != testLength) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to use the parentheses here, and you can use local variable assignment within the if statement to make your error message formatting more concise:
| if (len(data) != testLength) { | |
| if got, want := len(data), testLength; got != want { | |
| t.Errorf("got length %v, want %v", got, want) | |
| } |
| t.Errorf("Got %v, wanted %v", len(data), testLength) | ||
| } | ||
| // content of data is expected to match testString | ||
| if (string(data) != testString) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same pattern with the parentheses and "got, want" style here
| } | ||
| } | ||
|
|
||
| func TestReadUnsafe(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same style comments as above
| } | ||
| } | ||
|
|
||
| func TestReadNBufUnsafe(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same style comments as above
| r := strings.NewReader(testString) | ||
| var data [testLength]byte | ||
|
|
||
| length, err := ReadUnsafe(r, data[:]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to pass data here without specifying the range for the slice.
| length, err := ReadUnsafe(r, data[:]) | |
| length, err := ReadUnsafe(r, data) |
| ) | ||
|
|
||
| func TestWriteUnsafe(t *testing.T) { | ||
| var buff bytes.Buffer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Go style usually calls this buf
| "testing" | ||
| ) | ||
|
|
||
| func TestWriteUnsafe(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same style comments as in write.
|
Also: fix the typo in the PR title so it gets mapped to the Jira ticket properly, and when you request a reviewer you do it in a follow-up comment and not the body of the PR template. |
|
Assigning reviewers. If you would like to opt out of this review, comment R: @jrmccluskey for label go. Available commands:
|
|
@jrmccluskey Thanks for feedbacks! Comments addressed. |
jrmccluskey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, going to add a committer so the workflows can run and we can get this merged in!
|
Sorry, missed a few style change request and fixed. |
|
R: @youngoli |
|
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control |
|
LGTM |
youngoli
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks really good. I'm being a bit more nitpicky than I usually would be just to explain Go conventions, but even then this is nearly there.
| if got, want := length, len(data); got != want { | ||
| t.Errorf("got length %v, wanted %v", got, want) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect this test to also check that buf contains the input data. I.e. make sure that WriteUnsafe wrote properly.
| data, err := ReadN(r, testLength) | ||
| // err is expected to be nil | ||
| if err != nil { | ||
| t.Errorf("failed to read data: got %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: When nesting errors, the best convention is to separate errors with colons, like so. Otherwise it's difficult to tell which is your error and which is the nested error. Like in your case, the word "got" would look like it's the first word of the nested error.
| t.Errorf("failed to read data: got %v", err) | |
| t.Errorf("failed to read data, got error: %v", err) |
(Note: This is repeated in the other tests too, don't forget to fix all of them.)
| _, err := ReadN(r, testLength+1) | ||
| // err is expected to be io.EOF | ||
| if err != io.EOF { | ||
| t.Errorf("got error %v, wanted %v", err, io.EOF) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: In this case since we have two different errors to output, we'll have to go a little further to make it legible to readers. There aren't any common conventions here that I know of, but I'd suggest adding in a newline separating the errors, like so:
| t.Errorf("got error %v, wanted %v", err, io.EOF) | |
| t.Errorf("got error: %v\nwanted error: %v", err, io.EOF) |
Although alternatives like quotation marks, brackets, etc. would work in this case too. The important part is that readers can tell where one error ends and another begins.
* added writer test for content * message readibility fix
|
@youngoli Thanks for feedbacks! Comments addressed. |
youngoli
left a comment
There was a problem hiding this 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!
Add unit testing for everything in ioutilx package
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
[BEAM-XXX] Fixes bug in ApproximateQuantiles, where you replaceBEAM-XXXwith the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.CHANGES.mdwith noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md
GitHub Actions Tests Status (on master branch)
See CI.md for more information about GitHub Actions CI.