From c81b01e060ad80a5c4df579a96ff737df2d336b3 Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Fri, 30 Nov 2007 06:23:24 +0000 Subject: newforms-admin: Merged from trunk up to [6775]. git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@6777 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/forms/error_messages.py | 45 ++++++++++++++++++++ tests/regressiontests/forms/fields.py | 14 ++++++ tests/regressiontests/forms/localflavor/ca.py | 4 ++ tests/regressiontests/forms/models.py | 4 ++ tests/regressiontests/forms/widgets.py | 61 +++++++++++++++++++++++++++ 5 files changed, 128 insertions(+) (limited to 'tests/regressiontests/forms') diff --git a/tests/regressiontests/forms/error_messages.py b/tests/regressiontests/forms/error_messages.py index ff7e110f6f..9f972f5b90 100644 --- a/tests/regressiontests/forms/error_messages.py +++ b/tests/regressiontests/forms/error_messages.py @@ -312,4 +312,49 @@ ValidationError: [u'REQUIRED'] Traceback (most recent call last): ... ValidationError: [u'INVALID IP ADDRESS'] + +############################################################################### + +# Create choices for the model choice field tests below. + +>>> from regressiontests.forms.models import ChoiceModel +>>> ChoiceModel.objects.create(pk=1, name='a') + +>>> ChoiceModel.objects.create(pk=2, name='b') + +>>> ChoiceModel.objects.create(pk=3, name='c') + + +# ModelChoiceField ############################################################ + +>>> e = {'required': 'REQUIRED'} +>>> e['invalid_choice'] = 'INVALID CHOICE' +>>> f = ModelChoiceField(queryset=ChoiceModel.objects.all(), error_messages=e) +>>> f.clean('') +Traceback (most recent call last): +... +ValidationError: [u'REQUIRED'] +>>> f.clean('4') +Traceback (most recent call last): +... +ValidationError: [u'INVALID CHOICE'] + +# ModelMultipleChoiceField #################################################### + +>>> e = {'required': 'REQUIRED'} +>>> e['invalid_choice'] = '%s IS INVALID CHOICE' +>>> e['list'] = 'NOT A LIST OF VALUES' +>>> f = ModelMultipleChoiceField(queryset=ChoiceModel.objects.all(), error_messages=e) +>>> f.clean('') +Traceback (most recent call last): +... +ValidationError: [u'REQUIRED'] +>>> f.clean('3') +Traceback (most recent call last): +... +ValidationError: [u'NOT A LIST OF VALUES'] +>>> f.clean(['4']) +Traceback (most recent call last): +... +ValidationError: [u'4 IS INVALID CHOICE'] """ diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py index 3b93d70338..cff5db6fca 100644 --- a/tests/regressiontests/forms/fields.py +++ b/tests/regressiontests/forms/fields.py @@ -323,6 +323,10 @@ Decimal("3.14") Traceback (most recent call last): ... ValidationError: [u'Enter a number.'] +>>> f.clean(u'łąść') +Traceback (most recent call last): +... +ValidationError: [u'Enter a number.'] >>> f.clean('1.0 ') Decimal("1.0") >>> f.clean(' 1.0') @@ -914,6 +918,11 @@ False >>> f.clean('Django rocks') True +>>> f.clean('True') +True +>>> f.clean('False') +False + >>> f = BooleanField(required=False) >>> f.clean('') False @@ -930,6 +939,11 @@ False >>> f.clean('Django rocks') True +A form's BooleanField with a hidden widget will output the string 'False', so +that should clean to the boolean value False: +>>> f.clean('False') +False + # ChoiceField ################################################################# >>> f = ChoiceField(choices=[('1', '1'), ('2', '2')]) diff --git a/tests/regressiontests/forms/localflavor/ca.py b/tests/regressiontests/forms/localflavor/ca.py index baeb2ad9a8..a13a6de65f 100644 --- a/tests/regressiontests/forms/localflavor/ca.py +++ b/tests/regressiontests/forms/localflavor/ca.py @@ -147,6 +147,10 @@ u'BC' u'NS' >>> f.clean(' manitoba ') u'MB' +>>> f.clean(' new brunswick ') +u'NB' +>>> f.clean('NB') +u'NB' >>> f.clean('T2S 2H7') Traceback (most recent call last): ... diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py index 1a6f566b6b..c7ce128560 100644 --- a/tests/regressiontests/forms/models.py +++ b/tests/regressiontests/forms/models.py @@ -10,6 +10,10 @@ class Defaults(models.Model): def_date = models.DateField(default = datetime.date(1980, 1, 1)) value = models.IntegerField(default=42) +class ChoiceModel(models.Model): + """For ModelChoiceField and ModelMultipleChoiceField tests.""" + name = models.CharField(max_length=10) + __test__ = {'API_TESTS': """ >>> from django.newforms import form_for_model, form_for_instance diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py index cb1d084631..0e69602103 100644 --- a/tests/regressiontests/forms/widgets.py +++ b/tests/regressiontests/forms/widgets.py @@ -2,6 +2,7 @@ tests = r""" >>> from django.newforms import * >>> from django.newforms.widgets import RadioFieldRenderer +>>> from django.utils.safestring import mark_safe >>> import datetime >>> import time >>> import re @@ -128,6 +129,13 @@ u'' +Boolean values are rendered to their string forms ("True" and "False"). +>>> w = HiddenInput() +>>> w.render('get_spam', False) +u'' +>>> w.render('get_spam', True) +u'' + # MultipleHiddenInput Widget ################################################## >>> w = MultipleHiddenInput() @@ -205,6 +213,8 @@ u'' u'' >>> w.render('msg', 'some "quoted" & ampersanded value') u'' +>>> w.render('msg', mark_safe('pre "quoted" value')) +u'' >>> w.render('msg', 'value', attrs={'class': 'pretty', 'rows': 20}) u'' @@ -375,6 +385,17 @@ If 'choices' is passed to both the constructor and render(), then they'll both b +# Choices are escaped correctly +>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))) + + +# Unicode choices are correctly rendered as HTML >>> w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) u'' @@ -538,6 +559,17 @@ If 'choices' is passed to both the constructor and render(), then they'll both b +# Choices are escaped correctly +>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))) + + +# Unicode choices are correctly rendered as HTML >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) u'' @@ -663,6 +695,16 @@ You can create your own custom renderers for RadioSelect to use.
+Or you can use custom RadioSelect fields that use your custom renderer. +>>> class CustomRadioSelect(RadioSelect): +... renderer = MyRenderer +>>> w = CustomRadioSelect() +>>> print w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) +
+
+
+ + A RadioFieldRenderer object also allows index access to individual RadioInput objects. >>> w = RadioSelect() @@ -682,6 +724,14 @@ Traceback (most recent call last): ... IndexError: list index out of range +# Choices are escaped correctly +>>> w = RadioSelect() +>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))) +
    +
  • +
  • +
+ # Unicode choices are correctly rendered as HTML >>> w = RadioSelect() >>> unicode(w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])) @@ -811,6 +861,17 @@ If 'choices' is passed to both the constructor and render(), then they'll both b
  • +# Choices are escaped correctly +>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))) +
      +
    • +
    • +
    • +
    • +
    • +
    + +# Unicode choices are correctly rendered as HTML >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) u'
      \n
    • \n
    • \n
    • \n
    • \n
    • \n
    ' -- cgit v1.3