-
-
Notifications
You must be signed in to change notification settings - Fork 355
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
Calls info in BaseResponse #664
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3f7660a
Add calls to BaseResponse
zeezdev 77ca597
Reuse calls for matched BaseResponse and add add_call method into Cal…
zeezdev 0d29ad1
Merge branch 'getsentry:master' into call-info-in-response
zeezdev 5669688
Reuse call object with add_call & add example of usage Response.calls…
zeezdev 34d825f
Merge branch 'getsentry:master' into call-info-in-response
zeezdev 7f7085e
Update README.rst
zeezdev 0e515ff
Add CHANGELOG entry
zeezdev d78499d
Change example in README
zeezdev 48f9615
Merge branch 'master' into call-info-in-response
zeezdev aa29301
Fix for blacken-docs linter
zeezdev d1b0650
Add ignore comment for mypy
zeezdev f6a7dd3
Add ignore comments for mypy
zeezdev 9f5e733
Merge branch 'master' into call-info-in-response
zeezdev 654c607
Make tests readable
zeezdev 41bb479
Simplify readme example
zeezdev 51f4e56
Use f-string in readme
zeezdev ea1144d
Simplify new tests
zeezdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1079,6 +1079,59 @@ Assert that the request was called exactly n times. | |
responses.assert_call_count("http://www.example.com?hello=world", 1) is True | ||
|
||
|
||
Assert Request Calls data | ||
------------------ | ||
|
||
``Request`` object has ``calls`` list which elements correspond to ``Call`` objects | ||
in the global list of ``Registry``. This can be useful when the order of requests is not | ||
guaranteed, but you need to check their correctness, for example in multithreaded | ||
applications. | ||
|
||
.. code-block:: python | ||
|
||
import concurrent.futures | ||
import responses | ||
import requests | ||
|
||
|
||
@responses.activate | ||
def test_assert_calls_on_resp(): | ||
rsp1 = responses.patch("http://www.foo.bar/1/", status=200) | ||
rsp2 = responses.patch("http://www.foo.bar/2/", status=400) | ||
rsp3 = responses.patch("http://www.foo.bar/3/", status=200) | ||
|
||
def update_user(uid, is_active): | ||
url = f"http://www.foo.bar/{uid}/" | ||
response = requests.patch(url, json={"is_active": is_active}) | ||
return response | ||
|
||
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor: | ||
future_to_uid = { | ||
executor.submit(update_user, uid, is_active): uid | ||
for (uid, is_active) in [("3", True), ("2", True), ("1", False)] | ||
} | ||
for future in concurrent.futures.as_completed(future_to_uid): | ||
uid = future_to_uid[future] | ||
response = future.result() | ||
print("%s updated with %d status code" % (uid, response.status_code)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please update it with f-string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
assert len(responses.calls) == 3 # total calls count | ||
|
||
assert rsp1.call_count == 1 | ||
assert rsp1.calls[0] in responses.calls | ||
assert rsp1.calls[0].response.status_code == 200 | ||
assert json.loads(rsp1.calls[0].request.body) == {"is_active": False} | ||
|
||
assert rsp2.call_count == 1 | ||
assert rsp2.calls[0] in responses.calls | ||
assert rsp2.calls[0].response.status_code == 400 | ||
assert json.loads(rsp2.calls[0].request.body) == {"is_active": True} | ||
|
||
assert rsp3.call_count == 1 | ||
assert rsp3.calls[0] in responses.calls | ||
assert rsp3.calls[0].response.status_code == 200 | ||
assert json.loads(rsp3.calls[0].request.body) == {"is_active": True} | ||
|
||
Multiple Responses | ||
------------------ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
I understand that this example might look very close to what you have in production. But can we unload it a bit and reduce complexity?
we probably still want to show an example of
concurrent
since this could be one of the main usages, but we do not need all those complicated matrices of settings to show the usagecan you please simplify it to remove additional arguments and have more clear names like
rsp1, rsp2, rsp3
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.
Got you, I'll try to simplify this.
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.
simplified, please take a look
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.
@zeezdev now looks a way better, thank you!