Ticket #14503: 14503-assertraisesmessage-unification-simple.diff
File 14503-assertraisesmessage-unification-simple.diff, 73.4 KB (added by , 14 years ago) |
---|
-
django/test/testcases.py
diff --git a/django/test/testcases.py b/django/test/testcases.py
a b 516 516 finally: 517 517 context.__exit__(*sys.exc_info()) 518 518 519 def assertRaisesMessage(self, exc, msg, func, *args, **kwargs): 520 try: 521 func(*args, **kwargs) 522 except Exception, e: 523 self.assertEqual(msg, str(e)) 524 self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e))) 525 526 519 527 def connections_support_transactions(): 520 528 """ 521 529 Returns True if all connections support transactions. This is messy -
tests/regressiontests/admin_validation/tests.py
diff --git a/tests/regressiontests/admin_validation/tests.py b/tests/regressiontests/admin_validation/tests.py
a b 18 18 fields = ['spam'] 19 19 20 20 class ValidationTestCase(TestCase): 21 def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):22 try:23 func(*args, **kwargs)24 except Exception, e:25 self.assertEqual(msg, str(e))26 self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))27 28 21 def test_readonly_and_editable(self): 29 22 class SongAdmin(admin.ModelAdmin): 30 23 readonly_fields = ["original_release"] -
tests/regressiontests/custom_columns_regress/tests.py
diff --git a/tests/regressiontests/custom_columns_regress/tests.py b/tests/regressiontests/custom_columns_regress/tests.py
a b 9 9 10 10 class CustomColumnRegression(TestCase): 11 11 12 def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):13 try:14 func(*args, **kwargs)15 except Exception, e:16 self.assertEqual(msg, str(e))17 self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))18 19 12 def setUp(self): 20 13 self.a1 = Author.objects.create(first_name='John', last_name='Smith') 21 14 self.a2 = Author.objects.create(first_name='Peter', last_name='Jones') -
tests/regressiontests/file_storage/tests.py
diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py
a b 13 13 from django.core.files.storage import FileSystemStorage, get_storage_class 14 14 from django.core.files.uploadedfile import UploadedFile 15 15 from django.core.exceptions import ImproperlyConfigured 16 from django.test import TestCase 16 17 from django.utils import unittest 17 18 18 19 try: … … 31 32 except ImportError: 32 33 Image = None 33 34 34 class GetStorageClassTests(unittest.TestCase): 35 def assertRaisesErrorWithMessage(self, error, message, callable, 36 *args, **kwargs): 37 self.assertRaises(error, callable, *args, **kwargs) 38 try: 39 callable(*args, **kwargs) 40 except error, e: 41 self.assertEqual(message, str(e)) 42 35 class GetStorageClassTests(TestCase): 43 36 def test_get_filesystem_storage(self): 44 37 """ 45 38 get_storage_class returns the class for a storage backend name/path. … … 52 45 """ 53 46 get_storage_class raises an error if the requested import don't exist. 54 47 """ 55 self.assertRaises ErrorWithMessage(48 self.assertRaisesMessage( 56 49 ImproperlyConfigured, 57 50 "NonExistingStorage isn't a storage module.", 58 51 get_storage_class, … … 62 55 """ 63 56 get_storage_class raises an error if the requested class don't exist. 64 57 """ 65 self.assertRaises ErrorWithMessage(58 self.assertRaisesMessage( 66 59 ImproperlyConfigured, 67 60 'Storage module "django.core.files.storage" does not define a '\ 68 61 '"NonExistingStorage" class.', … … 73 66 """ 74 67 get_storage_class raises an error if the requested module don't exist. 75 68 """ 76 self.assertRaises ErrorWithMessage(69 self.assertRaisesMessage( 77 70 ImproperlyConfigured, 78 71 'Error importing storage module django.core.files.non_existing_'\ 79 72 'storage: "No module named non_existing_storage"', -
tests/regressiontests/fixtures_regress/tests.py
diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py
a b 343 343 344 344 345 345 class NaturalKeyFixtureTests(TestCase): 346 def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):347 try:348 func(*args, **kwargs)349 except Exception, e:350 self.assertEqual(msg, str(e))351 self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))352 353 346 def test_nk_deserialize(self): 354 347 """ 355 348 Test for ticket #13030 - Python based parser version -
tests/regressiontests/forms/fields.py
diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
a b 33 33 from django.core.files.uploadedfile import SimpleUploadedFile 34 34 from django.forms import * 35 35 from django.forms.widgets import RadioFieldRenderer 36 from django. utils.unittest import TestCase36 from django.test import TestCase 37 37 38 38 39 39 def fix_os_paths(x): … … 49 49 50 50 class FieldsTests(TestCase): 51 51 52 def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):53 self.assertRaises(error, callable, *args, **kwargs)54 try:55 callable(*args, **kwargs)56 except error, e:57 self.assertEqual(message, str(e))58 59 52 def test_field_sets_widget_is_required(self): 60 53 self.assertEqual(Field(required=True).widget.is_required, True) 61 54 self.assertEqual(Field(required=False).widget.is_required, False) … … 66 59 f = CharField() 67 60 self.assertEqual(u'1', f.clean(1)) 68 61 self.assertEqual(u'hello', f.clean('hello')) 69 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)70 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')62 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 63 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 71 64 self.assertEqual(u'[1, 2, 3]', f.clean([1, 2, 3])) 72 65 73 66 def test_charfield_1(self): … … 82 75 f = CharField(max_length=10, required=False) 83 76 self.assertEqual(u'12345', f.clean('12345')) 84 77 self.assertEqual(u'1234567890', f.clean('1234567890')) 85 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '1234567890a')78 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '1234567890a') 86 79 87 80 def test_charfield_3(self): 88 81 f = CharField(min_length=10, required=False) 89 82 self.assertEqual(u'', f.clean('')) 90 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345')83 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345') 91 84 self.assertEqual(u'1234567890', f.clean('1234567890')) 92 85 self.assertEqual(u'1234567890a', f.clean('1234567890a')) 93 86 94 87 def test_charfield_4(self): 95 88 f = CharField(min_length=10, required=True) 96 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')97 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345')89 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 90 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345') 98 91 self.assertEqual(u'1234567890', f.clean('1234567890')) 99 92 self.assertEqual(u'1234567890a', f.clean('1234567890a')) 100 93 … … 102 95 103 96 def test_integerfield_5(self): 104 97 f = IntegerField() 105 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')106 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)98 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 99 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 107 100 self.assertEqual(1, f.clean('1')) 108 101 self.assertEqual(True, isinstance(f.clean('1'), int)) 109 102 self.assertEqual(23, f.clean('23')) 110 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a')103 self.assertRaisesMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a') 111 104 self.assertEqual(42, f.clean(42)) 112 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 3.14)105 self.assertRaisesMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 3.14) 113 106 self.assertEqual(1, f.clean('1 ')) 114 107 self.assertEqual(1, f.clean(' 1')) 115 108 self.assertEqual(1, f.clean(' 1 ')) 116 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a')109 self.assertRaisesMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a') 117 110 118 111 def test_integerfield_6(self): 119 112 f = IntegerField(required=False) … … 124 117 self.assertEqual(1, f.clean('1')) 125 118 self.assertEqual(True, isinstance(f.clean('1'), int)) 126 119 self.assertEqual(23, f.clean('23')) 127 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a')120 self.assertRaisesMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a') 128 121 self.assertEqual(1, f.clean('1 ')) 129 122 self.assertEqual(1, f.clean(' 1')) 130 123 self.assertEqual(1, f.clean(' 1 ')) 131 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a')124 self.assertRaisesMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a') 132 125 133 126 def test_integerfield_7(self): 134 127 f = IntegerField(max_value=10) 135 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)128 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 136 129 self.assertEqual(1, f.clean(1)) 137 130 self.assertEqual(10, f.clean(10)) 138 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, 11)131 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, 11) 139 132 self.assertEqual(10, f.clean('10')) 140 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, '11')133 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, '11') 141 134 142 135 def test_integerfield_8(self): 143 136 f = IntegerField(min_value=10) 144 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)145 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1)137 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 138 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1) 146 139 self.assertEqual(10, f.clean(10)) 147 140 self.assertEqual(11, f.clean(11)) 148 141 self.assertEqual(10, f.clean('10')) … … 150 143 151 144 def test_integerfield_9(self): 152 145 f = IntegerField(min_value=10, max_value=20) 153 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)154 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1)146 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 147 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1) 155 148 self.assertEqual(10, f.clean(10)) 156 149 self.assertEqual(11, f.clean(11)) 157 150 self.assertEqual(10, f.clean('10')) 158 151 self.assertEqual(11, f.clean('11')) 159 152 self.assertEqual(20, f.clean(20)) 160 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 20.']", f.clean, 21)153 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is less than or equal to 20.']", f.clean, 21) 161 154 162 155 # FloatField ################################################################## 163 156 164 157 def test_floatfield_10(self): 165 158 f = FloatField() 166 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')167 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)159 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 160 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 168 161 self.assertEqual(1.0, f.clean('1')) 169 162 self.assertEqual(True, isinstance(f.clean('1'), float)) 170 163 self.assertEqual(23.0, f.clean('23')) 171 164 self.assertEqual(3.1400000000000001, f.clean('3.14')) 172 165 self.assertEqual(3.1400000000000001, f.clean(3.14)) 173 166 self.assertEqual(42.0, f.clean(42)) 174 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a')167 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a') 175 168 self.assertEqual(1.0, f.clean('1.0 ')) 176 169 self.assertEqual(1.0, f.clean(' 1.0')) 177 170 self.assertEqual(1.0, f.clean(' 1.0 ')) 178 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a')171 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a') 179 172 180 173 def test_floatfield_11(self): 181 174 f = FloatField(required=False) … … 185 178 186 179 def test_floatfield_12(self): 187 180 f = FloatField(max_value=1.5, min_value=0.5) 188 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 1.5.']", f.clean, '1.6')189 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 0.5.']", f.clean, '0.4')181 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is less than or equal to 1.5.']", f.clean, '1.6') 182 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is greater than or equal to 0.5.']", f.clean, '0.4') 190 183 self.assertEqual(1.5, f.clean('1.5')) 191 184 self.assertEqual(0.5, f.clean('0.5')) 192 185 … … 194 187 195 188 def test_decimalfield_13(self): 196 189 f = DecimalField(max_digits=4, decimal_places=2) 197 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')198 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)190 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 191 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 199 192 self.assertEqual(f.clean('1'), Decimal("1")) 200 193 self.assertEqual(True, isinstance(f.clean('1'), Decimal)) 201 194 self.assertEqual(f.clean('23'), Decimal("23")) 202 195 self.assertEqual(f.clean('3.14'), Decimal("3.14")) 203 196 self.assertEqual(f.clean(3.14), Decimal("3.14")) 204 197 self.assertEqual(f.clean(Decimal('3.14')), Decimal("3.14")) 205 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'NaN')206 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'Inf')207 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '-Inf')208 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a')209 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, u'łąść')198 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, 'NaN') 199 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, 'Inf') 200 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, '-Inf') 201 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a') 202 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, u'łąść') 210 203 self.assertEqual(f.clean('1.0 '), Decimal("1.0")) 211 204 self.assertEqual(f.clean(' 1.0'), Decimal("1.0")) 212 205 self.assertEqual(f.clean(' 1.0 '), Decimal("1.0")) 213 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a')214 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '123.45')215 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '1.234')216 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 digits before the decimal point.']", f.clean, '123.4')206 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a') 207 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '123.45') 208 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '1.234') 209 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 2 digits before the decimal point.']", f.clean, '123.4') 217 210 self.assertEqual(f.clean('-12.34'), Decimal("-12.34")) 218 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-123.45')211 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-123.45') 219 212 self.assertEqual(f.clean('-.12'), Decimal("-0.12")) 220 213 self.assertEqual(f.clean('-00.12'), Decimal("-0.12")) 221 214 self.assertEqual(f.clean('-000.12'), Decimal("-0.12")) 222 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '-000.123')223 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-000.12345')224 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '--0.12')215 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '-000.123') 216 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-000.12345') 217 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, '--0.12') 225 218 226 219 def test_decimalfield_14(self): 227 220 f = DecimalField(max_digits=4, decimal_places=2, required=False) … … 231 224 232 225 def test_decimalfield_15(self): 233 226 f = DecimalField(max_digits=4, decimal_places=2, max_value=Decimal('1.5'), min_value=Decimal('0.5')) 234 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 1.5.']", f.clean, '1.6')235 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 0.5.']", f.clean, '0.4')227 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is less than or equal to 1.5.']", f.clean, '1.6') 228 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is greater than or equal to 0.5.']", f.clean, '0.4') 236 229 self.assertEqual(f.clean('1.5'), Decimal("1.5")) 237 230 self.assertEqual(f.clean('0.5'), Decimal("0.5")) 238 231 self.assertEqual(f.clean('.5'), Decimal("0.5")) … … 240 233 241 234 def test_decimalfield_16(self): 242 235 f = DecimalField(decimal_places=2) 243 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '0.00000001')236 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '0.00000001') 244 237 245 238 def test_decimalfield_17(self): 246 239 f = DecimalField(max_digits=3) … … 250 243 self.assertEqual(f.clean('0000000.100'), Decimal("0.100")) 251 244 # Only leading whole zeros "collapse" to one digit. 252 245 self.assertEqual(f.clean('000000.02'), Decimal('0.02')) 253 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 3 digits in total.']", f.clean, '000000.0002')246 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 3 digits in total.']", f.clean, '000000.0002') 254 247 self.assertEqual(f.clean('.002'), Decimal("0.002")) 255 248 256 249 def test_decimalfield_18(self): 257 250 f = DecimalField(max_digits=2, decimal_places=2) 258 251 self.assertEqual(f.clean('.01'), Decimal(".01")) 259 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 0 digits before the decimal point.']", f.clean, '1.1')252 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 0 digits before the decimal point.']", f.clean, '1.1') 260 253 261 254 # DateField ################################################################### 262 255 … … 274 267 self.assertEqual(datetime.date(2006, 10, 25), f.clean('October 25, 2006')) 275 268 self.assertEqual(datetime.date(2006, 10, 25), f.clean('25 October 2006')) 276 269 self.assertEqual(datetime.date(2006, 10, 25), f.clean('25 October, 2006')) 277 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-4-31')278 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '200a-10-25')279 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '25/10/06')280 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)270 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-4-31') 271 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '200a-10-25') 272 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '25/10/06') 273 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 281 274 282 275 def test_datefield_20(self): 283 276 f = DateField(required=False) … … 291 284 self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.date(2006, 10, 25))) 292 285 self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.datetime(2006, 10, 25, 14, 30))) 293 286 self.assertEqual(datetime.date(2006, 10, 25), f.clean('2006 10 25')) 294 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-10-25')295 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/2006')296 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/06')287 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-10-25') 288 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/2006') 289 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/06') 297 290 298 291 # TimeField ################################################################### 299 292 … … 303 296 self.assertEqual(datetime.time(14, 25, 59), f.clean(datetime.time(14, 25, 59))) 304 297 self.assertEqual(datetime.time(14, 25), f.clean('14:25')) 305 298 self.assertEqual(datetime.time(14, 25, 59), f.clean('14:25:59')) 306 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, 'hello')307 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '1:24 p.m.')299 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, 'hello') 300 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '1:24 p.m.') 308 301 309 302 def test_timefield_23(self): 310 303 f = TimeField(input_formats=['%I:%M %p']) … … 312 305 self.assertEqual(datetime.time(14, 25, 59), f.clean(datetime.time(14, 25, 59))) 313 306 self.assertEqual(datetime.time(4, 25), f.clean('4:25 AM')) 314 307 self.assertEqual(datetime.time(16, 25), f.clean('4:25 PM')) 315 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '14:30:45')308 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '14:30:45') 316 309 317 310 # DateTimeField ############################################################### 318 311 … … 334 327 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('10/25/06 14:30:00')) 335 328 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('10/25/06 14:30')) 336 329 self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean('10/25/06')) 337 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, 'hello')338 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 4:30 p.m.')330 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, 'hello') 331 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 4:30 p.m.') 339 332 340 333 def test_datetimefield_25(self): 341 334 f = DateTimeField(input_formats=['%Y %m %d %I:%M %p']) … … 344 337 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))) 345 338 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59, 200), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))) 346 339 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006 10 25 2:30 PM')) 347 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 14:30:45')340 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 14:30:45') 348 341 349 342 def test_datetimefield_26(self): 350 343 f = DateTimeField(required=False) … … 359 352 f = RegexField('^\d[A-F]\d$') 360 353 self.assertEqual(u'2A2', f.clean('2A2')) 361 354 self.assertEqual(u'3F3', f.clean('3F3')) 362 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3')363 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2')364 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ')365 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')355 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3') 356 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2') 357 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ') 358 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 366 359 367 360 def test_regexfield_28(self): 368 361 f = RegexField('^\d[A-F]\d$', required=False) 369 362 self.assertEqual(u'2A2', f.clean('2A2')) 370 363 self.assertEqual(u'3F3', f.clean('3F3')) 371 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3')364 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3') 372 365 self.assertEqual(u'', f.clean('')) 373 366 374 367 def test_regexfield_29(self): 375 368 f = RegexField(re.compile('^\d[A-F]\d$')) 376 369 self.assertEqual(u'2A2', f.clean('2A2')) 377 370 self.assertEqual(u'3F3', f.clean('3F3')) 378 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3')379 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2')380 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ')371 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3') 372 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2') 373 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ') 381 374 382 375 def test_regexfield_30(self): 383 376 f = RegexField('^\d\d\d\d$', error_message='Enter a four-digit number.') 384 377 self.assertEqual(u'1234', f.clean('1234')) 385 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, '123')386 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, 'abcd')378 self.assertRaisesMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, '123') 379 self.assertRaisesMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, 'abcd') 387 380 388 381 def test_regexfield_31(self): 389 382 f = RegexField('^\d+$', min_length=5, max_length=10) 390 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, '123')391 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).', u'Enter a valid value.']", f.clean, 'abc')383 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, '123') 384 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).', u'Enter a valid value.']", f.clean, 'abc') 392 385 self.assertEqual(u'12345', f.clean('12345')) 393 386 self.assertEqual(u'1234567890', f.clean('1234567890')) 394 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '12345678901')395 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '12345a')387 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '12345678901') 388 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '12345a') 396 389 397 390 # EmailField ################################################################## 398 391 399 392 def test_emailfield_32(self): 400 393 f = EmailField() 401 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')402 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)394 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 395 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 403 396 self.assertEqual(u'person@example.com', f.clean('person@example.com')) 404 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo')405 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@')406 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar')407 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@invalid-.com')408 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@-invalid.com')409 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.alid-.com')410 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.-alid.com')397 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo') 398 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@') 399 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar') 400 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@invalid-.com') 401 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@-invalid.com') 402 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.alid-.com') 403 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.-alid.com') 411 404 self.assertEqual(u'example@valid-----hyphens.com', f.clean('example@valid-----hyphens.com')) 412 405 self.assertEqual(u'example@valid-with-hyphens.com', f.clean('example@valid-with-hyphens.com')) 413 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@.com')406 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@.com') 414 407 self.assertEqual(u'local@domain.with.idn.xyz\xe4\xf6\xfc\xdfabc.part.com', f.clean('local@domain.with.idn.xyzäöüßabc.part.com')) 415 408 416 409 def test_email_regexp_for_performance(self): 417 410 f = EmailField() 418 411 # Check for runaway regex security problem. This will take for-freeking-ever 419 412 # if the security fix isn't in place. 420 self.assertRaises ErrorWithMessage(413 self.assertRaisesMessage( 421 414 ValidationError, 422 415 "[u'Enter a valid e-mail address.']", 423 416 f.clean, … … 430 423 self.assertEqual(u'', f.clean(None)) 431 424 self.assertEqual(u'person@example.com', f.clean('person@example.com')) 432 425 self.assertEqual(u'example@example.com', f.clean(' example@example.com \t \t ')) 433 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo')434 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@')435 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar')426 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo') 427 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@') 428 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar') 436 429 437 430 def test_emailfield_34(self): 438 431 f = EmailField(min_length=10, max_length=15) 439 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 9).']", f.clean, 'a@foo.com')432 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 9).']", f.clean, 'a@foo.com') 440 433 self.assertEqual(u'alf@foo.com', f.clean('alf@foo.com')) 441 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 15 characters (it has 20).']", f.clean, 'alf123456788@foo.com')434 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 15 characters (it has 20).']", f.clean, 'alf123456788@foo.com') 442 435 443 436 # FileField ################################################################## 444 437 445 438 def test_filefield_35(self): 446 439 f = FileField() 447 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')448 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '', '')440 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 441 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '', '') 449 442 self.assertEqual('files/test1.pdf', f.clean('', 'files/test1.pdf')) 450 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)451 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None, '')443 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 444 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None, '') 452 445 self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) 453 self.assertRaises ErrorWithMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', ''))454 self.assertRaises ErrorWithMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', ''), '')446 self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', '')) 447 self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', ''), '') 455 448 self.assertEqual('files/test3.pdf', f.clean(None, 'files/test3.pdf')) 456 self.assertRaises ErrorWithMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, 'some content that is not a file')457 self.assertRaises ErrorWithMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', None))458 self.assertRaises ErrorWithMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', ''))449 self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, 'some content that is not a file') 450 self.assertRaisesMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', None)) 451 self.assertRaisesMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', '')) 459 452 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) 460 453 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह')))) 461 454 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf'))) 462 455 463 456 def test_filefield_36(self): 464 457 f = FileField(max_length = 5) 465 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this filename has at most 5 characters (it has 18).']", f.clean, SimpleUploadedFile('test_maxlength.txt', 'hello world'))458 self.assertRaisesMessage(ValidationError, "[u'Ensure this filename has at most 5 characters (it has 18).']", f.clean, SimpleUploadedFile('test_maxlength.txt', 'hello world')) 466 459 self.assertEqual('files/test1.pdf', f.clean('', 'files/test1.pdf')) 467 460 self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) 468 461 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) … … 471 464 472 465 def test_urlfield_37(self): 473 466 f = URLField() 474 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')475 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)467 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 468 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 476 469 self.assertEqual(u'http://localhost/', f.clean('http://localhost')) 477 470 self.assertEqual(u'http://example.com/', f.clean('http://example.com')) 478 471 self.assertEqual(u'http://example.com./', f.clean('http://example.com.')) … … 482 475 self.assertEqual(u'http://subdomain.domain.com/', f.clean('subdomain.domain.com')) 483 476 self.assertEqual(u'http://200.8.9.10/', f.clean('http://200.8.9.10')) 484 477 self.assertEqual(u'http://200.8.9.10:8000/test', f.clean('http://200.8.9.10:8000/test')) 485 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo')486 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://')487 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')488 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.')489 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'com.')490 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, '.')491 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com')492 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://invalid-.com')493 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://-invalid.com')494 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.alid-.com')495 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.-alid.com')478 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo') 479 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://') 480 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') 481 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.') 482 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'com.') 483 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, '.') 484 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com') 485 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://invalid-.com') 486 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://-invalid.com') 487 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.alid-.com') 488 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.-alid.com') 496 489 self.assertEqual(u'http://valid-----hyphens.com/', f.clean('http://valid-----hyphens.com')) 497 490 self.assertEqual(u'http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah', f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah')) 498 491 499 492 def test_url_regex_ticket11198(self): 500 493 f = URLField() 501 494 # hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed 502 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*200,))495 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*200,)) 503 496 504 497 # a second test, to make sure the problem is really addressed, even on 505 498 # domains that don't fail the domain label length check in the regex 506 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*60,))499 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*60,)) 507 500 508 501 def test_urlfield_38(self): 509 502 f = URLField(required=False) … … 511 504 self.assertEqual(u'', f.clean(None)) 512 505 self.assertEqual(u'http://example.com/', f.clean('http://example.com')) 513 506 self.assertEqual(u'http://www.example.com/', f.clean('http://www.example.com')) 514 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo')515 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://')516 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')517 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.')518 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com')507 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo') 508 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://') 509 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') 510 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.') 511 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com') 519 512 520 513 def test_urlfield_39(self): 521 514 f = URLField(verify_exists=True) 522 515 self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection 523 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')516 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') 524 517 self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain 525 518 try: 526 519 f.clean('http://www.broken.djangoproject.com') # bad domain … … 546 539 547 540 def test_urlfield_41(self): 548 541 f = URLField(min_length=15, max_length=20) 549 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 15 characters (it has 13).']", f.clean, 'http://f.com')542 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 15 characters (it has 13).']", f.clean, 'http://f.com') 550 543 self.assertEqual(u'http://example.com/', f.clean('http://example.com')) 551 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 38).']", f.clean, 'http://abcdefghijklmnopqrstuvwxyz.com')544 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 38).']", f.clean, 'http://abcdefghijklmnopqrstuvwxyz.com') 552 545 553 546 def test_urlfield_42(self): 554 547 f = URLField(required=False) … … 569 562 570 563 def test_booleanfield_44(self): 571 564 f = BooleanField() 572 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')573 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)565 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 566 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 574 567 self.assertEqual(True, f.clean(True)) 575 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, False)568 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, False) 576 569 self.assertEqual(True, f.clean(1)) 577 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, 0)570 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, 0) 578 571 self.assertEqual(True, f.clean('Django rocks')) 579 572 self.assertEqual(True, f.clean('True')) 580 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, 'False')573 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, 'False') 581 574 582 575 def test_booleanfield_45(self): 583 576 f = BooleanField(required=False) … … 596 589 597 590 def test_choicefield_46(self): 598 591 f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')]) 599 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')600 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)592 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 593 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 601 594 self.assertEqual(u'1', f.clean(1)) 602 595 self.assertEqual(u'1', f.clean('1')) 603 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3')596 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3') 604 597 605 598 def test_choicefield_47(self): 606 599 f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) … … 608 601 self.assertEqual(u'', f.clean(None)) 609 602 self.assertEqual(u'1', f.clean(1)) 610 603 self.assertEqual(u'1', f.clean('1')) 611 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3')604 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3') 612 605 613 606 def test_choicefield_48(self): 614 607 f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')]) 615 608 self.assertEqual(u'J', f.clean('J')) 616 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. John is not one of the available choices.']", f.clean, 'John')609 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. John is not one of the available choices.']", f.clean, 'John') 617 610 618 611 def test_choicefield_49(self): 619 612 f = ChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) … … 623 616 self.assertEqual(u'3', f.clean('3')) 624 617 self.assertEqual(u'5', f.clean(5)) 625 618 self.assertEqual(u'5', f.clean('5')) 626 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, '6')619 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, '6') 627 620 628 621 # TypedChoiceField ############################################################ 629 622 # TypedChoiceField is just like ChoiceField, except that coerced types will … … 632 625 def test_typedchoicefield_50(self): 633 626 f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int) 634 627 self.assertEqual(1, f.clean('1')) 635 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, '2')628 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, '2') 636 629 637 630 def test_typedchoicefield_51(self): 638 631 # Different coercion, same validation. … … 648 641 # Even more weirdness: if you have a valid choice but your coercion function 649 642 # can't coerce, you'll still get a validation error. Don't do this! 650 643 f = TypedChoiceField(choices=[('A', 'A'), ('B', 'B')], coerce=int) 651 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. B is not one of the available choices.']", f.clean, 'B')644 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. B is not one of the available choices.']", f.clean, 'B') 652 645 # Required fields require values 653 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')646 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 654 647 655 648 def test_typedchoicefield_54(self): 656 649 # Non-required fields aren't required … … 712 705 713 706 def test_multiplechoicefield_60(self): 714 707 f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')]) 715 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')716 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)708 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 709 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 717 710 self.assertEqual([u'1'], f.clean([1])) 718 711 self.assertEqual([u'1'], f.clean(['1'])) 719 712 self.assertEqual([u'1', u'2'], f.clean(['1', '2'])) 720 713 self.assertEqual([u'1', u'2'], f.clean([1, '2'])) 721 714 self.assertEqual([u'1', u'2'], f.clean((1, '2'))) 722 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')723 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, [])724 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, ())725 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3'])715 self.assertRaisesMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') 716 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, []) 717 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, ()) 718 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3']) 726 719 727 720 def test_multiplechoicefield_61(self): 728 721 f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) … … 733 726 self.assertEqual([u'1', u'2'], f.clean(['1', '2'])) 734 727 self.assertEqual([u'1', u'2'], f.clean([1, '2'])) 735 728 self.assertEqual([u'1', u'2'], f.clean((1, '2'))) 736 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')729 self.assertRaisesMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') 737 730 self.assertEqual([], f.clean([])) 738 731 self.assertEqual([], f.clean(())) 739 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3'])732 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3']) 740 733 741 734 def test_multiplechoicefield_62(self): 742 735 f = MultipleChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) … … 746 739 self.assertEqual([u'1', u'5'], f.clean([1, '5'])) 747 740 self.assertEqual([u'1', u'5'], f.clean(['1', 5])) 748 741 self.assertEqual([u'1', u'5'], f.clean(['1', '5'])) 749 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['6'])750 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['1','6'])742 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['6']) 743 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['1','6']) 751 744 752 745 # ComboField ################################################################## 753 746 754 747 def test_combofield_63(self): 755 748 f = ComboField(fields=[CharField(max_length=20), EmailField()]) 756 749 self.assertEqual(u'test@example.com', f.clean('test@example.com')) 757 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com')758 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail')759 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')760 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)750 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com') 751 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail') 752 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 753 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 761 754 762 755 def test_combofield_64(self): 763 756 f = ComboField(fields=[CharField(max_length=20), EmailField()], required=False) 764 757 self.assertEqual(u'test@example.com', f.clean('test@example.com')) 765 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com')766 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail')758 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com') 759 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail') 767 760 self.assertEqual(u'', f.clean('')) 768 761 self.assertEqual(u'', f.clean(None)) 769 762 … … 792 785 for exp, got in zip(expected, fix_os_paths(f.choices)): 793 786 self.assertEqual(exp[1], got[1]) 794 787 self.assertTrue(got[0].endswith(exp[0])) 795 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. fields.py is not one of the available choices.']", f.clean, 'fields.py')788 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. fields.py is not one of the available choices.']", f.clean, 'fields.py') 796 789 assert fix_os_paths(f.clean(path + 'fields.py')).endswith('/django/forms/fields.py') 797 790 798 791 def test_filepathfield_67(self): … … 840 833 f = SplitDateTimeField() 841 834 assert isinstance(f.widget, SplitDateTimeWidget) 842 835 self.assertEqual(datetime.datetime(2006, 1, 10, 7, 30), f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)])) 843 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)844 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')845 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')846 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there'])847 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there'])848 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30'])836 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 837 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 838 self.assertRaisesMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') 839 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there']) 840 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there']) 841 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30']) 849 842 850 843 def test_splitdatetimefield_70(self): 851 844 f = SplitDateTimeField(required=False) … … 855 848 self.assertEqual(None, f.clean('')) 856 849 self.assertEqual(None, f.clean([''])) 857 850 self.assertEqual(None, f.clean(['', ''])) 858 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')859 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there'])860 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there'])861 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30'])862 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', ''])863 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10'])864 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['', '07:30'])851 self.assertRaisesMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') 852 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there']) 853 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there']) 854 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30']) 855 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', '']) 856 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10']) 857 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['', '07:30']) -
tests/regressiontests/forms/localflavor/be.py
diff --git a/tests/regressiontests/forms/localflavor/be.py b/tests/regressiontests/forms/localflavor/be.py
a b 8 8 """ 9 9 Test case to validate BE localflavor 10 10 """ 11 def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):12 self.assertRaises(error, callable, *args, **kwargs)13 try:14 callable(*args, **kwargs)15 except error, e:16 self.assertEqual(message, str(e))17 18 11 def test_postal_code_field(self): 19 12 f = BEPostalCodeField() 20 13 self.assertEqual(u'1451', f.clean('1451')) 21 14 self.assertEqual(u'2540', f.clean('2540')) 22 15 err_message = "[u'Enter a valid postal code in the range and format 1XXX - 9XXX.']" 23 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '0287')24 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '14309')25 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '873')26 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '35 74')27 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '859A')16 self.assertRaisesMessage(ValidationError, err_message, f.clean, '0287') 17 self.assertRaisesMessage(ValidationError, err_message, f.clean, '14309') 18 self.assertRaisesMessage(ValidationError, err_message, f.clean, '873') 19 self.assertRaisesMessage(ValidationError, err_message, f.clean, '35 74') 20 self.assertRaisesMessage(ValidationError, err_message, f.clean, '859A') 28 21 err_message = "[u'This field is required.']" 29 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '')22 self.assertRaisesMessage(ValidationError, err_message, f.clean, '') 30 23 f = BEPostalCodeField(required=False) 31 24 self.assertEqual(u'1451', f.clean('1451')) 32 25 self.assertEqual(u'2540', f.clean('2540')) 33 26 self.assertEqual(u'', f.clean('')) 34 27 err_message = "[u'Enter a valid postal code in the range and format 1XXX - 9XXX.']" 35 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '0287')36 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '14309')37 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '873')38 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '35 74')39 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '859A')28 self.assertRaisesMessage(ValidationError, err_message, f.clean, '0287') 29 self.assertRaisesMessage(ValidationError, err_message, f.clean, '14309') 30 self.assertRaisesMessage(ValidationError, err_message, f.clean, '873') 31 self.assertRaisesMessage(ValidationError, err_message, f.clean, '35 74') 32 self.assertRaisesMessage(ValidationError, err_message, f.clean, '859A') 40 33 41 34 def test_phone_number_field(self): 42 35 f = BEPhoneNumberField() … … 52 45 self.assertEqual(u'012345678', f.clean('012345678')) 53 46 self.assertEqual(u'0412345678', f.clean('0412345678')) 54 47 err_message = "[u'Enter a valid phone number in one of the formats 0x xxx xx xx, 0xx xx xx xx, 04xx xx xx xx, 0x/xxx.xx.xx, 0xx/xx.xx.xx, 04xx/xx.xx.xx, 0xxxxxxxx, 04xxxxxxxx, 0x.xxx.xx.xx, 0xx.xx.xx.xx, 04xx.xx.xx.xx.']" 55 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '01234567')56 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '12/345.67.89')57 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '012/345.678.90')58 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '012/34.56.789')59 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '0123/45.67.89')60 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '012/345 678 90')61 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '012/34 56 789')62 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '012.34 56 789')48 self.assertRaisesMessage(ValidationError, err_message, f.clean, '01234567') 49 self.assertRaisesMessage(ValidationError, err_message, f.clean, '12/345.67.89') 50 self.assertRaisesMessage(ValidationError, err_message, f.clean, '012/345.678.90') 51 self.assertRaisesMessage(ValidationError, err_message, f.clean, '012/34.56.789') 52 self.assertRaisesMessage(ValidationError, err_message, f.clean, '0123/45.67.89') 53 self.assertRaisesMessage(ValidationError, err_message, f.clean, '012/345 678 90') 54 self.assertRaisesMessage(ValidationError, err_message, f.clean, '012/34 56 789') 55 self.assertRaisesMessage(ValidationError, err_message, f.clean, '012.34 56 789') 63 56 err_message = "[u'This field is required.']" 64 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '')57 self.assertRaisesMessage(ValidationError, err_message, f.clean, '') 65 58 f = BEPhoneNumberField(required=False) 66 59 self.assertEqual(u'01 234 56 78', f.clean('01 234 56 78')) 67 60 self.assertEqual(u'01/234.56.78', f.clean('01/234.56.78')) … … 76 69 self.assertEqual(u'0412345678', f.clean('0412345678')) 77 70 self.assertEqual(u'', f.clean('')) 78 71 err_message = "[u'Enter a valid phone number in one of the formats 0x xxx xx xx, 0xx xx xx xx, 04xx xx xx xx, 0x/xxx.xx.xx, 0xx/xx.xx.xx, 04xx/xx.xx.xx, 0xxxxxxxx, 04xxxxxxxx, 0x.xxx.xx.xx, 0xx.xx.xx.xx, 04xx.xx.xx.xx.']" 79 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '01234567')80 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '12/345.67.89')81 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '012/345.678.90')82 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '012/34.56.789')83 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '0123/45.67.89')84 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '012/345 678 90')85 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '012/34 56 789')86 self.assertRaises ErrorWithMessage(ValidationError, err_message, f.clean, '012.34 56 789')72 self.assertRaisesMessage(ValidationError, err_message, f.clean, '01234567') 73 self.assertRaisesMessage(ValidationError, err_message, f.clean, '12/345.67.89') 74 self.assertRaisesMessage(ValidationError, err_message, f.clean, '012/345.678.90') 75 self.assertRaisesMessage(ValidationError, err_message, f.clean, '012/34.56.789') 76 self.assertRaisesMessage(ValidationError, err_message, f.clean, '0123/45.67.89') 77 self.assertRaisesMessage(ValidationError, err_message, f.clean, '012/345 678 90') 78 self.assertRaisesMessage(ValidationError, err_message, f.clean, '012/34 56 789') 79 self.assertRaisesMessage(ValidationError, err_message, f.clean, '012.34 56 789') 87 80 88 81 def test_phone_number_field(self): 89 82 w = BERegionSelect() -
tests/regressiontests/queries/tests.py
diff --git a/tests/regressiontests/queries/tests.py b/tests/regressiontests/queries/tests.py
a b 21 21 def assertValueQuerysetEqual(self, qs, values): 22 22 return self.assertQuerysetEqual(qs, values, transform=lambda x: x) 23 23 24 def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):25 try:26 func(*args, **kwargs)27 except Exception, e:28 self.assertEqual(msg, str(e))29 self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))30 31 24 32 25 class Queries1Tests(BaseQuerysetTest): 33 26 def setUp(self): -
tests/regressiontests/urlpatterns_reverse/tests.py
diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py
a b 133 133 class NoURLPatternsTests(TestCase): 134 134 urls = 'regressiontests.urlpatterns_reverse.no_urls' 135 135 136 def assertRaisesErrorWithMessage(self, error, message, callable,137 *args, **kwargs):138 self.assertRaises(error, callable, *args, **kwargs)139 try:140 callable(*args, **kwargs)141 except error, e:142 self.assertEqual(message, str(e))143 144 136 def test_no_urls_exception(self): 145 137 """ 146 138 RegexURLResolver should raise an exception when no urlpatterns exist. 147 139 """ 148 140 resolver = RegexURLResolver(r'^$', self.urls) 149 141 150 self.assertRaises ErrorWithMessage(ImproperlyConfigured,142 self.assertRaisesMessage(ImproperlyConfigured, 151 143 "The included urlconf regressiontests.urlpatterns_reverse.no_urls "\ 152 144 "doesn't have any patterns in it", getattr, resolver, 'url_patterns') 153 145 -
tests/regressiontests/utils/datastructures.py
diff --git a/tests/regressiontests/utils/datastructures.py b/tests/regressiontests/utils/datastructures.py
a b 2 2 Tests for stuff in django.utils.datastructures. 3 3 """ 4 4 import pickle 5 import unittest6 5 6 from django.test import TestCase 7 7 from django.utils.datastructures import * 8 8 9 9 10 class DatastructuresTestCase(unittest.TestCase): 11 def assertRaisesErrorWithMessage(self, error, message, callable, 12 *args, **kwargs): 13 self.assertRaises(error, callable, *args, **kwargs) 14 try: 15 callable(*args, **kwargs) 16 except error, e: 17 self.assertEqual(message, str(e)) 18 19 20 class SortedDictTests(DatastructuresTestCase): 10 class SortedDictTests(TestCase): 21 11 def setUp(self): 22 12 self.d1 = SortedDict() 23 13 self.d1[7] = 'seven' … … 123 113 self.assertEquals(self.d1, {}) 124 114 self.assertEquals(self.d1.keyOrder, []) 125 115 126 class MergeDictTests( DatastructuresTestCase):116 class MergeDictTests(TestCase): 127 117 128 118 def test_simple_mergedict(self): 129 119 d1 = {'chris':'cool', 'camri':'cute', 'cotton':'adorable', … … 177 167 ('key2', ['value2', 'value3']), 178 168 ('key4', ['value5', 'value6'])]) 179 169 180 class MultiValueDictTests( DatastructuresTestCase):170 class MultiValueDictTests(TestCase): 181 171 182 172 def test_multivaluedict(self): 183 173 d = MultiValueDict({'name': ['Adrian', 'Simon'], … … 196 186 # MultiValueDictKeyError: "Key 'lastname' not found in 197 187 # <MultiValueDict: {'position': ['Developer'], 198 188 # 'name': ['Adrian', 'Simon']}>" 199 self.assertRaises ErrorWithMessage(MultiValueDictKeyError,189 self.assertRaisesMessage(MultiValueDictKeyError, 200 190 '"Key \'lastname\' not found in <MultiValueDict: {\'position\':'\ 201 191 ' [\'Developer\'], \'name\': [\'Adrian\', \'Simon\']}>"', 202 192 d.__getitem__, 'lastname') … … 212 202 ['Developer', 'Simon', 'Willison']) 213 203 214 204 215 class DotExpandedDictTests( DatastructuresTestCase):205 class DotExpandedDictTests(TestCase): 216 206 217 207 def test_dotexpandeddict(self): 218 208 … … 226 216 self.assertEquals(d['person']['2']['firstname'], ['Adrian']) 227 217 228 218 229 class ImmutableListTests( DatastructuresTestCase):219 class ImmutableListTests(TestCase): 230 220 231 221 def test_sort(self): 232 222 d = ImmutableList(range(10)) 233 223 234 224 # AttributeError: ImmutableList object is immutable. 235 self.assertRaises ErrorWithMessage(AttributeError,225 self.assertRaisesMessage(AttributeError, 236 226 'ImmutableList object is immutable.', d.sort) 237 227 238 228 self.assertEquals(repr(d), '(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)') … … 243 233 self.assertEquals(d[1], 1) 244 234 245 235 # AttributeError: Object is immutable! 246 self.assertRaises ErrorWithMessage(AttributeError,236 self.assertRaisesMessage(AttributeError, 247 237 'Object is immutable!', d.__setitem__, 1, 'test') 248 238 249 239 250 class DictWrapperTests( DatastructuresTestCase):240 class DictWrapperTests(TestCase): 251 241 252 242 def test_dictwrapper(self): 253 243 f = lambda x: "*%s" % x