无法发布Flask表单数据进行测试(FieldList)[DeprecationWarning] [英] Can't post Flask form data for testing (FieldList) [DeprecationWarning]

查看:58
本文介绍了无法发布Flask表单数据进行测试(FieldList)[DeprecationWarning]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写几种不同的烧瓶形式.到目前为止,我已经完成了测试这些表格使用的路线的工作.

I'm writing several different flask forms. So far, what I've done to test the routes that I use with those forms is.

  • 创建表单对象
  • 填充表格
  • 过帐表单数据

例如,这可行(该类的简短版本):

For example, this works (short version of the class):

class RequestForm(FlaskForm):
project = SelectField(label='Project', id='project', coerce=str)
name = StringField('Environment Name', id='env_name', validators=[DataRequired()])
requested_by = StringField('Requested By', validators=[DataRequired()])
tag = StringField('Tag')
required_from = DateField('Required From', format='%Y-%m-%d', validators=[DataRequired()])
required_to = DateField('Required To', format='%Y-%m-%d', validators=[Optional()])

在测试时:

def test_post_full_request_is_correct(self):
        with self.app.test_client() as http_client:
            self.login(http_client)

        required_from = str(datetime.datetime.today().date() + datetime.timedelta(days=1))

        form = RequestForm()
        form.project.data = 'SO'
        form.name.data = 'TEST02'
        form.requested_by.data = 'dev01'
        form.required_from.data = required_from
        form.env_type.data = 'DEV'
        form.location.data = 'Narnia'
        form.size.data = 'L'
        form.resilience.data = '0'
        form.submit.data = True

        response = http_client.post('/request', data=form.data)

这很好.

但是,我也有这些表格.主要的是RequestsComponentsForm.

However, I've also got these forms. The main one being RequestsComponentsForm.

class RequestComponentForm(FlaskForm):
component = StringField('Name', validators=[DataRequired()])
source = SelectField('Source', choices=[('nexus', 'Nexus')])
version = StringField('Version')

def __init__(self, *args, **kwargs):
    kwargs['csrf_enabled'] = False
    super(RequestComponentForm, self).__init__(*args, **kwargs)


class RequestComponentsForm(FlaskForm):
    components = FieldList(FormField(RequestComponentForm))
    submit = SubmitField('Request Environment')

手动测试时,它就像一个超级按钮.但是,当我尝试自动测试时:

It works like a charm when testing it manually. However, when I tried automated testing:

c_form = RequestComponentsForm()
components = ['app-a', 'app-b', 'app-c', 'app-d', 'app-e', 'app-f']
test_versions = ['1.2.3', '3.2.1', '2.1.3', '5.7.1', '3.6.3', '1.4.6']
for c, v in zip(log_design_components, test_versions):
  entry = RequestComponentForm()
  entry.component = c
  entry.source = 'nexus'
  entry.version = v
  c_form.components.append_entry(entry)

  response = http_client.post('/request{0}components'.format(env_request_id),
             headers={'Referer': good_referrer},
             data=c_form.data)

我得到以下信息:

venv/lib/python3.6/site-packages/werkzeug/test.py:349: DeprecationWarning: it's no longer possible to pass dicts as `data`.  Use tuples or FileStorage objects instead.

我不明白为什么我测试第一种形式的方法行得通,而第二种形式的方法却行不通.

I don't understand why my approach to testing the first form works and the same approach for the 2nd one doesn't.

任何帮助将不胜感激.

谢谢!

更新

这是第一个表格的数据的样子:

This is what the first form's data looks like:

{
  'project': 'SO',
  'name': 'TEST02',
  'requested_by': 'dev01',
  'tag': '',
  'required_from': '2018-07-11',
  'required_to': None,
  'monday_on': None,
  'monday_off': None,
  'tuesday_on': None,
  'tuesday_off': None,
  'wednesday_on': None,
  'wednesday_off': None,
  'thursday_on': None,
  'thursday_off': None,
  'friday_on': None,
  'friday_off': None,
  'saturday_on': None,
  'saturday_off': None,
  'sunday_on': None,
  'sunday_off': None,
  'env_type': 'DEV',
  'location': 'Narnia',
  'size': 'L',
  'resilience': '0',
  'submit': True
}

第二个:

{
  'components': [
    {
      'component': 'app-a',
      'source': 'nexus',
      'version': '1.2.3'
    },
    {
      'component': 'app-b',
      'source': 'nexus',
      'version': '3.2.1'
    },
    {
      'component': 'app-c',
      'source': 'nexus',
      'version': '2.1.3'
    },
    {
      'component': 'app-d',
      'source': 'nexus',
      'version': '5.7.1'
    },
    {
      'component': 'app-e',
      'source': 'nexus',
      'version': '3.6.3'
    },
    {
      'component': 'app-f',
      'source': 'nexus',
      'version': '1.4.6'
    }
  ],
  'submit': True
}

它们都是字典,所以我真的不明白为什么第一个可以工作而另一个不能工作.

They're both dictionaries, so I really don't understand why the first one would work and the other one won't.

推荐答案

FieldList是WTF中一组字段的便捷包装.发布数据时,应使用原始表单,对于第二种表单,有效负载应如下所示:

FieldList is a handy wrapper of a group of fields in WTF. While posting data, you should use the original form, for the second form, the payload should be as follows:

{
  'components-0-component': 'app-a',
  'components-0-source': 'nexus',
  'components-0-version': '1.2.3',
  'components-1-component': 'app-b',
  'components-1-source': 'nexus',
  'components-1-version': '3.2.1',
  ...
}

这篇关于无法发布Flask表单数据进行测试(FieldList)[DeprecationWarning]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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