test_util: Add test for build_headers_with_authorization
#443
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR creates a test for
build_headers_with_authorization
:token_type
, we don't set any authentication and handle it gracefully.User-Agent
header.I also added a docstring to the function and some type hinting.
Changes to
build_headers_with_authorization
When writing this test, I discovered a minor quirk that one could argue is a "defect": Python never passes copies, so when we perform operations on the current headers passed into the function,
request_headers
, we actually overwrite this dict. This is not really in line with what I expected based on how we use this function, which is to take in headers and return a new dict containing the headers with authentication. We never encountered this in practice becausebuild_headers_with_authorization
is always called with existing headers as an empty dict,{}
.When writing the test I decided to modify
build_headers_with_authorization
to use a copy of the headers passed in and modify that. But if we'd prefer to actually modify the parameters passed in and remove ourreturn`` altogether, we can modify the function to do that. In that case we would just call
build_headers_with_authorizationin places like ctmods and forego the
return` altogether.I'm fine with either approach, I have no strong preference either way.
Test Structure
This is a can of worms, but only a tiny one. A gourmet can of worms, if you will.
When writing this test, I followed the pattern I followed for the other tests I added, which followed the pattern laid out with
test_get_random_game_name
: One test per function that tests all cases. This has been fine for now but when writing this test specifically I noticed it is a little "unwieldy". I am wondering if there is a better way to write this test, which might end up with us restructuring the existing tests.There are a few different patterns online for how to structure tests, and one that I see somewhat commonly is to have multiple tests for the same function to test separate test cases. Some test frameworks, like RSpec, have separate blocks for this, but PyTest is structured differently here. This keeps tests small but can be a bit repetitive. Some people group tests into classes, and I have also seen some things like pytest-describe to group tests with nested functions.
I haven't found a clear distinct winner for how people organise tests in PyTest. It gives you a lot of flexibility. I think having separate tests for each test case for a function is actually a nice approach, in this case of this PR we would have separate functions to test the GitHub case, the GitLab case, etc. My two concerns with this approach are:
### BEGIN [blah] ###
and### END [blah] ###
blocks with spacing is a low-tech way 😄 But maybe there are better alternatives.request_headers
for each test case. Some test frameworks allow you to specifybefore
blocks for grouped tests to give that group of tests some stubbed data to work with, but I'm not sure what the approach is in PyTest to give ideally only a group of tests the same repeated data. Maybe this is something we would want to use PyTest fixtures for, but I mainly see that for stubbing larger pieces of data like database connections.If the existing approach is fine, we can stick with it, but this is something I wanted to raise just in case. 🙂
I'm good with whatever direction we decide to go with for
build_headers_with_authorization
, and I can update the test in this PR accordingly. On the same lines, however we decide to go with structuring our test functions is also good with me.Thanks!