def test_form_valid(self, existing):
        instance = sqlalchemy.ModelFormMixin()
        if existing:
            instance.object = obj = Mock()
        instance.model = Mock()

        form = Mock()

        mocks = Mock()
        with patch.object(sqlalchemy, 'session') as m1:
            with patch.object(sqlalchemy.FormMixin, 'form_valid') as m2:
                with patch.object(sqlalchemy, '_touch') as m3:

                    mocks.attach_mock(m1, 'session')
                    mocks.attach_mock(form, 'form')
                    mocks.attach_mock(m3, 'touch')

                    assert instance.form_valid(form) == m2.return_value

                    m2.assert_called_once_with(form)

                    calls = [call.form.populate_obj(instance.object),
                             call.session.commit(),
                             call.touch(instance.object)]

                    if not existing:
                        assert instance.object == instance.model.return_value

                        calls.insert(0, call.session.add(instance.object))
                    else:
                        assert instance.object == obj

                    mocks.assert_has_calls(calls)
    def test_delete(self):
        instance = sqlalchemy.DeletionMixin()
        instance.get_object = get_object = Mock()
        instance.get_success_url = get_success_url = Mock()

        mocks = Mock()

        with patch.object(sqlalchemy, 'session') as m1:
            with patch.object(sqlalchemy, 'redirect') as m2:
                with patch.object(sqlalchemy, '_touch') as m3:
                    mocks.attach_mock(m1, 'session')
                    mocks.attach_mock(m3, 'touch')

                    assert instance.delete() == m2.return_value
                    assert instance.object == get_object.return_value

                    calls = [call.session.delete(instance.object),
                             call.session.commit(),
                             call.touch(instance.object)]

                    mocks.assert_has_calls(calls)

                    m2.assert_called_once_with(get_success_url.return_value)