mardi 10 juillet 2018

Can't post Flask form data for testing

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

  • Creating the form object
  • Populating the form
  • Posting the form data

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()])

And when testing:

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)

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)

I get the following:

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 apprach to testing the first form works and the same approach for the 2nd one doesn't.

Any help would be greatly appreciated.

Thanks!

Aucun commentaire:

Enregistrer un commentaire