diff --git a/tests/regressiontests/forms/localflavor/__init__.py b/tests/regressiontests/forms/localflavor/__init__.py index e69de29bb2..b324e07810 100644 --- a/tests/regressiontests/forms/localflavor/__init__.py +++ b/tests/regressiontests/forms/localflavor/__init__.py @@ -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.']}) diff --git a/tests/regressiontests/forms/localflavor/au.py b/tests/regressiontests/forms/localflavor/au.py index 4bd8a76a48..ddf2b8eb6d 100644 --- a/tests/regressiontests/forms/localflavor/au.py +++ b/tests/regressiontests/forms/localflavor/au.py @@ -20,14 +20,14 @@ class AULocalFlavorTests(LocalFlavorTestCase): self.assertEqual(f.render('state', 'NSW'), out) def test_AUPostCodeField(self): - error_format = [u'Enter a 4 digit post code.'] + error_format = [u'Enter a 4 digit postcode.'] valid = { '1234': '1234', '2000': '2000', } invalid = { '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) diff --git a/tests/regressiontests/forms/localflavor/hr.py b/tests/regressiontests/forms/localflavor/hr.py index 5a7a6b167d..27d747ccc4 100644 --- a/tests/regressiontests/forms/localflavor/hr.py +++ b/tests/regressiontests/forms/localflavor/hr.py @@ -158,7 +158,7 @@ class HRLocalFlavorTests(LocalFlavorTestCase): '12345678901': '12345678901', } invalid = { - '1234567890': error_invalid, + '1234567890': [u'Ensure this value has at least 11 characters (it has 10).'] + error_invalid, 'ABCDEFGHIJK': error_invalid, } self.assertFieldOutput(HROIBField, valid, invalid) diff --git a/tests/regressiontests/forms/localflavor/pt.py b/tests/regressiontests/forms/localflavor/pt.py index ce52018277..b8d784af19 100644 --- a/tests/regressiontests/forms/localflavor/pt.py +++ b/tests/regressiontests/forms/localflavor/pt.py @@ -17,7 +17,7 @@ class PTLocalFlavorTests(LocalFlavorTestCase): self.assertFieldOutput(PTZipCodeField, valid, invalid) 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 = { '917845189': '917845189', '91 784 5189': '917845189', diff --git a/tests/regressiontests/forms/localflavor/ro.py b/tests/regressiontests/forms/localflavor/ro.py index 5b546cd68b..94e6652af8 100644 --- a/tests/regressiontests/forms/localflavor/ro.py +++ b/tests/regressiontests/forms/localflavor/ro.py @@ -66,7 +66,7 @@ class ROLocalFlavorTests(LocalFlavorTestCase): invalid = { '21694680': error_invalid, '21694680000': error_atmost, - '0': error_atleast, + '0': error_atleast + error_invalid, } self.assertFieldOutput(ROCIFField, valid, invalid) @@ -81,7 +81,7 @@ class ROLocalFlavorTests(LocalFlavorTestCase): '1981211204487': error_invalid, '1981232204489': error_invalid, '9981211204489': error_invalid, - '9981211209': error_atleast, + '9981211209': error_atleast + error_invalid, '19812112044891': error_atmost, } self.assertFieldOutput(ROCNPField, valid, invalid) @@ -109,32 +109,34 @@ class ROLocalFlavorTests(LocalFlavorTestCase): invalid = { 'RO56RZBR0000060003291176': error_invalid, 'AT61 1904 3002 3457 3201': error_invalid, - 'RO56RZBR000006000329117': error_atleast, + 'RO56RZBR000006000329117': error_atleast + error_invalid, } self.assertFieldOutput(ROIBANField, valid, invalid) def test_ROPhoneNumberField(self): 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_invalid = [u'Phone numbers must be in XXXX-XXXXXX format.'] valid = { '0264485936': '0264485936', '(0264)-485936': '0264485936', } invalid = { '02644859368': error_format, - '026448593': error_atleast, + '026448593': error_atleast + error_invalid, } self.assertFieldOutput(ROPhoneNumberField, valid, invalid) def test_ROPostalCodeField(self): 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_invalid = [u'Enter a valid postal code in the format XXXXXX'] valid = { '400473': '400473', } invalid = { - '40047': error_atleast, - '4004731': error_atmost, + '40047': error_atleast + error_invalid, + '4004731': error_atmost + error_invalid, } self.assertFieldOutput(ROPostalCodeField, valid, invalid) diff --git a/tests/regressiontests/forms/localflavor/utils.py b/tests/regressiontests/forms/localflavor/utils.py index 31d53dd255..0ba5b806a5 100644 --- a/tests/regressiontests/forms/localflavor/utils.py +++ b/tests/regressiontests/forms/localflavor/utils.py @@ -1,3 +1,5 @@ +from __future__ import with_statement + from django.core.exceptions import ValidationError from django.core.validators import EMPTY_VALUES from django.forms.fields import CharField @@ -42,17 +44,19 @@ class LocalFlavorTestCase(TestCase): self.assertEqual(optional.clean(input), output) # test invalid inputs for input, errors in invalid.items(): - self.assertRaisesRegexp(ValidationError, unicode(errors), - required.clean, input - ) - self.assertRaisesRegexp(ValidationError, unicode(errors), - optional.clean, input - ) + with self.assertRaises(ValidationError) as context_manager: + required.clean(input) + self.assertEqual(context_manager.exception.messages, errors) + + with self.assertRaises(ValidationError) as context_manager: + optional.clean(input) + self.assertEqual(context_manager.exception.messages, errors) # test required inputs - error_required = u'This field is required' + error_required = [u'This field is required.'] for e in EMPTY_VALUES: - self.assertRaisesRegexp(ValidationError, error_required, - required.clean, e) + with self.assertRaises(ValidationError) as context_manager: + required.clean(e) + self.assertEqual(context_manager.exception.messages, error_required) self.assertEqual(optional.clean(e), empty_value) # test that max_length and min_length are always accepted if issubclass(fieldclass, CharField): diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index fb501ab9d4..e2d5aa65ff 100644 --- a/tests/regressiontests/forms/localflavortests.py +++ b/tests/regressiontests/forms/localflavortests.py @@ -1,3 +1,4 @@ +from localflavor import AssertFieldOutputTests from localflavor.ar import ARLocalFlavorTests from localflavor.at import ATLocalFlavorTests from localflavor.au import AULocalFlavorTests diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index 6366a934cb..39db39f0df 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -46,5 +46,6 @@ from regressiontests.forms.localflavortests import ( TRLocalFlavorTests, USLocalFlavorTests, UYLocalFlavorTests, - ZALocalFlavorTests + ZALocalFlavorTests, + AssertFieldOutputTests, )