Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes #3212 -- Added code to test client to allow posting of multi-fo…

…rm values (e.g., MultipleChoiceFields). Thanks, Ben Dean Kawamura.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4774 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f157bcefcac9b921658c6782d4a64c173a203f12 1 parent 635fedc
Russell Keith-Magee authored March 22, 2007
8  django/test/client.py
@@ -69,6 +69,14 @@ def encode_multipart(boundary, data):
69 69
                 '',
70 70
                 value.read()
71 71
             ])
  72
+        elif hasattr(value, '__iter__'): 
  73
+            for item in value:
  74
+                lines.extend([ 
  75
+                    '--' + boundary, 
  76
+                    'Content-Disposition: form-data; name="%s"' % key, 
  77
+                    '', 
  78
+                    str(item) 
  79
+                ])
72 80
         else:
73 81
             lines.extend([
74 82
                 '--' + boundary,
38  tests/modeltests/test_client/models.py
@@ -81,7 +81,43 @@ def test_redirect(self):
81 81
         
82 82
         # Check that the response was a 302 (redirect)
83 83
         self.assertEqual(response.status_code, 302)
84  
-                
  84
+
  85
+    def test_valid_form(self):
  86
+        "POST valid data to a form"
  87
+        post_data = {
  88
+            'text': 'Hello World',
  89
+            'email': 'foo@example.com',
  90
+            'value': 37,
  91
+            'single': 'b',
  92
+            'multi': ('b','c','e')
  93
+        }
  94
+        response = self.client.post('/test_client/form_view/', post_data)
  95
+        self.assertEqual(response.status_code, 200)
  96
+        self.assertEqual(response.template.name, "Valid POST Template")
  97
+
  98
+    def test_incomplete_data_form(self):
  99
+        "POST incomplete data to a form"
  100
+        post_data = {
  101
+            'text': 'Hello World',
  102
+            'value': 37            
  103
+        }
  104
+        response = self.client.post('/test_client/form_view/', post_data)
  105
+        self.assertEqual(response.status_code, 200)
  106
+        self.assertEqual(response.template.name, "Invalid POST Template")
  107
+
  108
+    def test_form_error(self):
  109
+        "POST erroneous data to a form"
  110
+        post_data = {
  111
+            'text': 'Hello World',
  112
+            'email': 'not an email address',
  113
+            'value': 37,
  114
+            'single': 'b',
  115
+            'multi': ('b','c','e')
  116
+        }
  117
+        response = self.client.post('/test_client/form_view/', post_data)
  118
+        self.assertEqual(response.status_code, 200)
  119
+        self.assertEqual(response.template.name, "Invalid POST Template")
  120
+        
85 121
     def test_unknown_page(self):
86 122
         "GET an invalid URL"
87 123
         response = self.client.get('/test_client/unknown_view/')
1  tests/modeltests/test_client/urls.py
@@ -6,6 +6,7 @@
6 6
     (r'^post_view/$', views.post_view),
7 7
     (r'^raw_post_view/$', views.raw_post_view),
8 8
     (r'^redirect_view/$', views.redirect_view),
  9
+    (r'^form_view/$', views.form_view),
9 10
     (r'^login_protected_view/$', views.login_protected_view),
10 11
     (r'^session_view/$', views.session_view),
11 12
     (r'^broken_view/$', views.broken_view)
34  tests/modeltests/test_client/views.py
@@ -2,6 +2,8 @@
2 2
 from django.template import Context, Template
3 3
 from django.http import HttpResponse, HttpResponseRedirect
4 4
 from django.contrib.auth.decorators import login_required
  5
+from django.newforms.forms import Form
  6
+from django.newforms import fields
5 7
 
6 8
 def get_view(request):
7 9
     "A simple view that expects a GET request, and returns a rendered template"
@@ -45,7 +47,39 @@ def raw_post_view(request):
45 47
 def redirect_view(request):
46 48
     "A view that redirects all requests to the GET view"
47 49
     return HttpResponseRedirect('/test_client/get_view/')
  50
+
  51
+TestChoices = (
  52
+    ('a', 'First Choice'),
  53
+    ('b', 'Second Choice'),
  54
+    ('c', 'Third Choice'),
  55
+    ('d', 'Fourth Choice'),
  56
+    ('e', 'Fifth Choice')
  57
+)
  58
+
  59
+class TestForm(Form):
  60
+    text = fields.CharField()
  61
+    email = fields.EmailField()
  62
+    value = fields.IntegerField()
  63
+    single = fields.ChoiceField(choices=TestChoices)
  64
+    multi = fields.MultipleChoiceField(choices=TestChoices)
  65
+    
  66
+def form_view(request):
  67
+    "A view that tests a simple form"
  68
+    if request.method == 'POST':
  69
+        form = TestForm(request.POST)
  70
+        if form.is_valid():
  71
+            t = Template('Valid POST data.', name='Valid POST Template')
  72
+            c = Context()
  73
+        else:
  74
+            t = Template('Invalid POST data. {{ form.errors }}', name='Invalid POST Template')
  75
+            c = Context({'form': form})
  76
+    else:
  77
+        form = TestForm()
  78
+        t = Template('Viewing base form. {{ form }}.', name='Form GET Template')
  79
+        c = Context({'form': form})
48 80
     
  81
+    return HttpResponse(t.render(c))
  82
+        
49 83
 def login_protected_view(request):
50 84
     "A simple view that is login protected."
51 85
     t = Template('This is a login protected test. Username is {{ user.username }}.', name='Login Template')

0 notes on commit f157bce

Please sign in to comment.
Something went wrong with that request. Please try again.