def test_render_post_fail_callback(self):
        """
        Tests the form validation failure callback.
        """
        form = self.create_form('dummy_form')
        cstruct = {'dummy_cstruct': 'dummy_value'}
        form_validation_failure = self.create_form_validation_failure(
            form,
            cstruct=cstruct)
        request = self.create_post_request(form)
        result = {'test': 'value'}
        validation_failure_callback = MockCallback('failure_result')

        renderer = MultipleFormRenderer()
        renderer.add_form(form, validation_failure=validation_failure_callback)
        render_result = renderer.render(request, result)

        # validation failure occured
        self.assertEqual(form_validation_failure.get_render_counter(), 1)
        # validation failure callback called
        self.assertEqual(validation_failure_callback.get_call_counter(), 1)
        # request passed to callback
        self.assertEqual(validation_failure_callback.get_request(), request)
        # result passed to callback
        self.assertEqual(len(validation_failure_callback.get_result()), 2)
        self.assertTrue(
            'dummy_form' in validation_failure_callback.get_result())
        self.assertTrue('test' in validation_failure_callback.get_result())
        self.assertEqual(
            validation_failure_callback.get_result()['test'],
            'value')
        # validation appstruct passed to callback
        self.assertEqual(validation_failure_callback.get_appstruct(), cstruct)
        # render_result equals validation failure callback result
        self.assertEqual(render_result, 'failure_result')
    def test_render_post_succ_callback(self):
        """
        Tests the form validation success callback.
        """
        validate_appstruct = {'dummy_validate_appstruct': 'dummy_value'}
        form = self.create_form('dummy_form', validate_appstruct)
        request = self.create_post_request(form)
        result = {'test': 'value'}
        validation_success_callback = MockCallback('success_result')

        renderer = MultipleFormRenderer()
        renderer.add_form(form, validation_success=validation_success_callback)
        render_result = renderer.render(request, result)

        # validation success callback called
        self.assertEqual(validation_success_callback.get_call_counter(), 1)
        # request passed to callback
        self.assertEqual(validation_success_callback.get_request(), request)
        # result passed to callback
        self.assertEqual(len(validation_success_callback.get_result()), 2)
        self.assertTrue(
            'dummy_form' in validation_success_callback.get_result())
        self.assertTrue('test' in validation_success_callback.get_result())
        self.assertEqual(validation_success_callback.get_result()['test'],
                         'value')
        # validation appstruct passed to callback
        self.assertEqual(validation_success_callback.get_appstruct(),
                         validate_appstruct)
        # render_result equals validation success callback result
        self.assertEqual(render_result, 'success_result')
    def test_render_post_succ_callback(self):
        """
        Tests the form validation success callback.
        """
        validate_appstruct = {'dummy_validate_appstruct': 'dummy_value'}
        form = self.create_form('dummy_form', validate_appstruct)
        request = self.create_post_request(form)
        result = {'test': 'value'}
        validation_success_callback = MockCallback('success_result')

        renderer = MultipleFormRenderer()
        renderer.add_form(form, validation_success=validation_success_callback)
        render_result = renderer.render(request, result)

        # validation success callback called
        self.assertEqual(validation_success_callback.get_call_counter(), 1)
        # request passed to callback
        self.assertEqual(validation_success_callback.get_request(), request)
        # result passed to callback
        self.assertEqual(len(validation_success_callback.get_result()), 2)
        self.assertTrue(
            'dummy_form' in validation_success_callback.get_result())
        self.assertTrue('test' in validation_success_callback.get_result())
        self.assertEqual(
            validation_success_callback.get_result()['test'],
            'value')
        # validation appstruct passed to callback
        self.assertEqual(
            validation_success_callback.get_appstruct(),
            validate_appstruct)
        # render_result equals validation success callback result
        self.assertEqual(render_result, 'success_result')
    def test_render_post(self):
        """
        Tests the form rendering and form validation with a post request.
        """
        form1 = self.create_form('dummy_form1')
        form2 = self.create_form('dummy_form2')
        request = self.create_post_request(form1)
        result = {'test': 'value'}

        renderer = MultipleFormRenderer()
        renderer.add_form(form1)
        renderer.add_form(form2)
        render_result = renderer.render(request, result)

        # render_result contains result and rendered forms
        self.assertEqual(len(render_result), 3)
        # result in render_result
        self.assertTrue('test' in render_result)
        self.assertEqual(render_result['test'], 'value')
        # forms rendered
        self.assertTrue('dummy_form1' in render_result)
        self.assertTrue('dummy_form2' in render_result)
        form1.render.assert_has_calls([mock.call()])
        form2.render.assert_has_calls([mock.call()])
        # validation called with POST items
        form1.validate.assert_has_calls([mock.call(request.POST.items())])
예제 #5
0
def build_form_renderer():
    """
    Builds the form handler by creating and adding the forms.
    """

    # build forms
    membership_listing_date_pdf_form = deform.Form(
        MembershipListingDate(),
        buttons=[deform.Button('submit', _(u'Generate PDF'))],
        formid='membership_listing_date_pdf')

    membership_listing_year_end_pdf_form = deform.Form(
        MembershipListingYearEnd(),
        buttons=[deform.Button('submit', _(u'Generate PDF'))],
        formid='membership_listing_year_end_pdf'
    )

    # create form handler
    form_renderer = MultipleFormRenderer()

    # add forms
    form_renderer.add_form(
        membership_listing_date_pdf_form,
        membership_listing_date_pdf_callback)
    form_renderer.add_form(
        membership_listing_year_end_pdf_form,
        membership_listing_date_pdf_callback)
    return form_renderer
    def test_render_post_fail_callback(self):
        """
        Tests the form validation failure callback.
        """
        form = self.create_form('dummy_form')
        cstruct = {'dummy_cstruct': 'dummy_value'}
        form_validation_failure = self.create_form_validation_failure(
            form, cstruct=cstruct)
        request = self.create_post_request(form)
        result = {'test': 'value'}
        validation_failure_callback = MockCallback('failure_result')

        renderer = MultipleFormRenderer()
        renderer.add_form(form, validation_failure=validation_failure_callback)
        render_result = renderer.render(request, result)

        # validation failure occured
        self.assertEqual(form_validation_failure.get_render_counter(), 1)
        # validation failure callback called
        self.assertEqual(validation_failure_callback.get_call_counter(), 1)
        # request passed to callback
        self.assertEqual(validation_failure_callback.get_request(), request)
        # result passed to callback
        self.assertEqual(len(validation_failure_callback.get_result()), 2)
        self.assertTrue(
            'dummy_form' in validation_failure_callback.get_result())
        self.assertTrue('test' in validation_failure_callback.get_result())
        self.assertEqual(validation_failure_callback.get_result()['test'],
                         'value')
        # validation appstruct passed to callback
        self.assertEqual(validation_failure_callback.get_appstruct(), cstruct)
        # render_result equals validation failure callback result
        self.assertEqual(render_result, 'failure_result')
    def test_render_post(self):
        """
        Tests the form rendering and form validation with a post request.
        """
        form1 = self.create_form('dummy_form1')
        form2 = self.create_form('dummy_form2')
        request = self.create_post_request(form1)
        result = {'test': 'value'}

        renderer = MultipleFormRenderer()
        renderer.add_form(form1)
        renderer.add_form(form2)
        render_result = renderer.render(request, result)

        # render_result contains result and rendered forms
        self.assertEqual(len(render_result), 3)
        # result in render_result
        self.assertTrue('test' in render_result)
        self.assertEqual(render_result['test'], 'value')
        # forms rendered
        self.assertTrue('dummy_form1' in render_result)
        self.assertTrue('dummy_form2' in render_result)
        form1.render.assert_has_calls([mock.call()])
        form2.render.assert_has_calls([mock.call()])
        # validation called with POST items
        form1.validate.assert_has_calls([mock.call(request.POST.items())])
    def test_render_get(self):
        """
        Test the form rendering with a get request.
        """
        form = self.create_form('dummy_form')
        request = self.create_get_request()
        result = {'test': 'value'}

        renderer = MultipleFormRenderer()
        renderer.add_form(form)
        render_result = renderer.render(request, result)

        # render_result contains result and rendered form
        self.assertEqual(len(render_result), 2)
        # result in render_result
        self.assertTrue('test' in render_result)
        self.assertEqual(render_result['test'], 'value')
        # form rendered to render_result
        self.assertTrue('dummy_form' in render_result)
        form.render.assert_has_calls([mock.call()])
    def test_add_one_form(self):
        """
        Tests the MultipleFormRenderer.add() method by adding one single form.
        """
        form = self.create_form('dummy_form')
        request = self.create_get_request()
        result = {'test': 'value'}

        renderer = MultipleFormRenderer()
        renderer.add_form(form)
        render_result = renderer.render(request, result)

        # render_result contains result and rendered form
        self.assertEqual(len(render_result), 2)
        # result in render_result
        self.assertTrue('test' in render_result)
        self.assertEqual(render_result['test'], 'value')
        # form rendered to render_result
        self.assertTrue('dummy_form' in render_result)
        form.render.assert_called_once_with()
    def test_render_get(self):
        """
        Test the form rendering with a get request.
        """
        form = self.create_form('dummy_form')
        request = self.create_get_request()
        result = {'test': 'value'}

        renderer = MultipleFormRenderer()
        renderer.add_form(form)
        render_result = renderer.render(request, result)

        # render_result contains result and rendered form
        self.assertEqual(len(render_result), 2)
        # result in render_result
        self.assertTrue('test' in render_result)
        self.assertEqual(render_result['test'], 'value')
        # form rendered to render_result
        self.assertTrue('dummy_form' in render_result)
        form.render.assert_has_calls([mock.call()])
    def test_add_one_form(self):
        """
        Tests the MultipleFormRenderer.add() method by adding one single form.
        """
        form = self.create_form('dummy_form')
        request = self.create_get_request()
        result = {'test': 'value'}

        renderer = MultipleFormRenderer()
        renderer.add_form(form)
        render_result = renderer.render(request, result)

        # render_result contains result and rendered form
        self.assertEqual(len(render_result), 2)
        # result in render_result
        self.assertTrue('test' in render_result)
        self.assertEqual(render_result['test'], 'value')
        # form rendered to render_result
        self.assertTrue('dummy_form' in render_result)
        form.render.assert_called_once_with()
예제 #12
0
def build_form_renderer():
    """
    Builds the form handler by creating and adding the forms.
    """

    # build forms
    membership_listing_date_pdf_form = deform.Form(
        MembershipListingDate(),
        buttons=[deform.Button('submit', _(u'Generate PDF'))],
        formid='membership_listing_date_pdf')

    membership_listing_year_end_pdf_form = deform.Form(
        MembershipListingYearEnd(),
        buttons=[deform.Button('submit', _(u'Generate PDF'))],
        formid='membership_listing_year_end_pdf')

    # create form handler
    form_renderer = MultipleFormRenderer()

    # add forms
    form_renderer.add_form(membership_listing_date_pdf_form,
                           membership_listing_date_pdf_callback)
    form_renderer.add_form(membership_listing_year_end_pdf_form,
                           membership_listing_date_pdf_callback)
    return form_renderer
    def test_render_post_valid_exc(self):
        """
        Tests the form rendering and validation with a validateion failure
        exception.
        """
        form = self.create_form('dummy_form')
        validation_failure = self.create_form_validation_failure(
            form, 'rendered_invalid_form')
        request = self.create_post_request(form)

        renderer = MultipleFormRenderer()
        renderer.add_form(form)
        render_result = renderer.render(request, {})

        # render_result contains rendered form
        self.assertEqual(len(render_result), 1)
        # forms rendered with validation failure
        self.assertTrue('dummy_form' in render_result)
        form.render.assert_has_calls([mock.call()])
        self.assertEqual(render_result['dummy_form'], 'rendered_invalid_form')
        # form validation failure rendered
        self.assertEqual(validation_failure.get_render_counter(), 1)
    def test_render_post_valid_exc(self):
        """
        Tests the form rendering and validation with a validateion failure
        exception.
        """
        form = self.create_form('dummy_form')
        validation_failure = self.create_form_validation_failure(
            form,
            'rendered_invalid_form')
        request = self.create_post_request(form)

        renderer = MultipleFormRenderer()
        renderer.add_form(form)
        render_result = renderer.render(request, {})

        # render_result contains rendered form
        self.assertEqual(len(render_result), 1)
        # forms rendered with validation failure
        self.assertTrue('dummy_form' in render_result)
        form.render.assert_has_calls([mock.call()])
        self.assertEqual(render_result['dummy_form'], 'rendered_invalid_form')
        # form validation failure rendered
        self.assertEqual(validation_failure.get_render_counter(), 1)
    def test_add_two_forms(self):
        """
        Tests the MultipleFormRenderer.add() method by adding two forms.
        """
        form1 = self.create_form('dummy_form1')
        form2 = self.create_form('dummy_form2')
        request = self.create_get_request()
        result = {'test': 'value'}

        renderer = MultipleFormRenderer()
        renderer.add_form(form1)
        renderer.add_form(form2)
        render_result = renderer.render(request, result)

        # render_result contains result and rendered forms
        self.assertEqual(len(render_result), 3)
        # result in render_result
        self.assertTrue('test' in render_result)
        self.assertEqual(render_result['test'], 'value')
        # form rendered to render_result
        self.assertTrue('dummy_form1' in render_result)
        self.assertTrue('dummy_form2' in render_result)
        form1.render.assert_called_once_with()
        form2.render.assert_called_once_with()
    def test_add_two_forms(self):
        """
        Tests the MultipleFormRenderer.add() method by adding two forms.
        """
        form1 = self.create_form('dummy_form1')
        form2 = self.create_form('dummy_form2')
        request = self.create_get_request()
        result = {'test': 'value'}

        renderer = MultipleFormRenderer()
        renderer.add_form(form1)
        renderer.add_form(form2)
        render_result = renderer.render(request, result)

        # render_result contains result and rendered forms
        self.assertEqual(len(render_result), 3)
        # result in render_result
        self.assertTrue('test' in render_result)
        self.assertEqual(render_result['test'], 'value')
        # form rendered to render_result
        self.assertTrue('dummy_form1' in render_result)
        self.assertTrue('dummy_form2' in render_result)
        form1.render.assert_called_once_with()
        form2.render.assert_called_once_with()