def test_get_success_url(self):
        instance = core.FormMixin()
        instance.success_url = Mock()
        assert instance.get_success_url() == instance.success_url

        instance = core.FormMixin()
        with pytest.raises(NotImplementedError) as excinfo:
            instance.get_success_url()

        error = ("FormMixin requires either a definition of 'success_url' "
                 "or an implementation of 'get_success_url()'")

        assert excinfo.value.args[0] == error
    def test_get_form_class(self):
        instance = core.FormMixin()
        instance.form_class = Mock()

        assert instance.get_form_class() == instance.form_class

        instance = core.FormMixin()
        with pytest.raises(NotImplementedError) as excinfo:
            instance.get_form_class()

        error = ("FormMixin requires either a definition of 'form_class' or "
                 "an implementation of 'get_form_class()'")

        assert excinfo.value.args[0] == error
    def test_form_valid(self):
        instance = core.FormMixin()
        instance.get_success_url = Mock()

        with patch.object(core, 'redirect') as m:
            assert instance.form_valid(None) == m.return_value

            m.assert_called_once_with(instance.get_success_url.return_value)
    def test_get_form(self, kwargs):
        instance = core.FormMixin()
        instance.get_form_kwargs = lambda: kwargs
        instance.form_class = Mock()

        assert instance.get_form() == instance.form_class.return_value

        instance.form_class.assert_called_once_with(**kwargs)
    def test_get_formdata(self, form, files):
        instance = core.FormMixin()

        data = CombinedMultiDict([form, files])

        with patch.object(core, 'request') as m:
            m.form = form
            m.files = files

            assert instance.get_formdata() == data
    def test_get_context_data(self, kwargs):
        instance = core.FormMixin()
        instance.get_form = Mock()

        context = kwargs.copy()
        context.setdefault('form', instance.get_form.return_value)

        with patch.object(core.ContextMixin, 'get_context_data') as m:
            assert instance.get_context_data(**kwargs) == m.return_value

            m.assert_called_once_with(**context)
    def test_form_invalid(self):
        instance = core.FormMixin()
        instance.get_context_data = get_context_data = Mock()
        instance.create_response = render = Mock()

        form = Mock()

        assert instance.form_invalid(form) == render.return_value

        get_context_data.assert_called_once_with(form=form)
        render.assert_called_once_with(get_context_data.return_value)
    def test_get_form_kwargs(self, method):
        instance = core.FormMixin()
        instance.get_data = Mock()
        instance.get_prefix = Mock()
        instance.get_formdata = Mock()

        with patch.object(core, 'request') as m:
            m.method = method

            kwargs = {'data': instance.get_data.return_value,
                      'prefix': instance.get_prefix.return_value}

            if method in ('POST', 'PUT'):
                kwargs['formdata'] = instance.get_formdata.return_value

            assert instance.get_form_kwargs() == kwargs
    def test_get_prefix(self):
        instance = core.FormMixin()
        instance.prefix = Mock()

        assert instance.get_prefix() == instance.prefix
    def test_get_data(self):
        instance = core.FormMixin()
        instance.data = Mock()

        assert instance.get_data() == instance.data.copy.return_value