| 1 |
# -*- coding: utf-8 -*- |
|---|
| 2 |
tests = r""" |
|---|
| 3 |
>>> from django.forms import * |
|---|
| 4 |
>>> from django.core.files.uploadedfile import SimpleUploadedFile |
|---|
| 5 |
|
|---|
| 6 |
# CharField ################################################################### |
|---|
| 7 |
|
|---|
| 8 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 9 |
>>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s' |
|---|
| 10 |
>>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s' |
|---|
| 11 |
>>> f = CharField(min_length=5, max_length=10, error_messages=e) |
|---|
| 12 |
>>> f.clean('') |
|---|
| 13 |
Traceback (most recent call last): |
|---|
| 14 |
... |
|---|
| 15 |
ValidationError: [u'REQUIRED'] |
|---|
| 16 |
>>> f.clean('1234') |
|---|
| 17 |
Traceback (most recent call last): |
|---|
| 18 |
... |
|---|
| 19 |
ValidationError: [u'LENGTH 4, MIN LENGTH 5'] |
|---|
| 20 |
>>> f.clean('12345678901') |
|---|
| 21 |
Traceback (most recent call last): |
|---|
| 22 |
... |
|---|
| 23 |
ValidationError: [u'LENGTH 11, MAX LENGTH 10'] |
|---|
| 24 |
|
|---|
| 25 |
# IntegerField ################################################################ |
|---|
| 26 |
|
|---|
| 27 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 28 |
>>> e['invalid'] = 'INVALID' |
|---|
| 29 |
>>> e['min_value'] = 'MIN VALUE IS %s' |
|---|
| 30 |
>>> e['max_value'] = 'MAX VALUE IS %s' |
|---|
| 31 |
>>> f = IntegerField(min_value=5, max_value=10, error_messages=e) |
|---|
| 32 |
>>> f.clean('') |
|---|
| 33 |
Traceback (most recent call last): |
|---|
| 34 |
... |
|---|
| 35 |
ValidationError: [u'REQUIRED'] |
|---|
| 36 |
>>> f.clean('abc') |
|---|
| 37 |
Traceback (most recent call last): |
|---|
| 38 |
... |
|---|
| 39 |
ValidationError: [u'INVALID'] |
|---|
| 40 |
>>> f.clean('4') |
|---|
| 41 |
Traceback (most recent call last): |
|---|
| 42 |
... |
|---|
| 43 |
ValidationError: [u'MIN VALUE IS 5'] |
|---|
| 44 |
>>> f.clean('11') |
|---|
| 45 |
Traceback (most recent call last): |
|---|
| 46 |
... |
|---|
| 47 |
ValidationError: [u'MAX VALUE IS 10'] |
|---|
| 48 |
|
|---|
| 49 |
# FloatField ################################################################## |
|---|
| 50 |
|
|---|
| 51 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 52 |
>>> e['invalid'] = 'INVALID' |
|---|
| 53 |
>>> e['min_value'] = 'MIN VALUE IS %s' |
|---|
| 54 |
>>> e['max_value'] = 'MAX VALUE IS %s' |
|---|
| 55 |
>>> f = FloatField(min_value=5, max_value=10, error_messages=e) |
|---|
| 56 |
>>> f.clean('') |
|---|
| 57 |
Traceback (most recent call last): |
|---|
| 58 |
... |
|---|
| 59 |
ValidationError: [u'REQUIRED'] |
|---|
| 60 |
>>> f.clean('abc') |
|---|
| 61 |
Traceback (most recent call last): |
|---|
| 62 |
... |
|---|
| 63 |
ValidationError: [u'INVALID'] |
|---|
| 64 |
>>> f.clean('4') |
|---|
| 65 |
Traceback (most recent call last): |
|---|
| 66 |
... |
|---|
| 67 |
ValidationError: [u'MIN VALUE IS 5'] |
|---|
| 68 |
>>> f.clean('11') |
|---|
| 69 |
Traceback (most recent call last): |
|---|
| 70 |
... |
|---|
| 71 |
ValidationError: [u'MAX VALUE IS 10'] |
|---|
| 72 |
|
|---|
| 73 |
# DecimalField ################################################################ |
|---|
| 74 |
|
|---|
| 75 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 76 |
>>> e['invalid'] = 'INVALID' |
|---|
| 77 |
>>> e['min_value'] = 'MIN VALUE IS %s' |
|---|
| 78 |
>>> e['max_value'] = 'MAX VALUE IS %s' |
|---|
| 79 |
>>> e['max_digits'] = 'MAX DIGITS IS %s' |
|---|
| 80 |
>>> e['max_decimal_places'] = 'MAX DP IS %s' |
|---|
| 81 |
>>> e['max_whole_digits'] = 'MAX DIGITS BEFORE DP IS %s' |
|---|
| 82 |
>>> f = DecimalField(min_value=5, max_value=10, error_messages=e) |
|---|
| 83 |
>>> f2 = DecimalField(max_digits=4, decimal_places=2, error_messages=e) |
|---|
| 84 |
>>> f.clean('') |
|---|
| 85 |
Traceback (most recent call last): |
|---|
| 86 |
... |
|---|
| 87 |
ValidationError: [u'REQUIRED'] |
|---|
| 88 |
>>> f.clean('abc') |
|---|
| 89 |
Traceback (most recent call last): |
|---|
| 90 |
... |
|---|
| 91 |
ValidationError: [u'INVALID'] |
|---|
| 92 |
>>> f.clean('4') |
|---|
| 93 |
Traceback (most recent call last): |
|---|
| 94 |
... |
|---|
| 95 |
ValidationError: [u'MIN VALUE IS 5'] |
|---|
| 96 |
>>> f.clean('11') |
|---|
| 97 |
Traceback (most recent call last): |
|---|
| 98 |
... |
|---|
| 99 |
ValidationError: [u'MAX VALUE IS 10'] |
|---|
| 100 |
>>> f2.clean('123.45') |
|---|
| 101 |
Traceback (most recent call last): |
|---|
| 102 |
... |
|---|
| 103 |
ValidationError: [u'MAX DIGITS IS 4'] |
|---|
| 104 |
>>> f2.clean('1.234') |
|---|
| 105 |
Traceback (most recent call last): |
|---|
| 106 |
... |
|---|
| 107 |
ValidationError: [u'MAX DP IS 2'] |
|---|
| 108 |
>>> f2.clean('123.4') |
|---|
| 109 |
Traceback (most recent call last): |
|---|
| 110 |
... |
|---|
| 111 |
ValidationError: [u'MAX DIGITS BEFORE DP IS 2'] |
|---|
| 112 |
|
|---|
| 113 |
# DateField ################################################################### |
|---|
| 114 |
|
|---|
| 115 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 116 |
>>> e['invalid'] = 'INVALID' |
|---|
| 117 |
>>> f = DateField(error_messages=e) |
|---|
| 118 |
>>> f.clean('') |
|---|
| 119 |
Traceback (most recent call last): |
|---|
| 120 |
... |
|---|
| 121 |
ValidationError: [u'REQUIRED'] |
|---|
| 122 |
>>> f.clean('abc') |
|---|
| 123 |
Traceback (most recent call last): |
|---|
| 124 |
... |
|---|
| 125 |
ValidationError: [u'INVALID'] |
|---|
| 126 |
|
|---|
| 127 |
# TimeField ################################################################### |
|---|
| 128 |
|
|---|
| 129 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 130 |
>>> e['invalid'] = 'INVALID' |
|---|
| 131 |
>>> f = TimeField(error_messages=e) |
|---|
| 132 |
>>> f.clean('') |
|---|
| 133 |
Traceback (most recent call last): |
|---|
| 134 |
... |
|---|
| 135 |
ValidationError: [u'REQUIRED'] |
|---|
| 136 |
>>> f.clean('abc') |
|---|
| 137 |
Traceback (most recent call last): |
|---|
| 138 |
... |
|---|
| 139 |
ValidationError: [u'INVALID'] |
|---|
| 140 |
|
|---|
| 141 |
# DateTimeField ############################################################### |
|---|
| 142 |
|
|---|
| 143 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 144 |
>>> e['invalid'] = 'INVALID' |
|---|
| 145 |
>>> f = DateTimeField(error_messages=e) |
|---|
| 146 |
>>> f.clean('') |
|---|
| 147 |
Traceback (most recent call last): |
|---|
| 148 |
... |
|---|
| 149 |
ValidationError: [u'REQUIRED'] |
|---|
| 150 |
>>> f.clean('abc') |
|---|
| 151 |
Traceback (most recent call last): |
|---|
| 152 |
... |
|---|
| 153 |
ValidationError: [u'INVALID'] |
|---|
| 154 |
|
|---|
| 155 |
# RegexField ################################################################## |
|---|
| 156 |
|
|---|
| 157 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 158 |
>>> e['invalid'] = 'INVALID' |
|---|
| 159 |
>>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s' |
|---|
| 160 |
>>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s' |
|---|
| 161 |
>>> f = RegexField(r'^\d+$', min_length=5, max_length=10, error_messages=e) |
|---|
| 162 |
>>> f.clean('') |
|---|
| 163 |
Traceback (most recent call last): |
|---|
| 164 |
... |
|---|
| 165 |
ValidationError: [u'REQUIRED'] |
|---|
| 166 |
>>> f.clean('abcde') |
|---|
| 167 |
Traceback (most recent call last): |
|---|
| 168 |
... |
|---|
| 169 |
ValidationError: [u'INVALID'] |
|---|
| 170 |
>>> f.clean('1234') |
|---|
| 171 |
Traceback (most recent call last): |
|---|
| 172 |
... |
|---|
| 173 |
ValidationError: [u'LENGTH 4, MIN LENGTH 5'] |
|---|
| 174 |
>>> f.clean('12345678901') |
|---|
| 175 |
Traceback (most recent call last): |
|---|
| 176 |
... |
|---|
| 177 |
ValidationError: [u'LENGTH 11, MAX LENGTH 10'] |
|---|
| 178 |
|
|---|
| 179 |
# EmailField ################################################################## |
|---|
| 180 |
|
|---|
| 181 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 182 |
>>> e['invalid'] = 'INVALID' |
|---|
| 183 |
>>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s' |
|---|
| 184 |
>>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s' |
|---|
| 185 |
>>> f = EmailField(min_length=8, max_length=10, error_messages=e) |
|---|
| 186 |
>>> f.clean('') |
|---|
| 187 |
Traceback (most recent call last): |
|---|
| 188 |
... |
|---|
| 189 |
ValidationError: [u'REQUIRED'] |
|---|
| 190 |
>>> f.clean('abcdefgh') |
|---|
| 191 |
Traceback (most recent call last): |
|---|
| 192 |
... |
|---|
| 193 |
ValidationError: [u'INVALID'] |
|---|
| 194 |
>>> f.clean('a@b.com') |
|---|
| 195 |
Traceback (most recent call last): |
|---|
| 196 |
... |
|---|
| 197 |
ValidationError: [u'LENGTH 7, MIN LENGTH 8'] |
|---|
| 198 |
>>> f.clean('aye@bee.com') |
|---|
| 199 |
Traceback (most recent call last): |
|---|
| 200 |
... |
|---|
| 201 |
ValidationError: [u'LENGTH 11, MAX LENGTH 10'] |
|---|
| 202 |
|
|---|
| 203 |
# FileField ################################################################## |
|---|
| 204 |
|
|---|
| 205 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 206 |
>>> e['invalid'] = 'INVALID' |
|---|
| 207 |
>>> e['missing'] = 'MISSING' |
|---|
| 208 |
>>> e['empty'] = 'EMPTY FILE' |
|---|
| 209 |
>>> f = FileField(error_messages=e) |
|---|
| 210 |
>>> f.clean('') |
|---|
| 211 |
Traceback (most recent call last): |
|---|
| 212 |
... |
|---|
| 213 |
ValidationError: [u'REQUIRED'] |
|---|
| 214 |
>>> f.clean('abc') |
|---|
| 215 |
Traceback (most recent call last): |
|---|
| 216 |
... |
|---|
| 217 |
ValidationError: [u'INVALID'] |
|---|
| 218 |
>>> f.clean(SimpleUploadedFile('name', None)) |
|---|
| 219 |
Traceback (most recent call last): |
|---|
| 220 |
... |
|---|
| 221 |
ValidationError: [u'EMPTY FILE'] |
|---|
| 222 |
>>> f.clean(SimpleUploadedFile('name', '')) |
|---|
| 223 |
Traceback (most recent call last): |
|---|
| 224 |
... |
|---|
| 225 |
ValidationError: [u'EMPTY FILE'] |
|---|
| 226 |
|
|---|
| 227 |
# URLField ################################################################## |
|---|
| 228 |
|
|---|
| 229 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 230 |
>>> e['invalid'] = 'INVALID' |
|---|
| 231 |
>>> e['invalid_link'] = 'INVALID LINK' |
|---|
| 232 |
>>> f = URLField(verify_exists=True, error_messages=e) |
|---|
| 233 |
>>> f.clean('') |
|---|
| 234 |
Traceback (most recent call last): |
|---|
| 235 |
... |
|---|
| 236 |
ValidationError: [u'REQUIRED'] |
|---|
| 237 |
>>> f.clean('abc.c') |
|---|
| 238 |
Traceback (most recent call last): |
|---|
| 239 |
... |
|---|
| 240 |
ValidationError: [u'INVALID'] |
|---|
| 241 |
>>> f.clean('http://www.broken.djangoproject.com') |
|---|
| 242 |
Traceback (most recent call last): |
|---|
| 243 |
... |
|---|
| 244 |
ValidationError: [u'INVALID LINK'] |
|---|
| 245 |
|
|---|
| 246 |
# BooleanField ################################################################ |
|---|
| 247 |
|
|---|
| 248 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 249 |
>>> f = BooleanField(error_messages=e) |
|---|
| 250 |
>>> f.clean('') |
|---|
| 251 |
Traceback (most recent call last): |
|---|
| 252 |
... |
|---|
| 253 |
ValidationError: [u'REQUIRED'] |
|---|
| 254 |
|
|---|
| 255 |
# ChoiceField ################################################################# |
|---|
| 256 |
|
|---|
| 257 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 258 |
>>> e['invalid_choice'] = '%(value)s IS INVALID CHOICE' |
|---|
| 259 |
>>> f = ChoiceField(choices=[('a', 'aye')], error_messages=e) |
|---|
| 260 |
>>> f.clean('') |
|---|
| 261 |
Traceback (most recent call last): |
|---|
| 262 |
... |
|---|
| 263 |
ValidationError: [u'REQUIRED'] |
|---|
| 264 |
>>> f.clean('b') |
|---|
| 265 |
Traceback (most recent call last): |
|---|
| 266 |
... |
|---|
| 267 |
ValidationError: [u'b IS INVALID CHOICE'] |
|---|
| 268 |
|
|---|
| 269 |
# MultipleChoiceField ######################################################### |
|---|
| 270 |
|
|---|
| 271 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 272 |
>>> e['invalid_choice'] = '%(value)s IS INVALID CHOICE' |
|---|
| 273 |
>>> e['invalid_list'] = 'NOT A LIST' |
|---|
| 274 |
>>> f = MultipleChoiceField(choices=[('a', 'aye')], error_messages=e) |
|---|
| 275 |
>>> f.clean('') |
|---|
| 276 |
Traceback (most recent call last): |
|---|
| 277 |
... |
|---|
| 278 |
ValidationError: [u'REQUIRED'] |
|---|
| 279 |
>>> f.clean('b') |
|---|
| 280 |
Traceback (most recent call last): |
|---|
| 281 |
... |
|---|
| 282 |
ValidationError: [u'NOT A LIST'] |
|---|
| 283 |
>>> f.clean(['b']) |
|---|
| 284 |
Traceback (most recent call last): |
|---|
| 285 |
... |
|---|
| 286 |
ValidationError: [u'b IS INVALID CHOICE'] |
|---|
| 287 |
|
|---|
| 288 |
# SplitDateTimeField ########################################################## |
|---|
| 289 |
|
|---|
| 290 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 291 |
>>> e['invalid_date'] = 'INVALID DATE' |
|---|
| 292 |
>>> e['invalid_time'] = 'INVALID TIME' |
|---|
| 293 |
>>> f = SplitDateTimeField(error_messages=e) |
|---|
| 294 |
>>> f.clean('') |
|---|
| 295 |
Traceback (most recent call last): |
|---|
| 296 |
... |
|---|
| 297 |
ValidationError: [u'REQUIRED'] |
|---|
| 298 |
>>> f.clean(['a', 'b']) |
|---|
| 299 |
Traceback (most recent call last): |
|---|
| 300 |
... |
|---|
| 301 |
ValidationError: [u'INVALID DATE', u'INVALID TIME'] |
|---|
| 302 |
|
|---|
| 303 |
# IPAddressField ############################################################## |
|---|
| 304 |
|
|---|
| 305 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 306 |
>>> e['invalid'] = 'INVALID IP ADDRESS' |
|---|
| 307 |
>>> f = IPAddressField(error_messages=e) |
|---|
| 308 |
>>> f.clean('') |
|---|
| 309 |
Traceback (most recent call last): |
|---|
| 310 |
... |
|---|
| 311 |
ValidationError: [u'REQUIRED'] |
|---|
| 312 |
>>> f.clean('127.0.0') |
|---|
| 313 |
Traceback (most recent call last): |
|---|
| 314 |
... |
|---|
| 315 |
ValidationError: [u'INVALID IP ADDRESS'] |
|---|
| 316 |
|
|---|
| 317 |
############################################################################### |
|---|
| 318 |
|
|---|
| 319 |
# Create choices for the model choice field tests below. |
|---|
| 320 |
|
|---|
| 321 |
>>> from regressiontests.forms.models import ChoiceModel |
|---|
| 322 |
>>> ChoiceModel.objects.create(pk=1, name='a') |
|---|
| 323 |
<ChoiceModel: ChoiceModel object> |
|---|
| 324 |
>>> ChoiceModel.objects.create(pk=2, name='b') |
|---|
| 325 |
<ChoiceModel: ChoiceModel object> |
|---|
| 326 |
>>> ChoiceModel.objects.create(pk=3, name='c') |
|---|
| 327 |
<ChoiceModel: ChoiceModel object> |
|---|
| 328 |
|
|---|
| 329 |
# ModelChoiceField ############################################################ |
|---|
| 330 |
|
|---|
| 331 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 332 |
>>> e['invalid_choice'] = 'INVALID CHOICE' |
|---|
| 333 |
>>> f = ModelChoiceField(queryset=ChoiceModel.objects.all(), error_messages=e) |
|---|
| 334 |
>>> f.clean('') |
|---|
| 335 |
Traceback (most recent call last): |
|---|
| 336 |
... |
|---|
| 337 |
ValidationError: [u'REQUIRED'] |
|---|
| 338 |
>>> f.clean('4') |
|---|
| 339 |
Traceback (most recent call last): |
|---|
| 340 |
... |
|---|
| 341 |
ValidationError: [u'INVALID CHOICE'] |
|---|
| 342 |
|
|---|
| 343 |
# ModelMultipleChoiceField #################################################### |
|---|
| 344 |
|
|---|
| 345 |
>>> e = {'required': 'REQUIRED'} |
|---|
| 346 |
>>> e['invalid_choice'] = '%s IS INVALID CHOICE' |
|---|
| 347 |
>>> e['list'] = 'NOT A LIST OF VALUES' |
|---|
| 348 |
>>> f = ModelMultipleChoiceField(queryset=ChoiceModel.objects.all(), error_messages=e) |
|---|
| 349 |
>>> f.clean('') |
|---|
| 350 |
Traceback (most recent call last): |
|---|
| 351 |
... |
|---|
| 352 |
ValidationError: [u'REQUIRED'] |
|---|
| 353 |
>>> f.clean('3') |
|---|
| 354 |
Traceback (most recent call last): |
|---|
| 355 |
... |
|---|
| 356 |
ValidationError: [u'NOT A LIST OF VALUES'] |
|---|
| 357 |
>>> f.clean(['4']) |
|---|
| 358 |
Traceback (most recent call last): |
|---|
| 359 |
... |
|---|
| 360 |
ValidationError: [u'4 IS INVALID CHOICE'] |
|---|
| 361 |
""" |
|---|