Ticket #9200: wizard_tests.patch

File wizard_tests.patch, 6.8 KB (added by ddurham, 7 years ago)
  • home/david/work/django/django-trunk/django/contrib/formtools/tests.py

     
    11from django import forms
     2from django.db import models
    23from django.contrib.formtools import preview, wizard
    34from django import http
    45from django.test import TestCase
     6from django.test.client import Client
    57
     8
    69success_string = "Done was called!"
    710
    811class TestFormPreview(preview.FormPreview):
     
    141144        request = DummyRequest(POST={"0-field":"test", "wizard_step":"0"})
    142145        response = wizard(request)
    143146        self.assertEquals(1, wizard.step)
     147       
    144148
     149#
     150# SessionWizard tests
     151#
     152class SessionWizardModel(models.Model):
     153    field = models.CharField(max_length=10)
     154   
     155class SessionWizardPageOneForm(forms.Form):
     156    field = forms.CharField(required=True)
     157
     158class SessionWizardPageTwoForm(forms.ModelForm):
     159    class Meta:
     160        model = SessionWizardModel
     161
     162class SessionWizardPageThreeForm(forms.Form):
     163    field = forms.CharField()
     164
     165class SessionWizardDynamicPageForm(forms.Form):
     166    field = forms.CharField()
     167       
     168class SessionWizardClass(wizard.SessionWizard):
     169    def get_page_title(self, session, page0):
     170        return "Custom Page Title: %s" % str(page0 + 1)
     171
     172    def process_show_form(self, request, page0, form):
     173        return {'form_title' : 'Form %s' % str(page0 + 1)}
     174
     175    def preprocess_submit_form(self, request, page0, form):
     176        if page0 == 1 and request.POST['field'] == "":
     177            self._remove_page(request.session, page0)
     178            return page0 - 1
     179
     180    def process_submit_form(self, request, page0, form):
     181        if page0 == 2:
     182            self._insert_page(request.session, page0 + 1,
     183                              SessionWizardDynamicPageForm)
     184   
     185    def get_template(self, page0):
     186        return "formtools/form.html"
     187
     188    def done(self, request):
     189        return http.HttpResponse(success_string)
     190
     191class SessionWizardTests(TestCase):
     192    urls = 'django.contrib.formtools.test_urls'
     193
     194    def test_valid_POST(self):
     195        """
     196        Tests that a post containing valid data will set session values
     197        correctly and redirect to the next page.
     198        """
     199        response = self.client.post('/sessionwizard/0', {"page0":"0",
     200                                                          "field":"test"})
     201        self.assertEquals(302, response.status_code)
     202        self.assertEquals("http://testserver/sessionwizard/1",
     203                          response['Location'])
     204        session = self.client.session
     205        cleaned_data = session['session_wizard_data']['cleaned_data']
     206        post_data = session['session_wizard_data']['POST_data']
     207        self.assertEquals('test', cleaned_data[0]['field'])
     208        self.assertEquals('test', post_data[0]['field'])
     209
     210    def test_invalid_POST(self):
     211        """
     212        Tests that a post containing invalid data will set session values
     213        correctly and redisplay the form.
     214        """
     215        response = self.client.post('/sessionwizard/0', {"page0":"0",
     216                                                          "field":""})
     217        self.assertEquals(200, response.status_code)
     218        session = self.client.session
     219        post_data = session['session_wizard_data']['POST_data']
     220        self.assertEquals('', post_data[0]['field'])
     221   
     222    def test_GET(self):
     223        """
     224        Tests that a get will display a page properly.
     225        """
     226        response = self.client.get('/sessionwizard/0')
     227        self.assertEquals(200, response.status_code)
     228   
     229    def test_preprocess_submit_form(self):
     230        """
     231        Tests the preprocess_submit_form hook of SessionWizard POSTs.
     232        The SessionWizardClass is coded to short-circuit a POST for page index 1
     233        when form.cleaned_data['field'] == '' by returning a reference to page0
     234        index 0.
     235        """
     236        response = self.client.post('/sessionwizard/1', {"page0":"1",
     237                                                          "field":""})
     238        self.assertEquals(302, response.status_code)
     239        self.assertEquals("http://testserver/sessionwizard/0",
     240                          response['Location'])
     241   
     242    def test_process_submit_form(self):
     243        """
     244        Tests the process_submit_form hook of SessionWizard POSTs.
     245        The SessionWizardClass is coded to insert a new page at index 3 on a
     246        POST for page index 2.
     247        """
     248        response = self.client.post('/sessionwizard/2', {"page0":"2",
     249                                                          "field":"test"})
     250        self.assertEquals(302, response.status_code)
     251        self.assertEquals("http://testserver/sessionwizard/3",
     252                          response['Location'])
     253        self.assertEquals([SessionWizardPageOneForm,
     254                           SessionWizardPageTwoForm,
     255                           SessionWizardPageThreeForm,
     256                           SessionWizardDynamicPageForm],
     257            self.client.session['session_wizard_data']['form_list'])
     258   
     259    def test_process_show_form(self):
     260        """
     261        Tests the process_show_form hook.  SessionWizardClass is coded to
     262        return a extra_context having a specific 'form_title' attribute.
     263        """
     264        response = self.client.get('/sessionwizard/0')
     265        self.assertEquals(200, response.status_code)
     266        self.assertEquals("Form 1",
     267                          response.context[0]['extra_context']['form_title'])
     268   
     269    def test_validate_all(self):
     270        """
     271        Submits all forms, with one of them being invalid, and tests that
     272        submitting the last form will catch an invalid form earlier in the
     273        workflow and redisplay it.
     274        """
     275        self.client.post('/sessionwizard/0', {"page0":"0", "field":""})
     276        self.client.post('/sessionwizard/1', {"page0":"1", "field":"test1"})
     277        self.client.post('/sessionwizard/2', {"page0":"2", "field":"test2"})
     278        response = self.client.post('/sessionwizard/3',
     279                                   {"page0":"3", "field":"test3"})
     280        self.assertEquals("Form 1",
     281                          response.context[0]['extra_context']['form_title'])
     282   
     283    def test_done(self):
     284        self.client.post('/sessionwizard/0', {"page0":"0", "field":"test0"})
     285        self.client.post('/sessionwizard/1', {"page0":"1", "field":"test1"})
     286        self.client.post('/sessionwizard/2', {"page0":"2", "field":"test2"})
     287        response = self.client.post('/sessionwizard/3',
     288                                   {"page0":"3", "field":"test3"})
     289        self.assertEqual(response.content, success_string)
Back to Top