Update flask form to require fields only when appropriate

This commit is contained in:
Patrick Smith 2018-08-12 12:06:12 -04:00
parent 6e763773b5
commit 3e6c4d7f13
2 changed files with 79 additions and 0 deletions

View File

@ -1,11 +1,27 @@
from wtforms.fields.html5 import IntegerField from wtforms.fields.html5 import IntegerField
from wtforms.fields import RadioField, TextAreaField, SelectField from wtforms.fields import RadioField, TextAreaField, SelectField
from wtforms.validators import Optional
from .fields import DateField from .fields import DateField
from .forms import ValidatedForm from .forms import ValidatedForm
class RequestForm(ValidatedForm): class RequestForm(ValidatedForm):
def validate(self, *args, **kwargs):
if self.jedi_migration.data == 'no':
self.rationalization_software_systems.validators.append(Optional())
self.technical_support_team.validators.append(Optional())
self.organization_providing_assistance.validators.append(Optional())
self.engineering_assessment.validators.append(Optional())
self.data_transfers.validators.append(Optional())
self.expected_completion_date.validators.append(Optional())
elif self.jedi_migration.data == 'yes':
if self.technical_support_team.data == 'no':
self.organization_providing_assistance.validators.append(Optional())
self.cloud_native.validators.append(Optional())
return super(RequestForm, self).validate(*args, **kwargs)
# Details of Use: General # Details of Use: General
dod_component = SelectField( dod_component = SelectField(
"DoD Component", "DoD Component",

View File

@ -0,0 +1,63 @@
import pytest
from atst.forms.request import RequestForm
class TestRequestForm:
form_data = {
'dod_component': 'us_air_force',
'jedi_usage': 'cloud-ify all the things',
'num_software_systems': '12',
'estimated_monthly_spend': '34',
'dollar_value': '42',
'number_user_sessions': '6',
'average_daily_traffic': '0',
'start_date': '12/12/2012',
}
def test_require_cloud_native_when_not_migrating(self):
extra_data = { 'jedi_migration': 'no' }
request_form = RequestForm(data={ **self.form_data, **extra_data })
assert not request_form.validate()
assert request_form.errors == { 'cloud_native': ['Not a valid choice'] }
def test_require_migration_questions_when_migrating(self):
extra_data = { 'jedi_migration': 'yes' }
request_form = RequestForm(data={ **self.form_data, **extra_data })
assert not request_form.validate()
assert request_form.errors == {
'rationalization_software_systems': ['Not a valid choice'],
'technical_support_team': ['Not a valid choice'],
'organization_providing_assistance': ['Not a valid choice'],
'engineering_assessment': ['Not a valid choice'],
'data_transfers': ['Not a valid choice'],
'expected_completion_date': ['Not a valid choice']
}
def test_require_organization_when_technical_support_team(self):
extra_data = {
'jedi_migration': 'yes',
'rationalization_software_systems': 'yes',
'technical_support_team': 'yes',
'engineering_assessment': 'yes',
'data_transfers': 'less_than_100gb',
'expected_completion_date': 'less_than_1_month'
}
request_form = RequestForm(data={ **self.form_data, **extra_data })
assert not request_form.validate()
assert request_form.errors == {
'organization_providing_assistance': ['Not a valid choice'],
}
def test_valid_form_data(self):
extra_data = {
'jedi_migration': 'yes',
'rationalization_software_systems': 'yes',
'technical_support_team': 'no',
'engineering_assessment': 'yes',
'data_transfers': 'less_than_100gb',
'expected_completion_date': 'less_than_1_month'
}
request_form = RequestForm(data={ **self.form_data, **extra_data })
assert request_form.validate()