Skip to content
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

BED-4908: Add unit tests #901

Merged
merged 3 commits into from
Oct 11, 2024
Merged

BED-4908: Add unit tests #901

merged 3 commits into from
Oct 11, 2024

Conversation

wes-mil
Copy link
Contributor

@wes-mil wes-mil commented Oct 11, 2024

Description

Add a few missing unit tests

Motivation and Context

BED-4908

Why is this change required? What problem does it solve?

How Has This Been Tested?

New units tests run successfully

Screenshots (optional):

Types of changes

  • Chore (a change that does not modify the application functionality)

Checklist:

@wes-mil wes-mil added the work in progress This pull request is a work in progress and should not be merged label Oct 11, 2024
@wes-mil wes-mil requested a review from mvlipka October 11, 2024 15:40
@wes-mil wes-mil self-assigned this Oct 11, 2024
Copy link

github-actions bot commented Oct 11, 2024

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

endpoint := "/api/v2/collectors/%s"

t.Run("sharphound", func(t *testing.T) {
req, err := http.NewRequestWithContext(context.Background(), "GET", fmt.Sprintf(endpoint, "sharphound"), nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: we can replace the "GET" with http.MethodGet


endpoint := "/api/version"
req, err := http.NewRequestWithContext(context.Background(), "GET", endpoint, nil)
require.Nil(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be require.NoError

}

func filter(testStruct TestStruct) bool {
return testStruct.val < 2
Copy link
Contributor

Choose a reason for hiding this comment

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

This is clean, but I'd maybe move these definitions to within the test itself.

Not that it looks like we'll be adding more tests here, but to me it makes sense to have this defined in the relative test since it hard defines a requirement (testStruct.val < 2)

type testStruct struct {
		val int
	}

	result := FilterStructSlice(structs, func(s testStruct) bool {
		return s.val < 2
	})

func TestParseOptionalBoolMisspelledValue(t *testing.T) {
result, err := ParseOptionalBool("trueee", false)
require.Equal(t, false, result)
require.Error(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Convention would be to check the error first and then assert that the values are correct.

require.Error(t, err)
assert.Equal(t, false, result)

It doesn't make that much of a difference with this test, but it comes in handy with things such as:

resultSlice, err := SortTest()
require.NoError(t, err)
require.Len(t, resultSlice 2)
assert.Equal(t, "hello", resultSlice[0])
assert.Equal(t, "world", resultSlice[1])

In this scenario, we require err to be nil and require the length of resultSlice to be 2 in order to then assert the individual values of the slice.
This would allow us to run the unit test and get all the potential problems from within the array in one go.

If the test cannot run on a failure condition: require
If the test can run with the failure condition: assert

require.Equal(t, http.StatusOK, response.Code)
})

t.Run("invalid", func(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.

We should also test the 500 status error.

You can do this by defining a new resources object in a new test

var (
			resources = v2.Resources{
				CollectorManifests: map[string]config.CollectorManifest{},
			}
		)

@wes-mil
Copy link
Contributor Author

wes-mil commented Oct 11, 2024

I have read the CLA Document and I hereby sign the CLA

@wes-mil wes-mil marked this pull request as ready for review October 11, 2024 17:47
@wes-mil wes-mil merged commit dac0bbb into main Oct 11, 2024
4 checks passed
@wes-mil wes-mil deleted the BED-4908 branch October 11, 2024 17:56
@github-actions github-actions bot locked and limited conversation to collaborators Oct 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
work in progress This pull request is a work in progress and should not be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants