mirror of https://github.com/django/django.git
Fixes #3212 -- Added code to test client to allow posting of multi-form values (e.g., MultipleChoiceFields). Thanks, Ben Dean Kawamura.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4774 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
635fedcfa8
commit
f157bcefca
|
@ -69,6 +69,14 @@ def encode_multipart(boundary, data):
|
|||
'',
|
||||
value.read()
|
||||
])
|
||||
elif hasattr(value, '__iter__'):
|
||||
for item in value:
|
||||
lines.extend([
|
||||
'--' + boundary,
|
||||
'Content-Disposition: form-data; name="%s"' % key,
|
||||
'',
|
||||
str(item)
|
||||
])
|
||||
else:
|
||||
lines.extend([
|
||||
'--' + boundary,
|
||||
|
|
|
@ -81,7 +81,43 @@ class ClientTest(TestCase):
|
|||
|
||||
# Check that the response was a 302 (redirect)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
|
||||
|
||||
def test_valid_form(self):
|
||||
"POST valid data to a form"
|
||||
post_data = {
|
||||
'text': 'Hello World',
|
||||
'email': 'foo@example.com',
|
||||
'value': 37,
|
||||
'single': 'b',
|
||||
'multi': ('b','c','e')
|
||||
}
|
||||
response = self.client.post('/test_client/form_view/', post_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.template.name, "Valid POST Template")
|
||||
|
||||
def test_incomplete_data_form(self):
|
||||
"POST incomplete data to a form"
|
||||
post_data = {
|
||||
'text': 'Hello World',
|
||||
'value': 37
|
||||
}
|
||||
response = self.client.post('/test_client/form_view/', post_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.template.name, "Invalid POST Template")
|
||||
|
||||
def test_form_error(self):
|
||||
"POST erroneous data to a form"
|
||||
post_data = {
|
||||
'text': 'Hello World',
|
||||
'email': 'not an email address',
|
||||
'value': 37,
|
||||
'single': 'b',
|
||||
'multi': ('b','c','e')
|
||||
}
|
||||
response = self.client.post('/test_client/form_view/', post_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.template.name, "Invalid POST Template")
|
||||
|
||||
def test_unknown_page(self):
|
||||
"GET an invalid URL"
|
||||
response = self.client.get('/test_client/unknown_view/')
|
||||
|
|
|
@ -6,6 +6,7 @@ urlpatterns = patterns('',
|
|||
(r'^post_view/$', views.post_view),
|
||||
(r'^raw_post_view/$', views.raw_post_view),
|
||||
(r'^redirect_view/$', views.redirect_view),
|
||||
(r'^form_view/$', views.form_view),
|
||||
(r'^login_protected_view/$', views.login_protected_view),
|
||||
(r'^session_view/$', views.session_view),
|
||||
(r'^broken_view/$', views.broken_view)
|
||||
|
|
|
@ -2,6 +2,8 @@ from xml.dom.minidom import parseString
|
|||
from django.template import Context, Template
|
||||
from django.http import HttpResponse, HttpResponseRedirect
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.newforms.forms import Form
|
||||
from django.newforms import fields
|
||||
|
||||
def get_view(request):
|
||||
"A simple view that expects a GET request, and returns a rendered template"
|
||||
|
@ -45,7 +47,39 @@ def raw_post_view(request):
|
|||
def redirect_view(request):
|
||||
"A view that redirects all requests to the GET view"
|
||||
return HttpResponseRedirect('/test_client/get_view/')
|
||||
|
||||
TestChoices = (
|
||||
('a', 'First Choice'),
|
||||
('b', 'Second Choice'),
|
||||
('c', 'Third Choice'),
|
||||
('d', 'Fourth Choice'),
|
||||
('e', 'Fifth Choice')
|
||||
)
|
||||
|
||||
class TestForm(Form):
|
||||
text = fields.CharField()
|
||||
email = fields.EmailField()
|
||||
value = fields.IntegerField()
|
||||
single = fields.ChoiceField(choices=TestChoices)
|
||||
multi = fields.MultipleChoiceField(choices=TestChoices)
|
||||
|
||||
def form_view(request):
|
||||
"A view that tests a simple form"
|
||||
if request.method == 'POST':
|
||||
form = TestForm(request.POST)
|
||||
if form.is_valid():
|
||||
t = Template('Valid POST data.', name='Valid POST Template')
|
||||
c = Context()
|
||||
else:
|
||||
t = Template('Invalid POST data. {{ form.errors }}', name='Invalid POST Template')
|
||||
c = Context({'form': form})
|
||||
else:
|
||||
form = TestForm()
|
||||
t = Template('Viewing base form. {{ form }}.', name='Form GET Template')
|
||||
c = Context({'form': form})
|
||||
|
||||
return HttpResponse(t.render(c))
|
||||
|
||||
def login_protected_view(request):
|
||||
"A simple view that is login protected."
|
||||
t = Template('This is a login protected test. Username is {{ user.username }}.', name='Login Template')
|
||||
|
|
Loading…
Reference in New Issue