测试使用Flask-WTF validate_on_submit的POST [英] Testing a POST that uses Flask-WTF validate_on_submit

查看:69
本文介绍了测试使用Flask-WTF validate_on_submit的POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑地测试POST,以向使用Flask_WTF进行验证和CSRF保护的数据库中添加类别.对于CRUD操作,请访问我的网站.我用过Flask,Flask_WTF和Flask-SQLAlchemy.这是我的第一个独立项目,我对如何测试Flask-WTForm validate_on_submit函数有些困惑.

I am stumped on testing a POST to add a category to the database where I've used Flask_WTF for validation and CSRF protection. For the CRUD operations pm my website. I've used Flask, Flask_WTF and Flask-SQLAlchemy. It is my first independent project, and I find myself a little at a lost on how to test the Flask-WTForm validate_on_submit function.

这是模型:

class Users(db.Model):
    id = db.Column(db.Integer, primary_key=True, unique=True)
    name = db.Column(db.String(80), nullable=False)
    email = db.Column(db.String(250), unique=True)

class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True, unique=True)
    name = db.Column(db.String(250), nullable=False, unique=True)
    users_id = db.Column(db.Integer, db.ForeignKey('users.id'))

这是表格:

class CategoryForm(Form):
    name = StringField(
        'Name', [validators.Length(min=4, max=250, message="name problem")])

这是控制器:

@category.route('/category/add', methods=['GET', 'POST'])
@login_required
def addCategory():
    """ Add a new category.
        Returns: Redirect Home.
    """
    # Initiate the form.
    form = CategoryForm()
    # On POST of a valid form, add the new category.
    if form.validate_on_submit():
        category = Category(
            form.name.data, login_session['users_id'])
        db.session.add(category)
        db.session.commit()
        flash('New Category %s Successfully Created' % category.name)
        return redirect(url_for('category.showHome'))
    else:
        # Render the form to add the category.
        return render_template('newCategory.html', form=form)

如何使用validate_on_submit函数为if语句编写测试?

How do I write a test for the if statement with the validate_on_submit function?

推荐答案

使用py.test和使用pytest和SQLAlchemy进行了令人愉快的测试,这是一个确认添加类别的测试.

Using py.test and a conftest.py recommended by Delightful testing with pytest and SQLAlchemy, here's a test that confirms the added category.

def test_add_category_post(app, session):
    """Does add category post a new category?"""
    TESTEMAIL = "test@test.org"
    TESTUSER = "Joe Test"
    user = Users.query.filter(Users.email==TESTEMAIL).first()
    category = Category(name="Added Category", users_id=user.id)
    form = CategoryForm(formdata=None, obj=category)
    with app.test_client() as c:
        with c.session_transaction() as sess:
            sess['email'] = TESTEMAIL
            sess['username'] = TESTUSER 
            sess['users_id'] = user.id
            response = c.post(
                '/category/add', data=form.data, follow_redirects=True)
    assert response.status_code == 200
    added_category = Category.query.filter(
        Category.name=="Added Category").first()
    assert added_category
    session.delete(added_category)
    session.commit()

请注意,新类别已分配给变量,然后用于创建表单.表单中的数据将在帖子中使用.

Note that the new category is assigned to a variable and then used to create a form. The form's data is used in the post.

这篇关于测试使用Flask-WTF validate_on_submit的POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆