Test for specific error messages

This commit:
- improves the tests to check for specific error messages, rather than just
  pass/fail
- makes the error messages more human, and more suggestive of what the user
  needs to do to fix the error
This commit is contained in:
Chris Hill-Scott
2016-01-12 20:55:46 +00:00
parent faa3b9ca7c
commit 791324588b
2 changed files with 54 additions and 47 deletions

View File

@@ -37,10 +37,10 @@ class UKMobileNumber(StringField):
if self.data.startswith('+'): if self.data.startswith('+'):
self.data = self.data[1:] self.data = self.data[1:]
if not sum([ if not sum(
self.data.startswith(prefix) for prefix in ['07', '447', '4407', '00447'] self.data.startswith(prefix) for prefix in ['07', '447', '4407', '00447']
]): ):
raise ValidationError('Must be a mobile number') raise ValidationError('Must be a UK mobile number (eg 07700 900460)')
for digit in self.data: for digit in self.data:
try: try:
@@ -54,7 +54,7 @@ class UKMobileNumber(StringField):
raise ValidationError('Too many digits') raise ValidationError('Too many digits')
if len(self.data) < 9: if len(self.data) < 9:
raise ValidationError('Too few digits') raise ValidationError('Not enough digits')
def post_validate(self, form, validation_stopped): def post_validate(self, form, validation_stopped):

View File

@@ -6,62 +6,69 @@ from app.main.forms import UKMobileNumber
class FormExample(Form): class FormExample(Form):
phone_number = UKMobileNumber() phone_number = UKMobileNumber()
phone_numbers = { invalid_phone_numbers = sum([
'invalid': [ [
# Too long (phone_number, error) for phone_number in group
'0712345678910', ] for error, group in [
'0044712345678910', ('Too many digits', (
'0044712345678910', '0712345678910',
'+44 (0)7123 456 789 10', '0044712345678910',
# Too short '0044712345678910',
'0712345678', '+44 (0)7123 456 789 10',
'004471234567', )),
'00447123456', ('Not enough digits', (
'+44 (0)7123 456 78', '0712345678',
# Not mobile (from https://fakenumber.org/generator/freephone) '004471234567',
'08081 570364', '00447123456',
'+44 8081 570364', '+44 (0)7123 456 78',
'0117 496 0860', )),
'+44 117 496 0860', ('Must be a UK mobile number (eg 07700 900460)', (
'020 7946 0991', '08081 570364',
'+44 20 7946 0991', '+44 8081 570364',
# Contains non-numbers '0117 496 0860',
'07890x32109', '+44 117 496 0860',
'07123 456789...', '020 7946 0991',
'07123 ☟☜⬇⬆☞☝', '+44 20 7946 0991',
'07123☟☜⬇⬆☞☝', '71234567890',
'07";DROP TABLE;"', )),
'+44 07ab cde fgh', ('Must not contain letters or symbols', (
], '07890x32109',
'valid': [ '07123 456789...',
'07123456789', '07123 ☟☜⬇⬆☞☝',
'07123 456789', '07123☟☜⬇⬆☞☝',
'07123-456-789', '07";DROP TABLE;"',
'00447123456789', '+44 07ab cde fgh',
'00 44 7123456789', ))
'+447123456789',
'+44 7123 456 789',
'+44 (0)7123 456 789'
] ]
} ], [])
valid_phone_numbers = [
'07123456789',
'07123 456789',
'07123-456-789',
'00447123456789',
'00 44 7123456789',
'+447123456789',
'+44 7123 456 789',
'+44 (0)7123 456 789'
]
@pytest.mark.parametrize("phone_number", phone_numbers['valid']) @pytest.mark.parametrize("phone_number", valid_phone_numbers)
def test_phone_number_accepts_valid_values(phone_number): def test_phone_number_accepts_valid_values(phone_number):
form = FormExample(phone_number=phone_number) form = FormExample(phone_number=phone_number)
form.validate() form.validate()
assert form.errors == {} assert form.errors == {}
@pytest.mark.parametrize("phone_number", phone_numbers['invalid']) @pytest.mark.parametrize("phone_number, error_message", invalid_phone_numbers)
def test_phone_number_rejects_invalid_values(phone_number): def test_phone_number_rejects_invalid_values(phone_number, error_message):
form = FormExample(phone_number=phone_number) form = FormExample(phone_number=phone_number)
form.validate() form.validate()
print(phone_number) assert form.phone_number.errors[0] == error_message
assert form.errors != {}
@pytest.mark.parametrize("phone_number", phone_numbers['valid']) @pytest.mark.parametrize("phone_number", valid_phone_numbers)
def test_phone_number_outputs_in_correct_format(phone_number): def test_phone_number_outputs_in_correct_format(phone_number):
form = FormExample(phone_number=phone_number) form = FormExample(phone_number=phone_number)
form.validate() form.validate()