Commits

Anonymous committed 6345118

Fixes #3212 -- Added code to test client to allow posting of multi-form values (e.g., MultipleChoiceFields). Thanks, Ben Dean Kawamura.

Comments (0)

Files changed (4)

django/test/client.py

                 '',
                 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,

tests/modeltests/test_client/models.py

         
         # 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/')

tests/modeltests/test_client/urls.py

     (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)

tests/modeltests/test_client/views.py

 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"
 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')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.