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

Add should package #663

Merged
merged 1 commit into from
Oct 9, 2023
Merged

Add should package #663

merged 1 commit into from
Oct 9, 2023

Conversation

kegsay
Copy link
Member

@kegsay kegsay commented Oct 5, 2023

The should package is a more lenient form of the must package, in that it does not fail the test if the assertion fails. Instead, these functions return an error, which the test can then handle.

This is particularly useful in cases where a test needs to retry a certain request. Previously, you had to hand-roll the checks on the response because using must.MatchResponse would fail the test, even though it might have been okay as the next retry may pass the matchers.

Fixes #546

Tests need to be revisited to see if this can be used in more places.

An example of this. Before:

res = alice.DoFunc(
	t,
	"GET",
	[]string{"_matrix", "client", "v3", "rooms", roomID, "aliases"},
	client.WithRetryUntil(
		1*time.Second,
		func(res *http.Response) bool {
			if res.StatusCode != 200 {
				return false
			}
			eventResBody := client.ParseJSON(t, res)
			matcher := match.JSONKeyEqual("aliases", []interface{}{roomAlias})
			err := matcher(eventResBody)
			if err != nil {
				t.Log(err)
				return false
			}
			return true
		},
	),
)

After:

res = alice.Do(
	t,
	"GET",
	[]string{"_matrix", "client", "v3", "rooms", roomID, "aliases"},
	client.WithRetryUntil(
		1*time.Second,
		func(res *http.Response) bool {
			_, err := should.MatchResponse(res, match.HTTPResponse{
				StatusCode: 200,
				JSON: []match.JSON{
					match.JSONKeyEqual("aliases", []interface{}{roomAlias}),
				},
			})
			if err != nil {
				t.Log(err)
				return false
			}
			return true
		},
	),
)

The `should` package is a more lenient form of the `must` package, in
that it does not fail the test if the assertion fails. Instead, these
functions return an error, which the test can then handle.

This is particularly useful in cases where a test needs to retry a
certain request. Previously, you had to hand-roll the checks on the
response because using `must.MatchResponse` would fail the test, even
though it might have been okay as the next retry may pass the matchers.

Fixes #546

Tests need to be revisited to see if this can be used in more places.
@kegsay kegsay requested review from a team as code owners October 5, 2023 17:26
Copy link
Contributor

@DMRobertson DMRobertson left a comment

Choose a reason for hiding this comment

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

Seems very sensible.

@kegsay kegsay merged commit 00b3326 into main Oct 9, 2023
4 checks passed
@kegsay kegsay deleted the kegan/should branch October 9, 2023 13:01
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.

should counterpart to must
2 participants