Ticket #14503: 14503-with-testcase-class.diff
File 14503-with-testcase-class.diff, 70.4 KB (added by , 13 years ago) |
---|
-
django/test/__init__.py
diff --git a/django/test/__init__.py b/django/test/__init__.py
a b 3 3 """ 4 4 5 5 from django.test.client import Client, RequestFactory 6 from django.test.testcases import TestCase, TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature 6 from django.test.testcases import (TestCase, TransactionTestCase, 7 skipIfDBFeature, skipUnlessDBFeature, SimpleTestCase) 7 8 from django.test.utils import Approximate -
django/test/testcases.py
diff --git a/django/test/testcases.py b/django/test/testcases.py
a b 21 21 from django.utils.encoding import smart_str 22 22 23 23 __all__ = ('DocTestRunner', 'OutputChecker', 'TestCase', 'TransactionTestCase', 24 'skipIfDBFeature', 'skipUnlessDBFeature' )24 'skipIfDBFeature', 'skipUnlessDBFeature', 'SimpleTestCase') 25 25 26 26 normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s) 27 27 normalize_decimals = lambda s: re.sub(r"Decimal\('(\d+(\.\d*)?)'\)", lambda m: "Decimal(\"%s\")" % m.groups()[0], s) … … 235 235 ) 236 236 ) 237 237 238 class SimpleTestCase(ut2.TestCase): 238 239 239 class TransactionTestCase(ut2.TestCase): 240 def assertRaisesMessage(self, expected_exception, expected_message, 241 callable_obj=None, *args, **kwargs): 242 """Asserts that the message in a raised exception matches the passe value. 243 244 Args: 245 expected_exception: Exception class expected to be raised. 246 expected_message: expected error message string value. 247 callable_obj: Function to be called. 248 args: Extra args. 249 kwargs: Extra kwargs. 250 """ 251 return self.assertRaisesRegexp(expected_exception, 252 re.escape(expected_message), callable_obj, *args, **kwargs) 253 254 class TransactionTestCase(SimpleTestCase): 240 255 # The class we'll use for the test client self.client. 241 256 # Can be overridden in derived classes. 242 257 client_class = Client -
tests/regressiontests/admin_validation/tests.py
diff --git a/tests/regressiontests/admin_validation/tests.py b/tests/regressiontests/admin_validation/tests.py
a b 19 19 fields = ['spam'] 20 20 21 21 class ValidationTestCase(TestCase): 22 def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):23 try:24 func(*args, **kwargs)25 except Exception, e:26 self.assertEqual(msg, str(e))27 self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))28 22 29 23 def test_readonly_and_editable(self): 30 24 class SongAdmin(admin.ModelAdmin): -
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 22 22 from django.core.files.images import get_image_dimensions 23 23 from django.core.files.storage import FileSystemStorage, get_storage_class 24 24 from django.core.files.uploadedfile import UploadedFile 25 from django.test import SimpleTestCase 25 26 from django.utils import unittest 26 27 27 28 # Try to import PIL in either of the two ways it can end up installed. … … 36 37 Image = None 37 38 38 39 39 class GetStorageClassTests(unittest.TestCase): 40 def assertRaisesErrorWithMessage(self, error, message, callable, 41 *args, **kwargs): 42 self.assertRaises(error, callable, *args, **kwargs) 43 try: 44 callable(*args, **kwargs) 45 except error, e: 46 self.assertEqual(message, str(e)) 40 class GetStorageClassTests(SimpleTestCase): 47 41 48 42 def test_get_filesystem_storage(self): 49 43 """ … … 57 51 """ 58 52 get_storage_class raises an error if the requested import don't exist. 59 53 """ 60 self.assertRaises ErrorWithMessage(54 self.assertRaisesMessage( 61 55 ImproperlyConfigured, 62 56 "NonExistingStorage isn't a storage module.", 63 57 get_storage_class, … … 67 61 """ 68 62 get_storage_class raises an error if the requested class don't exist. 69 63 """ 70 self.assertRaises ErrorWithMessage(64 self.assertRaisesMessage( 71 65 ImproperlyConfigured, 72 66 'Storage module "django.core.files.storage" does not define a '\ 73 67 '"NonExistingStorage" class.', -
tests/regressiontests/fixtures_regress/tests.py
diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py
a b 393 393 394 394 395 395 class NaturalKeyFixtureTests(TestCase): 396 def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):397 try:398 func(*args, **kwargs)399 except Exception, e:400 self.assertEqual(msg, str(e))401 self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))402 396 403 397 def test_nk_deserialize(self): 404 398 """ -
tests/regressiontests/forms/tests/fields.py
diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
a b 33 33 34 34 from django.core.files.uploadedfile import SimpleUploadedFile 35 35 from django.forms import * 36 from django. utils.unittest importTestCase36 from django.test import SimpleTestCase 37 37 38 38 39 39 def fix_os_paths(x): … … 69 69 return decorator 70 70 71 71 72 class FieldsTests(TestCase): 73 74 def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs): 75 self.assertRaises(error, callable, *args, **kwargs) 76 try: 77 callable(*args, **kwargs) 78 except error, e: 79 self.assertEqual(message, str(e)) 72 class FieldsTests(SimpleTestCase): 80 73 81 74 def test_field_sets_widget_is_required(self): 82 75 self.assertTrue(Field(required=True).widget.is_required) … … 88 81 f = CharField() 89 82 self.assertEqual(u'1', f.clean(1)) 90 83 self.assertEqual(u'hello', f.clean('hello')) 91 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)92 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')84 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 85 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 93 86 self.assertEqual(u'[1, 2, 3]', f.clean([1, 2, 3])) 94 87 self.assertEqual(f.max_length, None) 95 88 self.assertEqual(f.min_length, None) … … 108 101 f = CharField(max_length=10, required=False) 109 102 self.assertEqual(u'12345', f.clean('12345')) 110 103 self.assertEqual(u'1234567890', f.clean('1234567890')) 111 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '1234567890a')104 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '1234567890a') 112 105 self.assertEqual(f.max_length, 10) 113 106 self.assertEqual(f.min_length, None) 114 107 115 108 def test_charfield_4(self): 116 109 f = CharField(min_length=10, required=False) 117 110 self.assertEqual(u'', f.clean('')) 118 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345')111 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345') 119 112 self.assertEqual(u'1234567890', f.clean('1234567890')) 120 113 self.assertEqual(u'1234567890a', f.clean('1234567890a')) 121 114 self.assertEqual(f.max_length, None) … … 123 116 124 117 def test_charfield_5(self): 125 118 f = CharField(min_length=10, required=True) 126 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')127 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345')119 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 120 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345') 128 121 self.assertEqual(u'1234567890', f.clean('1234567890')) 129 122 self.assertEqual(u'1234567890a', f.clean('1234567890a')) 130 123 self.assertEqual(f.max_length, None) … … 134 127 135 128 def test_integerfield_1(self): 136 129 f = IntegerField() 137 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')138 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)130 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 131 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 139 132 self.assertEqual(1, f.clean('1')) 140 133 self.assertEqual(True, isinstance(f.clean('1'), int)) 141 134 self.assertEqual(23, f.clean('23')) 142 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a')135 self.assertRaisesMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a') 143 136 self.assertEqual(42, f.clean(42)) 144 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 3.14)137 self.assertRaisesMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 3.14) 145 138 self.assertEqual(1, f.clean('1 ')) 146 139 self.assertEqual(1, f.clean(' 1')) 147 140 self.assertEqual(1, f.clean(' 1 ')) 148 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a')141 self.assertRaisesMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a') 149 142 self.assertEqual(f.max_value, None) 150 143 self.assertEqual(f.min_value, None) 151 144 … … 158 151 self.assertEqual(1, f.clean('1')) 159 152 self.assertEqual(True, isinstance(f.clean('1'), int)) 160 153 self.assertEqual(23, f.clean('23')) 161 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a')154 self.assertRaisesMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a') 162 155 self.assertEqual(1, f.clean('1 ')) 163 156 self.assertEqual(1, f.clean(' 1')) 164 157 self.assertEqual(1, f.clean(' 1 ')) 165 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a')158 self.assertRaisesMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a') 166 159 self.assertEqual(f.max_value, None) 167 160 self.assertEqual(f.min_value, None) 168 161 169 162 def test_integerfield_3(self): 170 163 f = IntegerField(max_value=10) 171 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)164 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 172 165 self.assertEqual(1, f.clean(1)) 173 166 self.assertEqual(10, f.clean(10)) 174 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, 11)167 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, 11) 175 168 self.assertEqual(10, f.clean('10')) 176 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, '11')169 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, '11') 177 170 self.assertEqual(f.max_value, 10) 178 171 self.assertEqual(f.min_value, None) 179 172 180 173 def test_integerfield_4(self): 181 174 f = IntegerField(min_value=10) 182 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)183 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1)175 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 176 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1) 184 177 self.assertEqual(10, f.clean(10)) 185 178 self.assertEqual(11, f.clean(11)) 186 179 self.assertEqual(10, f.clean('10')) … … 190 183 191 184 def test_integerfield_5(self): 192 185 f = IntegerField(min_value=10, max_value=20) 193 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)194 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1)186 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 187 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1) 195 188 self.assertEqual(10, f.clean(10)) 196 189 self.assertEqual(11, f.clean(11)) 197 190 self.assertEqual(10, f.clean('10')) 198 191 self.assertEqual(11, f.clean('11')) 199 192 self.assertEqual(20, f.clean(20)) 200 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 20.']", f.clean, 21)193 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is less than or equal to 20.']", f.clean, 21) 201 194 self.assertEqual(f.max_value, 20) 202 195 self.assertEqual(f.min_value, 10) 203 196 … … 205 198 206 199 def test_floatfield_1(self): 207 200 f = FloatField() 208 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')209 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)201 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 202 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 210 203 self.assertEqual(1.0, f.clean('1')) 211 204 self.assertEqual(True, isinstance(f.clean('1'), float)) 212 205 self.assertEqual(23.0, f.clean('23')) 213 206 self.assertEqual(3.1400000000000001, f.clean('3.14')) 214 207 self.assertEqual(3.1400000000000001, f.clean(3.14)) 215 208 self.assertEqual(42.0, f.clean(42)) 216 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a')209 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a') 217 210 self.assertEqual(1.0, f.clean('1.0 ')) 218 211 self.assertEqual(1.0, f.clean(' 1.0')) 219 212 self.assertEqual(1.0, f.clean(' 1.0 ')) 220 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a')213 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a') 221 214 self.assertEqual(f.max_value, None) 222 215 self.assertEqual(f.min_value, None) 223 216 … … 231 224 232 225 def test_floatfield_3(self): 233 226 f = FloatField(max_value=1.5, min_value=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(1.5, f.clean('1.5')) 237 230 self.assertEqual(0.5, f.clean('0.5')) 238 231 self.assertEqual(f.max_value, 1.5) … … 242 235 243 236 def test_decimalfield_1(self): 244 237 f = DecimalField(max_digits=4, decimal_places=2) 245 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')246 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)238 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 239 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 247 240 self.assertEqual(f.clean('1'), Decimal("1")) 248 241 self.assertEqual(True, isinstance(f.clean('1'), Decimal)) 249 242 self.assertEqual(f.clean('23'), Decimal("23")) 250 243 self.assertEqual(f.clean('3.14'), Decimal("3.14")) 251 244 self.assertEqual(f.clean(3.14), Decimal("3.14")) 252 245 self.assertEqual(f.clean(Decimal('3.14')), Decimal("3.14")) 253 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'NaN')254 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'Inf')255 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '-Inf')256 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a')257 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, u'łąść')246 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, 'NaN') 247 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, 'Inf') 248 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, '-Inf') 249 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a') 250 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, u'łąść') 258 251 self.assertEqual(f.clean('1.0 '), Decimal("1.0")) 259 252 self.assertEqual(f.clean(' 1.0'), Decimal("1.0")) 260 253 self.assertEqual(f.clean(' 1.0 '), Decimal("1.0")) 261 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a')262 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '123.45')263 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '1.234')264 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 digits before the decimal point.']", f.clean, '123.4')254 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a') 255 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '123.45') 256 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '1.234') 257 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 2 digits before the decimal point.']", f.clean, '123.4') 265 258 self.assertEqual(f.clean('-12.34'), Decimal("-12.34")) 266 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-123.45')259 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-123.45') 267 260 self.assertEqual(f.clean('-.12'), Decimal("-0.12")) 268 261 self.assertEqual(f.clean('-00.12'), Decimal("-0.12")) 269 262 self.assertEqual(f.clean('-000.12'), Decimal("-0.12")) 270 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '-000.123')271 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-000.12345')272 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '--0.12')263 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '-000.123') 264 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-000.12345') 265 self.assertRaisesMessage(ValidationError, "[u'Enter a number.']", f.clean, '--0.12') 273 266 self.assertEqual(f.max_digits, 4) 274 267 self.assertEqual(f.decimal_places, 2) 275 268 self.assertEqual(f.max_value, None) … … 287 280 288 281 def test_decimalfield_3(self): 289 282 f = DecimalField(max_digits=4, decimal_places=2, max_value=Decimal('1.5'), min_value=Decimal('0.5')) 290 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 1.5.']", f.clean, '1.6')291 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 0.5.']", f.clean, '0.4')283 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is less than or equal to 1.5.']", f.clean, '1.6') 284 self.assertRaisesMessage(ValidationError, "[u'Ensure this value is greater than or equal to 0.5.']", f.clean, '0.4') 292 285 self.assertEqual(f.clean('1.5'), Decimal("1.5")) 293 286 self.assertEqual(f.clean('0.5'), Decimal("0.5")) 294 287 self.assertEqual(f.clean('.5'), Decimal("0.5")) … … 300 293 301 294 def test_decimalfield_4(self): 302 295 f = DecimalField(decimal_places=2) 303 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '0.00000001')296 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '0.00000001') 304 297 305 298 def test_decimalfield_5(self): 306 299 f = DecimalField(max_digits=3) … … 310 303 self.assertEqual(f.clean('0000000.100'), Decimal("0.100")) 311 304 # Only leading whole zeros "collapse" to one digit. 312 305 self.assertEqual(f.clean('000000.02'), Decimal('0.02')) 313 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 3 digits in total.']", f.clean, '000000.0002')306 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 3 digits in total.']", f.clean, '000000.0002') 314 307 self.assertEqual(f.clean('.002'), Decimal("0.002")) 315 308 316 309 def test_decimalfield_6(self): 317 310 f = DecimalField(max_digits=2, decimal_places=2) 318 311 self.assertEqual(f.clean('.01'), Decimal(".01")) 319 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 0 digits before the decimal point.']", f.clean, '1.1')312 self.assertRaisesMessage(ValidationError, "[u'Ensure that there are no more than 0 digits before the decimal point.']", f.clean, '1.1') 320 313 321 314 # DateField ################################################################### 322 315 … … 334 327 self.assertEqual(datetime.date(2006, 10, 25), f.clean('October 25, 2006')) 335 328 self.assertEqual(datetime.date(2006, 10, 25), f.clean('25 October 2006')) 336 329 self.assertEqual(datetime.date(2006, 10, 25), f.clean('25 October, 2006')) 337 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-4-31')338 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '200a-10-25')339 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '25/10/06')340 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)330 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-4-31') 331 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '200a-10-25') 332 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '25/10/06') 333 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 341 334 342 335 def test_datefield_2(self): 343 336 f = DateField(required=False) … … 351 344 self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.date(2006, 10, 25))) 352 345 self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.datetime(2006, 10, 25, 14, 30))) 353 346 self.assertEqual(datetime.date(2006, 10, 25), f.clean('2006 10 25')) 354 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-10-25')355 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/2006')356 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/06')347 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-10-25') 348 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/2006') 349 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/06') 357 350 358 351 def test_datefield_4(self): 359 352 # Test whitespace stripping behavior (#5714) … … 364 357 self.assertEqual(datetime.date(2006, 10, 25), f.clean(' October 25 2006 ')) 365 358 self.assertEqual(datetime.date(2006, 10, 25), f.clean(' October 25, 2006 ')) 366 359 self.assertEqual(datetime.date(2006, 10, 25), f.clean(' 25 October 2006 ')) 367 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ' ')360 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ' ') 368 361 369 362 # TimeField ################################################################### 370 363 … … 374 367 self.assertEqual(datetime.time(14, 25, 59), f.clean(datetime.time(14, 25, 59))) 375 368 self.assertEqual(datetime.time(14, 25), f.clean('14:25')) 376 369 self.assertEqual(datetime.time(14, 25, 59), f.clean('14:25:59')) 377 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, 'hello')378 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '1:24 p.m.')370 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, 'hello') 371 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '1:24 p.m.') 379 372 380 373 def test_timefield_2(self): 381 374 f = TimeField(input_formats=['%I:%M %p']) … … 383 376 self.assertEqual(datetime.time(14, 25, 59), f.clean(datetime.time(14, 25, 59))) 384 377 self.assertEqual(datetime.time(4, 25), f.clean('4:25 AM')) 385 378 self.assertEqual(datetime.time(16, 25), f.clean('4:25 PM')) 386 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '14:30:45')379 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '14:30:45') 387 380 388 381 def test_timefield_3(self): 389 382 f = TimeField() 390 383 # Test whitespace stripping behavior (#5714) 391 384 self.assertEqual(datetime.time(14, 25), f.clean(' 14:25 ')) 392 385 self.assertEqual(datetime.time(14, 25, 59), f.clean(' 14:25:59 ')) 393 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ' ')386 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ' ') 394 387 395 388 # DateTimeField ############################################################### 396 389 … … 415 408 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('10/25/06 14:30:00')) 416 409 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('10/25/06 14:30')) 417 410 self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean('10/25/06')) 418 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, 'hello')419 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 4:30 p.m.')411 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, 'hello') 412 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 4:30 p.m.') 420 413 421 414 def test_datetimefield_2(self): 422 415 f = DateTimeField(input_formats=['%Y %m %d %I:%M %p']) … … 425 418 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))) 426 419 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59, 200), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))) 427 420 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006 10 25 2:30 PM')) 428 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 14:30:45')421 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 14:30:45') 429 422 430 423 def test_datetimefield_3(self): 431 424 f = DateTimeField(required=False) … … 444 437 self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(' 10/25/2006 ')) 445 438 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45), f.clean(' 10/25/06 14:30:45 ')) 446 439 self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(' 10/25/06 ')) 447 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, ' ')440 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, ' ') 448 441 449 442 # RegexField ################################################################## 450 443 … … 452 445 f = RegexField('^\d[A-F]\d$') 453 446 self.assertEqual(u'2A2', f.clean('2A2')) 454 447 self.assertEqual(u'3F3', f.clean('3F3')) 455 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3')456 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2')457 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ')458 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')448 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3') 449 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2') 450 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ') 451 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 459 452 460 453 def test_regexfield_2(self): 461 454 f = RegexField('^\d[A-F]\d$', required=False) 462 455 self.assertEqual(u'2A2', f.clean('2A2')) 463 456 self.assertEqual(u'3F3', f.clean('3F3')) 464 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3')457 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3') 465 458 self.assertEqual(u'', f.clean('')) 466 459 467 460 def test_regexfield_3(self): 468 461 f = RegexField(re.compile('^\d[A-F]\d$')) 469 462 self.assertEqual(u'2A2', f.clean('2A2')) 470 463 self.assertEqual(u'3F3', f.clean('3F3')) 471 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3')472 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2')473 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ')464 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3') 465 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2') 466 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ') 474 467 475 468 def test_regexfield_4(self): 476 469 f = RegexField('^\d\d\d\d$', error_message='Enter a four-digit number.') 477 470 self.assertEqual(u'1234', f.clean('1234')) 478 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, '123')479 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, 'abcd')471 self.assertRaisesMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, '123') 472 self.assertRaisesMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, 'abcd') 480 473 481 474 def test_regexfield_5(self): 482 475 f = RegexField('^\d+$', min_length=5, max_length=10) 483 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, '123')484 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).', u'Enter a valid value.']", f.clean, 'abc')476 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, '123') 477 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).', u'Enter a valid value.']", f.clean, 'abc') 485 478 self.assertEqual(u'12345', f.clean('12345')) 486 479 self.assertEqual(u'1234567890', f.clean('1234567890')) 487 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '12345678901')488 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '12345a')480 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '12345678901') 481 self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '12345a') 489 482 490 483 # EmailField ################################################################## 491 484 492 485 def test_emailfield_1(self): 493 486 f = EmailField() 494 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')495 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)487 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 488 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 496 489 self.assertEqual(u'person@example.com', f.clean('person@example.com')) 497 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo')498 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@')499 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar')500 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@invalid-.com')501 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@-invalid.com')502 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.alid-.com')503 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.-alid.com')490 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo') 491 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@') 492 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar') 493 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@invalid-.com') 494 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@-invalid.com') 495 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.alid-.com') 496 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.-alid.com') 504 497 self.assertEqual(u'example@valid-----hyphens.com', f.clean('example@valid-----hyphens.com')) 505 498 self.assertEqual(u'example@valid-with-hyphens.com', f.clean('example@valid-with-hyphens.com')) 506 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@.com')499 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@.com') 507 500 self.assertEqual(u'local@domain.with.idn.xyz\xe4\xf6\xfc\xdfabc.part.com', f.clean('local@domain.with.idn.xyzäöüßabc.part.com')) 508 501 509 502 def test_email_regexp_for_performance(self): 510 503 f = EmailField() 511 504 # Check for runaway regex security problem. This will take for-freeking-ever 512 505 # if the security fix isn't in place. 513 self.assertRaises ErrorWithMessage(506 self.assertRaisesMessage( 514 507 ValidationError, 515 508 "[u'Enter a valid e-mail address.']", 516 509 f.clean, … … 523 516 self.assertEqual(u'', f.clean(None)) 524 517 self.assertEqual(u'person@example.com', f.clean('person@example.com')) 525 518 self.assertEqual(u'example@example.com', f.clean(' example@example.com \t \t ')) 526 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo')527 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@')528 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar')519 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo') 520 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@') 521 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar') 529 522 530 523 def test_emailfield_3(self): 531 524 f = EmailField(min_length=10, max_length=15) 532 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 9).']", f.clean, 'a@foo.com')525 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 9).']", f.clean, 'a@foo.com') 533 526 self.assertEqual(u'alf@foo.com', f.clean('alf@foo.com')) 534 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 15 characters (it has 20).']", f.clean, 'alf123456788@foo.com')527 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 15 characters (it has 20).']", f.clean, 'alf123456788@foo.com') 535 528 536 529 # FileField ################################################################## 537 530 538 531 def test_filefield_1(self): 539 532 f = FileField() 540 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')541 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '', '')533 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 534 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '', '') 542 535 self.assertEqual('files/test1.pdf', f.clean('', 'files/test1.pdf')) 543 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)544 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None, '')536 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 537 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None, '') 545 538 self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) 546 self.assertRaises ErrorWithMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', ''))547 self.assertRaises ErrorWithMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', ''), '')539 self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', '')) 540 self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', ''), '') 548 541 self.assertEqual('files/test3.pdf', f.clean(None, 'files/test3.pdf')) 549 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')550 self.assertRaises ErrorWithMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', None))551 self.assertRaises ErrorWithMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', ''))542 self.assertRaisesMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, 'some content that is not a file') 543 self.assertRaisesMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', None)) 544 self.assertRaisesMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', '')) 552 545 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) 553 546 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह')))) 554 547 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf'))) 555 548 556 549 def test_filefield_2(self): 557 550 f = FileField(max_length = 5) 558 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this filename has at most 5 characters (it has 18).']", f.clean, SimpleUploadedFile('test_maxlength.txt', 'hello world'))551 self.assertRaisesMessage(ValidationError, "[u'Ensure this filename has at most 5 characters (it has 18).']", f.clean, SimpleUploadedFile('test_maxlength.txt', 'hello world')) 559 552 self.assertEqual('files/test1.pdf', f.clean('', 'files/test1.pdf')) 560 553 self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) 561 554 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) … … 569 562 570 563 def test_urlfield_1(self): 571 564 f = URLField() 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(u'http://localhost/', f.clean('http://localhost')) 575 568 self.assertEqual(u'http://example.com/', f.clean('http://example.com')) 576 569 self.assertEqual(u'http://example.com./', f.clean('http://example.com.')) … … 580 573 self.assertEqual(u'http://subdomain.domain.com/', f.clean('subdomain.domain.com')) 581 574 self.assertEqual(u'http://200.8.9.10/', f.clean('http://200.8.9.10')) 582 575 self.assertEqual(u'http://200.8.9.10:8000/test', f.clean('http://200.8.9.10:8000/test')) 583 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo')584 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://')585 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')586 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.')587 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'com.')588 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, '.')589 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com')590 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://invalid-.com')591 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://-invalid.com')592 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.alid-.com')593 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.-alid.com')576 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo') 577 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://') 578 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') 579 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.') 580 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'com.') 581 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, '.') 582 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com') 583 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://invalid-.com') 584 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://-invalid.com') 585 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.alid-.com') 586 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.-alid.com') 594 587 self.assertEqual(u'http://valid-----hyphens.com/', f.clean('http://valid-----hyphens.com')) 595 588 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')) 596 589 self.assertEqual(u'http://www.example.com/s/http://code.djangoproject.com/ticket/13804', f.clean('www.example.com/s/http://code.djangoproject.com/ticket/13804')) … … 598 591 def test_url_regex_ticket11198(self): 599 592 f = URLField() 600 593 # hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed 601 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*200,))594 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*200,)) 602 595 603 596 # a second test, to make sure the problem is really addressed, even on 604 597 # domains that don't fail the domain label length check in the regex 605 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*60,))598 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*60,)) 606 599 607 600 def test_urlfield_2(self): 608 601 f = URLField(required=False) … … 610 603 self.assertEqual(u'', f.clean(None)) 611 604 self.assertEqual(u'http://example.com/', f.clean('http://example.com')) 612 605 self.assertEqual(u'http://www.example.com/', f.clean('http://www.example.com')) 613 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo')614 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://')615 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')616 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.')617 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com')606 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo') 607 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://') 608 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') 609 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.') 610 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com') 618 611 619 612 @verify_exists_urls(('http://www.google.com/',)) 620 613 def test_urlfield_3(self): 621 614 f = URLField(verify_exists=True) 622 615 self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) 623 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')616 self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') 624 617 self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain 625 618 self.assertRaises(ValidationError, f.clean, 'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed 626 619 try: … … 641 634 642 635 def test_urlfield_5(self): 643 636 f = URLField(min_length=15, max_length=20) 644 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 15 characters (it has 13).']", f.clean, 'http://f.com')637 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at least 15 characters (it has 13).']", f.clean, 'http://f.com') 645 638 self.assertEqual(u'http://example.com/', f.clean('http://example.com')) 646 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 38).']", f.clean, 'http://abcdefghijklmnopqrstuvwxyz.com')639 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 38).']", f.clean, 'http://abcdefghijklmnopqrstuvwxyz.com') 647 640 648 641 def test_urlfield_6(self): 649 642 f = URLField(required=False) … … 697 690 698 691 def test_booleanfield_1(self): 699 692 f = BooleanField() 700 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')701 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)693 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 694 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 702 695 self.assertEqual(True, f.clean(True)) 703 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, False)696 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, False) 704 697 self.assertEqual(True, f.clean(1)) 705 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, 0)698 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, 0) 706 699 self.assertEqual(True, f.clean('Django rocks')) 707 700 self.assertEqual(True, f.clean('True')) 708 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, 'False')701 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, 'False') 709 702 710 703 def test_booleanfield_2(self): 711 704 f = BooleanField(required=False) … … 726 719 727 720 def test_choicefield_1(self): 728 721 f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')]) 729 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')730 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)722 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 723 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 731 724 self.assertEqual(u'1', f.clean(1)) 732 725 self.assertEqual(u'1', f.clean('1')) 733 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3')726 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3') 734 727 735 728 def test_choicefield_2(self): 736 729 f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) … … 738 731 self.assertEqual(u'', f.clean(None)) 739 732 self.assertEqual(u'1', f.clean(1)) 740 733 self.assertEqual(u'1', f.clean('1')) 741 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3')734 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3') 742 735 743 736 def test_choicefield_3(self): 744 737 f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')]) 745 738 self.assertEqual(u'J', f.clean('J')) 746 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. John is not one of the available choices.']", f.clean, 'John')739 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. John is not one of the available choices.']", f.clean, 'John') 747 740 748 741 def test_choicefield_4(self): 749 742 f = ChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) … … 753 746 self.assertEqual(u'3', f.clean('3')) 754 747 self.assertEqual(u'5', f.clean(5)) 755 748 self.assertEqual(u'5', f.clean('5')) 756 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, '6')749 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, '6') 757 750 758 751 # TypedChoiceField ############################################################ 759 752 # TypedChoiceField is just like ChoiceField, except that coerced types will … … 762 755 def test_typedchoicefield_1(self): 763 756 f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int) 764 757 self.assertEqual(1, f.clean('1')) 765 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, '2')758 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, '2') 766 759 767 760 def test_typedchoicefield_2(self): 768 761 # Different coercion, same validation. … … 778 771 # Even more weirdness: if you have a valid choice but your coercion function 779 772 # can't coerce, you'll still get a validation error. Don't do this! 780 773 f = TypedChoiceField(choices=[('A', 'A'), ('B', 'B')], coerce=int) 781 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. B is not one of the available choices.']", f.clean, 'B')774 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. B is not one of the available choices.']", f.clean, 'B') 782 775 # Required fields require values 783 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')776 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 784 777 785 778 def test_typedchoicefield_5(self): 786 779 # Non-required fields aren't required … … 842 835 843 836 def test_multiplechoicefield_1(self): 844 837 f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')]) 845 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')846 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)838 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 839 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 847 840 self.assertEqual([u'1'], f.clean([1])) 848 841 self.assertEqual([u'1'], f.clean(['1'])) 849 842 self.assertEqual([u'1', u'2'], f.clean(['1', '2'])) 850 843 self.assertEqual([u'1', u'2'], f.clean([1, '2'])) 851 844 self.assertEqual([u'1', u'2'], f.clean((1, '2'))) 852 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')853 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, [])854 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, ())855 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3'])845 self.assertRaisesMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') 846 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, []) 847 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, ()) 848 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3']) 856 849 857 850 def test_multiplechoicefield_2(self): 858 851 f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) … … 863 856 self.assertEqual([u'1', u'2'], f.clean(['1', '2'])) 864 857 self.assertEqual([u'1', u'2'], f.clean([1, '2'])) 865 858 self.assertEqual([u'1', u'2'], f.clean((1, '2'))) 866 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')859 self.assertRaisesMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') 867 860 self.assertEqual([], f.clean([])) 868 861 self.assertEqual([], f.clean(())) 869 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3'])862 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3']) 870 863 871 864 def test_multiplechoicefield_3(self): 872 865 f = MultipleChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) … … 876 869 self.assertEqual([u'1', u'5'], f.clean([1, '5'])) 877 870 self.assertEqual([u'1', u'5'], f.clean(['1', 5])) 878 871 self.assertEqual([u'1', u'5'], f.clean(['1', '5'])) 879 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['6'])880 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['1','6'])872 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['6']) 873 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['1','6']) 881 874 882 875 # TypedMultipleChoiceField ############################################################ 883 876 # TypedMultipleChoiceField is just like MultipleChoiceField, except that coerced types … … 886 879 def test_typedmultiplechoicefield_1(self): 887 880 f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int) 888 881 self.assertEqual([1], f.clean(['1'])) 889 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, ['2'])882 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, ['2']) 890 883 891 884 def test_typedmultiplechoicefield_2(self): 892 885 # Different coercion, same validation. … … 901 894 def test_typedmultiplechoicefield_4(self): 902 895 f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int) 903 896 self.assertEqual([1, -1], f.clean(['1','-1'])) 904 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, ['1','2'])897 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, ['1','2']) 905 898 906 899 def test_typedmultiplechoicefield_5(self): 907 900 # Even more weirdness: if you have a valid choice but your coercion function 908 901 # can't coerce, you'll still get a validation error. Don't do this! 909 902 f = TypedMultipleChoiceField(choices=[('A', 'A'), ('B', 'B')], coerce=int) 910 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. B is not one of the available choices.']", f.clean, ['B'])903 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. B is not one of the available choices.']", f.clean, ['B']) 911 904 # Required fields require values 912 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, [])905 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, []) 913 906 914 907 def test_typedmultiplechoicefield_6(self): 915 908 # Non-required fields aren't required … … 926 919 def test_combofield_1(self): 927 920 f = ComboField(fields=[CharField(max_length=20), EmailField()]) 928 921 self.assertEqual(u'test@example.com', f.clean('test@example.com')) 929 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com')930 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail')931 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')932 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)922 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com') 923 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail') 924 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 925 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 933 926 934 927 def test_combofield_2(self): 935 928 f = ComboField(fields=[CharField(max_length=20), EmailField()], required=False) 936 929 self.assertEqual(u'test@example.com', f.clean('test@example.com')) 937 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com')938 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail')930 self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com') 931 self.assertRaisesMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail') 939 932 self.assertEqual(u'', f.clean('')) 940 933 self.assertEqual(u'', f.clean(None)) 941 934 … … 964 957 for exp, got in zip(expected, fix_os_paths(f.choices)): 965 958 self.assertEqual(exp[1], got[1]) 966 959 self.assertTrue(got[0].endswith(exp[0])) 967 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. fields.py is not one of the available choices.']", f.clean, 'fields.py')960 self.assertRaisesMessage(ValidationError, "[u'Select a valid choice. fields.py is not one of the available choices.']", f.clean, 'fields.py') 968 961 assert fix_os_paths(f.clean(path + 'fields.py')).endswith('/django/forms/fields.py') 969 962 970 963 def test_filepathfield_3(self): … … 1012 1005 f = SplitDateTimeField() 1013 1006 assert isinstance(f.widget, SplitDateTimeWidget) 1014 1007 self.assertEqual(datetime.datetime(2006, 1, 10, 7, 30), f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)])) 1015 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)1016 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')1017 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')1018 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there'])1019 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there'])1020 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30'])1008 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, None) 1009 self.assertRaisesMessage(ValidationError, "[u'This field is required.']", f.clean, '') 1010 self.assertRaisesMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') 1011 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there']) 1012 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there']) 1013 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30']) 1021 1014 1022 1015 def test_splitdatetimefield_2(self): 1023 1016 f = SplitDateTimeField(required=False) … … 1027 1020 self.assertEqual(None, f.clean('')) 1028 1021 self.assertEqual(None, f.clean([''])) 1029 1022 self.assertEqual(None, f.clean(['', ''])) 1030 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')1031 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there'])1032 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there'])1033 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30'])1034 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', ''])1035 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10'])1036 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['', '07:30'])1023 self.assertRaisesMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') 1024 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there']) 1025 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there']) 1026 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30']) 1027 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', '']) 1028 self.assertRaisesMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10']) 1029 self.assertRaisesMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['', '07:30']) -
tests/regressiontests/queries/tests.py
diff --git a/tests/regressiontests/queries/tests.py b/tests/regressiontests/queries/tests.py
a b 22 22 def assertValueQuerysetEqual(self, qs, values): 23 23 return self.assertQuerysetEqual(qs, values, transform=lambda x: x) 24 24 25 def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):26 try:27 func(*args, **kwargs)28 except Exception, e:29 self.assertEqual(msg, str(e))30 self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))31 else:32 if hasattr(exc, '__name__'):33 excName = exc.__name__34 else:35 excName = str(exc)36 raise AssertionError("%s not raised" % excName)37 38 25 39 26 class Queries1Tests(BaseQuerysetTest): 40 27 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 138 138 class NoURLPatternsTests(TestCase): 139 139 urls = 'regressiontests.urlpatterns_reverse.no_urls' 140 140 141 def assertRaisesErrorWithMessage(self, error, message, callable,142 *args, **kwargs):143 self.assertRaises(error, callable, *args, **kwargs)144 try:145 callable(*args, **kwargs)146 except error, e:147 self.assertEqual(message, str(e))148 149 141 def test_no_urls_exception(self): 150 142 """ 151 143 RegexURLResolver should raise an exception when no urlpatterns exist. 152 144 """ 153 145 resolver = RegexURLResolver(r'^$', self.urls) 154 146 155 self.assertRaises ErrorWithMessage(ImproperlyConfigured,147 self.assertRaisesMessage(ImproperlyConfigured, 156 148 "The included urlconf regressiontests.urlpatterns_reverse.no_urls "\ 157 149 "doesn't have any patterns in it", getattr, resolver, 'url_patterns') 158 150 -
tests/regressiontests/utils/datastructures.py
diff --git a/tests/regressiontests/utils/datastructures.py b/tests/regressiontests/utils/datastructures.py
a b 4 4 5 5 import copy 6 6 import pickle 7 import unittest8 7 8 from django.test import SimpleTestCase 9 9 from django.utils.datastructures import * 10 10 11 11 12 class DatastructuresTestCase(unittest.TestCase): 13 def assertRaisesErrorWithMessage(self, error, message, callable, 14 *args, **kwargs): 15 self.assertRaises(error, callable, *args, **kwargs) 16 try: 17 callable(*args, **kwargs) 18 except error, e: 19 self.assertEqual(message, str(e)) 12 class DatastructuresTestCase(SimpleTestCase): 13 pass 20 14 21 15 22 16 class SortedDictTests(DatastructuresTestCase): … … 198 192 # MultiValueDictKeyError: "Key 'lastname' not found in 199 193 # <MultiValueDict: {'position': ['Developer'], 200 194 # 'name': ['Adrian', 'Simon']}>" 201 self.assertRaises ErrorWithMessage(MultiValueDictKeyError,195 self.assertRaisesMessage(MultiValueDictKeyError, 202 196 '"Key \'lastname\' not found in <MultiValueDict: {\'position\':'\ 203 197 ' [\'Developer\'], \'name\': [\'Adrian\', \'Simon\']}>"', 204 198 d.__getitem__, 'lastname') … … 268 262 d = ImmutableList(range(10)) 269 263 270 264 # AttributeError: ImmutableList object is immutable. 271 self.assertRaises ErrorWithMessage(AttributeError,265 self.assertRaisesMessage(AttributeError, 272 266 'ImmutableList object is immutable.', d.sort) 273 267 274 268 self.assertEqual(repr(d), '(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)') … … 279 273 self.assertEqual(d[1], 1) 280 274 281 275 # AttributeError: Object is immutable! 282 self.assertRaises ErrorWithMessage(AttributeError,276 self.assertRaisesMessage(AttributeError, 283 277 'Object is immutable!', d.__setitem__, 1, 'test') 284 278 285 279