Changeset 7967 for django/trunk/tests/regressiontests/forms
- Timestamp:
- 07/18/08 18:54:34 (6 months ago)
- Files:
-
- django/trunk/tests/regressiontests/forms/formsets.py (added)
- django/trunk/tests/regressiontests/forms/forms.py (modified) (1 diff)
- django/trunk/tests/regressiontests/forms/media.py (added)
- django/trunk/tests/regressiontests/forms/tests.py (modified) (2 diffs)
- django/trunk/tests/regressiontests/forms/widgets.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/tests/regressiontests/forms/forms.py
r7814 r7967 1668 1668 <input type="submit" /> 1669 1669 </form> 1670 1671 1672 # The empty_permitted attribute ############################################## 1673 1674 Sometimes (pretty much in formsets) we want to allow a form to pass validation 1675 if it is completely empty. We can accomplish this by using the empty_permitted 1676 agrument to a form constructor. 1677 1678 >>> class SongForm(Form): 1679 ... artist = CharField() 1680 ... name = CharField() 1681 1682 First let's show what happens id empty_permitted=False (the default): 1683 1684 >>> data = {'artist': '', 'song': ''} 1685 1686 >>> form = SongForm(data, empty_permitted=False) 1687 >>> form.is_valid() 1688 False 1689 >>> form.errors 1690 {'name': [u'This field is required.'], 'artist': [u'This field is required.']} 1691 >>> form.cleaned_data 1692 Traceback (most recent call last): 1693 ... 1694 AttributeError: 'SongForm' object has no attribute 'cleaned_data' 1695 1696 1697 Now let's show what happens when empty_permitted=True and the form is empty. 1698 1699 >>> form = SongForm(data, empty_permitted=True) 1700 >>> form.is_valid() 1701 True 1702 >>> form.errors 1703 {} 1704 >>> form.cleaned_data 1705 {} 1706 1707 But if we fill in data for one of the fields, the form is no longer empty and 1708 the whole thing must pass validation. 1709 1710 >>> data = {'artist': 'The Doors', 'song': ''} 1711 >>> form = SongForm(data, empty_permitted=False) 1712 >>> form.is_valid() 1713 False 1714 >>> form.errors 1715 {'name': [u'This field is required.']} 1716 >>> form.cleaned_data 1717 Traceback (most recent call last): 1718 ... 1719 AttributeError: 'SongForm' object has no attribute 'cleaned_data' 1720 1721 If a field is not given in the data then None is returned for its data. Lets 1722 make sure that when checking for empty_permitted that None is treated 1723 accordingly. 1724 1725 >>> data = {'artist': None, 'song': ''} 1726 >>> form = SongForm(data, empty_permitted=True) 1727 >>> form.is_valid() 1728 True 1729 1730 However, we *really* need to be sure we are checking for None as any data in 1731 initial that returns False on a boolean call needs to be treated literally. 1732 1733 >>> class PriceForm(Form): 1734 ... amount = FloatField() 1735 ... qty = IntegerField() 1736 1737 >>> data = {'amount': '0.0', 'qty': ''} 1738 >>> form = PriceForm(data, initial={'amount': 0.0}, empty_permitted=True) 1739 >>> form.is_valid() 1740 True 1741 1670 1742 """ django/trunk/tests/regressiontests/forms/tests.py
r6843 r7967 27 27 from util import tests as util_tests 28 28 from widgets import tests as widgets_tests 29 from formsets import tests as formset_tests 30 from media import media_tests 29 31 30 32 __test__ = { … … 54 56 'localflavor_za_tests': localflavor_za_tests, 55 57 'regression_tests': regression_tests, 58 'formset_tests': formset_tests, 59 'media_tests': media_tests, 56 60 'util_tests': util_tests, 57 61 'widgets_tests': widgets_tests, django/trunk/tests/regressiontests/forms/widgets.py
r7693 r7967 203 203 u'<input type="file" class="fun" name="email" />' 204 204 205 Test for the behavior of _has_changed for FileInput. The value of data will 206 more than likely come from request.FILES. The value of initial data will 207 likely be a filename stored in the database. Since its value is of no use to 208 a FileInput it is ignored. 209 210 >>> w = FileInput() 211 212 # No file was uploaded and no initial data. 213 >>> w._has_changed(u'', None) 214 False 215 216 # A file was uploaded and no initial data. 217 >>> w._has_changed(u'', {'filename': 'resume.txt', 'content': 'My resume'}) 218 True 219 220 # A file was not uploaded, but there is initial data 221 >>> w._has_changed(u'resume.txt', None) 222 False 223 224 # A file was uploaded and there is initial data (file identity is not dealt 225 # with here) 226 >>> w._has_changed('resume.txt', {'filename': 'resume.txt', 'content': 'My resume'}) 227 True 228 205 229 # Textarea Widget ############################################################# 206 230 … … 293 317 False 294 318 319 >>> w._has_changed(None, None) 320 False 321 >>> w._has_changed(None, u'') 322 False 323 >>> w._has_changed(u'', None) 324 False 325 >>> w._has_changed(u'', u'') 326 False 327 >>> w._has_changed(False, u'on') 328 True 329 >>> w._has_changed(True, u'on') 330 False 331 >>> w._has_changed(True, u'') 332 True 333 295 334 # Select Widget ############################################################### 296 335 … … 573 612 >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) 574 613 u'<select multiple="multiple" name="nums">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</option>\n<option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>\n</select>' 614 615 # Test the usage of _has_changed 616 >>> w._has_changed(None, None) 617 False 618 >>> w._has_changed([], None) 619 False 620 >>> w._has_changed(None, [u'1']) 621 True 622 >>> w._has_changed([1, 2], [u'1', u'2']) 623 False 624 >>> w._has_changed([1, 2], [u'1']) 625 True 626 >>> w._has_changed([1, 2], [u'1', u'3']) 627 True 575 628 576 629 # RadioSelect Widget ########################################################## … … 872 925 </ul> 873 926 927 # Test the usage of _has_changed 928 >>> w._has_changed(None, None) 929 False 930 >>> w._has_changed([], None) 931 False 932 >>> w._has_changed(None, [u'1']) 933 True 934 >>> w._has_changed([1, 2], [u'1', u'2']) 935 False 936 >>> w._has_changed([1, 2], [u'1']) 937 True 938 >>> w._has_changed([1, 2], [u'1', u'3']) 939 True 940 874 941 # Unicode choices are correctly rendered as HTML 875 942 >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) … … 896 963 u'<input id="bar_0" type="text" class="big" value="john" name="name_0" /><br /><input id="bar_1" type="text" class="small" value="lennon" name="name_1" />' 897 964 965 >>> w = MyMultiWidget(widgets=(TextInput(), TextInput())) 966 967 # test with no initial data 968 >>> w._has_changed(None, [u'john', u'lennon']) 969 True 970 971 # test when the data is the same as initial 972 >>> w._has_changed(u'john__lennon', [u'john', u'lennon']) 973 False 974 975 # test when the first widget's data has changed 976 >>> w._has_changed(u'john__lennon', [u'alfred', u'lennon']) 977 True 978 979 # test when the last widget's data has changed. this ensures that it is not 980 # short circuiting while testing the widgets. 981 >>> w._has_changed(u'john__lennon', [u'john', u'denver']) 982 True 983 898 984 # SplitDateTimeWidget ######################################################### 899 985 … … 914 1000 u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />' 915 1001 1002 >>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:40:00']) 1003 False 1004 >>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:41:00']) 1005 True 1006 916 1007 # DateTimeInput ############################################################### 917 1008
