diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py index 9d407d9ea7..c9736d38e1 100644 --- a/tests/regressiontests/forms/fields.py +++ b/tests/regressiontests/forms/fields.py @@ -1,17 +1,5 @@ # -*- coding: utf-8 -*- -tests = r""" ->>> from django.forms import * ->>> from django.forms.widgets import RadioFieldRenderer ->>> from django.core.files.uploadedfile import SimpleUploadedFile ->>> import datetime ->>> import time ->>> import re ->>> try: -... from decimal import Decimal -... except ImportError: -... from django.utils._decimal import Decimal - - +""" ########## # Fields # ########## @@ -35,1480 +23,825 @@ Each Field's __init__() takes at least these parameters: Other than that, the Field subclasses have class-specific options for __init__(). For example, CharField has a max_length option. - -# CharField ################################################################### - ->>> f = CharField() ->>> f.clean(1) -u'1' ->>> f.clean('hello') -u'hello' ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean([1, 2, 3]) -u'[1, 2, 3]' - ->>> f = CharField(required=False) ->>> f.clean(1) -u'1' ->>> f.clean('hello') -u'hello' ->>> f.clean(None) -u'' ->>> f.clean('') -u'' ->>> f.clean([1, 2, 3]) -u'[1, 2, 3]' - -CharField accepts an optional max_length parameter: ->>> f = CharField(max_length=10, required=False) ->>> f.clean('12345') -u'12345' ->>> f.clean('1234567890') -u'1234567890' ->>> f.clean('1234567890a') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at most 10 characters (it has 11).'] - -CharField accepts an optional min_length parameter: ->>> f = CharField(min_length=10, required=False) ->>> f.clean('') -u'' ->>> f.clean('12345') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at least 10 characters (it has 5).'] ->>> f.clean('1234567890') -u'1234567890' ->>> f.clean('1234567890a') -u'1234567890a' - ->>> f = CharField(min_length=10, required=True) ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean('12345') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at least 10 characters (it has 5).'] ->>> f.clean('1234567890') -u'1234567890' ->>> f.clean('1234567890a') -u'1234567890a' - -# IntegerField ################################################################ - ->>> f = IntegerField() ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean('1') -1 ->>> isinstance(f.clean('1'), int) -True ->>> f.clean('23') -23 ->>> f.clean('a') -Traceback (most recent call last): -... -ValidationError: [u'Enter a whole number.'] ->>> f.clean(42) -42 ->>> f.clean(3.14) -Traceback (most recent call last): -... -ValidationError: [u'Enter a whole number.'] ->>> f.clean('1 ') -1 ->>> f.clean(' 1') -1 ->>> f.clean(' 1 ') -1 ->>> f.clean('1a') -Traceback (most recent call last): -... -ValidationError: [u'Enter a whole number.'] - ->>> f = IntegerField(required=False) ->>> f.clean('') ->>> repr(f.clean('')) -'None' ->>> f.clean(None) ->>> repr(f.clean(None)) -'None' ->>> f.clean('1') -1 ->>> isinstance(f.clean('1'), int) -True ->>> f.clean('23') -23 ->>> f.clean('a') -Traceback (most recent call last): -... -ValidationError: [u'Enter a whole number.'] ->>> f.clean('1 ') -1 ->>> f.clean(' 1') -1 ->>> f.clean(' 1 ') -1 ->>> f.clean('1a') -Traceback (most recent call last): -... -ValidationError: [u'Enter a whole number.'] - -IntegerField accepts an optional max_value parameter: ->>> f = IntegerField(max_value=10) ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(1) -1 ->>> f.clean(10) -10 ->>> f.clean(11) -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value is less than or equal to 10.'] ->>> f.clean('10') -10 ->>> f.clean('11') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value is less than or equal to 10.'] - -IntegerField accepts an optional min_value parameter: ->>> f = IntegerField(min_value=10) ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(1) -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value is greater than or equal to 10.'] ->>> f.clean(10) -10 ->>> f.clean(11) -11 ->>> f.clean('10') -10 ->>> f.clean('11') -11 - -min_value and max_value can be used together: ->>> f = IntegerField(min_value=10, max_value=20) ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(1) -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value is greater than or equal to 10.'] ->>> f.clean(10) -10 ->>> f.clean(11) -11 ->>> f.clean('10') -10 ->>> f.clean('11') -11 ->>> f.clean(20) -20 ->>> f.clean(21) -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value is less than or equal to 20.'] - -# FloatField ################################################################## - ->>> f = FloatField() ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean('1') -1.0 ->>> isinstance(f.clean('1'), float) -True ->>> f.clean('23') -23.0 ->>> f.clean('3.14') -3.1400000000000001 ->>> f.clean(3.14) -3.1400000000000001 ->>> f.clean(42) -42.0 ->>> f.clean('a') -Traceback (most recent call last): -... -ValidationError: [u'Enter a number.'] ->>> f.clean('1.0 ') -1.0 ->>> f.clean(' 1.0') -1.0 ->>> f.clean(' 1.0 ') -1.0 ->>> f.clean('1.0a') -Traceback (most recent call last): -... -ValidationError: [u'Enter a number.'] - ->>> f = FloatField(required=False) ->>> f.clean('') - ->>> f.clean(None) - ->>> f.clean('1') -1.0 - -FloatField accepts min_value and max_value just like IntegerField: ->>> f = FloatField(max_value=1.5, min_value=0.5) - ->>> f.clean('1.6') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value is less than or equal to 1.5.'] ->>> f.clean('0.4') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value is greater than or equal to 0.5.'] ->>> f.clean('1.5') -1.5 ->>> f.clean('0.5') -0.5 - -# DecimalField ################################################################ - ->>> f = DecimalField(max_digits=4, decimal_places=2) ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean('1') == Decimal("1") -True ->>> isinstance(f.clean('1'), Decimal) -True ->>> f.clean('23') == Decimal("23") -True ->>> f.clean('3.14') == Decimal("3.14") -True ->>> f.clean(3.14) == Decimal("3.14") -True ->>> f.clean(Decimal('3.14')) == Decimal("3.14") -True ->>> f.clean('a') -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") -True ->>> f.clean(' 1.0') == Decimal("1.0") -True ->>> f.clean(' 1.0 ') == Decimal("1.0") -True ->>> f.clean('1.0a') -Traceback (most recent call last): -... -ValidationError: [u'Enter a number.'] ->>> f.clean('123.45') -Traceback (most recent call last): -... -ValidationError: [u'Ensure that there are no more than 4 digits in total.'] ->>> f.clean('1.234') -Traceback (most recent call last): -... -ValidationError: [u'Ensure that there are no more than 2 decimal places.'] ->>> f.clean('123.4') -Traceback (most recent call last): -... -ValidationError: [u'Ensure that there are no more than 2 digits before the decimal point.'] ->>> f.clean('-12.34') == Decimal("-12.34") -True ->>> f.clean('-123.45') -Traceback (most recent call last): -... -ValidationError: [u'Ensure that there are no more than 4 digits in total.'] ->>> f.clean('-.12') == Decimal("-0.12") -True ->>> f.clean('-00.12') == Decimal("-0.12") -True ->>> f.clean('-000.12') == Decimal("-0.12") -True ->>> f.clean('-000.123') -Traceback (most recent call last): -... -ValidationError: [u'Ensure that there are no more than 2 decimal places.'] ->>> f.clean('-000.12345') -Traceback (most recent call last): -... -ValidationError: [u'Ensure that there are no more than 4 digits in total.'] ->>> f.clean('--0.12') -Traceback (most recent call last): -... -ValidationError: [u'Enter a number.'] - ->>> f = DecimalField(max_digits=4, decimal_places=2, required=False) ->>> f.clean('') - ->>> f.clean(None) - ->>> f.clean('1') == Decimal("1") -True - -DecimalField accepts min_value and max_value just like IntegerField: ->>> f = DecimalField(max_digits=4, decimal_places=2, max_value=Decimal('1.5'), min_value=Decimal('0.5')) - ->>> f.clean('1.6') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value is less than or equal to 1.5.'] ->>> f.clean('0.4') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value is greater than or equal to 0.5.'] ->>> f.clean('1.5') == Decimal("1.5") -True ->>> f.clean('0.5') == Decimal("0.5") -True ->>> f.clean('.5') == Decimal("0.5") -True ->>> f.clean('00.50') == Decimal("0.50") -True - - ->>> f = DecimalField(decimal_places=2) ->>> f.clean('0.00000001') -Traceback (most recent call last): -... -ValidationError: [u'Ensure that there are no more than 2 decimal places.'] - - ->>> f = DecimalField(max_digits=3) - -# Leading whole zeros "collapse" to one digit. ->>> f.clean('0000000.10') == Decimal("0.1") -True - -# But a leading 0 before the . doesn't count towards max_digits ->>> f.clean('0000000.100') == Decimal("0.100") -True - -# Only leading whole zeros "collapse" to one digit. ->>> f.clean('000000.02') == Decimal('0.02') -True ->>> f.clean('000000.0002') -Traceback (most recent call last): -... -ValidationError: [u'Ensure that there are no more than 3 digits in total.'] ->>> f.clean('.002') == Decimal("0.002") -True - ->>> f = DecimalField(max_digits=2, decimal_places=2) ->>> f.clean('.01') == Decimal(".01") -True ->>> f.clean('1.1') -Traceback (most recent call last): -... -ValidationError: [u'Ensure that there are no more than 0 digits before the decimal point.'] - - -# DateField ################################################################### - ->>> import datetime ->>> f = DateField() ->>> f.clean(datetime.date(2006, 10, 25)) -datetime.date(2006, 10, 25) ->>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) -datetime.date(2006, 10, 25) ->>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) -datetime.date(2006, 10, 25) ->>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) -datetime.date(2006, 10, 25) ->>> f.clean('2006-10-25') -datetime.date(2006, 10, 25) ->>> f.clean('10/25/2006') -datetime.date(2006, 10, 25) ->>> f.clean('10/25/06') -datetime.date(2006, 10, 25) ->>> f.clean('Oct 25 2006') -datetime.date(2006, 10, 25) ->>> f.clean('October 25 2006') -datetime.date(2006, 10, 25) ->>> f.clean('October 25, 2006') -datetime.date(2006, 10, 25) ->>> f.clean('25 October 2006') -datetime.date(2006, 10, 25) ->>> f.clean('25 October, 2006') -datetime.date(2006, 10, 25) ->>> f.clean('2006-4-31') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date.'] ->>> f.clean('200a-10-25') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date.'] ->>> f.clean('25/10/06') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date.'] ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] - ->>> f = DateField(required=False) ->>> f.clean(None) ->>> repr(f.clean(None)) -'None' ->>> f.clean('') ->>> repr(f.clean('')) -'None' - -DateField accepts an optional input_formats parameter: ->>> f = DateField(input_formats=['%Y %m %d']) ->>> f.clean(datetime.date(2006, 10, 25)) -datetime.date(2006, 10, 25) ->>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) -datetime.date(2006, 10, 25) ->>> f.clean('2006 10 25') -datetime.date(2006, 10, 25) - -The input_formats parameter overrides all default input formats, -so the default formats won't work unless you specify them: ->>> f.clean('2006-10-25') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date.'] ->>> f.clean('10/25/2006') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date.'] ->>> f.clean('10/25/06') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date.'] - -# TimeField ################################################################### - ->>> import datetime ->>> f = TimeField() ->>> f.clean(datetime.time(14, 25)) -datetime.time(14, 25) ->>> f.clean(datetime.time(14, 25, 59)) -datetime.time(14, 25, 59) ->>> f.clean('14:25') -datetime.time(14, 25) ->>> f.clean('14:25:59') -datetime.time(14, 25, 59) ->>> f.clean('hello') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid time.'] ->>> f.clean('1:24 p.m.') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid time.'] - -TimeField accepts an optional input_formats parameter: ->>> f = TimeField(input_formats=['%I:%M %p']) ->>> f.clean(datetime.time(14, 25)) -datetime.time(14, 25) ->>> f.clean(datetime.time(14, 25, 59)) -datetime.time(14, 25, 59) ->>> f.clean('4:25 AM') -datetime.time(4, 25) ->>> f.clean('4:25 PM') -datetime.time(16, 25) - -The input_formats parameter overrides all default input formats, -so the default formats won't work unless you specify them: ->>> f.clean('14:30:45') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid time.'] - -# DateTimeField ############################################################### - ->>> import datetime ->>> f = DateTimeField() ->>> f.clean(datetime.date(2006, 10, 25)) -datetime.datetime(2006, 10, 25, 0, 0) ->>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) -datetime.datetime(2006, 10, 25, 14, 30) ->>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) -datetime.datetime(2006, 10, 25, 14, 30, 59) ->>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) -datetime.datetime(2006, 10, 25, 14, 30, 59, 200) ->>> f.clean('2006-10-25 14:30:45') -datetime.datetime(2006, 10, 25, 14, 30, 45) ->>> f.clean('2006-10-25 14:30:00') -datetime.datetime(2006, 10, 25, 14, 30) ->>> f.clean('2006-10-25 14:30') -datetime.datetime(2006, 10, 25, 14, 30) ->>> f.clean('2006-10-25') -datetime.datetime(2006, 10, 25, 0, 0) ->>> f.clean('10/25/2006 14:30:45') -datetime.datetime(2006, 10, 25, 14, 30, 45) ->>> f.clean('10/25/2006 14:30:00') -datetime.datetime(2006, 10, 25, 14, 30) ->>> f.clean('10/25/2006 14:30') -datetime.datetime(2006, 10, 25, 14, 30) ->>> f.clean('10/25/2006') -datetime.datetime(2006, 10, 25, 0, 0) ->>> f.clean('10/25/06 14:30:45') -datetime.datetime(2006, 10, 25, 14, 30, 45) ->>> f.clean('10/25/06 14:30:00') -datetime.datetime(2006, 10, 25, 14, 30) ->>> f.clean('10/25/06 14:30') -datetime.datetime(2006, 10, 25, 14, 30) ->>> f.clean('10/25/06') -datetime.datetime(2006, 10, 25, 0, 0) ->>> f.clean('hello') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date/time.'] ->>> f.clean('2006-10-25 4:30 p.m.') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date/time.'] - -DateField accepts an optional input_formats parameter: ->>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p']) ->>> f.clean(datetime.date(2006, 10, 25)) -datetime.datetime(2006, 10, 25, 0, 0) ->>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) -datetime.datetime(2006, 10, 25, 14, 30) ->>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) -datetime.datetime(2006, 10, 25, 14, 30, 59) ->>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) -datetime.datetime(2006, 10, 25, 14, 30, 59, 200) ->>> f.clean('2006 10 25 2:30 PM') -datetime.datetime(2006, 10, 25, 14, 30) - -The input_formats parameter overrides all default input formats, -so the default formats won't work unless you specify them: ->>> f.clean('2006-10-25 14:30:45') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date/time.'] - ->>> f = DateTimeField(required=False) ->>> f.clean(None) ->>> repr(f.clean(None)) -'None' ->>> f.clean('') ->>> repr(f.clean('')) -'None' - -# RegexField ################################################################## - ->>> f = RegexField('^\d[A-F]\d$') ->>> f.clean('2A2') -u'2A2' ->>> f.clean('3F3') -u'3F3' ->>> f.clean('3G3') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid value.'] ->>> f.clean(' 2A2') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid value.'] ->>> f.clean('2A2 ') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid value.'] ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] - ->>> f = RegexField('^\d[A-F]\d$', required=False) ->>> f.clean('2A2') -u'2A2' ->>> f.clean('3F3') -u'3F3' ->>> f.clean('3G3') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid value.'] ->>> f.clean('') -u'' - -Alternatively, RegexField can take a compiled regular expression: ->>> f = RegexField(re.compile('^\d[A-F]\d$')) ->>> f.clean('2A2') -u'2A2' ->>> f.clean('3F3') -u'3F3' ->>> f.clean('3G3') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid value.'] ->>> f.clean(' 2A2') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid value.'] ->>> f.clean('2A2 ') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid value.'] - -RegexField takes an optional error_message argument: ->>> f = RegexField('^\d\d\d\d$', error_message='Enter a four-digit number.') ->>> f.clean('1234') -u'1234' ->>> f.clean('123') -Traceback (most recent call last): -... -ValidationError: [u'Enter a four-digit number.'] ->>> f.clean('abcd') -Traceback (most recent call last): -... -ValidationError: [u'Enter a four-digit number.'] - -RegexField also access min_length and max_length parameters, for convenience. ->>> f = RegexField('^\d+$', min_length=5, max_length=10) ->>> f.clean('123') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at least 5 characters (it has 3).'] ->>> f.clean('abc') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at least 5 characters (it has 3).'] ->>> f.clean('12345') -u'12345' ->>> f.clean('1234567890') -u'1234567890' ->>> f.clean('12345678901') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at most 10 characters (it has 11).'] ->>> f.clean('12345a') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid value.'] - -# EmailField ################################################################## - ->>> f = EmailField() ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean('person@example.com') -u'person@example.com' ->>> f.clean('foo') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] ->>> f.clean('foo@') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] ->>> f.clean('foo@bar') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] ->>> f.clean('example@invalid-.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] ->>> f.clean('example@-invalid.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] ->>> f.clean('example@inv-.alid-.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] ->>> f.clean('example@inv-.-alid.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] ->>> f.clean('example@valid-----hyphens.com') -u'example@valid-----hyphens.com' - ->>> f.clean('example@valid-with-hyphens.com') -u'example@valid-with-hyphens.com' - -# Check for runaway regex security problem. This will take for-freeking-ever -# if the security fix isn't in place. ->>> f.clean('viewx3dtextx26qx3d@yahoo.comx26latlngx3d15854521645943074058') -Traceback (most recent call last): - ... -ValidationError: [u'Enter a valid e-mail address.'] - ->>> f = EmailField(required=False) ->>> f.clean('') -u'' ->>> f.clean(None) -u'' ->>> f.clean('person@example.com') -u'person@example.com' ->>> f.clean('foo') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] ->>> f.clean('foo@') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] ->>> f.clean('foo@bar') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] - -EmailField also access min_length and max_length parameters, for convenience. ->>> f = EmailField(min_length=10, max_length=15) ->>> f.clean('a@foo.com') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at least 10 characters (it has 9).'] ->>> f.clean('alf@foo.com') -u'alf@foo.com' ->>> f.clean('alf123456788@foo.com') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at most 15 characters (it has 20).'] - -# FileField ################################################################## - ->>> f = FileField() ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] - ->>> f.clean('', '') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] - ->>> f.clean('', 'files/test1.pdf') -'files/test1.pdf' - ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] - ->>> f.clean(None, '') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] - ->>> f.clean(None, 'files/test2.pdf') -'files/test2.pdf' - ->>> f.clean(SimpleUploadedFile('', '')) -Traceback (most recent call last): -... -ValidationError: [u'No file was submitted. Check the encoding type on the form.'] - ->>> f.clean(SimpleUploadedFile('', ''), '') -Traceback (most recent call last): -... -ValidationError: [u'No file was submitted. Check the encoding type on the form.'] - ->>> f.clean(None, 'files/test3.pdf') -'files/test3.pdf' - ->>> f.clean('some content that is not a file') -Traceback (most recent call last): -... -ValidationError: [u'No file was submitted. Check the encoding type on the form.'] - ->>> f.clean(SimpleUploadedFile('name', None)) -Traceback (most recent call last): -... -ValidationError: [u'The submitted file is empty.'] - ->>> f.clean(SimpleUploadedFile('name', '')) -Traceback (most recent call last): -... -ValidationError: [u'The submitted file is empty.'] - ->>> type(f.clean(SimpleUploadedFile('name', 'Some File Content'))) - - ->>> type(f.clean(SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह'))) - - ->>> type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf')) - - ->>> f = FileField(max_length = 5) ->>> f.clean(SimpleUploadedFile('test_maxlength.txt', 'hello world')) -Traceback (most recent call last): -... -ValidationError: [u'Ensure this filename has at most 5 characters (it has 18).'] - ->>> f.clean('', 'files/test1.pdf') -'files/test1.pdf' - ->>> f.clean(None, 'files/test2.pdf') -'files/test2.pdf' - ->>> type(f.clean(SimpleUploadedFile('name', 'Some File Content'))) - - -# URLField ################################################################## - ->>> f = URLField() ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean('http://localhost') -u'http://localhost/' ->>> f.clean('http://example.com') -u'http://example.com/' ->>> f.clean('http://www.example.com') -u'http://www.example.com/' ->>> f.clean('http://www.example.com:8000/test') -u'http://www.example.com:8000/test' ->>> f.clean('valid-with-hyphens.com') -u'http://valid-with-hyphens.com/' ->>> f.clean('subdomain.domain.com') -u'http://subdomain.domain.com/' ->>> f.clean('http://200.8.9.10') -u'http://200.8.9.10/' ->>> f.clean('http://200.8.9.10:8000/test') -u'http://200.8.9.10:8000/test' ->>> f.clean('foo') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://example') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://example.') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://invalid-.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://-invalid.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://inv-.alid-.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://inv-.-alid.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://valid-----hyphens.com') -u'http://valid-----hyphens.com/' - ->>> f = URLField(required=False) ->>> f.clean('') -u'' ->>> f.clean(None) -u'' ->>> f.clean('http://example.com') -u'http://example.com/' ->>> f.clean('http://www.example.com') -u'http://www.example.com/' ->>> f.clean('foo') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://example') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://example.') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('.') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('com.') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://example.com.') -u'http://example.com./' ->>> f.clean('example.com.') -u'http://example.com./' - -# hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed ->>> f.clean('http://%s' % ("X"*200,)) -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] - -# a second test, to make sure the problem is really addressed, even on -# domains that don't fail the domain label length check in the regex ->>> f.clean('http://%s' % ("X"*60,)) -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] - ->>> f.clean('http://.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] - -URLField takes an optional verify_exists parameter, which is False by default. -This verifies that the URL is live on the Internet and doesn't return a 404 or 500: ->>> f = URLField(verify_exists=True) ->>> f.clean('http://www.google.com') # This will fail if there's no Internet connection -u'http://www.google.com/' ->>> f.clean('http://example') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] ->>> f.clean('http://www.broken.djangoproject.com') # bad domain -Traceback (most recent call last): -... -ValidationError: [u'This URL appears to be a broken link.'] ->>> f.clean('http://google.com/we-love-microsoft.html') # good domain, bad page -Traceback (most recent call last): -... -ValidationError: [u'This URL appears to be a broken link.'] ->>> f = URLField(verify_exists=True, required=False) ->>> f.clean('') -u'' ->>> f.clean('http://www.google.com') # This will fail if there's no Internet connection -u'http://www.google.com/' - -URLField also access min_length and max_length parameters, for convenience. ->>> f = URLField(min_length=15, max_length=20) ->>> f.clean('http://f.com') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at least 15 characters (it has 13).'] ->>> f.clean('http://example.com') -u'http://example.com/' ->>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at most 20 characters (it has 38).'] - -URLField should prepend 'http://' if no scheme was given ->>> f = URLField(required=False) ->>> f.clean('example.com') -u'http://example.com/' ->>> f.clean('') -u'' ->>> f.clean('https://example.com') -u'https://example.com/' - -URLField should append '/' if no path was given ->>> f = URLField() ->>> f.clean('http://example.com') -u'http://example.com/' - -URLField shouldn't change the path if it was given ->>> f.clean('http://example.com/test') -u'http://example.com/test' - -# BooleanField ################################################################ - ->>> f = BooleanField() ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(True) -True ->>> f.clean(False) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(1) -True ->>> f.clean(0) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean('Django rocks') -True - ->>> f.clean('True') -True ->>> f.clean('False') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] - ->>> f = BooleanField(required=False) ->>> f.clean('') -False ->>> f.clean(None) -False ->>> f.clean(True) -True ->>> f.clean(False) -False ->>> f.clean(1) -True ->>> f.clean(0) -False ->>> f.clean('1') -True ->>> f.clean('0') -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', 'One'), ('2', 'Two')]) ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(1) -u'1' ->>> f.clean('1') -u'1' ->>> f.clean('3') -Traceback (most recent call last): -... -ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] - ->>> f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) ->>> f.clean('') -u'' ->>> f.clean(None) -u'' ->>> f.clean(1) -u'1' ->>> f.clean('1') -u'1' ->>> f.clean('3') -Traceback (most recent call last): -... -ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] - ->>> f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')]) ->>> f.clean('J') -u'J' ->>> f.clean('John') -Traceback (most recent call last): -... -ValidationError: [u'Select a valid choice. John is not one of the available choices.'] - ->>> f = ChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) ->>> f.clean(1) -u'1' ->>> f.clean('1') -u'1' ->>> f.clean(3) -u'3' ->>> f.clean('3') -u'3' ->>> f.clean(5) -u'5' ->>> f.clean('5') -u'5' ->>> f.clean('6') -Traceback (most recent call last): -... -ValidationError: [u'Select a valid choice. 6 is not one of the available choices.'] - -# TypedChoiceField ############################################################ - -# TypedChoiceField is just like ChoiceField, except that coerced types will -# be returned: ->>> f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int) ->>> f.clean('1') -1 ->>> f.clean('2') -Traceback (most recent call last): -... -ValidationError: [u'Select a valid choice. 2 is not one of the available choices.'] - -# Different coercion, same validation. ->>> f.coerce = float ->>> f.clean('1') -1.0 - - -# This can also cause weirdness: be careful (bool(-1) == True, remember) ->>> f.coerce = bool ->>> f.clean('-1') -True - -# Even more weirdness: if you have a valid choice but your coercion function -# can't coerce, you'll still get a validation error. Don't do this! ->>> f = TypedChoiceField(choices=[('A', 'A'), ('B', 'B')], coerce=int) ->>> f.clean('B') -Traceback (most recent call last): -... -ValidationError: [u'Select a valid choice. B is not one of the available choices.'] - -# Required fields require values ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] - -# Non-required fields aren't required ->>> f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False) ->>> f.clean('') -'' - -# If you want cleaning an empty value to return a different type, tell the field ->>> f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None) ->>> print f.clean('') -None - -# NullBooleanField ############################################################ - ->>> f = NullBooleanField() ->>> f.clean('') ->>> f.clean(True) -True ->>> f.clean(False) -False ->>> f.clean(None) ->>> f.clean('0') -False ->>> f.clean('1') -True ->>> f.clean('2') ->>> f.clean('3') ->>> f.clean('hello') - -# Make sure that the internal value is preserved if using HiddenInput (#7753) ->>> class HiddenNullBooleanForm(Form): -... hidden_nullbool1 = NullBooleanField(widget=HiddenInput, initial=True) -... hidden_nullbool2 = NullBooleanField(widget=HiddenInput, initial=False) ->>> f = HiddenNullBooleanForm() ->>> print f - ->>> f = HiddenNullBooleanForm({ 'hidden_nullbool1': 'True', 'hidden_nullbool2': 'False' }) ->>> f.full_clean() ->>> f.cleaned_data['hidden_nullbool1'] -True ->>> f.cleaned_data['hidden_nullbool2'] -False - -# Make sure we're compatible with MySQL, which uses 0 and 1 for its boolean -# values. (#9609) ->>> NULLBOOL_CHOICES = (('1', 'Yes'), ('0', 'No'), ('', 'Unknown')) ->>> class MySQLNullBooleanForm(Form): -... nullbool0 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES)) -... nullbool1 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES)) -... nullbool2 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES)) ->>> f = MySQLNullBooleanForm({ 'nullbool0': '1', 'nullbool1': '0', 'nullbool2': '' }) ->>> f.full_clean() ->>> f.cleaned_data['nullbool0'] -True ->>> f.cleaned_data['nullbool1'] -False ->>> f.cleaned_data['nullbool2'] - -# MultipleChoiceField ######################################################### - ->>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')]) ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean([1]) -[u'1'] ->>> f.clean(['1']) -[u'1'] ->>> f.clean(['1', '2']) -[u'1', u'2'] ->>> f.clean([1, '2']) -[u'1', u'2'] ->>> f.clean((1, '2')) -[u'1', u'2'] ->>> f.clean('hello') -Traceback (most recent call last): -... -ValidationError: [u'Enter a list of values.'] ->>> f.clean([]) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(()) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(['3']) -Traceback (most recent call last): -... -ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] - ->>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) ->>> f.clean('') -[] ->>> f.clean(None) -[] ->>> f.clean([1]) -[u'1'] ->>> f.clean(['1']) -[u'1'] ->>> f.clean(['1', '2']) -[u'1', u'2'] ->>> f.clean([1, '2']) -[u'1', u'2'] ->>> f.clean((1, '2')) -[u'1', u'2'] ->>> f.clean('hello') -Traceback (most recent call last): -... -ValidationError: [u'Enter a list of values.'] ->>> f.clean([]) -[] ->>> f.clean(()) -[] ->>> f.clean(['3']) -Traceback (most recent call last): -... -ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] - ->>> f = MultipleChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) ->>> f.clean([1]) -[u'1'] ->>> f.clean(['1']) -[u'1'] ->>> f.clean([1, 5]) -[u'1', u'5'] ->>> f.clean([1, '5']) -[u'1', u'5'] ->>> f.clean(['1', 5]) -[u'1', u'5'] ->>> f.clean(['1', '5']) -[u'1', u'5'] ->>> f.clean(['6']) -Traceback (most recent call last): -... -ValidationError: [u'Select a valid choice. 6 is not one of the available choices.'] ->>> f.clean(['1','6']) -Traceback (most recent call last): -... -ValidationError: [u'Select a valid choice. 6 is not one of the available choices.'] - - -# ComboField ################################################################## - -ComboField takes a list of fields that should be used to validate a value, -in that order. ->>> f = ComboField(fields=[CharField(max_length=20), EmailField()]) ->>> f.clean('test@example.com') -u'test@example.com' ->>> f.clean('longemailaddress@example.com') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at most 20 characters (it has 28).'] ->>> f.clean('not an e-mail') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] - ->>> f = ComboField(fields=[CharField(max_length=20), EmailField()], required=False) ->>> f.clean('test@example.com') -u'test@example.com' ->>> f.clean('longemailaddress@example.com') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at most 20 characters (it has 28).'] ->>> f.clean('not an e-mail') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid e-mail address.'] ->>> f.clean('') -u'' ->>> f.clean(None) -u'' - -# FilePathField ############################################################### - ->>> def fix_os_paths(x): -... if isinstance(x, basestring): -... return x.replace('\\', '/') -... elif isinstance(x, tuple): -... return tuple(fix_os_paths(list(x))) -... elif isinstance(x, list): -... return [fix_os_paths(y) for y in x] -... else: -... return x -... ->>> import os ->>> from django import forms ->>> path = forms.__file__ ->>> path = os.path.dirname(path) + '/' ->>> fix_os_paths(path) -'.../django/forms/' ->>> f = forms.FilePathField(path=path) ->>> f.choices = [p for p in f.choices if p[0].endswith('.py')] ->>> f.choices.sort() ->>> fix_os_paths(f.choices) -[('.../django/forms/__init__.py', '__init__.py'), ('.../django/forms/fields.py', 'fields.py'), ('.../django/forms/forms.py', 'forms.py'), ('.../django/forms/models.py', 'models.py'), ('.../django/forms/util.py', 'util.py'), ('.../django/forms/widgets.py', 'widgets.py')] ->>> f.clean('fields.py') -Traceback (most recent call last): -... -ValidationError: [u'Select a valid choice. fields.py is not one of the available choices.'] ->>> fix_os_paths(f.clean(path + 'fields.py')) -u'.../django/forms/fields.py' ->>> f = forms.FilePathField(path=path, match='^.*?\.py$') ->>> f.choices.sort() ->>> fix_os_paths(f.choices) -[('.../django/forms/__init__.py', '__init__.py'), ('.../django/forms/fields.py', 'fields.py'), ('.../django/forms/forms.py', 'forms.py'), ('.../django/forms/models.py', 'models.py'), ('.../django/forms/util.py', 'util.py'), ('.../django/forms/widgets.py', 'widgets.py')] ->>> f = forms.FilePathField(path=path, recursive=True, match='^.*?\.py$') ->>> f.choices.sort() ->>> fix_os_paths(f.choices) -[('.../django/forms/__init__.py', '__init__.py'), ('.../django/forms/extras/__init__.py', 'extras/__init__.py'), ('.../django/forms/extras/widgets.py', 'extras/widgets.py'), ('.../django/forms/fields.py', 'fields.py'), ('.../django/forms/forms.py', 'forms.py'), ('.../django/forms/models.py', 'models.py'), ('.../django/forms/util.py', 'util.py'), ('.../django/forms/widgets.py', 'widgets.py')] - -# SplitDateTimeField ########################################################## - ->>> f = SplitDateTimeField() ->>> f.widget ->> f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)]) -datetime.datetime(2006, 1, 10, 7, 30) ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean('hello') -Traceback (most recent call last): -... -ValidationError: [u'Enter a list of values.'] ->>> f.clean(['hello', 'there']) -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date.', u'Enter a valid time.'] ->>> f.clean(['2006-01-10', 'there']) -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid time.'] ->>> f.clean(['hello', '07:30']) -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date.'] - ->>> f = SplitDateTimeField(required=False) ->>> f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)]) -datetime.datetime(2006, 1, 10, 7, 30) ->>> f.clean(['2006-01-10', '07:30']) -datetime.datetime(2006, 1, 10, 7, 30) ->>> f.clean(None) ->>> f.clean('') ->>> f.clean(['']) ->>> f.clean(['', '']) ->>> f.clean('hello') -Traceback (most recent call last): -... -ValidationError: [u'Enter a list of values.'] ->>> f.clean(['hello', 'there']) -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date.', u'Enter a valid time.'] ->>> f.clean(['2006-01-10', 'there']) -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid time.'] ->>> f.clean(['hello', '07:30']) -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date.'] ->>> f.clean(['2006-01-10', '']) -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid time.'] ->>> f.clean(['2006-01-10']) -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid time.'] ->>> f.clean(['', '07:30']) -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid date.'] """ +import datetime +import time +import re +import os + +from unittest import TestCase + +from django.core.files.uploadedfile import SimpleUploadedFile +from django.forms import * +from django.forms.widgets import RadioFieldRenderer + +try: + from decimal import Decimal +except ImportError: + from django.utils._decimal import Decimal + + +def fix_os_paths(x): + if isinstance(x, basestring): + return x.replace('\\', '/') + elif isinstance(x, tuple): + return tuple(fix_os_paths(list(x))) + elif isinstance(x, list): + return [fix_os_paths(y) for y in x] + else: + return x + + +class FieldsTests(TestCase): + + def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs): + self.assertRaises(error, callable, *args, **kwargs) + try: + callable(*args, **kwargs) + except error, e: + self.assertEqual(message, str(e)) + + # CharField ################################################################### + + def test_charfield_0(self): + f = CharField() + self.assertEqual(u'1', f.clean(1)) + self.assertEqual(u'hello', f.clean('hello')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertEqual(u'[1, 2, 3]', f.clean([1, 2, 3])) + + def test_charfield_1(self): + f = CharField(required=False) + self.assertEqual(u'1', f.clean(1)) + self.assertEqual(u'hello', f.clean('hello')) + self.assertEqual(u'', f.clean(None)) + self.assertEqual(u'', f.clean('')) + self.assertEqual(u'[1, 2, 3]', f.clean([1, 2, 3])) + + def test_charfield_2(self): + f = CharField(max_length=10, required=False) + self.assertEqual(u'12345', f.clean('12345')) + self.assertEqual(u'1234567890', f.clean('1234567890')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '1234567890a') + + def test_charfield_3(self): + f = CharField(min_length=10, required=False) + self.assertEqual(u'', f.clean('')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345') + self.assertEqual(u'1234567890', f.clean('1234567890')) + self.assertEqual(u'1234567890a', f.clean('1234567890a')) + + def test_charfield_4(self): + f = CharField(min_length=10, required=True) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 5).']", f.clean, '12345') + self.assertEqual(u'1234567890', f.clean('1234567890')) + self.assertEqual(u'1234567890a', f.clean('1234567890a')) + + # IntegerField ################################################################ + + def test_integerfield_5(self): + f = IntegerField() + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertEqual(1, f.clean('1')) + self.assertEqual(True, isinstance(f.clean('1'), int)) + self.assertEqual(23, f.clean('23')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a') + self.assertEqual(42, f.clean(42)) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 3.14) + self.assertEqual(1, f.clean('1 ')) + self.assertEqual(1, f.clean(' 1')) + self.assertEqual(1, f.clean(' 1 ')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a') + + def test_integerfield_6(self): + f = IntegerField(required=False) + self.assertEqual(None, f.clean('')) + self.assertEqual('None', repr(f.clean(''))) + self.assertEqual(None, f.clean(None)) + self.assertEqual('None', repr(f.clean(None))) + self.assertEqual(1, f.clean('1')) + self.assertEqual(True, isinstance(f.clean('1'), int)) + self.assertEqual(23, f.clean('23')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, 'a') + self.assertEqual(1, f.clean('1 ')) + self.assertEqual(1, f.clean(' 1')) + self.assertEqual(1, f.clean(' 1 ')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a whole number.']", f.clean, '1a') + + def test_integerfield_7(self): + f = IntegerField(max_value=10) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertEqual(1, f.clean(1)) + self.assertEqual(10, f.clean(10)) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, 11) + self.assertEqual(10, f.clean('10')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 10.']", f.clean, '11') + + def test_integerfield_8(self): + f = IntegerField(min_value=10) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1) + self.assertEqual(10, f.clean(10)) + self.assertEqual(11, f.clean(11)) + self.assertEqual(10, f.clean('10')) + self.assertEqual(11, f.clean('11')) + + def test_integerfield_9(self): + f = IntegerField(min_value=10, max_value=20) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 10.']", f.clean, 1) + self.assertEqual(10, f.clean(10)) + self.assertEqual(11, f.clean(11)) + self.assertEqual(10, f.clean('10')) + self.assertEqual(11, f.clean('11')) + self.assertEqual(20, f.clean(20)) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 20.']", f.clean, 21) + + # FloatField ################################################################## + + def test_floatfield_10(self): + f = FloatField() + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertEqual(1.0, f.clean('1')) + self.assertEqual(True, isinstance(f.clean('1'), float)) + self.assertEqual(23.0, f.clean('23')) + self.assertEqual(3.1400000000000001, f.clean('3.14')) + self.assertEqual(3.1400000000000001, f.clean(3.14)) + self.assertEqual(42.0, f.clean(42)) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a') + self.assertEqual(1.0, f.clean('1.0 ')) + self.assertEqual(1.0, f.clean(' 1.0')) + self.assertEqual(1.0, f.clean(' 1.0 ')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a') + + def test_floatfield_11(self): + f = FloatField(required=False) + self.assertEqual(None, f.clean('')) + self.assertEqual(None, f.clean(None)) + self.assertEqual(1.0, f.clean('1')) + + def test_floatfield_12(self): + f = FloatField(max_value=1.5, min_value=0.5) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 1.5.']", f.clean, '1.6') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 0.5.']", f.clean, '0.4') + self.assertEqual(1.5, f.clean('1.5')) + self.assertEqual(0.5, f.clean('0.5')) + + # DecimalField ################################################################ + + def test_decimalfield_13(self): + f = DecimalField(max_digits=4, decimal_places=2) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertEqual(f.clean('1'), Decimal("1")) + self.assertEqual(True, isinstance(f.clean('1'), Decimal)) + self.assertEqual(f.clean('23'), Decimal("23")) + self.assertEqual(f.clean('3.14'), Decimal("3.14")) + self.assertEqual(f.clean(3.14), Decimal("3.14")) + self.assertEqual(f.clean(Decimal('3.14')), Decimal("3.14")) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, 'a') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, u'łąść') + self.assertEqual(f.clean('1.0 '), Decimal("1.0")) + self.assertEqual(f.clean(' 1.0'), Decimal("1.0")) + self.assertEqual(f.clean(' 1.0 '), Decimal("1.0")) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '1.0a') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '123.45') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '1.234') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 digits before the decimal point.']", f.clean, '123.4') + self.assertEqual(f.clean('-12.34'), Decimal("-12.34")) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-123.45') + self.assertEqual(f.clean('-.12'), Decimal("-0.12")) + self.assertEqual(f.clean('-00.12'), Decimal("-0.12")) + self.assertEqual(f.clean('-000.12'), Decimal("-0.12")) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '-000.123') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 4 digits in total.']", f.clean, '-000.12345') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a number.']", f.clean, '--0.12') + + def test_decimalfield_14(self): + f = DecimalField(max_digits=4, decimal_places=2, required=False) + self.assertEqual(None, f.clean('')) + self.assertEqual(None, f.clean(None)) + self.assertEqual(f.clean('1'), Decimal("1")) + + def test_decimalfield_15(self): + f = DecimalField(max_digits=4, decimal_places=2, max_value=Decimal('1.5'), min_value=Decimal('0.5')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value is less than or equal to 1.5.']", f.clean, '1.6') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value is greater than or equal to 0.5.']", f.clean, '0.4') + self.assertEqual(f.clean('1.5'), Decimal("1.5")) + self.assertEqual(f.clean('0.5'), Decimal("0.5")) + self.assertEqual(f.clean('.5'), Decimal("0.5")) + self.assertEqual(f.clean('00.50'), Decimal("0.50")) + + def test_decimalfield_16(self): + f = DecimalField(decimal_places=2) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 2 decimal places.']", f.clean, '0.00000001') + + def test_decimalfield_17(self): + f = DecimalField(max_digits=3) + # Leading whole zeros "collapse" to one digit. + self.assertEqual(f.clean('0000000.10'), Decimal("0.1")) + # But a leading 0 before the . doesn't count towards max_digits + self.assertEqual(f.clean('0000000.100'), Decimal("0.100")) + # Only leading whole zeros "collapse" to one digit. + self.assertEqual(f.clean('000000.02'), Decimal('0.02')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 3 digits in total.']", f.clean, '000000.0002') + self.assertEqual(f.clean('.002'), Decimal("0.002")) + + def test_decimalfield_18(self): + f = DecimalField(max_digits=2, decimal_places=2) + self.assertEqual(f.clean('.01'), Decimal(".01")) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure that there are no more than 0 digits before the decimal point.']", f.clean, '1.1') + + # DateField ################################################################### + + def test_datefield_19(self): + f = DateField() + self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.date(2006, 10, 25))) + self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.datetime(2006, 10, 25, 14, 30))) + self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))) + self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))) + self.assertEqual(datetime.date(2006, 10, 25), f.clean('2006-10-25')) + self.assertEqual(datetime.date(2006, 10, 25), f.clean('10/25/2006')) + self.assertEqual(datetime.date(2006, 10, 25), f.clean('10/25/06')) + self.assertEqual(datetime.date(2006, 10, 25), f.clean('Oct 25 2006')) + self.assertEqual(datetime.date(2006, 10, 25), f.clean('October 25 2006')) + self.assertEqual(datetime.date(2006, 10, 25), f.clean('October 25, 2006')) + self.assertEqual(datetime.date(2006, 10, 25), f.clean('25 October 2006')) + self.assertEqual(datetime.date(2006, 10, 25), f.clean('25 October, 2006')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-4-31') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '200a-10-25') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '25/10/06') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + + def test_datefield_20(self): + f = DateField(required=False) + self.assertEqual(None, f.clean(None)) + self.assertEqual('None', repr(f.clean(None))) + self.assertEqual(None, f.clean('')) + self.assertEqual('None', repr(f.clean(''))) + + def test_datefield_21(self): + f = DateField(input_formats=['%Y %m %d']) + self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.date(2006, 10, 25))) + self.assertEqual(datetime.date(2006, 10, 25), f.clean(datetime.datetime(2006, 10, 25, 14, 30))) + self.assertEqual(datetime.date(2006, 10, 25), f.clean('2006 10 25')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '2006-10-25') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/2006') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, '10/25/06') + + # TimeField ################################################################### + + def test_timefield_22(self): + f = TimeField() + self.assertEqual(datetime.time(14, 25), f.clean(datetime.time(14, 25))) + self.assertEqual(datetime.time(14, 25, 59), f.clean(datetime.time(14, 25, 59))) + self.assertEqual(datetime.time(14, 25), f.clean('14:25')) + self.assertEqual(datetime.time(14, 25, 59), f.clean('14:25:59')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, 'hello') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '1:24 p.m.') + + def test_timefield_23(self): + f = TimeField(input_formats=['%I:%M %p']) + self.assertEqual(datetime.time(14, 25), f.clean(datetime.time(14, 25))) + self.assertEqual(datetime.time(14, 25, 59), f.clean(datetime.time(14, 25, 59))) + self.assertEqual(datetime.time(4, 25), f.clean('4:25 AM')) + self.assertEqual(datetime.time(16, 25), f.clean('4:25 PM')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, '14:30:45') + + # DateTimeField ############################################################### + + def test_datetimefield_24(self): + f = DateTimeField() + self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(datetime.date(2006, 10, 25))) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean(datetime.datetime(2006, 10, 25, 14, 30))) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59, 200), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45), f.clean('2006-10-25 14:30:45')) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006-10-25 14:30:00')) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006-10-25 14:30')) + self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean('2006-10-25')) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45), f.clean('10/25/2006 14:30:45')) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('10/25/2006 14:30:00')) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('10/25/2006 14:30')) + self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean('10/25/2006')) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 45), f.clean('10/25/06 14:30:45')) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('10/25/06 14:30:00')) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('10/25/06 14:30')) + self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean('10/25/06')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, 'hello') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 4:30 p.m.') + + def test_datetimefield_25(self): + f = DateTimeField(input_formats=['%Y %m %d %I:%M %p']) + self.assertEqual(datetime.datetime(2006, 10, 25, 0, 0), f.clean(datetime.date(2006, 10, 25))) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean(datetime.datetime(2006, 10, 25, 14, 30))) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59))) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30, 59, 200), f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200))) + self.assertEqual(datetime.datetime(2006, 10, 25, 14, 30), f.clean('2006 10 25 2:30 PM')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date/time.']", f.clean, '2006-10-25 14:30:45') + + def test_datetimefield_26(self): + f = DateTimeField(required=False) + self.assertEqual(None, f.clean(None)) + self.assertEqual('None', repr(f.clean(None))) + self.assertEqual(None, f.clean('')) + self.assertEqual('None', repr(f.clean(''))) + + # RegexField ################################################################## + + def test_regexfield_27(self): + f = RegexField('^\d[A-F]\d$') + self.assertEqual(u'2A2', f.clean('2A2')) + self.assertEqual(u'3F3', f.clean('3F3')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + + def test_regexfield_28(self): + f = RegexField('^\d[A-F]\d$', required=False) + self.assertEqual(u'2A2', f.clean('2A2')) + self.assertEqual(u'3F3', f.clean('3F3')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3') + self.assertEqual(u'', f.clean('')) + + def test_regexfield_29(self): + f = RegexField(re.compile('^\d[A-F]\d$')) + self.assertEqual(u'2A2', f.clean('2A2')) + self.assertEqual(u'3F3', f.clean('3F3')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '3G3') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, ' 2A2') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '2A2 ') + + def test_regexfield_30(self): + f = RegexField('^\d\d\d\d$', error_message='Enter a four-digit number.') + self.assertEqual(u'1234', f.clean('1234')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, '123') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a four-digit number.']", f.clean, 'abcd') + + def test_regexfield_31(self): + f = RegexField('^\d+$', min_length=5, max_length=10) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, '123') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, 'abc') + self.assertEqual(u'12345', f.clean('12345')) + self.assertEqual(u'1234567890', f.clean('1234567890')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '12345678901') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '12345a') + + # EmailField ################################################################## + + def test_emailfield_32(self): + f = EmailField() + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertEqual(u'person@example.com', f.clean('person@example.com')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@invalid-.com') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@-invalid.com') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.alid-.com') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'example@inv-.-alid.com') + self.assertEqual(u'example@valid-----hyphens.com', f.clean('example@valid-----hyphens.com')) + self.assertEqual(u'example@valid-with-hyphens.com', f.clean('example@valid-with-hyphens.com')) + + def test_email_regexp_for_performance(self): + f = EmailField() + # Check for runaway regex security problem. This will take for-freeking-ever + # if the security fix isn't in place. + self.assertRaisesErrorWithMessage( + ValidationError, + "[u'Enter a valid e-mail address.']", + f.clean, + 'viewx3dtextx26qx3d@yahoo.comx26latlngx3d15854521645943074058' + ) + + def test_emailfield_33(self): + f = EmailField(required=False) + self.assertEqual(u'', f.clean('')) + self.assertEqual(u'', f.clean(None)) + self.assertEqual(u'person@example.com', f.clean('person@example.com')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar') + + def test_emailfield_34(self): + f = EmailField(min_length=10, max_length=15) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 10 characters (it has 9).']", f.clean, 'a@foo.com') + self.assertEqual(u'alf@foo.com', f.clean('alf@foo.com')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at most 15 characters (it has 20).']", f.clean, 'alf123456788@foo.com') + + # FileField ################################################################## + + def test_filefield_35(self): + f = FileField() + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '', '') + self.assertEqual('files/test1.pdf', f.clean('', 'files/test1.pdf')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None, '') + self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', '')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, SimpleUploadedFile('', ''), '') + self.assertEqual('files/test3.pdf', f.clean(None, 'files/test3.pdf')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'No file was submitted. Check the encoding type on the form.']", f.clean, 'some content that is not a file') + self.assertRaisesErrorWithMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', None)) + self.assertRaisesErrorWithMessage(ValidationError, "[u'The submitted file is empty.']", f.clean, SimpleUploadedFile('name', '')) + self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) + self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह')))) + self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content'), 'files/test4.pdf'))) + + def test_filefield_36(self): + f = FileField(max_length = 5) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this filename has at most 5 characters (it has 18).']", f.clean, SimpleUploadedFile('test_maxlength.txt', 'hello world')) + self.assertEqual('files/test1.pdf', f.clean('', 'files/test1.pdf')) + self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) + self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) + + # URLField ################################################################## + + def test_urlfield_37(self): + f = URLField() + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertEqual(u'http://localhost/', f.clean('http://localhost')) + self.assertEqual(u'http://example.com/', f.clean('http://example.com')) + self.assertEqual(u'http://example.com./', f.clean('http://example.com.')) + self.assertEqual(u'http://www.example.com/', f.clean('http://www.example.com')) + self.assertEqual(u'http://www.example.com:8000/test', f.clean('http://www.example.com:8000/test')) + self.assertEqual(u'http://valid-with-hyphens.com/', f.clean('valid-with-hyphens.com')) + self.assertEqual(u'http://subdomain.domain.com/', f.clean('subdomain.domain.com')) + self.assertEqual(u'http://200.8.9.10/', f.clean('http://200.8.9.10')) + self.assertEqual(u'http://200.8.9.10:8000/test', f.clean('http://200.8.9.10:8000/test')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'com.') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, '.') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://invalid-.com') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://-invalid.com') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.alid-.com') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://inv-.-alid.com') + self.assertEqual(u'http://valid-----hyphens.com/', f.clean('http://valid-----hyphens.com')) + + def test_url_regex_ticket11198(self): + f = URLField() + # hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*200,)) + + # a second test, to make sure the problem is really addressed, even on + # domains that don't fail the domain label length check in the regex + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://%s' % ("X"*60,)) + + def test_urlfield_38(self): + f = URLField(required=False) + self.assertEqual(u'', f.clean('')) + self.assertEqual(u'', f.clean(None)) + self.assertEqual(u'http://example.com/', f.clean('http://example.com')) + self.assertEqual(u'http://www.example.com/', f.clean('http://www.example.com')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'foo') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example.') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://.com') + + def test_urlfield_39(self): + f = URLField(verify_exists=True) + self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://example') + self.assertRaises(ValidationError, f.clean, 'http://www.broken.djangoproject.com') # bad domain + try: + f.clean('http://www.broken.djangoproject.com') # bad domain + except ValidationError, e: + self.assertEqual("[u'This URL appears to be a broken link.']", str(e)) + self.assertRaises(ValidationError, f.clean, 'http://google.com/we-love-microsoft.html') # good domain, bad page + try: + f.clean('http://google.com/we-love-microsoft.html') # good domain, bad page + except ValidationError, e: + self.assertEqual("[u'This URL appears to be a broken link.']", str(e)) + + def test_urlfield_40(self): + f = URLField(verify_exists=True, required=False) + self.assertEqual(u'', f.clean('')) + self.assertEqual(u'http://www.google.com/', f.clean('http://www.google.com')) # This will fail if there's no Internet connection + + def test_urlfield_41(self): + f = URLField(min_length=15, max_length=20) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 15 characters (it has 13).']", f.clean, 'http://f.com') + self.assertEqual(u'http://example.com/', f.clean('http://example.com')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 38).']", f.clean, 'http://abcdefghijklmnopqrstuvwxyz.com') + + def test_urlfield_42(self): + f = URLField(required=False) + self.assertEqual(u'http://example.com/', f.clean('example.com')) + self.assertEqual(u'', f.clean('')) + self.assertEqual(u'https://example.com/', f.clean('https://example.com')) + + def test_urlfield_43(self): + f = URLField() + self.assertEqual(u'http://example.com/', f.clean('http://example.com')) + self.assertEqual(u'http://example.com/test', f.clean('http://example.com/test')) + + # BooleanField ################################################################ + + def test_booleanfield_44(self): + f = BooleanField() + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertEqual(True, f.clean(True)) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, False) + self.assertEqual(True, f.clean(1)) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, 0) + self.assertEqual(True, f.clean('Django rocks')) + self.assertEqual(True, f.clean('True')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, 'False') + + def test_booleanfield_45(self): + f = BooleanField(required=False) + self.assertEqual(False, f.clean('')) + self.assertEqual(False, f.clean(None)) + self.assertEqual(True, f.clean(True)) + self.assertEqual(False, f.clean(False)) + self.assertEqual(True, f.clean(1)) + self.assertEqual(False, f.clean(0)) + self.assertEqual(True, f.clean('1')) + self.assertEqual(False, f.clean('0')) + self.assertEqual(True, f.clean('Django rocks')) + self.assertEqual(False, f.clean('False')) + + # ChoiceField ################################################################# + + def test_choicefield_46(self): + f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')]) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertEqual(u'1', f.clean(1)) + self.assertEqual(u'1', f.clean('1')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3') + + def test_choicefield_47(self): + f = ChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) + self.assertEqual(u'', f.clean('')) + self.assertEqual(u'', f.clean(None)) + self.assertEqual(u'1', f.clean(1)) + self.assertEqual(u'1', f.clean('1')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, '3') + + def test_choicefield_48(self): + f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')]) + self.assertEqual(u'J', f.clean('J')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. John is not one of the available choices.']", f.clean, 'John') + + def test_choicefield_49(self): + f = ChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) + self.assertEqual(u'1', f.clean(1)) + self.assertEqual(u'1', f.clean('1')) + self.assertEqual(u'3', f.clean(3)) + self.assertEqual(u'3', f.clean('3')) + self.assertEqual(u'5', f.clean(5)) + self.assertEqual(u'5', f.clean('5')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, '6') + + # TypedChoiceField ############################################################ + # TypedChoiceField is just like ChoiceField, except that coerced types will + # be returned: + + def test_typedchoicefield_50(self): + f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int) + self.assertEqual(1, f.clean('1')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, '2') + + def test_typedchoicefield_51(self): + # Different coercion, same validation. + f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=float) + self.assertEqual(1.0, f.clean('1')) + + def test_typedchoicefield_52(self): + # This can also cause weirdness: be careful (bool(-1) == True, remember) + f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=bool) + self.assertEqual(True, f.clean('-1')) + + def test_typedchoicefield_53(self): + # Even more weirdness: if you have a valid choice but your coercion function + # can't coerce, you'll still get a validation error. Don't do this! + f = TypedChoiceField(choices=[('A', 'A'), ('B', 'B')], coerce=int) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. B is not one of the available choices.']", f.clean, 'B') + # Required fields require values + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + + def test_typedchoicefield_54(self): + # Non-required fields aren't required + f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False) + self.assertEqual('', f.clean('')) + # If you want cleaning an empty value to return a different type, tell the field + + def test_typedchoicefield_55(self): + f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None) + self.assertEqual(None, f.clean('')) + + # NullBooleanField ############################################################ + + def test_nullbooleanfield_56(self): + f = NullBooleanField() + self.assertEqual(None, f.clean('')) + self.assertEqual(True, f.clean(True)) + self.assertEqual(False, f.clean(False)) + self.assertEqual(None, f.clean(None)) + self.assertEqual(False, f.clean('0')) + self.assertEqual(True, f.clean('1')) + self.assertEqual(None, f.clean('2')) + self.assertEqual(None, f.clean('3')) + self.assertEqual(None, f.clean('hello')) + + + def test_nullbooleanfield_57(self): + # Make sure that the internal value is preserved if using HiddenInput (#7753) + class HiddenNullBooleanForm(Form): + hidden_nullbool1 = NullBooleanField(widget=HiddenInput, initial=True) + hidden_nullbool2 = NullBooleanField(widget=HiddenInput, initial=False) + f = HiddenNullBooleanForm() + self.assertEqual('', str(f)) + + def test_nullbooleanfield_58(self): + class HiddenNullBooleanForm(Form): + hidden_nullbool1 = NullBooleanField(widget=HiddenInput, initial=True) + hidden_nullbool2 = NullBooleanField(widget=HiddenInput, initial=False) + f = HiddenNullBooleanForm({ 'hidden_nullbool1': 'True', 'hidden_nullbool2': 'False' }) + self.assertEqual(None, f.full_clean()) + self.assertEqual(True, f.cleaned_data['hidden_nullbool1']) + self.assertEqual(False, f.cleaned_data['hidden_nullbool2']) + + def test_nullbooleanfield_59(self): + # Make sure we're compatible with MySQL, which uses 0 and 1 for its boolean + # values. (#9609) + NULLBOOL_CHOICES = (('1', 'Yes'), ('0', 'No'), ('', 'Unknown')) + class MySQLNullBooleanForm(Form): + nullbool0 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES)) + nullbool1 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES)) + nullbool2 = NullBooleanField(widget=RadioSelect(choices=NULLBOOL_CHOICES)) + f = MySQLNullBooleanForm({ 'nullbool0': '1', 'nullbool1': '0', 'nullbool2': '' }) + self.assertEqual(None, f.full_clean()) + self.assertEqual(True, f.cleaned_data['nullbool0']) + self.assertEqual(False, f.cleaned_data['nullbool1']) + self.assertEqual(None, f.cleaned_data['nullbool2']) + + # MultipleChoiceField ######################################################### + + def test_multiplechoicefield_60(self): + f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')]) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertEqual([u'1'], f.clean([1])) + self.assertEqual([u'1'], f.clean(['1'])) + self.assertEqual([u'1', u'2'], f.clean(['1', '2'])) + self.assertEqual([u'1', u'2'], f.clean([1, '2'])) + self.assertEqual([u'1', u'2'], f.clean((1, '2'))) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, []) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, ()) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3']) + + def test_multiplechoicefield_61(self): + f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')], required=False) + self.assertEqual([], f.clean('')) + self.assertEqual([], f.clean(None)) + self.assertEqual([u'1'], f.clean([1])) + self.assertEqual([u'1'], f.clean(['1'])) + self.assertEqual([u'1', u'2'], f.clean(['1', '2'])) + self.assertEqual([u'1', u'2'], f.clean([1, '2'])) + self.assertEqual([u'1', u'2'], f.clean((1, '2'))) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') + self.assertEqual([], f.clean([])) + self.assertEqual([], f.clean(())) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 3 is not one of the available choices.']", f.clean, ['3']) + + def test_multiplechoicefield_62(self): + f = MultipleChoiceField(choices=[('Numbers', (('1', 'One'), ('2', 'Two'))), ('Letters', (('3','A'),('4','B'))), ('5','Other')]) + self.assertEqual([u'1'], f.clean([1])) + self.assertEqual([u'1'], f.clean(['1'])) + self.assertEqual([u'1', u'5'], f.clean([1, 5])) + self.assertEqual([u'1', u'5'], f.clean([1, '5'])) + self.assertEqual([u'1', u'5'], f.clean(['1', 5])) + self.assertEqual([u'1', u'5'], f.clean(['1', '5'])) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['6']) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['1','6']) + + # ComboField ################################################################## + + def test_combofield_63(self): + f = ComboField(fields=[CharField(max_length=20), EmailField()]) + self.assertEqual(u'test@example.com', f.clean('test@example.com')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + + def test_combofield_64(self): + f = ComboField(fields=[CharField(max_length=20), EmailField()], required=False) + self.assertEqual(u'test@example.com', f.clean('test@example.com')) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at most 20 characters (it has 28).']", f.clean, 'longemailaddress@example.com') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'not an e-mail') + self.assertEqual(u'', f.clean('')) + self.assertEqual(u'', f.clean(None)) + + # FilePathField ############################################################### + + def test_filepathfield_65(self): + path = forms.__file__ + path = os.path.dirname(path) + '/' + assert fix_os_paths(path).endswith('/django/forms/') + + def test_filepathfield_66(self): + path = forms.__file__ + path = os.path.dirname(path) + '/' + f = FilePathField(path=path) + f.choices = [p for p in f.choices if p[0].endswith('.py')] + f.choices.sort() + expected = [ + ('/django/forms/__init__.py', '__init__.py'), + ('/django/forms/fields.py', 'fields.py'), + ('/django/forms/forms.py', 'forms.py'), + ('/django/forms/formsets.py', 'formsets.py'), + ('/django/forms/models.py', 'models.py'), + ('/django/forms/util.py', 'util.py'), + ('/django/forms/widgets.py', 'widgets.py') + ] + for exp, got in zip(expected, fix_os_paths(f.choices)): + self.assertEqual(exp[1], got[1]) + assert got[0].endswith(exp[0]) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. fields.py is not one of the available choices.']", f.clean, 'fields.py') + assert fix_os_paths(f.clean(path + 'fields.py')).endswith('/django/forms/fields.py') + + def test_filepathfield_67(self): + path = forms.__file__ + path = os.path.dirname(path) + '/' + f = FilePathField(path=path, match='^.*?\.py$') + f.choices.sort() + expected = [ + ('/django/forms/__init__.py', '__init__.py'), + ('/django/forms/fields.py', 'fields.py'), + ('/django/forms/forms.py', 'forms.py'), + ('/django/forms/formsets.py', 'formsets.py'), + ('/django/forms/models.py', 'models.py'), + ('/django/forms/util.py', 'util.py'), + ('/django/forms/widgets.py', 'widgets.py') + ] + for exp, got in zip(expected, fix_os_paths(f.choices)): + self.assertEqual(exp[1], got[1]) + assert got[0].endswith(exp[0]) + + def test_filepathfield_68(self): + path = forms.__file__ + path = os.path.dirname(path) + '/' + f = FilePathField(path=path, recursive=True, match='^.*?\.py$') + f.choices.sort() + expected = [ + ('/django/forms/__init__.py', '__init__.py'), + ('/django/forms/extras/__init__.py', 'extras/__init__.py'), + ('/django/forms/extras/widgets.py', 'extras/widgets.py'), + ('/django/forms/fields.py', 'fields.py'), + ('/django/forms/forms.py', 'forms.py'), + ('/django/forms/formsets.py', 'formsets.py'), + ('/django/forms/models.py', 'models.py'), + ('/django/forms/util.py', 'util.py'), + ('/django/forms/widgets.py', 'widgets.py') + ] + for exp, got in zip(expected, fix_os_paths(f.choices)): + self.assertEqual(exp[1], got[1]) + assert got[0].endswith(exp[0]) + + # SplitDateTimeField ########################################################## + + def test_splitdatetimefield_69(self): + from django.forms.widgets import SplitDateTimeWidget + f = SplitDateTimeField() + assert isinstance(f.widget, SplitDateTimeWidget) + self.assertEqual(datetime.datetime(2006, 1, 10, 7, 30), f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)])) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, None) + self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, '') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there']) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there']) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30']) + + def test_splitdatetimefield_70(self): + f = SplitDateTimeField(required=False) + self.assertEqual(datetime.datetime(2006, 1, 10, 7, 30), f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)])) + self.assertEqual(datetime.datetime(2006, 1, 10, 7, 30), f.clean(['2006-01-10', '07:30'])) + self.assertEqual(None, f.clean(None)) + self.assertEqual(None, f.clean('')) + self.assertEqual(None, f.clean([''])) + self.assertEqual(None, f.clean(['', ''])) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a list of values.']", f.clean, 'hello') + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.', u'Enter a valid time.']", f.clean, ['hello', 'there']) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', 'there']) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['hello', '07:30']) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10', '']) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid time.']", f.clean, ['2006-01-10']) + self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid date.']", f.clean, ['', '07:30']) diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 6d418fa5a3..6f00a06fdd 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- from extra import tests as extra_tests -from fields import tests as fields_tests from forms import tests as form_tests from error_messages import tests as custom_error_message_tests from localflavor.ar import tests as localflavor_ar_tests @@ -32,9 +31,10 @@ from widgets import tests as widgets_tests from formsets import tests as formset_tests from media import media_tests +from fields import FieldsTests + __test__ = { 'extra_tests': extra_tests, - 'fields_tests': fields_tests, 'form_tests': form_tests, 'custom_error_message_tests': custom_error_message_tests, 'localflavor_ar_tests': localflavor_ar_tests,