Skip to content

Conversation

@Abacn
Copy link
Contributor

@Abacn Abacn commented Mar 9, 2022

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:

  • Choose reviewer(s) and mention them in a comment ( @jrmccluskey ).
  • Format the pull request title like [BEAM-XXX] Fixes bug in ApproximateQuantiles, where you replace BEAM-XXX with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

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)

Build python source distribution and wheels
Python tests
Java tests

See CI.md for more information about GitHub Actions CI.

* Add basic unit testing to the go ioutilx package
@github-actions github-actions bot added the go label Mar 9, 2022
Copy link
Contributor

@jrmccluskey jrmccluskey left a 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)
Copy link
Contributor

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

Suggested change
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!"
Copy link
Contributor

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

Suggested change
const testString = "hello world!"
testString := "hello world!"

t.Errorf("Got %q, wanted %q", string(data), testString)
}

// test bad read
Copy link
Contributor

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) {
Copy link
Contributor

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:

Suggested change
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) {
Copy link
Contributor

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) {
Copy link
Contributor

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) {
Copy link
Contributor

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[:])
Copy link
Contributor

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.

Suggested change
length, err := ReadUnsafe(r, data[:])
length, err := ReadUnsafe(r, data)

)

func TestWriteUnsafe(t *testing.T) {
var buff bytes.Buffer
Copy link
Contributor

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) {
Copy link
Contributor

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.

@jrmccluskey
Copy link
Contributor

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.

@Abacn Abacn changed the title [BREAM-13888] Add unit testing to ioutilx [BEAM-13888] Add unit testing to ioutilx Mar 9, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Mar 9, 2022

Assigning reviewers. If you would like to opt out of this review, comment assign to next reviewer:

R: @jrmccluskey for label go.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

@Abacn
Copy link
Contributor Author

Abacn commented Mar 10, 2022

@jrmccluskey Thanks for feedbacks! Comments addressed.

Copy link
Contributor

@jrmccluskey jrmccluskey left a 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!

@Abacn
Copy link
Contributor Author

Abacn commented Mar 10, 2022

Sorry, missed a few style change request and fixed.

@jrmccluskey
Copy link
Contributor

R: @youngoli

@github-actions
Copy link
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control

@johnjcasey
Copy link
Contributor

LGTM

Copy link
Contributor

@youngoli youngoli left a 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)
}
}
Copy link
Contributor

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)
Copy link
Contributor

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.

Suggested change
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)
Copy link
Contributor

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:

Suggested change
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
@Abacn
Copy link
Contributor Author

Abacn commented Mar 16, 2022

@youngoli Thanks for feedbacks! Comments addressed.

Copy link
Contributor

@youngoli youngoli 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!

@youngoli youngoli merged commit 15e636f into apache:master Mar 16, 2022
@Abacn Abacn deleted the yhu-BEAM-13888-ioutilxUnitTest branch March 16, 2022 18:29
Sign up for free to 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.

4 participants