some time ago I wrote a snippet which does this:
http://www.djangosnippets.org/snippets/467/
{{{
If you want to test a post request to a form, you need to give all input fields, even if you just want to test if one
value gets changed.
This snippets parses the HTML of a view and gives you a dictionary that you can give to django.test.Client.
Needs ClientForm from: http://wwwsearch.sourceforge.net/ClientForm/
}}}
Joshua Russo schrieb:
--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
It's certainly worth putting up as a ticket. I can agree with the
original use case - that is, using the client.get() to construct the
prototype data dictionary that will be passed to client.post(). You
only have to look at the work that is done in the admin tests to see
the value in this proposal.
However, I have some reservations about the patch.
Most notably, I'm not sure I see how your patch answers the original
use case. The form data structure that is returned by the parser
appears to contain a top-level dictionary containing the form
instances. The form structure itself is irrelevant to reposting.
However, thats not to say that keeping the form data is irrelevant.
For example, consider the case where a page has multiple forms, or
multiple submit buttons on a single form. If we're going to introduce
a utility like this, then it should be able to behave at least
partially like a browser - that is, I should be able to get the post
data that is needed to respond to a specific button press:
response = self.client.get('/path/to/page')
# Get a copy of a data dictionary that contains the form data
# that would be submitted if I pressed the submit button labeled 'button 3'
data = response.form_data('button 3')
data['some-field'] = 'new value'
response = self.client.post('/path/to/page', data)
We also need to be careful of feature creep - this sort of testing is
exactly what Twill does, and we don't want to duplicate the efforts of
that project (or any other automated Python client test framework for
that matter).
Yours,
Russ Magee %-)
This should work:
response.__class__ = TestHttpResponse
-Forest
--
Forest Bond
http://www.alittletooquiet.net
http://www.pytagsfs.org
Hi,
On Thu, Aug 27, 2009 at 03:28:03PM -0100, Joshua Russo wrote:
> On Thu, Aug 27, 2009 at 11:54 AM, Joshua Russo <josh.r...@gmail.com> wrote:
> Ok, so I found that the way I was 'casting' the response object didn't work. IsThis should work:
> there no way to cast an instance of a base class to a child class in Python?
>
> What I did was to create a class method on my child class that takes the
> current response instance and creates a copy of it using my new response
> object.
>
> http://dpaste.com/hold/86193/
>
> Does this look appropriate, or is there a better way to do this?
response.__class__ = TestHttpResponse
Okay, you'd have to copy the class attributes, too. Or, you could just define a
sub-class on-the-fly:
class _TestHttpResponse(TestHttpResponse, response.__class__):
pass
response.__class__ = _TestHttpResponse
I haven't tested this.