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
Generalise request matching in PublishingApi assertions #337
Conversation
3d3b748
to
179eeb4
Compare
when Hash | ||
return false unless actual_value.is_a?(Hash) | ||
expected_value.all? do |expected_sub_key, expected_sub_value| | ||
key = symbols_as_strings(expected_sub_key) |
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.
Why not just call expected_sub_key.to_s
?
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.
keys could in theory be integers or booleans
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.
OK
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.
just read the JSON spec, I was mistaken. Object keys must be strings.
That simplifies things.
General approach seems fine. Specifics seem mostly fine. Regarding your questions, I'd personally assume that I'm a little unsure about extending the partial matching to hashes within arrays. Do we have a concrete use case for that? |
thanks for your comments re: naming, how about
re: use case for matching hashes within arrays, yes that's needed for matching the manuals table of contents which look like this: "details": {
"child_section_groups": [
{
"title": "Contents",
"child_sections": [
{
"title": "Child title",
"description": "Child description",
"base_path": "/guidance/manual/child-title",
}
]
}
]
} |
Name change sounds good to me. |
76e79d2
to
b65fae4
Compare
The assertions provided by the PublishingApi test helpers are useful. The existing implementation allows an assertion to require that a request was made to a publishing api endpoint with a hash containing a certain list of required attributes. However the underlying matching process would only take into account the outermost level of the hash when performing the comparison. This means that you could make an flexible assertion about the required elements at the top level of a document sent to the publishing api, but if you wanted to make assertions about the details section or elements nested within the details section, you could only perform an exact match on the details hash. This would lead to brittle, difficult to maintain tests. This commit allows passing custom matcher predicates to the assertions and defines a new matcher which can match nested structures more loosely. The original strict matching behaviour is retained as a default to avoid possible unintended side-effects on the test suites of other apps. We considered making the looser behaviour the default, but after reviewing the usage of some other apps I had enough doubt about that I thought it would be better to be conservative and avoid changing the default matching behaviour. To use the assertions with the looser matcher you can do the following: ``` assert_publishing_api_put_item( base_path, request_json_including(details: {title: "My title"}) ) ``` Note that both matchers will convert symbols to strings so the hash can use either symbol or string keys.
b65fae4
to
a90d053
Compare
…tion-matchers Generalise request matching in PublishingApi assertions
The assertions provided by the PublishingApi test helpers are useful.
The existing implementation allows an assertion to require that a
request was made to a publishing api endpoint with a hash containing a
certain list of required attributes.
However the underlying matching process would only take into account the
outermost level of the hash when performing the comparison. This means
that you could make an flexible assertion about the required elements at
the top level of a document sent to the publishing api, but if you
wanted to make assertions about the details section or elements nested
within the details section, you could only perform an exact match on the
details hash.
This would lead to brittle, difficult to maintain tests.
This commit allows passing custom matcher predicates to the assertions
and defines a new matcher which can match nested structures more
loosely.
The original strict matching behaviour is retained as a default to avoid
possible unintended side-effects on the test suites of other apps.
We considered making the looser behaviour the default, but after
reviewing the usage of some other apps I had enough doubt about that I
thought it would be better to be conservative and avoid changing the
default matching behaviour.
To use the assertions with the looser matcher you can do the following:
Note that both matchers will convert symbols to strings so the
hash can use either symbol or string keys.
Feedback suggestions
request_json_matching
,request_json_strictly_matching
)