-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Add documentation about how to transform factory request to DRF request #9380
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
base: master
Are you sure you want to change the base?
Add documentation about how to transform factory request to DRF request #9380
Conversation
Hi @carltongibson! Is something like that the documentation you had in mind? |
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.
Hi @mgaligniana, it's similar!
I think people do this when they're testing individual view methods, without going via dispatch.
So, I'd be inclined to say, and show, that. I.e. what you have plus initial()
and calling a (fictional) view method, which would be the point of the test.
Make sense?
But, yes, great.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
docs/api-guide/testing.md
Outdated
@@ -102,6 +102,20 @@ This means that setting attributes directly on the request object may not always | |||
request.user = user | |||
response = view(request) | |||
|
|||
In case you want to test the request having a REST famework's `Request` you have to transform it by-hand before: |
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 think we can rewrite the text
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I think this is still relevant and shouldn't be marked as stale. |
Hi ulgens! Yes, auvipy fixed the text I added but let me know if there is other change required! |
@mgaligniana No change requests & recommendations on my end. I just saw the notification from the stale bot and thought that this is better merged than lost. |
factory = APIRequestFactory() | ||
|
||
class DummyView(APIView): | ||
... |
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 pass
here?
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.
...
is commonly used to indicate that something is missing - left blank on purpose. I think it suits here just fine.
request = factory.get('/', {'demo': 'test'}) | ||
DRF_request = DummyView().initialize_request(request) | ||
assert DRF_request.query_params == {'demo': ['test']} | ||
assert not hasattr(DRF_request, 'accepted_media_type') |
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.
This is not readable. Prefer:
assert <something> is False
or assert <something> is True
or similar.
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.
+1 about having room to improve readability, but IMO what is needed is not changing the content, it's adding an empty line before the first assert
.
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.
Thanks for the clarifications
Description
Hi there!
As there were many issues opened around this topic: "
APIRequestFactory
does not return aRequest
object" I think it would be worth to have a place in the documentation where explains it.Following the Tom's comment in the last issue #6488 (comment):
*Past issues related: #4440 and #3608
This is what my proposal looks like:
I've also added a test to follow the Carlton's comment #6488 (comment)
Thank you!