Changeset 7322
- Timestamp:
- 03/19/08 14:11:51 (4 months ago)
- Files:
-
- django/trunk/tests/modeltests/custom_pk/models.py (modified) (1 diff)
- django/trunk/tests/modeltests/lookup/models.py (modified) (1 diff)
- django/trunk/tests/modeltests/manipulators/models.py (modified) (1 diff)
- django/trunk/tests/modeltests/model_forms/models.py (modified) (5 diffs)
- django/trunk/tests/modeltests/validation/models.py (modified) (4 diffs)
- django/trunk/tests/regressiontests/forms/extra.py (modified) (1 diff)
- django/trunk/tests/regressiontests/forms/forms.py (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/tests/modeltests/custom_pk/models.py
r6346 r7322 72 72 >>> Employee.objects.filter(last_name__exact='Jones') 73 73 [<Employee: Dan Jones>, <Employee: Fran Jones>] 74 >>> Employee.objects.in_bulk(['ABC123', 'XYZ456']) 75 {u'XYZ456': <Employee: Fran Jones>, u'ABC123': <Employee: Dan Jones>} 74 >>> emps = Employee.objects.in_bulk(['ABC123', 'XYZ456']) 75 >>> emps['ABC123'] 76 <Employee: Dan Jones> 76 77 77 78 >>> b = Business(name='Sears') django/trunk/tests/modeltests/lookup/models.py
r5876 r7322 77 77 # in_bulk() takes a list of IDs and returns a dictionary mapping IDs 78 78 # to objects. 79 >>> Article.objects.in_bulk([1, 2]) 80 {1: <Article: Article 1>, 2: <Article: Article 2>} 79 >>> arts = Article.objects.in_bulk([1, 2]) 80 >>> arts[1] 81 <Article: Article 1> 82 >>> arts[2] 83 <Article: Article 2> 81 84 >>> Article.objects.in_bulk([3]) 82 85 {3: <Article: Article 3>} django/trunk/tests/modeltests/manipulators/models.py
r7258 r7322 42 42 43 43 # Attempt to add a Musician without a first_name. 44 >>> man.get_validation_errors(MultiValueDict({'last_name': ['Blakey']})) 45 {'first_name': [u'This field is required.']} 44 >>> man.get_validation_errors(MultiValueDict({'last_name': ['Blakey']}))['first_name'] 45 [u'This field is required.'] 46 46 47 47 # Attempt to add a Musician without a first_name and last_name. 48 >>> man.get_validation_errors(MultiValueDict({})) 49 {'first_name': [u'This field is required.'], 'last_name': [u'This field is required.']} 48 >>> errors = man.get_validation_errors(MultiValueDict({})) 49 >>> errors['first_name'] 50 [u'This field is required.'] 51 >>> errors['last_name'] 52 [u'This field is required.'] 50 53 51 54 # Attempt to create an Album without a name or musician. 52 55 >>> man = Album.AddManipulator() 53 >>> man.get_validation_errors(MultiValueDict({})) 54 {'musician': [u'This field is required.'], 'name': [u'This field is required.']} 56 >>> errors = man.get_validation_errors(MultiValueDict({})) 57 >>> errors['musician'] 58 [u'This field is required.'] 59 >>> errors['name'] 60 [u'This field is required.'] 55 61 56 62 # Attempt to create an Album with an invalid musician. 57 >>> man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['foo']})) 58 {'musician': [u"Select a valid choice; 'foo' is not in [u'', u'1']."]} 63 >>> errors = man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['foo']})) 64 >>> errors['musician'] 65 [u"Select a valid choice; 'foo' is not in [u'', u'1']."] 59 66 60 67 # Attempt to create an Album with an invalid release_date. 61 >>> man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['1'], 'release_date': 'today'})) 62 {'release_date': [u'Enter a valid date in YYYY-MM-DD format.']} 68 >>> errors = man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['1'], 'release_date': 'today'})) 69 >>> errors['release_date'] 70 [u'Enter a valid date in YYYY-MM-DD format.'] 63 71 64 72 # Create an Album without a release_date (because it's optional). django/trunk/tests/modeltests/model_forms/models.py
r7115 r7322 235 235 >>> f.is_valid() 236 236 True 237 >>> f.cleaned_data 238 {'url': u'entertainment', 'name': u'Entertainment', 'slug': u'entertainment'} 237 >>> f.cleaned_data['url'] 238 u'entertainment' 239 >>> f.cleaned_data['name'] 240 u'Entertainment' 241 >>> f.cleaned_data['slug'] 242 u'entertainment' 239 243 >>> obj = f.save() 240 244 >>> obj … … 246 250 >>> f.is_valid() 247 251 True 248 >>> f.cleaned_data 249 {'url': u'test', 'name': u"It's a test", 'slug': u'its-test'} 252 >>> f.cleaned_data['url'] 253 u'test' 254 >>> f.cleaned_data['name'] 255 u"It's a test" 256 >>> f.cleaned_data['slug'] 257 u'its-test' 250 258 >>> obj = f.save() 251 259 >>> obj … … 260 268 >>> f.is_valid() 261 269 True 262 >>> f.cleaned_data 263 {'url': u'third', 'name': u'Third test', 'slug': u'third-test'} 270 >>> f.cleaned_data['url'] 271 u'third' 272 >>> f.cleaned_data['name'] 273 u'Third test' 274 >>> f.cleaned_data['slug'] 275 u'third-test' 264 276 >>> obj = f.save(commit=False) 265 277 >>> obj … … 273 285 If you call save() with invalid data, you'll get a ValueError. 274 286 >>> f = CategoryForm({'name': '', 'slug': '', 'url': 'foo'}) 275 >>> f.errors 276 {'name': [u'This field is required.'], 'slug': [u'This field is required.']} 287 >>> f.errors['name'] 288 [u'This field is required.'] 289 >>> f.errors['slug'] 290 [u'This field is required.'] 277 291 >>> f.cleaned_data 278 292 Traceback (most recent call last): … … 740 754 >>> f.is_valid() 741 755 True 742 >>> f.cleaned_data 743 {'phone': u'312-555-1212', 'description': u'Assistance'} 756 >>> f.cleaned_data['phone'] 757 u'312-555-1212' 758 >>> f.cleaned_data['description'] 759 u'Assistance' 744 760 745 761 # FileField ################################################################### django/trunk/tests/modeltests/validation/models.py
r5876 r7322 42 42 43 43 >>> p = Person(**dict(valid_params, id='foo')) 44 >>> p.validate() 45 {'id': [u'This value must be an integer.']} 44 >>> p.validate()['id'] 45 [u'This value must be an integer.'] 46 46 47 47 >>> p = Person(**dict(valid_params, id=None)) … … 76 76 77 77 >>> p = Person(**dict(valid_params, is_child='foo')) 78 >>> p.validate() 79 {'is_child': [u'This value must be either True or False.']} 78 >>> p.validate()['is_child'] 79 [u'This value must be either True or False.'] 80 80 81 81 >>> p = Person(**dict(valid_params, name=u'Jose')) … … 116 116 117 117 >>> p = Person(**dict(valid_params, birthdate='foo')) 118 >>> p.validate() 119 {'birthdate': [u'Enter a valid date in YYYY-MM-DD format.']} 118 >>> p.validate()['birthdate'] 119 [u'Enter a valid date in YYYY-MM-DD format.'] 120 120 121 121 >>> p = Person(**dict(valid_params, favorite_moment=datetime.datetime(2002, 4, 3, 13, 23))) … … 144 144 145 145 >>> p = Person(**dict(valid_params, email=22)) 146 >>> p.validate() 147 {'email': [u'Enter a valid e-mail address.']} 146 >>> p.validate()['email'] 147 [u'Enter a valid e-mail address.'] 148 148 149 149 # Make sure that Date and DateTime return validation errors and don't raise Python errors. 150 >>> Person(name='John Doe', is_child=True, email='abc@def.com').validate() 151 {'favorite_moment': [u'This field is required.'], 'birthdate': [u'This field is required.']} 150 >>> p = Person(name='John Doe', is_child=True, email='abc@def.com') 151 >>> errors = p.validate() 152 >>> errors['favorite_moment'] 153 [u'This field is required.'] 154 >>> errors['birthdate'] 155 [u'This field is required.'] 152 156 153 157 """} django/trunk/tests/regressiontests/forms/extra.py
r7294 r7322 253 253 <input type="text" name="field1_2_0" value="2007-04-25" id="id_field1_2_0" /><input type="text" name="field1_2_1" value="06:24:00" id="id_field1_2_1" /></td></tr> 254 254 255 >>> f.cleaned_data 256 {'field1': u'some text,JP,2007-04-25 06:24:00'} 255 >>> f.cleaned_data['field1'] 256 u'some text,JP,2007-04-25 06:24:00' 257 257 258 258 django/trunk/tests/regressiontests/forms/forms.py
r7294 r7322 37 37 >>> p.errors.as_text() 38 38 u'' 39 >>> p.cleaned_data 40 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 39 >>> p.cleaned_data["first_name"], p.cleaned_data["last_name"], p.cleaned_data["birthday"] 40 (u'John', u'Lennon', datetime.date(1940, 10, 9)) 41 41 >>> print p['first_name'] 42 42 <input type="text" name="first_name" value="John" id="id_first_name" /> … … 69 69 >>> p.is_bound 70 70 True 71 >>> p.errors 72 {'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']} 71 >>> p.errors['first_name'] 72 [u'This field is required.'] 73 >>> p.errors['last_name'] 74 [u'This field is required.'] 75 >>> p.errors['birthday'] 76 [u'This field is required.'] 73 77 >>> p.is_valid() 74 78 False … … 138 142 139 143 >>> p = Person({'last_name': u'Lennon'}) 140 >>> p.errors 141 {'first_name': [u'This field is required.'], 'birthday': [u'This field is required.']} 144 >>> p.errors['first_name'] 145 [u'This field is required.'] 146 >>> p.errors['birthday'] 147 [u'This field is required.'] 142 148 >>> p.is_valid() 143 149 False … … 176 182 >>> p.is_valid() 177 183 True 178 >>> p.cleaned_data 179 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 184 >>> p.cleaned_data['first_name'] 185 u'John' 186 >>> p.cleaned_data['last_name'] 187 u'Lennon' 188 >>> p.cleaned_data['birthday'] 189 datetime.date(1940, 10, 9) 190 180 191 181 192 cleaned_data will include a key and value for *all* fields defined in the Form, … … 192 203 >>> f.is_valid() 193 204 True 194 >>> f.cleaned_data 195 {'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'} 205 >>> f.cleaned_data['nick_name'] 206 u'' 207 >>> f.cleaned_data['first_name'] 208 u'John' 209 >>> f.cleaned_data['last_name'] 210 u'Lennon' 196 211 197 212 For DateFields, it's set to None. … … 204 219 >>> f.is_valid() 205 220 True 206 >>> f.cleaned_data 207 {'birth_date': None, 'first_name': u'John', 'last_name': u'Lennon'} 221 >>> print f.cleaned_data['birth_date'] 222 None 223 >>> f.cleaned_data['first_name'] 224 u'John' 225 >>> f.cleaned_data['last_name'] 226 u'Lennon' 208 227 209 228 "auto_id" tells the Form to add an "id" attribute to each form element. … … 550 569 returns a list of input. 551 570 >>> f = SongForm({'name': 'Yesterday'}, auto_id=False) 552 >>> f.errors 553 {'composers': [u'This field is required.']} 571 >>> f.errors['composers'] 572 [u'This field is required.'] 554 573 >>> f = SongForm({'name': 'Yesterday', 'composers': ['J']}, auto_id=False) 555 574 >>> f.errors 556 575 {} 557 >>> f.cleaned_data 558 {'composers': [u'J'], 'name': u'Yesterday'} 576 >>> f.cleaned_data['composers'] 577 [u'J'] 578 >>> f.cleaned_data['name'] 579 u'Yesterday' 559 580 >>> f = SongForm({'name': 'Yesterday', 'composers': ['J', 'P']}, auto_id=False) 560 581 >>> f.errors 561 582 {} 562 >>> f.cleaned_data 563 {'composers': [u'J', u'P'], 'name': u'Yesterday'} 583 >>> f.cleaned_data['composers'] 584 [u'J', u'P'] 585 >>> f.cleaned_data['name'] 586 u'Yesterday' 564 587 565 588 Validation errors are HTML-escaped when output as HTML. … … 599 622 {} 600 623 >>> f = UserRegistration({}, auto_id=False) 601 >>> f.errors 602 {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']} 624 >>> f.errors['username'] 625 [u'This field is required.'] 626 >>> f.errors['password1'] 627 [u'This field is required.'] 628 >>> f.errors['password2'] 629 [u'This field is required.'] 603 630 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False) 604 >>> f.errors 605 {'password2': [u'Please make sure your passwords match.']} 631 >>> f.errors['password2'] 632 [u'Please make sure your passwords match.'] 606 633 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False) 607 634 >>> f.errors 608 635 {} 609 >>> f.cleaned_data 610 {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} 636 >>> f.cleaned_data['username'] 637 u'adrian' 638 >>> f.cleaned_data['password1'] 639 u'foo' 640 >>> f.cleaned_data['password2'] 641 u'foo' 611 642 612 643 Another way of doing multiple-field validation is by implementing the … … 633 664 <tr><th>Password1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password1" /></td></tr> 634 665 <tr><th>Password2:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password2" /></td></tr> 635 >>> f.errors 636 {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']} 666 >>> f.errors['username'] 667 [u'This field is required.'] 668 >>> f.errors['password1'] 669 [u'This field is required.'] 670 >>> f.errors['password2'] 671 [u'This field is required.'] 637 672 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False) 638 >>> f.errors 639 {'__all__': [u'Please make sure your passwords match.']} 673 >>> f.errors['__all__'] 674 [u'Please make sure your passwords match.'] 640 675 >>> print f.as_table() 641 676 <tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr> … … 651 686 >>> f.errors 652 687 {} 653 >>> f.cleaned_data 654 {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} 688 >>> f.cleaned_data['username'] 689 u'adrian' 690 >>> f.cleaned_data['password1'] 691 u'foo' 692 >>> f.cleaned_data['password2'] 693 u'foo' 655 694 656 695 # Dynamic construction ######################################################## … … 1025 1064 validation error rather than using the initial value for 'username'. 1026 1065 >>> p = UserRegistration({'password': 'secret'}) 1027 >>> p.errors 1028 {'username': [u'This field is required.']} 1066 >>> p.errors['username'] 1067 [u'This field is required.'] 1029 1068 >>> p.is_valid() 1030 1069 False … … 1070 1109 validation error rather than using the initial value for 'username'. 1071 1110 >>> p = UserRegistration({'password': 'secret'}, initial={'username': 'django'}) 1072 >>> p.errors 1073 {'username': [u'This field is required.']} 1111 >>> p.errors['username'] 1112 [u'This field is required.'] 1074 1113 >>> p.is_valid() 1075 1114 False … … 1124 1163 validation error rather than using the initial value for 'username'. 1125 1164 >>> p = UserRegistration({'password': 'secret'}, initial={'username': initial_django}) 1126 >>> p.errors 1127 {'username': [u'This field is required.']} 1165 >>> p.errors['username'] 1166 [u'This field is required.'] 1128 1167 >>> p.is_valid() 1129 1168 False … … 1259 1298 >>> p.is_valid() 1260 1299 True 1261 >>> p.cleaned_data 1262 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 1300 >>> p.cleaned_data['first_name'] 1301 u'John' 1302 >>> p.cleaned_data['last_name'] 1303 u'Lennon' 1304 >>> p.cleaned_data['birthday'] 1305 datetime.date(1940, 10, 9) 1263 1306 1264 1307 Let's try submitting some bad data to make sure form.errors and field.errors … … 1270 1313 ... } 1271 1314 >>> p = Person(data, prefix='person1') 1272 >>> p.errors 1273 {'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']} 1315 >>> p.errors['first_name'] 1316 [u'This field is required.'] 1317 >>> p.errors['last_name'] 1318 [u'This field is required.'] 1319 >>> p.errors['birthday'] 1320 [u'This field is required.'] 1274 1321 >>> p['first_name'].errors 1275 1322 [u'This field is required.'] … … 1287 1334 ... } 1288 1335 >>> p = Person(data, prefix='person1') 1289 >>> p.errors 1290 {'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']} 1336 >>> p.errors['first_name'] 1337 [u'This field is required.'] 1338 >>> p.errors['last_name'] 1339 [u'This field is required.'] 1340 >>> p.errors['birthday'] 1341 [u'This field is required.'] 1291 1342 1292 1343 With prefixes, a single data dictionary can hold data for multiple instances … … 1303 1354 >>> p1.is_valid() 1304 1355 True 1305 >>> p1.cleaned_data 1306 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 1356 >>> p1.cleaned_data['first_name'] 1357 u'John' 1358 >>> p1.cleaned_data['last_name'] 1359 u'Lennon' 1360 >>> p1.cleaned_data['birthday'] 1361 datetime.date(1940, 10, 9) 1307 1362 >>> p2 = Person(data, prefix='person2') 1308 1363 >>> p2.is_valid() 1309 1364 True 1310 >>> p2.cleaned_data 1311 {'first_name': u'Jim', 'last_name': u'Morrison', 'birthday': datetime.date(1943, 12, 8)} 1365 >>> p2.cleaned_data['first_name'] 1366 u'Jim' 1367 >>> p2.cleaned_data['last_name'] 1368 u'Morrison' 1369 >>> p2.cleaned_data['birthday'] 1370 datetime.date(1943, 12, 8) 1312 1371 1313 1372 By default, forms append a hyphen between the prefix and the field name, but a … … 1334 1393 >>> p.is_valid() 1335 1394 True 1336 >>> p.cleaned_data 1337 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 1395 >>> p.cleaned_data['first_name'] 1396 u'John' 1397 >>> p.cleaned_data['last_name'] 1398 u'Lennon' 1399 >>> p.cleaned_data['birthday'] 1400 datetime.date(1940, 10, 9) 1338 1401 1339 1402 # Forms with NullBooleanFields ################################################
