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