Fixed #15805 - assertFieldOutput should not use assertRaisesRegexp

Thanks to julien for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16303 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2011-05-31 14:02:22 +00:00
parent caefdc8246
commit 93e9d91501
8 changed files with 39 additions and 20 deletions

View File

@ -0,0 +1,11 @@
from django.forms import EmailField
from utils import LocalFlavorTestCase
class AssertFieldOutputTests(LocalFlavorTestCase):
def test_assert_field_output(self):
error_invalid = [u'Enter a valid e-mail address.']
self.assertFieldOutput(EmailField, {'a@a.com': 'a@a.com'}, {'aaa': error_invalid})
self.assertRaises(AssertionError, self.assertFieldOutput, EmailField, {'a@a.com': 'a@a.com'}, {'aaa': error_invalid + [u'Another error']})
self.assertRaises(AssertionError, self.assertFieldOutput, EmailField, {'a@a.com': 'Wrong output'}, {'aaa': error_invalid})
self.assertRaises(AssertionError, self.assertFieldOutput, EmailField, {'a@a.com': 'a@a.com'}, {'aaa': [u'Come on, gimme some well formatted data, dude.']})

View File

@ -20,14 +20,14 @@ class AULocalFlavorTests(LocalFlavorTestCase):
self.assertEqual(f.render('state', 'NSW'), out) self.assertEqual(f.render('state', 'NSW'), out)
def test_AUPostCodeField(self): def test_AUPostCodeField(self):
error_format = [u'Enter a 4 digit post code.'] error_format = [u'Enter a 4 digit postcode.']
valid = { valid = {
'1234': '1234', '1234': '1234',
'2000': '2000', '2000': '2000',
} }
invalid = { invalid = {
'abcd': error_format, 'abcd': error_format,
'20001': error_format, '20001': [u'Ensure this value has at most 4 characters (it has 5).'] + error_format,
} }
self.assertFieldOutput(AUPostCodeField, valid, invalid) self.assertFieldOutput(AUPostCodeField, valid, invalid)

View File

@ -158,7 +158,7 @@ class HRLocalFlavorTests(LocalFlavorTestCase):
'12345678901': '12345678901', '12345678901': '12345678901',
} }
invalid = { invalid = {
'1234567890': error_invalid, '1234567890': [u'Ensure this value has at least 11 characters (it has 10).'] + error_invalid,
'ABCDEFGHIJK': error_invalid, 'ABCDEFGHIJK': error_invalid,
} }
self.assertFieldOutput(HROIBField, valid, invalid) self.assertFieldOutput(HROIBField, valid, invalid)

View File

@ -17,7 +17,7 @@ class PTLocalFlavorTests(LocalFlavorTestCase):
self.assertFieldOutput(PTZipCodeField, valid, invalid) self.assertFieldOutput(PTZipCodeField, valid, invalid)
def test_PTPhoneNumberField(self): def test_PTPhoneNumberField(self):
error_format = [u'Phone numbers must have 9 digits, or start by + or 00'] error_format = [u'Phone numbers must have 9 digits, or start by + or 00.']
valid = { valid = {
'917845189': '917845189', '917845189': '917845189',
'91 784 5189': '917845189', '91 784 5189': '917845189',

View File

@ -66,7 +66,7 @@ class ROLocalFlavorTests(LocalFlavorTestCase):
invalid = { invalid = {
'21694680': error_invalid, '21694680': error_invalid,
'21694680000': error_atmost, '21694680000': error_atmost,
'0': error_atleast, '0': error_atleast + error_invalid,
} }
self.assertFieldOutput(ROCIFField, valid, invalid) self.assertFieldOutput(ROCIFField, valid, invalid)
@ -81,7 +81,7 @@ class ROLocalFlavorTests(LocalFlavorTestCase):
'1981211204487': error_invalid, '1981211204487': error_invalid,
'1981232204489': error_invalid, '1981232204489': error_invalid,
'9981211204489': error_invalid, '9981211204489': error_invalid,
'9981211209': error_atleast, '9981211209': error_atleast + error_invalid,
'19812112044891': error_atmost, '19812112044891': error_atmost,
} }
self.assertFieldOutput(ROCNPField, valid, invalid) self.assertFieldOutput(ROCNPField, valid, invalid)
@ -109,32 +109,34 @@ class ROLocalFlavorTests(LocalFlavorTestCase):
invalid = { invalid = {
'RO56RZBR0000060003291176': error_invalid, 'RO56RZBR0000060003291176': error_invalid,
'AT61 1904 3002 3457 3201': error_invalid, 'AT61 1904 3002 3457 3201': error_invalid,
'RO56RZBR000006000329117': error_atleast, 'RO56RZBR000006000329117': error_atleast + error_invalid,
} }
self.assertFieldOutput(ROIBANField, valid, invalid) self.assertFieldOutput(ROIBANField, valid, invalid)
def test_ROPhoneNumberField(self): def test_ROPhoneNumberField(self):
error_format = [u'Phone numbers must be in XXXX-XXXXXX format.'] error_format = [u'Phone numbers must be in XXXX-XXXXXX format.']
error_atleast = [u'Ensure this value has at least 10 characters (it has 9).'] error_atleast = [u'Ensure this value has at least 10 characters (it has 9).']
error_invalid = [u'Phone numbers must be in XXXX-XXXXXX format.']
valid = { valid = {
'0264485936': '0264485936', '0264485936': '0264485936',
'(0264)-485936': '0264485936', '(0264)-485936': '0264485936',
} }
invalid = { invalid = {
'02644859368': error_format, '02644859368': error_format,
'026448593': error_atleast, '026448593': error_atleast + error_invalid,
} }
self.assertFieldOutput(ROPhoneNumberField, valid, invalid) self.assertFieldOutput(ROPhoneNumberField, valid, invalid)
def test_ROPostalCodeField(self): def test_ROPostalCodeField(self):
error_atleast = [u'Ensure this value has at least 6 characters (it has 5).'] error_atleast = [u'Ensure this value has at least 6 characters (it has 5).']
error_atmost = [u'Ensure this value has at most 6 characters (it has 7).'] error_atmost = [u'Ensure this value has at most 6 characters (it has 7).']
error_invalid = [u'Enter a valid postal code in the format XXXXXX']
valid = { valid = {
'400473': '400473', '400473': '400473',
} }
invalid = { invalid = {
'40047': error_atleast, '40047': error_atleast + error_invalid,
'4004731': error_atmost, '4004731': error_atmost + error_invalid,
} }
self.assertFieldOutput(ROPostalCodeField, valid, invalid) self.assertFieldOutput(ROPostalCodeField, valid, invalid)

View File

@ -1,3 +1,5 @@
from __future__ import with_statement
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms.fields import CharField from django.forms.fields import CharField
@ -42,17 +44,19 @@ class LocalFlavorTestCase(TestCase):
self.assertEqual(optional.clean(input), output) self.assertEqual(optional.clean(input), output)
# test invalid inputs # test invalid inputs
for input, errors in invalid.items(): for input, errors in invalid.items():
self.assertRaisesRegexp(ValidationError, unicode(errors), with self.assertRaises(ValidationError) as context_manager:
required.clean, input required.clean(input)
) self.assertEqual(context_manager.exception.messages, errors)
self.assertRaisesRegexp(ValidationError, unicode(errors),
optional.clean, input with self.assertRaises(ValidationError) as context_manager:
) optional.clean(input)
self.assertEqual(context_manager.exception.messages, errors)
# test required inputs # test required inputs
error_required = u'This field is required' error_required = [u'This field is required.']
for e in EMPTY_VALUES: for e in EMPTY_VALUES:
self.assertRaisesRegexp(ValidationError, error_required, with self.assertRaises(ValidationError) as context_manager:
required.clean, e) required.clean(e)
self.assertEqual(context_manager.exception.messages, error_required)
self.assertEqual(optional.clean(e), empty_value) self.assertEqual(optional.clean(e), empty_value)
# test that max_length and min_length are always accepted # test that max_length and min_length are always accepted
if issubclass(fieldclass, CharField): if issubclass(fieldclass, CharField):

View File

@ -1,3 +1,4 @@
from localflavor import AssertFieldOutputTests
from localflavor.ar import ARLocalFlavorTests from localflavor.ar import ARLocalFlavorTests
from localflavor.at import ATLocalFlavorTests from localflavor.at import ATLocalFlavorTests
from localflavor.au import AULocalFlavorTests from localflavor.au import AULocalFlavorTests

View File

@ -46,5 +46,6 @@ from regressiontests.forms.localflavortests import (
TRLocalFlavorTests, TRLocalFlavorTests,
USLocalFlavorTests, USLocalFlavorTests,
UYLocalFlavorTests, UYLocalFlavorTests,
ZALocalFlavorTests ZALocalFlavorTests,
AssertFieldOutputTests,
) )