无法发布Flask表单数据进行测试(FieldList)[DeprecationWarning]

时间:2018-07-10 10:50:23

标签: python python-3.x flask flask-wtforms werkzeug

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

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

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

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

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')

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

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.

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

任何帮助将不胜感激。

谢谢!

更新

第一个表格的数据如下所示:

{
  '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
}

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

0 个答案:

没有答案