Ticket #14503: 14503-assertraisesregex-r14981.diff
File 14503-assertraisesregex-r14981.diff, 79.7 KB (added by , 14 years ago) |
---|
-
tests/regressiontests/admin_validation/tests.py
diff --git a/tests/regressiontests/admin_validation/tests.py b/tests/regressiontests/admin_validation/tests.py
a b 18 18 fields = ['spam'] 19 19 20 20 class ValidationTestCase(TestCase): 21 def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):22 try:23 func(*args, **kwargs)24 except Exception, e:25 self.assertEqual(msg, str(e))26 self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))27 28 21 def test_readonly_and_editable(self): 29 22 class SongAdmin(admin.ModelAdmin): 30 23 readonly_fields = ["original_release"] … … 40 33 # Regression test for #8027: custom ModelForms with fields/fieldsets 41 34 """ 42 35 validate(ValidFields, Song) 43 self.assertRaises Message(ImproperlyConfigured,44 "'InvalidFields .fields' refers to field 'spam' that is missing from the form.",36 self.assertRaisesRegexp(ImproperlyConfigured, 37 "'InvalidFields\.fields' refers to field 'spam' that is missing from the form\.", 45 38 validate, 46 39 InvalidFields, Song) 47 40 … … 51 44 """ 52 45 class ExcludedFields1(admin.ModelAdmin): 53 46 exclude = ('foo') 54 self.assertRaises Message(ImproperlyConfigured,55 "'ExcludedFields1 .exclude' must be a list or tuple.",47 self.assertRaisesRegexp(ImproperlyConfigured, 48 "'ExcludedFields1\.exclude' must be a list or tuple\.", 56 49 validate, 57 50 ExcludedFields1, Book) 58 51 59 52 def test_exclude_duplicate_values(self): 60 53 class ExcludedFields2(admin.ModelAdmin): 61 54 exclude = ('name', 'name') 62 self.assertRaises Message(ImproperlyConfigured,63 "There are duplicate field (s) in ExcludedFields2.exclude",55 self.assertRaisesRegexp(ImproperlyConfigured, 56 "There are duplicate field\(s\) in ExcludedFields2\.exclude", 64 57 validate, 65 58 ExcludedFields2, Book) 66 59 … … 73 66 model = Album 74 67 inlines = [ExcludedFieldsInline] 75 68 76 self.assertRaises Message(ImproperlyConfigured,77 "'ExcludedFieldsInline .exclude' must be a list or tuple.",69 self.assertRaisesRegexp(ImproperlyConfigured, 70 "'ExcludedFieldsInline\.exclude' must be a list or tuple\.", 78 71 validate, 79 72 ExcludedFieldsAlbumAdmin, Album) 80 73 … … 91 84 model = Album 92 85 inlines = [SongInline] 93 86 94 self.assertRaises Message(ImproperlyConfigured,95 "SongInline cannot exclude the field 'album' - this is the foreign key to the parent model Album .",87 self.assertRaisesRegexp(ImproperlyConfigured, 88 "SongInline cannot exclude the field 'album' - this is the foreign key to the parent model Album\.", 96 89 validate, 97 90 AlbumAdmin, Album) 98 91 … … 112 105 class TwoAlbumFKAndAnEInline(admin.TabularInline): 113 106 model = TwoAlbumFKAndAnE 114 107 115 self.assertRaises Message(Exception,116 "<class 'regressiontests.admin_validation .models.TwoAlbumFKAndAnE'> has more than 1 ForeignKey to <class 'regressiontests.admin_validation.models.Album'>",108 self.assertRaisesRegexp(Exception, 109 "<class 'regressiontests.admin_validation\.models\.TwoAlbumFKAndAnE'> has more than 1 ForeignKey to <class 'regressiontests\.admin_validation\.models\.Album'>", 117 110 validate_inline, 118 111 TwoAlbumFKAndAnEInline, None, Album) 119 112 … … 157 150 class SongAdmin(admin.ModelAdmin): 158 151 readonly_fields = ("title", "nonexistant") 159 152 160 self.assertRaises Message(ImproperlyConfigured,161 "SongAdmin.readonly_fields [1], 'nonexistant' is not a callable or an attribute of 'SongAdmin' or found in the model 'Song'.",153 self.assertRaisesRegexp(ImproperlyConfigured, 154 "SongAdmin.readonly_fields\[1\], 'nonexistant' is not a callable or an attribute of 'SongAdmin' or found in the model 'Song'\.", 162 155 validate, 163 156 SongAdmin, Song) 164 157 … … 186 179 class BookAdmin(admin.ModelAdmin): 187 180 fields = ['authors'] 188 181 189 self.assertRaises Message(ImproperlyConfigured,190 "'BookAdmin .fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",182 self.assertRaisesRegexp(ImproperlyConfigured, 183 "'BookAdmin\.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model\.", 191 184 validate, 192 185 BookAdmin, Book) 193 186 … … 197 190 ('Header 1', {'fields': ('name',)}), 198 191 ('Header 2', {'fields': ('authors',)}), 199 192 ) 200 self.assertRaises Message(ImproperlyConfigured,201 "'FieldsetBookAdmin .fieldsets[1][1]['fields']' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.",193 self.assertRaisesRegexp(ImproperlyConfigured, 194 "'FieldsetBookAdmin\.fieldsets\[1\]\[1\]\['fields'\]' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model\.", 202 195 validate, 203 196 FieldsetBookAdmin, Book) 204 197 -
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') … … 42 35 self.assertEqual(self.a1, Author.objects.get(first_name__exact='John')) 43 36 44 37 def test_filter_on_nonexistant_field(self): 45 self.assertRaises Message(38 self.assertRaisesRegexp( 46 39 FieldError, 47 "Cannot resolve keyword 'firstname' into field . Choices are: Author_ID, article, first_name, last_name, primary_set",40 "Cannot resolve keyword 'firstname' into field\. Choices are: Author_ID, article, first_name, last_name, primary_set", 48 41 Author.objects.filter, 49 42 firstname__exact='John' 50 43 ) … … 53 46 a = Author.objects.get(last_name__exact='Smith') 54 47 self.assertEqual('John', a.first_name) 55 48 self.assertEqual('Smith', a.last_name) 56 self.assertRaises Message(49 self.assertRaisesRegexp( 57 50 AttributeError, 58 51 "'Author' object has no attribute 'firstname'", 59 52 getattr, 60 53 a, 'firstname' 61 54 ) 62 55 63 self.assertRaises Message(56 self.assertRaisesRegexp( 64 57 AttributeError, 65 58 "'Author' object has no attribute 'last'", 66 59 getattr, -
tests/regressiontests/file_storage/tests.py
diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py
a b 36 36 37 37 38 38 class GetStorageClassTests(unittest.TestCase): 39 def assertRaisesErrorWithMessage(self, error, message, callable,40 *args, **kwargs):41 self.assertRaises(error, callable, *args, **kwargs)42 try:43 callable(*args, **kwargs)44 except error, e:45 self.assertEqual(message, str(e))46 47 39 def test_get_filesystem_storage(self): 48 40 """ 49 41 get_storage_class returns the class for a storage backend name/path. … … 56 48 """ 57 49 get_storage_class raises an error if the requested import don't exist. 58 50 """ 59 self.assertRaises ErrorWithMessage(51 self.assertRaisesRegexp( 60 52 ImproperlyConfigured, 61 "NonExistingStorage isn't a storage module .",53 "NonExistingStorage isn't a storage module\.", 62 54 get_storage_class, 63 55 'NonExistingStorage') 64 56 … … 66 58 """ 67 59 get_storage_class raises an error if the requested class don't exist. 68 60 """ 69 self.assertRaises ErrorWithMessage(61 self.assertRaisesRegexp( 70 62 ImproperlyConfigured, 71 'Storage module "django .core.files.storage" does not define a '\72 '"NonExistingStorage" class .',63 'Storage module "django\.core\.files\.storage" does not define a '\ 64 '"NonExistingStorage" class\.', 73 65 get_storage_class, 74 66 'django.core.files.storage.NonExistingStorage') 75 67 -
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 374 367 def test_nk_deserialize(self): 375 368 """ 376 369 Test for ticket #13030 - Python based parser version … … 528 521 ) 529 522 530 523 def test_dependency_sorting_tight_circular(self): 531 self.assertRaises Message(524 self.assertRaisesRegexp( 532 525 CommandError, 533 """Can't resolve dependencies for fixtures_regress .Circle1, fixtures_regress.Circle2 in serialized app list.""",526 """Can't resolve dependencies for fixtures_regress\.Circle1, fixtures_regress\.Circle2 in serialized app list\.""", 534 527 sort_dependencies, 535 528 [('fixtures_regress', [Person, Circle2, Circle1, Store, Book])], 536 529 ) 537 530 538 531 def test_dependency_sorting_tight_circular_2(self): 539 self.assertRaises Message(532 self.assertRaisesRegexp( 540 533 CommandError, 541 """Can't resolve dependencies for fixtures_regress .Circle1, fixtures_regress.Circle2 in serialized app list.""",534 """Can't resolve dependencies for fixtures_regress\.Circle1, fixtures_regress\.Circle2 in serialized app list\.""", 542 535 sort_dependencies, 543 536 [('fixtures_regress', [Circle1, Book, Circle2])], 544 537 ) 545 538 546 539 def test_dependency_self_referential(self): 547 self.assertRaises Message(540 self.assertRaisesRegexp( 548 541 CommandError, 549 """Can't resolve dependencies for fixtures_regress .Circle3 in serialized app list.""",542 """Can't resolve dependencies for fixtures_regress\.Circle3 in serialized app list\.""", 550 543 sort_dependencies, 551 544 [('fixtures_regress', [Book, Circle3])], 552 545 ) 553 546 554 547 def test_dependency_sorting_long(self): 555 self.assertRaises Message(548 self.assertRaisesRegexp( 556 549 CommandError, 557 """Can't resolve dependencies for fixtures_regress .Circle1, fixtures_regress.Circle2, fixtures_regress.Circle3 in serialized app list.""",550 """Can't resolve dependencies for fixtures_regress\.Circle1, fixtures_regress\.Circle2, fixtures_regress\.Circle3 in serialized app list\.""", 558 551 sort_dependencies, 559 552 [('fixtures_regress', [Person, Circle2, Circle1, Circle3, Store, Book])], 560 553 ) -
tests/regressiontests/forms/tests/fields.py
diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
a b 49 49 50 50 class FieldsTests(TestCase): 51 51 52 def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs):53 self.assertRaises(error, callable, *args, **kwargs)54 try:55 callable(*args, **kwargs)56 except error, e:57 self.assertEqual(message, str(e))58 59 52 def test_field_sets_widget_is_required(self): 60 53 self.assertTrue(Field(required=True).widget.is_required) 61 54 self.assertFalse(Field(required=False).widget.is_required) … … 66 59 f = CharField() 67 60 self.assertEqual(u'1', f.clean(1)) 68 61 self.assertEqual(u'hello', f.clean('hello')) 69 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)70 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')62 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 63 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 71 64 self.assertEqual(u'[1, 2, 3]', f.clean([1, 2, 3])) 72 65 73 66 def test_charfield_2(self): … … 82 75 f = CharField(max_length=10, required=False) 83 76 self.assertEqual(u'12345', f.clean('12345')) 84 77 self.assertEqual(u'1234567890', f.clean('1234567890')) 85 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '1234567890a')78 self.assertRaisesRegexp(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '1234567890a') 86 79 87 80 def test_charfield_4(self): 88 81 f = CharField(min_length=10, required=False) 89 82 self.assertEqual(u'', f.clean('')) 90 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345')83 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value has at least 10 characters \(it has 5\)\.'\]", f.clean, '12345') 91 84 self.assertEqual(u'1234567890', f.clean('1234567890')) 92 85 self.assertEqual(u'1234567890a', f.clean('1234567890a')) 93 86 94 87 def test_charfield_5(self): 95 88 f = CharField(min_length=10, required=True) 96 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')97 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345')89 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 90 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value has at least 10 characters \(it has 5\)\.'\]", f.clean, '12345') 98 91 self.assertEqual(u'1234567890', f.clean('1234567890')) 99 92 self.assertEqual(u'1234567890a', f.clean('1234567890a')) 100 93 … … 102 95 103 96 def test_integerfield_1(self): 104 97 f = IntegerField() 105 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')106 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)98 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 99 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 107 100 self.assertEqual(1, f.clean('1')) 108 101 self.assertEqual(True, isinstance(f.clean('1'), int)) 109 102 self.assertEqual(23, f.clean('23')) 110 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a')103 self.assertRaisesRegexp(ValidationError, "\[u'Enter a whole number\.'\]", f.clean, 'a') 111 104 self.assertEqual(42, f.clean(42)) 112 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 3.14)105 self.assertRaisesRegexp(ValidationError, "\[u'Enter a whole number\.'\]", f.clean, 3.14) 113 106 self.assertEqual(1, f.clean('1 ')) 114 107 self.assertEqual(1, f.clean(' 1')) 115 108 self.assertEqual(1, f.clean(' 1 ')) 116 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a')109 self.assertRaisesRegexp(ValidationError, "\[u'Enter a whole number\.'\]", f.clean, '1a') 117 110 118 111 def test_integerfield_2(self): 119 112 f = IntegerField(required=False) … … 124 117 self.assertEqual(1, f.clean('1')) 125 118 self.assertEqual(True, isinstance(f.clean('1'), int)) 126 119 self.assertEqual(23, f.clean('23')) 127 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a')120 self.assertRaisesRegexp(ValidationError, "\[u'Enter a whole number\.'\]", f.clean, 'a') 128 121 self.assertEqual(1, f.clean('1 ')) 129 122 self.assertEqual(1, f.clean(' 1')) 130 123 self.assertEqual(1, f.clean(' 1 ')) 131 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a')124 self.assertRaisesRegexp(ValidationError, "\[u'Enter a whole number\.'\]", f.clean, '1a') 132 125 133 126 def test_integerfield_3(self): 134 127 f = IntegerField(max_value=10) 135 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)128 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 136 129 self.assertEqual(1, f.clean(1)) 137 130 self.assertEqual(10, f.clean(10)) 138 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, 11)131 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value is less than or equal to 10\.'\]", f.clean, 11) 139 132 self.assertEqual(10, f.clean('10')) 140 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, '11')133 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value is less than or equal to 10\.'\]", f.clean, '11') 141 134 142 135 def test_integerfield_4(self): 143 136 f = IntegerField(min_value=10) 144 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)145 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1)137 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 138 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value is greater than or equal to 10\.'\]", f.clean, 1) 146 139 self.assertEqual(10, f.clean(10)) 147 140 self.assertEqual(11, f.clean(11)) 148 141 self.assertEqual(10, f.clean('10')) … … 150 143 151 144 def test_integerfield_5(self): 152 145 f = IntegerField(min_value=10, max_value=20) 153 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)154 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1)146 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 147 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value is greater than or equal to 10\.'\]", f.clean, 1) 155 148 self.assertEqual(10, f.clean(10)) 156 149 self.assertEqual(11, f.clean(11)) 157 150 self.assertEqual(10, f.clean('10')) 158 151 self.assertEqual(11, f.clean('11')) 159 152 self.assertEqual(20, f.clean(20)) 160 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 20.']", f.clean, 21)153 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value is less than or equal to 20\.'\]", f.clean, 21) 161 154 162 155 # FloatField ################################################################## 163 156 164 157 def test_floatfield_1(self): 165 158 f = FloatField() 166 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')167 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)159 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 160 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 168 161 self.assertEqual(1.0, f.clean('1')) 169 162 self.assertEqual(True, isinstance(f.clean('1'), float)) 170 163 self.assertEqual(23.0, f.clean('23')) 171 164 self.assertEqual(3.1400000000000001, f.clean('3.14')) 172 165 self.assertEqual(3.1400000000000001, f.clean(3.14)) 173 166 self.assertEqual(42.0, f.clean(42)) 174 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a')167 self.assertRaisesRegexp(ValidationError, "\[u'Enter a number\.'\]", f.clean, 'a') 175 168 self.assertEqual(1.0, f.clean('1.0 ')) 176 169 self.assertEqual(1.0, f.clean(' 1.0')) 177 170 self.assertEqual(1.0, f.clean(' 1.0 ')) 178 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a')171 self.assertRaisesRegexp(ValidationError, "\[u'Enter a number\.'\]", f.clean, '1.0a') 179 172 180 173 def test_floatfield_2(self): 181 174 f = FloatField(required=False) … … 185 178 186 179 def test_floatfield_3(self): 187 180 f = FloatField(max_value=1.5, min_value=0.5) 188 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 1.5.']", f.clean, '1.6')189 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 0.5.']", f.clean, '0.4')181 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value is less than or equal to 1\.5\.'\]", f.clean, '1.6') 182 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value is greater than or equal to 0\.5\.'\]", f.clean, '0.4') 190 183 self.assertEqual(1.5, f.clean('1.5')) 191 184 self.assertEqual(0.5, f.clean('0.5')) 192 185 … … 194 187 195 188 def test_decimalfield_1(self): 196 189 f = DecimalField(max_digits=4, decimal_places=2) 197 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')198 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)190 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 191 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 199 192 self.assertEqual(f.clean('1'), Decimal("1")) 200 193 self.assertEqual(True, isinstance(f.clean('1'), Decimal)) 201 194 self.assertEqual(f.clean('23'), Decimal("23")) 202 195 self.assertEqual(f.clean('3.14'), Decimal("3.14")) 203 196 self.assertEqual(f.clean(3.14), Decimal("3.14")) 204 197 self.assertEqual(f.clean(Decimal('3.14')), Decimal("3.14")) 205 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'NaN')206 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'Inf')207 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '-Inf')208 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a')209 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, u'łąść')198 self.assertRaisesRegexp(ValidationError, "\[u'Enter a number\.'\]", f.clean, 'NaN') 199 self.assertRaisesRegexp(ValidationError, "\[u'Enter a number\.'\]", f.clean, 'Inf') 200 self.assertRaisesRegexp(ValidationError, "\[u'Enter a number\.'\]", f.clean, '-Inf') 201 self.assertRaisesRegexp(ValidationError, "\[u'Enter a number\.'\]", f.clean, 'a') 202 self.assertRaisesRegexp(ValidationError, "\[u'Enter a number\.'\]", f.clean, u'łąść') 210 203 self.assertEqual(f.clean('1.0 '), Decimal("1.0")) 211 204 self.assertEqual(f.clean(' 1.0'), Decimal("1.0")) 212 205 self.assertEqual(f.clean(' 1.0 '), Decimal("1.0")) 213 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a')214 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '123.45')215 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '1.234')216 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 digits before the decimal point.']", f.clean, '123.4')206 self.assertRaisesRegexp(ValidationError, "\[u'Enter a number\.'\]", f.clean, '1.0a') 207 self.assertRaisesRegexp(ValidationError, "\[u'Ensure that there are no more than 4 digits in total\.'\]", f.clean, '123.45') 208 self.assertRaisesRegexp(ValidationError, "\[u'Ensure that there are no more than 2 decimal places\.'\]", f.clean, '1.234') 209 self.assertRaisesRegexp(ValidationError, "\[u'Ensure that there are no more than 2 digits before the decimal point\.'\]", f.clean, '123.4') 217 210 self.assertEqual(f.clean('-12.34'), Decimal("-12.34")) 218 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-123.45')211 self.assertRaisesRegexp(ValidationError, "\[u'Ensure that there are no more than 4 digits in total\.'\]", f.clean, '-123.45') 219 212 self.assertEqual(f.clean('-.12'), Decimal("-0.12")) 220 213 self.assertEqual(f.clean('-00.12'), Decimal("-0.12")) 221 214 self.assertEqual(f.clean('-000.12'), Decimal("-0.12")) 222 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '-000.123')223 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-000.12345')224 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '--0.12')215 self.assertRaisesRegexp(ValidationError, "\[u'Ensure that there are no more than 2 decimal places\.'\]", f.clean, '-000.123') 216 self.assertRaisesRegexp(ValidationError, "\[u'Ensure that there are no more than 4 digits in total\.'\]", f.clean, '-000.12345') 217 self.assertRaisesRegexp(ValidationError, "\[u'Enter a number\.'\]", f.clean, '--0.12') 225 218 226 219 def test_decimalfield_2(self): 227 220 f = DecimalField(max_digits=4, decimal_places=2, required=False) … … 231 224 232 225 def test_decimalfield_3(self): 233 226 f = DecimalField(max_digits=4, decimal_places=2, max_value=Decimal('1.5'), min_value=Decimal('0.5')) 234 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 1.5.']", f.clean, '1.6')235 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 0.5.']", f.clean, '0.4')227 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value is less than or equal to 1\.5\.'\]", f.clean, '1.6') 228 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value is greater than or equal to 0\.5\.'\]", f.clean, '0.4') 236 229 self.assertEqual(f.clean('1.5'), Decimal("1.5")) 237 230 self.assertEqual(f.clean('0.5'), Decimal("0.5")) 238 231 self.assertEqual(f.clean('.5'), Decimal("0.5")) … … 240 233 241 234 def test_decimalfield_4(self): 242 235 f = DecimalField(decimal_places=2) 243 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '0.00000001')236 self.assertRaisesRegexp(ValidationError, "\[u'Ensure that there are no more than 2 decimal places\.'\]", f.clean, '0.00000001') 244 237 245 238 def test_decimalfield_5(self): 246 239 f = DecimalField(max_digits=3) … … 250 243 self.assertEqual(f.clean('0000000.100'), Decimal("0.100")) 251 244 # Only leading whole zeros "collapse" to one digit. 252 245 self.assertEqual(f.clean('000000.02'), Decimal('0.02')) 253 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 3 digits in total.']", f.clean, '000000.0002')246 self.assertRaisesRegexp(ValidationError, "\[u'Ensure that there are no more than 3 digits in total\.'\]", f.clean, '000000.0002') 254 247 self.assertEqual(f.clean('.002'), Decimal("0.002")) 255 248 256 249 def test_decimalfield_6(self): 257 250 f = DecimalField(max_digits=2, decimal_places=2) 258 251 self.assertEqual(f.clean('.01'), Decimal(".01")) 259 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 0 digits before the decimal point.']", f.clean, '1.1')252 self.assertRaisesRegexp(ValidationError, "\[u'Ensure that there are no more than 0 digits before the decimal point\.'\]", f.clean, '1.1') 260 253 261 254 # DateField ################################################################### 262 255 … … 274 267 self.assertEqual(datetime.date(2006, 10, 25), f.clean('October 25, 2006')) 275 268 self.assertEqual(datetime.date(2006, 10, 25), f.clean('25 October 2006')) 276 269 self.assertEqual(datetime.date(2006, 10, 25), f.clean('25 October, 2006')) 277 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-4-31')278 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '200a-10-25')279 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '25/10/06')280 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)270 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date\.'\]", f.clean, '2006-4-31') 271 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date\.'\]", f.clean, '200a-10-25') 272 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date\.'\]", f.clean, '25/10/06') 273 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 281 274 282 275 def test_datefield_2(self): 283 276 f = DateField(required=False) … … 291 284 self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.date(2006, 10, 25))) 292 285 self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.datetime(2006, 10, 25, 14, 30))) 293 286 self.assertEqual(datetime.date(2006, 10, 25), f.clean('2006 10 25')) 294 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-10-25')295 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/2006')296 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/06')287 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date\.'\]", f.clean, '2006-10-25') 288 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date\.'\]", f.clean, '10/25/2006') 289 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date\.'\]", f.clean, '10/25/06') 297 290 298 291 # TimeField ################################################################### 299 292 … … 303 296 self.assertEqual(datetime.time(14, 25, 59), f.clean(datetime.time(14, 25, 59))) 304 297 self.assertEqual(datetime.time(14, 25), f.clean('14:25')) 305 298 self.assertEqual(datetime.time(14, 25, 59), f.clean('14:25:59')) 306 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, 'hello')307 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '1:24 p.m.')299 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid time\.'\]", f.clean, 'hello') 300 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid time\.'\]", f.clean, '1:24 p.m.') 308 301 309 302 def test_timefield_2(self): 310 303 f = TimeField(input_formats=['%I:%M %p']) … … 312 305 self.assertEqual(datetime.time(14, 25, 59), f.clean(datetime.time(14, 25, 59))) 313 306 self.assertEqual(datetime.time(4, 25), f.clean('4:25 AM')) 314 307 self.assertEqual(datetime.time(16, 25), f.clean('4:25 PM')) 315 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '14:30:45')308 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid time\.'\]", f.clean, '14:30:45') 316 309 317 310 # DateTimeField ############################################################### 318 311 … … 334 327 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('10/25/06 14:30:00')) 335 328 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('10/25/06 14:30')) 336 329 self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean('10/25/06')) 337 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, 'hello')338 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 4:30 p.m.')330 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date/time\.'\]", f.clean, 'hello') 331 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date/time\.'\]", f.clean, '2006-10-25 4:30 p.m.') 339 332 340 333 def test_datetimefield_2(self): 341 334 f = DateTimeField(input_formats=['%Y %m %d %I:%M %p']) … … 344 337 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))) 345 338 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59, 200), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))) 346 339 self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006 10 25 2:30 PM')) 347 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 14:30:45')340 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date/time\.'\]", f.clean, '2006-10-25 14:30:45') 348 341 349 342 def test_datetimefield_3(self): 350 343 f = DateTimeField(required=False) … … 359 352 f = RegexField('^\d[A-F]\d$') 360 353 self.assertEqual(u'2A2', f.clean('2A2')) 361 354 self.assertEqual(u'3F3', f.clean('3F3')) 362 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3')363 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2')364 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ')365 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')355 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid value\.'\]", f.clean, '3G3') 356 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid value\.'\]", f.clean, ' 2A2') 357 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid value\.'\]", f.clean, '2A2 ') 358 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 366 359 367 360 def test_regexfield_2(self): 368 361 f = RegexField('^\d[A-F]\d$', required=False) 369 362 self.assertEqual(u'2A2', f.clean('2A2')) 370 363 self.assertEqual(u'3F3', f.clean('3F3')) 371 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3')364 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid value\.'\]", f.clean, '3G3') 372 365 self.assertEqual(u'', f.clean('')) 373 366 374 367 def test_regexfield_3(self): 375 368 f = RegexField(re.compile('^\d[A-F]\d$')) 376 369 self.assertEqual(u'2A2', f.clean('2A2')) 377 370 self.assertEqual(u'3F3', f.clean('3F3')) 378 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3')379 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2')380 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ')371 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid value\.'\]", f.clean, '3G3') 372 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid value\.'\]", f.clean, ' 2A2') 373 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid value\.'\]", f.clean, '2A2 ') 381 374 382 375 def test_regexfield_4(self): 383 376 f = RegexField('^\d\d\d\d$', error_message='Enter a four-digit number.') 384 377 self.assertEqual(u'1234', f.clean('1234')) 385 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, '123')386 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, 'abcd')378 self.assertRaisesRegexp(ValidationError, "\[u'Enter a four-digit number\.'\]", f.clean, '123') 379 self.assertRaisesRegexp(ValidationError, "\[u'Enter a four-digit number\.'\]", f.clean, 'abcd') 387 380 388 381 def test_regexfield_5(self): 389 382 f = RegexField('^\d+$', min_length=5, max_length=10) 390 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, '123')391 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).', u'Enter a valid value.']", f.clean, 'abc')383 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value has at least 5 characters \(it has 3\)\.'\]", f.clean, '123') 384 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value has at least 5 characters \(it has 3\)\.', u'Enter a valid value.']", f.clean, 'abc') 392 385 self.assertEqual(u'12345', f.clean('12345')) 393 386 self.assertEqual(u'1234567890', f.clean('1234567890')) 394 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '12345678901')395 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '12345a')387 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value has at most 10 characters \(it has 11\)\.'\]", f.clean, '12345678901') 388 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid value\.'\]", f.clean, '12345a') 396 389 397 390 # EmailField ################################################################## 398 391 399 392 def test_emailfield_1(self): 400 393 f = EmailField() 401 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')402 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)394 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 395 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 403 396 self.assertEqual(u'person@example.com', f.clean('person@example.com')) 404 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo')405 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@')406 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar')407 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@invalid-.com')408 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@-invalid.com')409 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.alid-.com')410 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.-alid.com')397 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'foo') 398 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'foo@') 399 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'foo@bar') 400 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'example@invalid-.com') 401 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'example@-invalid.com') 402 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'example@inv-.alid-.com') 403 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'example@inv-.-alid.com') 411 404 self.assertEqual(u'example@valid-----hyphens.com', f.clean('example@valid-----hyphens.com')) 412 405 self.assertEqual(u'example@valid-with-hyphens.com', f.clean('example@valid-with-hyphens.com')) 413 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@.com')406 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'example@.com') 414 407 self.assertEqual(u'local@domain.with.idn.xyz\xe4\xf6\xfc\xdfabc.part.com', f.clean('local@domain.with.idn.xyzäöüßabc.part.com')) 415 408 416 409 def test_email_regexp_for_performance(self): 417 410 f = EmailField() 418 411 # Check for runaway regex security problem. This will take for-freeking-ever 419 412 # if the security fix isn't in place. 420 self.assertRaises ErrorWithMessage(413 self.assertRaisesRegexp( 421 414 ValidationError, 422 " [u'Enter a valid e-mail address.']",415 "\[u'Enter a valid e-mail address\.'\]", 423 416 f.clean, 424 417 'viewx3dtextx26qx3d@yahoo.comx26latlngx3d15854521645943074058' 425 418 ) … … 430 423 self.assertEqual(u'', f.clean(None)) 431 424 self.assertEqual(u'person@example.com', f.clean('person@example.com')) 432 425 self.assertEqual(u'example@example.com', f.clean(' example@example.com \t \t ')) 433 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo')434 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@')435 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar')426 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'foo') 427 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'foo@') 428 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'foo@bar') 436 429 437 430 def test_emailfield_3(self): 438 431 f = EmailField(min_length=10, max_length=15) 439 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 9).']", f.clean, 'a@foo.com')432 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value has at least 10 characters \(it has 9\)\.'\]", f.clean, 'a@foo.com') 440 433 self.assertEqual(u'alf@foo.com', f.clean('alf@foo.com')) 441 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 15 characters (it has 20).']", f.clean, 'alf123456788@foo.com')434 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value has at most 15 characters \(it has 20\)\.'\]", f.clean, 'alf123456788@foo.com') 442 435 443 436 # FileField ################################################################## 444 437 445 438 def test_filefield_1(self): 446 439 f = FileField() 447 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')448 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '', '')440 self.assertRaisesRegexp(ValidationError, "[u'This field is required.']", f.clean, '') 441 self.assertRaisesRegexp(ValidationError, "[u'This field is required.']", f.clean, '', '') 449 442 self.assertEqual('files/test1.pdf', f.clean('', 'files/test1.pdf')) 450 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)451 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None, '')443 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 444 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None, '') 452 445 self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) 453 self.assertRaises ErrorWithMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', ''))454 self.assertRaises ErrorWithMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', ''), '')446 self.assertRaisesRegexp(ValidationError, "\[u'No file was submitted\. Check the encoding type on the form\.'\]", f.clean, SimpleUploadedFile('', '')) 447 self.assertRaisesRegexp(ValidationError, "\[u'No file was submitted\. Check the encoding type on the form\.'\]", f.clean, SimpleUploadedFile('', ''), '') 455 448 self.assertEqual('files/test3.pdf', f.clean(None, 'files/test3.pdf')) 456 self.assertRaises ErrorWithMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, 'some content that is not a file')457 self.assertRaises ErrorWithMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', None))458 self.assertRaises ErrorWithMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', ''))449 self.assertRaisesRegexp(ValidationError, "\[u'No file was submitted\. Check the encoding type on the form\.'\]", f.clean, 'some content that is not a file') 450 self.assertRaisesRegexp(ValidationError, "\[u'The submitted file is empty\.'\]", f.clean, SimpleUploadedFile('name', None)) 451 self.assertRaisesRegexp(ValidationError, "\[u'The submitted file is empty\.'\]", f.clean, SimpleUploadedFile('name', '')) 459 452 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) 460 453 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह')))) 461 454 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf'))) 462 455 463 456 def test_filefield_2(self): 464 457 f = FileField(max_length = 5) 465 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this filename has at most 5 characters (it has 18).']", f.clean, SimpleUploadedFile('test_maxlength.txt', 'hello world'))458 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this filename has at most 5 characters \(it has 18\)\.'\]", f.clean, SimpleUploadedFile('test_maxlength.txt', 'hello world')) 466 459 self.assertEqual('files/test1.pdf', f.clean('', 'files/test1.pdf')) 467 460 self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) 468 461 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) … … 471 464 472 465 def test_urlfield_1(self): 473 466 f = URLField() 474 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')475 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)467 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 468 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 476 469 self.assertEqual(u'http://localhost/', f.clean('http://localhost')) 477 470 self.assertEqual(u'http://example.com/', f.clean('http://example.com')) 478 471 self.assertEqual(u'http://example.com./', f.clean('http://example.com.')) … … 482 475 self.assertEqual(u'http://subdomain.domain.com/', f.clean('subdomain.domain.com')) 483 476 self.assertEqual(u'http://200.8.9.10/', f.clean('http://200.8.9.10')) 484 477 self.assertEqual(u'http://200.8.9.10:8000/test', f.clean('http://200.8.9.10:8000/test')) 485 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo')486 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://')487 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')488 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.')489 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'com.')490 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, '.')491 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com')492 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://invalid-.com')493 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://-invalid.com')494 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.alid-.com')495 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.-alid.com')478 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'foo') 479 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://') 480 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://example') 481 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://example.') 482 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'com.') 483 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, '.') 484 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://.com') 485 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://invalid-.com') 486 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://-invalid.com') 487 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://inv-.alid-.com') 488 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://inv-.-alid.com') 496 489 self.assertEqual(u'http://valid-----hyphens.com/', f.clean('http://valid-----hyphens.com')) 497 490 self.assertEqual(u'http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah', f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah')) 498 491 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')) … … 500 493 def test_url_regex_ticket11198(self): 501 494 f = URLField() 502 495 # hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed 503 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*200,))496 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://%s' % ("X"*200,)) 504 497 505 498 # a second test, to make sure the problem is really addressed, even on 506 499 # domains that don't fail the domain label length check in the regex 507 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*60,))500 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://%s' % ("X"*60,)) 508 501 509 502 def test_urlfield_2(self): 510 503 f = URLField(required=False) … … 512 505 self.assertEqual(u'', f.clean(None)) 513 506 self.assertEqual(u'http://example.com/', f.clean('http://example.com')) 514 507 self.assertEqual(u'http://www.example.com/', f.clean('http://www.example.com')) 515 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo')516 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://')517 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')518 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.')519 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com')508 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'foo') 509 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://') 510 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://example') 511 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://example.') 512 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://.com') 520 513 521 514 def test_urlfield_3(self): 522 515 f = URLField(verify_exists=True) 523 516 self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection 524 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example')517 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid URL\.'\]", f.clean, 'http://example') 525 518 self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain 526 519 try: 527 520 f.clean('http://www.broken.djangoproject.com') # bad domain … … 547 540 548 541 def test_urlfield_5(self): 549 542 f = URLField(min_length=15, max_length=20) 550 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at least 15 characters (it has 13).']", f.clean, 'http://f.com')543 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value has at least 15 characters \(it has 13\)\.'\]", f.clean, 'http://f.com') 551 544 self.assertEqual(u'http://example.com/', f.clean('http://example.com')) 552 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 38).']", f.clean, 'http://abcdefghijklmnopqrstuvwxyz.com')545 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value has at most 20 characters \(it has 38\)\.'\]", f.clean, 'http://abcdefghijklmnopqrstuvwxyz.com') 553 546 554 547 def test_urlfield_6(self): 555 548 f = URLField(required=False) … … 570 563 571 564 def test_booleanfield_1(self): 572 565 f = BooleanField() 573 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')574 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)566 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 567 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 575 568 self.assertEqual(True, f.clean(True)) 576 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, False)569 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, False) 577 570 self.assertEqual(True, f.clean(1)) 578 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, 0)571 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, 0) 579 572 self.assertEqual(True, f.clean('Django rocks')) 580 573 self.assertEqual(True, f.clean('True')) 581 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, 'False')574 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, 'False') 582 575 583 576 def test_booleanfield_2(self): 584 577 f = BooleanField(required=False) … … 597 590 598 591 def test_choicefield_1(self): 599 592 f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')]) 600 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')601 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)593 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 594 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 602 595 self.assertEqual(u'1', f.clean(1)) 603 596 self.assertEqual(u'1', f.clean('1')) 604 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3')597 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. 3 is not one of the available choices\.'\]", f.clean, '3') 605 598 606 599 def test_choicefield_2(self): 607 600 f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) … … 609 602 self.assertEqual(u'', f.clean(None)) 610 603 self.assertEqual(u'1', f.clean(1)) 611 604 self.assertEqual(u'1', f.clean('1')) 612 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3')605 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. 3 is not one of the available choices\.'\]", f.clean, '3') 613 606 614 607 def test_choicefield_3(self): 615 608 f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')]) 616 609 self.assertEqual(u'J', f.clean('J')) 617 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. John is not one of the available choices.']", f.clean, 'John')610 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. John is not one of the available choices\.'\]", f.clean, 'John') 618 611 619 612 def test_choicefield_4(self): 620 613 f = ChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) … … 624 617 self.assertEqual(u'3', f.clean('3')) 625 618 self.assertEqual(u'5', f.clean(5)) 626 619 self.assertEqual(u'5', f.clean('5')) 627 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, '6')620 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. 6 is not one of the available choices\.'\]", f.clean, '6') 628 621 629 622 # TypedChoiceField ############################################################ 630 623 # TypedChoiceField is just like ChoiceField, except that coerced types will … … 633 626 def test_typedchoicefield_1(self): 634 627 f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int) 635 628 self.assertEqual(1, f.clean('1')) 636 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, '2')629 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. 2 is not one of the available choices\.'\]", f.clean, '2') 637 630 638 631 def test_typedchoicefield_2(self): 639 632 # Different coercion, same validation. … … 649 642 # Even more weirdness: if you have a valid choice but your coercion function 650 643 # can't coerce, you'll still get a validation error. Don't do this! 651 644 f = TypedChoiceField(choices=[('A', 'A'), ('B', 'B')], coerce=int) 652 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. B is not one of the available choices.']", f.clean, 'B')645 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. B is not one of the available choices\.'\]", f.clean, 'B') 653 646 # Required fields require values 654 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')647 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 655 648 656 649 def test_typedchoicefield_5(self): 657 650 # Non-required fields aren't required … … 713 706 714 707 def test_multiplechoicefield_1(self): 715 708 f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')]) 716 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')717 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)709 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 710 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 718 711 self.assertEqual([u'1'], f.clean([1])) 719 712 self.assertEqual([u'1'], f.clean(['1'])) 720 713 self.assertEqual([u'1', u'2'], f.clean(['1', '2'])) 721 714 self.assertEqual([u'1', u'2'], f.clean([1, '2'])) 722 715 self.assertEqual([u'1', u'2'], f.clean((1, '2'))) 723 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')724 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, [])725 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, ())726 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3'])716 self.assertRaisesRegexp(ValidationError, "\[u'Enter a list of values\.'\]", f.clean, 'hello') 717 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, []) 718 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, ()) 719 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. 3 is not one of the available choices\.'\]", f.clean, ['3']) 727 720 728 721 def test_multiplechoicefield_2(self): 729 722 f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) … … 734 727 self.assertEqual([u'1', u'2'], f.clean(['1', '2'])) 735 728 self.assertEqual([u'1', u'2'], f.clean([1, '2'])) 736 729 self.assertEqual([u'1', u'2'], f.clean((1, '2'))) 737 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')730 self.assertRaisesRegexp(ValidationError, "\[u'Enter a list of values\.'\]", f.clean, 'hello') 738 731 self.assertEqual([], f.clean([])) 739 732 self.assertEqual([], f.clean(())) 740 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3'])733 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. 3 is not one of the available choices\.'\]", f.clean, ['3']) 741 734 742 735 def test_multiplechoicefield_3(self): 743 736 f = MultipleChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) … … 747 740 self.assertEqual([u'1', u'5'], f.clean([1, '5'])) 748 741 self.assertEqual([u'1', u'5'], f.clean(['1', 5])) 749 742 self.assertEqual([u'1', u'5'], f.clean(['1', '5'])) 750 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['6'])751 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['1','6'])743 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. 6 is not one of the available choices\.'\]", f.clean, ['6']) 744 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. 6 is not one of the available choices\.'\]", f.clean, ['1','6']) 752 745 753 746 # TypedMultipleChoiceField ############################################################ 754 747 # TypedMultipleChoiceField is just like MultipleChoiceField, except that coerced types … … 757 750 def test_typedmultiplechoicefield_1(self): 758 751 f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int) 759 752 self.assertEqual([1], f.clean(['1'])) 760 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, ['2'])753 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. 2 is not one of the available choices\.'\]", f.clean, ['2']) 761 754 762 755 def test_typedmultiplechoicefield_2(self): 763 756 # Different coercion, same validation. … … 772 765 def test_typedmultiplechoicefield_4(self): 773 766 f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int) 774 767 self.assertEqual([1, -1], f.clean(['1','-1'])) 775 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, ['1','2'])768 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. 2 is not one of the available choices\.'\]", f.clean, ['1','2']) 776 769 777 770 def test_typedmultiplechoicefield_5(self): 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 = TypedMultipleChoiceField(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.assertRaisesRegexp(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.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, []) 784 777 785 778 def test_typedmultiplechoicefield_6(self): 786 779 # Non-required fields aren't required … … 797 790 def test_combofield_1(self): 798 791 f = ComboField(fields=[CharField(max_length=20), EmailField()]) 799 792 self.assertEqual(u'test@example.com', f.clean('test@example.com')) 800 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com')801 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail')802 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')803 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)793 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value has at most 20 characters \(it has 28\)\.'\]", f.clean, 'longemailaddress@example.com') 794 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'not an e-mail') 795 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 796 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 804 797 805 798 def test_combofield_2(self): 806 799 f = ComboField(fields=[CharField(max_length=20), EmailField()], required=False) 807 800 self.assertEqual(u'test@example.com', f.clean('test@example.com')) 808 self.assertRaises ErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com')809 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail')801 self.assertRaisesRegexp(ValidationError, "\[u'Ensure this value has at most 20 characters \(it has 28\)\.'\]", f.clean, 'longemailaddress@example.com') 802 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid e-mail address\.'\]", f.clean, 'not an e-mail') 810 803 self.assertEqual(u'', f.clean('')) 811 804 self.assertEqual(u'', f.clean(None)) 812 805 … … 835 828 for exp, got in zip(expected, fix_os_paths(f.choices)): 836 829 self.assertEqual(exp[1], got[1]) 837 830 self.assertTrue(got[0].endswith(exp[0])) 838 self.assertRaises ErrorWithMessage(ValidationError, "[u'Select a valid choice. fields.py is not one of the available choices.']", f.clean, 'fields.py')831 self.assertRaisesRegexp(ValidationError, "\[u'Select a valid choice\. fields\.py is not one of the available choices\.'\]", f.clean, 'fields.py') 839 832 assert fix_os_paths(f.clean(path + 'fields.py')).endswith('/django/forms/fields.py') 840 833 841 834 def test_filepathfield_3(self): … … 883 876 f = SplitDateTimeField() 884 877 assert isinstance(f.widget, SplitDateTimeWidget) 885 878 self.assertEqual(datetime.datetime(2006, 1, 10, 7, 30), f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)])) 886 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None)887 self.assertRaises ErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '')888 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')889 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there'])890 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there'])891 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30'])879 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, None) 880 self.assertRaisesRegexp(ValidationError, "\[u'This field is required\.'\]", f.clean, '') 881 self.assertRaisesRegexp(ValidationError, "\[u'Enter a list of values\.'\]", f.clean, 'hello') 882 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date\.', u'Enter a valid time\.'\]", f.clean, ['hello', 'there']) 883 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid time\.'\]", f.clean, ['2006-01-10', 'there']) 884 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date\.'\]", f.clean, ['hello', '07:30']) 892 885 893 886 def test_splitdatetimefield_2(self): 894 887 f = SplitDateTimeField(required=False) … … 898 891 self.assertEqual(None, f.clean('')) 899 892 self.assertEqual(None, f.clean([''])) 900 893 self.assertEqual(None, f.clean(['', ''])) 901 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello')902 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there'])903 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there'])904 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30'])905 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', ''])906 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10'])907 self.assertRaises ErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['', '07:30'])894 self.assertRaisesRegexp(ValidationError, "\[u'Enter a list of values\.'\]", f.clean, 'hello') 895 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date\.', u'Enter a valid time\.'\]", f.clean, ['hello', 'there']) 896 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid time\.'\]", f.clean, ['2006-01-10', 'there']) 897 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid date\.'\]", f.clean, ['hello', '07:30']) 898 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid time\.'\]", f.clean, ['2006-01-10', '']) 899 self.assertRaisesRegexp(ValidationError, "\[u'Enter a valid time\.'\]", f.clean, ['2006-01-10']) 900 self.assertRaisesRegexp(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 21 21 def assertValueQuerysetEqual(self, qs, values): 22 22 return self.assertQuerysetEqual(qs, values, transform=lambda x: x) 23 23 24 def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):25 try:26 func(*args, **kwargs)27 except Exception, e:28 self.assertEqual(msg, str(e))29 self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))30 else:31 if hasattr(exc, '__name__'):32 excName = exc.__name__33 else:34 excName = str(exc)35 raise AssertionError, "%s not raised" % excName36 37 24 38 25 class Queries1Tests(BaseQuerysetTest): 39 26 def setUp(self): … … 362 349 def test_heterogeneous_qs_combination(self): 363 350 # Combining querysets built on different models should behave in a well-defined 364 351 # fashion. We raise an error. 365 self.assertRaises Message(352 self.assertRaisesRegexp( 366 353 AssertionError, 367 'Cannot combine queries on two different base models .',354 'Cannot combine queries on two different base models\.', 368 355 lambda: Author.objects.all() & Tag.objects.all() 369 356 ) 370 self.assertRaises Message(357 self.assertRaisesRegexp( 371 358 AssertionError, 372 'Cannot combine queries on two different base models .',359 'Cannot combine queries on two different base models\.', 373 360 lambda: Author.objects.all() | Tag.objects.all() 374 361 ) 375 362 … … 676 663 [] 677 664 ) 678 665 q.query.low_mark = 1 679 self.assertRaises Message(666 self.assertRaisesRegexp( 680 667 AssertionError, 681 668 'Cannot change a query once a slice has been taken', 682 669 q.extra, select={'is_recent': "pub_date > '2006-01-01'"} … … 707 694 ) 708 695 709 696 # Multi-valued values() and values_list() querysets should raise errors. 710 self.assertRaises Message(697 self.assertRaisesRegexp( 711 698 TypeError, 712 'Cannot use a multi-field ValuesQuerySet as a filter value .',699 'Cannot use a multi-field ValuesQuerySet as a filter value\.', 713 700 lambda: Tag.objects.filter(name__in=Tag.objects.filter(parent=self.t1).values('name', 'id')) 714 701 ) 715 self.assertRaises Message(702 self.assertRaisesRegexp( 716 703 TypeError, 717 'Cannot use a multi-field ValuesListQuerySet as a filter value .',704 'Cannot use a multi-field ValuesListQuerySet as a filter value\.', 718 705 lambda: Tag.objects.filter(name__in=Tag.objects.filter(parent=self.t1).values_list('name', 'id')) 719 706 ) 720 707 … … 954 941 def test_ticket8683(self): 955 942 # Raise proper error when a DateQuerySet gets passed a wrong type of 956 943 # field 957 self.assertRaises Message(944 self.assertRaisesRegexp( 958 945 AssertionError, 959 "'name' isn't a DateField .",946 "'name' isn't a DateField\.", 960 947 Item.objects.dates, 'name', 'month' 961 948 ) 962 949 … … 1484 1471 self.assertQuerysetEqual(Article.objects.all()[0:0], []) 1485 1472 self.assertQuerysetEqual(Article.objects.all()[0:0][:10], []) 1486 1473 self.assertEqual(Article.objects.all()[:0].count(), 0) 1487 self.assertRaises Message(1474 self.assertRaisesRegexp( 1488 1475 AssertionError, 1489 'Cannot change a query once a slice has been taken .',1476 'Cannot change a query once a slice has been taken\.', 1490 1477 Article.objects.all()[:0].latest, 'created' 1491 1478 ) 1492 1479 … … 1531 1518 def test_infinite_loop(self): 1532 1519 # If you're not careful, it's possible to introduce infinite loops via 1533 1520 # default ordering on foreign keys in a cycle. We detect that. 1534 self.assertRaises Message(1521 self.assertRaisesRegexp( 1535 1522 FieldError, 1536 'Infinite loop caused by ordering .',1523 'Infinite loop caused by ordering\.', 1537 1524 lambda: list(LoopX.objects.all()) # Force queryset evaluation with list() 1538 1525 ) 1539 self.assertRaises Message(1526 self.assertRaisesRegexp( 1540 1527 FieldError, 1541 'Infinite loop caused by ordering .',1528 'Infinite loop caused by ordering\.', 1542 1529 lambda: list(LoopZ.objects.all()) # Force queryset evaluation with list() 1543 1530 ) 1544 1531 -
tests/regressiontests/urlpatterns_reverse/tests.py
diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py
a b 133 133 class NoURLPatternsTests(TestCase): 134 134 urls = 'regressiontests.urlpatterns_reverse.no_urls' 135 135 136 def assertRaisesErrorWithMessage(self, error, message, callable,137 *args, **kwargs):138 self.assertRaises(error, callable, *args, **kwargs)139 try:140 callable(*args, **kwargs)141 except error, e:142 self.assertEqual(message, str(e))143 144 136 def test_no_urls_exception(self): 145 137 """ 146 138 RegexURLResolver should raise an exception when no urlpatterns exist. 147 139 """ 148 140 resolver = RegexURLResolver(r'^$', self.urls) 149 141 150 self.assertRaises ErrorWithMessage(ImproperlyConfigured,151 "The included urlconf regressiontests .urlpatterns_reverse.no_urls "\142 self.assertRaisesRegexp(ImproperlyConfigured, 143 "The included urlconf regressiontests\.urlpatterns_reverse\.no_urls "\ 152 144 "doesn't have any patterns in it", getattr, resolver, 'url_patterns') 153 145 154 146 class URLPatternReverse(TestCase): -
tests/regressiontests/utils/datastructures.py
diff --git a/tests/regressiontests/utils/datastructures.py b/tests/regressiontests/utils/datastructures.py
a b 2 2 Tests for stuff in django.utils.datastructures. 3 3 """ 4 4 import pickle 5 import unittest6 5 7 6 from django.utils.copycompat import copy 8 7 from django.utils.datastructures import * 8 from django.utils.unittest import TestCase 9 9 10 10 11 class DatastructuresTestCase(unittest.TestCase): 12 def assertRaisesErrorWithMessage(self, error, message, callable, 13 *args, **kwargs): 14 self.assertRaises(error, callable, *args, **kwargs) 15 try: 16 callable(*args, **kwargs) 17 except error, e: 18 self.assertEqual(message, str(e)) 19 20 21 class SortedDictTests(DatastructuresTestCase): 11 class SortedDictTests(TestCase): 22 12 def setUp(self): 23 13 self.d1 = SortedDict() 24 14 self.d1[7] = 'seven' … … 124 114 self.assertEquals(self.d1, {}) 125 115 self.assertEquals(self.d1.keyOrder, []) 126 116 127 class MergeDictTests( DatastructuresTestCase):117 class MergeDictTests(TestCase): 128 118 129 119 def test_simple_mergedict(self): 130 120 d1 = {'chris':'cool', 'camri':'cute', 'cotton':'adorable', … … 178 168 ('key2', ['value2', 'value3']), 179 169 ('key4', ['value5', 'value6'])]) 180 170 181 class MultiValueDictTests( DatastructuresTestCase):171 class MultiValueDictTests(TestCase): 182 172 183 173 def test_multivaluedict(self): 184 174 d = MultiValueDict({'name': ['Adrian', 'Simon'], … … 197 187 # MultiValueDictKeyError: "Key 'lastname' not found in 198 188 # <MultiValueDict: {'position': ['Developer'], 199 189 # 'name': ['Adrian', 'Simon']}>" 200 self.assertRaises ErrorWithMessage(MultiValueDictKeyError,201 '"Key \'lastname\' not found in <MultiValueDict:{\'position\':'\202 ' [\'Developer\'], \'name\': [\'Adrian\', \'Simon\']}>"',190 self.assertRaisesRegexp(MultiValueDictKeyError, 191 '"Key \'lastname\' not found in \<MultiValueDict: \{\'position\':'\ 192 ' \[\'Developer\'\], \'name\': \[\'Adrian\', \'Simon\'\]\}\>"', 203 193 d.__getitem__, 'lastname') 204 194 205 195 self.assertEquals(d.get('lastname'), None) … … 233 223 self.assertEqual(d2["key"], ["Penguin"]) 234 224 235 225 236 class DotExpandedDictTests( DatastructuresTestCase):226 class DotExpandedDictTests(TestCase): 237 227 238 228 def test_dotexpandeddict(self): 239 229 … … 247 237 self.assertEquals(d['person']['2']['firstname'], ['Adrian']) 248 238 249 239 250 class ImmutableListTests( DatastructuresTestCase):240 class ImmutableListTests(TestCase): 251 241 252 242 def test_sort(self): 253 243 d = ImmutableList(range(10)) 254 244 255 245 # AttributeError: ImmutableList object is immutable. 256 self.assertRaises ErrorWithMessage(AttributeError,257 'ImmutableList object is immutable .', d.sort)246 self.assertRaisesRegexp(AttributeError, 247 'ImmutableList object is immutable\.', d.sort) 258 248 259 249 self.assertEquals(repr(d), '(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)') 260 250 … … 264 254 self.assertEquals(d[1], 1) 265 255 266 256 # AttributeError: Object is immutable! 267 self.assertRaises ErrorWithMessage(AttributeError,257 self.assertRaisesRegexp(AttributeError, 268 258 'Object is immutable!', d.__setitem__, 1, 'test') 269 259 270 260 271 class DictWrapperTests( DatastructuresTestCase):261 class DictWrapperTests(TestCase): 272 262 273 263 def test_dictwrapper(self): 274 264 f = lambda x: "*%s" % x