Opened 10 years ago

Closed 10 years ago

#23606 closed New feature (fixed)

RequestFactory does not have a trace method.

Reported by: Kévin Etienne Owned by: Rigel Di Scala
Component: Testing framework Version: dev
Severity: Normal Keywords: test
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When testing for views, RequestFactory is very useful to get, post, put, patch, delete, head, options a view.

However trace is missing from RequestFactory even if it's allowed by django.views.generic.View.

A simple fix would be to add trace to RequestFactory.

Example (taken from the doc):

rf = RequestFactory()
get_request = rf.get('/hello/')
post_request = rf.post('/submit/', {'foo': 'bar'})

I have one case where I'm extending django.views.generic.View and where I'm only allowing dispatch
to only use get and I've written a test to check this condition.

Example:

My view:

from django.views.generics import View


class CustomView(View):
    http_method_names = ('get',)

My test:

from django.test import RequestFactory, TestCase


class TestCustomView(TestCase):
    method_not_allowed = (
        'get', 
        'post',
        'put', 
        'patch', 
        'delete', 
        'head', 
        'options', 
        'trace',
    )

    def test_method_not_allowed(self):
        url = '/'
        for method in method_not_allowed:
            with self.subTest(method=method):
                request = getattr(RequestFactory(), method)(url)
                ...
                self.assert(status_code, 405)

The previous test when hit return the following error:

Traceback (most recent call last):
  File "test_views.py", line 85, in test_method_not_allowed
    request = getattr(RequestFactory(), method)(url)
AttributeError: 'RequestFactory' object has no attribute 'trace'

Change History (8)

comment:1 by Tim Graham, 10 years ago

Triage Stage: UnreviewedAccepted
Version: 1.7master

comment:2 by Rigel Di Scala, 10 years ago

Owner: changed from nobody to Rigel Di Scala
Status: newassigned

comment:3 by Rigel Di Scala, 10 years ago

The trace() method was not implemented in RequestFactory, and the RequestFactoryTests suite was testing just the GET requests.

I have submitted https://github.com/django/django/pull/3353 , that implements the trace() method, extends the scope of the existing test suite for the RequestFactory, and adds a test for the new method.

comment:4 by Rigel Di Scala, 10 years ago

Has patch: set

comment:5 by Baptiste Mispelon, 10 years ago

Needs documentation: set

The patch looks good (I just made a minor comment about using del ... instead of del(...)).

It just needs a mention in the release notes for 1.8 and we're good to go.

Thanks!

comment:6 by Rigel Di Scala, 10 years ago

While I was adding the notes in the https://docs.djangoproject.com/en/1.7/topics/testing/advanced/#django.test.RequestFactory docs, I noticed that the django.test.Client, that subtypes the RequestFactory, does not implemenent the trace() method either. We would probably need to implement this method in the test client also.

What is the suggested course of action?

Last edited 10 years ago by Rigel Di Scala (previous) (diff)

comment:7 by Tim Graham, 10 years ago

Needs documentation: unset
Triage Stage: AcceptedReady for checkin

comment:8 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 28634394f53b9ab27d3419cfde047ee78e491d97:

Fixed #23606 -- Implemented Client and RequestFactory trace() methods.

Thanks KevinEtienne for the suggestion.

Note: See TracTickets for help on using tickets.
Back to Top