Opened 9 years ago

Closed 9 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 Changed 9 years ago by Tim Graham

Triage Stage: UnreviewedAccepted
Version: 1.7master

comment:2 Changed 9 years ago by Rigel Di Scala

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

comment:3 Changed 9 years ago by Rigel Di Scala

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 Changed 9 years ago by Rigel Di Scala

Has patch: set

comment:5 Changed 9 years ago by Baptiste Mispelon

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 Changed 9 years ago by Rigel Di Scala

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 9 years ago by Rigel Di Scala (previous) (diff)

comment:7 Changed 9 years ago by Tim Graham

Needs documentation: unset
Triage Stage: AcceptedReady for checkin

comment:8 Changed 9 years ago by Tim Graham <timograham@…>

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