[Django] #16087: Add ResolverMatch object to test client responses.

20 views
Skip to first unread message

Django

unread,
May 24, 2011, 9:21:13 AM5/24/11
to django-...@googlegroups.com
#16087: Add ResolverMatch object to test client responses.
-------------------------------------------------+-------------------------
Reporter: mrmachine | Owner: nobody
Type: Bug | Status: new
Milestone: 1.4 | Component: Testing
Version: 1.3 | framework
Keywords: test client view url resolve | Severity: Normal
reverse | Triage Stage:
Has patch: 0 | Unreviewed
| Easy pickings: 0
-------------------------------------------------+-------------------------
The docs say that you can use the test client to "Test that the correct
view is executed for a given URL." but this doesn't seem to be possible
without requiring users to perform their own `resolve()`, when Django has
already done it and discarded the resulting ResolverMatch object.

{{{
response = client.post(some_url, some_data, follow=True)
self.assertEqual(resolve(response.request["PATH_INFO"]).url_name,
'some_url')
}}}

In my case, `some_url` is a view that processes some form data and
redirects according to the data, and I'm testing that with `some_data` as
input, the request is redirected to `some_url`. With different data, it
might redirect to a different URL.

Initially I tried to use `assertRedirects` and `response.redirect_chain`
to test this, but ran into trouble because I had to try and construct a
URL string that would match the redirect_chain from the view function or
URL name, without knowing the exact URL arguments that were used to
generate the redirect URL (auto field and a hash that is unique to the
newly created object).

I'd like to see Django add the ResolverMatch object directly to all test
client responses, so that users can easily test that a specific view
function or named URL was executed.

I guess this would be implemented similarly to the way the context and
templates are added.

I think this is either a feature bug or a documentation bug. If this is
not a valid feature of the test client, the docs should be clarified. I
read "correct view is executed" to mean "correct view function (or class
based view) is executed".

--
Ticket URL: <https://code.djangoproject.com/ticket/16087>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 27, 2011, 11:10:57 AM5/27/11
to django-...@googlegroups.com
#16087: Add ResolverMatch object to test client responses.
-------------------------------------+-------------------------------------
Reporter: mrmachine | Owner: nobody
Type: Bug | Status: new
Milestone: 1.4 | Component: Testing framework
Version: 1.3 | Severity: Normal
Resolution: | Keywords: test client view
Triage Stage: Design | url resolve reverse
decision needed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* needs_docs: => 0
* stage: Unreviewed => Design decision needed
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

I think you're expected to use assertRedirects. Can't you just recover the
auto field and hash values by fetching the last object from the database?
If not, that's really an edge case, I'm not sure we want to modify Django
for this.

--
Ticket URL: <https://code.djangoproject.com/ticket/16087#comment:1>

Django

unread,
May 27, 2011, 9:07:33 PM5/27/11
to django-...@googlegroups.com
#16087: Add ResolverMatch object to test client responses.
-------------------------------------+-------------------------------------
Reporter: mrmachine | Owner: nobody
Type: Bug | Status: new
Milestone: 1.4 | Component: Testing framework
Version: 1.3 | Severity: Normal
Resolution: | Keywords: test client view
Triage Stage: Design | url resolve reverse
decision needed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
-------------------------------------+-------------------------------------

Comment (by mrmachine):

`assertRedirects` tells me the URLs that were used in a chain of
redirects. It does not tell me the view (function or named URL) that was
executed for a given request. Unless I'm reading the docs wrong (in which
case this should be a doc fix), one of the goals of the test client is to
allow users to verify the view that was executed for a given request.

What I want to test is, what view function or named URL was executed to
generate the response for a given request. Not was an object created in
the database (and it may not always be), then perform a redundant URL
lookup (Django already did it when processing the request) so I can
finally test if the correct view function or named URL was executed.

It would be a lot easier to write and read tests (and DRY) to simply POST
your data to a URL, and verify the view function or named URL that
generated the response. Django works some magic to make available a list
of templatest that were used, the context, etc. Why not make the
ResolverMatch object available, too?

I had a +1 on making this possible from cramm on IRC. I'm happy to take a
crack at implementing this functionality OR writing a doc patch if that
ends up being the decision. I just don't want to work on a fix if the core
devs don't want it.

--
Ticket URL: <https://code.djangoproject.com/ticket/16087#comment:2>

Django

unread,
May 28, 2011, 2:25:11 AM5/28/11
to django-...@googlegroups.com
#16087: Add ResolverMatch object to test client responses.
-------------------------------------+-------------------------------------
Reporter: mrmachine | Owner: nobody
Type: Bug | Status: new
Milestone: 1.4 | Component: Testing framework
Version: 1.3 | Severity: Normal
Resolution: | Keywords: test client view
Triage Stage: Accepted | url resolve reverse
Needs documentation: 0 | Has patch: 0
Patch needs improvement: 0 | Needs tests: 0
| Easy pickings: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* stage: Design decision needed => Accepted


Comment:

Since we have a +1 from a core developer, I'm going to accept the ticket.
If you want to discuss the idea further before implementing it, you can
send an email to django-developers.

--
Ticket URL: <https://code.djangoproject.com/ticket/16087#comment:3>

Django

unread,
Sep 25, 2012, 7:53:39 AM9/25/12
to django-...@googlegroups.com
#16087: Add ResolverMatch object to test client responses.
-------------------------------------+-------------------------------------

Reporter: mrmachine | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.3
Severity: Normal | Resolution:
Keywords: test client view | Triage Stage: Accepted
url resolve reverse | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by aaugustin):

#15695 is related.

Django

unread,
Sep 29, 2012, 5:16:01 PM9/29/12
to django-...@googlegroups.com
#16087: Add ResolverMatch object to test client responses.
-------------------------------------+-------------------------------------
Reporter: mrmachine | Owner: nobody
Type: Bug | Status: closed

Component: Testing framework | Version: 1.3
Severity: Normal | Resolution: fixed

Keywords: test client view | Triage Stage: Accepted
url resolve reverse | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by mrmachine):

* status: new => closed
* resolution: => fixed


Comment:

Closing this as fixed. Since #15695 was fixed, the resolver match object
is added to all requests (not only test client requests).

--
Ticket URL: <https://code.djangoproject.com/ticket/16087#comment:4>

Django

unread,
Sep 29, 2012, 5:32:29 PM9/29/12
to django-...@googlegroups.com
#16087: Add ResolverMatch object to test client responses.
-------------------------------------+-------------------------------------
Reporter: mrmachine | Owner: nobody
Type: Bug | Status: reopened

Component: Testing framework | Version: 1.3
Severity: Normal | Resolution:
Keywords: test client view | Triage Stage: Accepted
url resolve reverse | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by mrmachine):

* status: closed => reopened
* resolution: fixed =>


Comment:

Sorry, it must be too early in the morning. Got my requests and responses
mixed up!

--
Ticket URL: <https://code.djangoproject.com/ticket/16087#comment:5>

Django

unread,
Jun 6, 2014, 10:15:23 AM6/6/14
to django-...@googlegroups.com
#16087: Add ResolverMatch object to test client responses.
-------------------------------------+-------------------------------------
Reporter: mrmachine | Owner: gchp
Type: Bug | Status: assigned

Component: Testing framework | Version: 1.3
Severity: Normal | Resolution:
Keywords: test client view | Triage Stage: Accepted
url resolve reverse | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by gchp):

* owner: nobody => gchp
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/16087#comment:7>

Django

unread,
Jun 6, 2014, 10:55:03 AM6/6/14
to django-...@googlegroups.com
#16087: Add ResolverMatch object to test client responses.
-------------------------------------+-------------------------------------
Reporter: mrmachine | Owner: gchp
Type: Bug | Status: assigned
Component: Testing framework | Version: 1.3
Severity: Normal | Resolution:
Keywords: test client view | Triage Stage: Accepted
url resolve reverse | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0

Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by gchp):

* has_patch: 0 => 1


Comment:

Created PR for this issue: https://github.com/django/django/pull/2768

--
Ticket URL: <https://code.djangoproject.com/ticket/16087#comment:8>

Django

unread,
Jun 13, 2014, 8:52:58 AM6/13/14
to django-...@googlegroups.com
#16087: Add ResolverMatch object to test client responses.
-------------------------------------+-------------------------------------
Reporter: mrmachine | Owner: gchp
Type: Bug | Status: closed

Component: Testing framework | Version: 1.3
Severity: Normal | Resolution: fixed

Keywords: test client view | Triage Stage: Accepted
url resolve reverse | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"bf743a4d571bbb7da276bc21c61f6ada5d26942c"]:
{{{
#!CommitTicketReference repository=""
revision="bf743a4d571bbb7da276bc21c61f6ada5d26942c"
Fixed #16087 -- Added ResolverMatch instance to test client response.

Thanks mrmachine for the suggestion.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/16087#comment:9>

Reply all
Reply to author
Forward
0 new messages