Converted generic localflavor doctests to unittests. We have always been at war with doctests. Thanks to Idan Gazit.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14940 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-12-18 20:30:50 +00:00
parent 6127cf13cd
commit 1eee7757b7
3 changed files with 83 additions and 158 deletions

View File

@ -1,163 +1,88 @@
# -*- coding: utf-8 -*- import datetime
# Tests for the contrib/localflavor/ generic form fields.
tests = r""" from django.contrib.localflavor.generic.forms import DateField, DateTimeField
## Generic DateField ##########################################################
>>> from django.contrib.localflavor.generic.forms import * from utils import LocalFlavorTestCase
A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where
appropriate.
>>> import datetime class GenericLocalFlavorTests(LocalFlavorTestCase):
>>> f = DateField() def test_GenericDateField(self):
>>> f.clean(datetime.date(2006, 10, 25)) error_invalid = [u'Enter a valid date.']
datetime.date(2006, 10, 25) valid = {
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) datetime.date(2006, 10, 25): datetime.date(2006, 10, 25),
datetime.date(2006, 10, 25) datetime.datetime(2006, 10, 25, 14, 30): datetime.date(2006, 10, 25),
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) datetime.datetime(2006, 10, 25, 14, 30, 59): datetime.date(2006, 10, 25),
datetime.date(2006, 10, 25) datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.date(2006, 10, 25),
>>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) '2006-10-25': datetime.date(2006, 10, 25),
datetime.date(2006, 10, 25) '25/10/2006': datetime.date(2006, 10, 25),
>>> f.clean('2006-10-25') '25/10/06': datetime.date(2006, 10, 25),
datetime.date(2006, 10, 25) 'Oct 25 2006': datetime.date(2006, 10, 25),
>>> f.clean('25/10/2006') 'October 25 2006': datetime.date(2006, 10, 25),
datetime.date(2006, 10, 25) 'October 25, 2006': datetime.date(2006, 10, 25),
>>> f.clean('25/10/06') '25 October 2006': datetime.date(2006, 10, 25),
datetime.date(2006, 10, 25) '25 October, 2006': datetime.date(2006, 10, 25),
>>> f.clean('Oct 25 2006') }
datetime.date(2006, 10, 25) invalid = {
>>> f.clean('October 25 2006') '2006-4-31': error_invalid,
datetime.date(2006, 10, 25) '200a-10-25': error_invalid,
>>> f.clean('October 25, 2006') '10/25/06': error_invalid,
datetime.date(2006, 10, 25) }
>>> f.clean('25 October 2006') self.assertFieldOutput(DateField, valid, invalid, empty_value=None)
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('10/25/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) # DateField with optional input_formats parameter
>>> f.clean(None) valid = {
>>> repr(f.clean(None)) datetime.date(2006, 10, 25): datetime.date(2006, 10, 25),
'None' datetime.datetime(2006, 10, 25, 14, 30): datetime.date(2006, 10, 25),
>>> f.clean('') '2006 10 25': datetime.date(2006, 10, 25),
>>> repr(f.clean('')) }
'None' invalid = {
'2006-10-25': error_invalid,
'25/10/2006': error_invalid,
'25/10/06': error_invalid,
}
kwargs = {'input_formats':['%Y %m %d'],}
self.assertFieldOutput(DateField,
valid, invalid, field_kwargs=kwargs, empty_value=None
)
DateField accepts an optional input_formats parameter: def test_GenericDateTimeField(self):
>>> f = DateField(input_formats=['%Y %m %d']) error_invalid = [u'Enter a valid date/time.']
>>> f.clean(datetime.date(2006, 10, 25)) valid = {
datetime.date(2006, 10, 25) 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): datetime.datetime(2006, 10, 25, 14, 30),
datetime.date(2006, 10, 25) datetime.datetime(2006, 10, 25, 14, 30, 59): datetime.datetime(2006, 10, 25, 14, 30, 59),
>>> f.clean('2006 10 25') datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.datetime(2006, 10, 25, 14, 30, 59, 200),
datetime.date(2006, 10, 25) '2006-10-25 14:30:45': datetime.datetime(2006, 10, 25, 14, 30, 45),
'2006-10-25 14:30:00': datetime.datetime(2006, 10, 25, 14, 30),
'2006-10-25 14:30': datetime.datetime(2006, 10, 25, 14, 30),
'2006-10-25': datetime.datetime(2006, 10, 25, 0, 0),
'25/10/2006 14:30:45': datetime.datetime(2006, 10, 25, 14, 30, 45),
'25/10/2006 14:30:00': datetime.datetime(2006, 10, 25, 14, 30),
'25/10/2006 14:30': datetime.datetime(2006, 10, 25, 14, 30),
'25/10/2006': datetime.datetime(2006, 10, 25, 0, 0),
'25/10/06 14:30:45': datetime.datetime(2006, 10, 25, 14, 30, 45),
'25/10/06 14:30:00': datetime.datetime(2006, 10, 25, 14, 30),
'25/10/06 14:30': datetime.datetime(2006, 10, 25, 14, 30),
'25/10/06': datetime.datetime(2006, 10, 25, 0, 0),
}
invalid = {
'hello': error_invalid,
'2006-10-25 4:30 p.m.': error_invalid,
}
self.assertFieldOutput(DateTimeField, valid, invalid, empty_value=None)
The input_formats parameter overrides all default input formats, # DateTimeField with optional input_formats paramter
so the default formats won't work unless you specify them: valid = {
>>> f.clean('2006-10-25') datetime.date(2006, 10, 25): datetime.datetime(2006, 10, 25, 0, 0),
Traceback (most recent call last): datetime.datetime(2006, 10, 25, 14, 30): datetime.datetime(2006, 10, 25, 14, 30),
... datetime.datetime(2006, 10, 25, 14, 30, 59): datetime.datetime(2006, 10, 25, 14, 30, 59),
ValidationError: [u'Enter a valid date.'] datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.datetime(2006, 10, 25, 14, 30, 59, 200),
>>> f.clean('25/10/2006') '2006 10 25 2:30 PM': datetime.datetime(2006, 10, 25, 14, 30),
Traceback (most recent call last): }
... invalid = {
ValidationError: [u'Enter a valid date.'] '2006-10-25 14:30:45': error_invalid,
>>> f.clean('25/10/06') }
Traceback (most recent call last): kwargs = {'input_formats':['%Y %m %d %I:%M %p'],}
... self.assertFieldOutput(DateTimeField,
ValidationError: [u'Enter a valid date.'] valid, invalid, field_kwargs=kwargs, empty_value=None
)
## Generic DateTimeField ######################################################
A DateField that uses generic dd/mm/yy dates instead of mm/dd/yy where
appropriate.
>>> 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('25/10/2006 14:30:45')
datetime.datetime(2006, 10, 25, 14, 30, 45)
>>> f.clean('25/10/2006 14:30:00')
datetime.datetime(2006, 10, 25, 14, 30)
>>> f.clean('25/10/2006 14:30')
datetime.datetime(2006, 10, 25, 14, 30)
>>> f.clean('25/10/2006')
datetime.datetime(2006, 10, 25, 0, 0)
>>> f.clean('25/10/06 14:30:45')
datetime.datetime(2006, 10, 25, 14, 30, 45)
>>> f.clean('25/10/06 14:30:00')
datetime.datetime(2006, 10, 25, 14, 30)
>>> f.clean('25/10/06 14:30')
datetime.datetime(2006, 10, 25, 14, 30)
>>> f.clean('25/10/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'
"""

View File

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from localflavor.cz import tests as localflavor_cz_tests from localflavor.cz import tests as localflavor_cz_tests
from localflavor.generic import tests as localflavor_generic_tests
from localflavor.id import tests as localflavor_id_tests from localflavor.id import tests as localflavor_id_tests
from localflavor.ie import tests as localflavor_ie_tests from localflavor.ie import tests as localflavor_ie_tests
from localflavor.is_ import tests as localflavor_is_tests from localflavor.is_ import tests as localflavor_is_tests
@ -30,13 +29,13 @@ from localflavor.de import DELocalFlavorTests
from localflavor.es import ESLocalFlavorTests from localflavor.es import ESLocalFlavorTests
from localflavor.fi import FILocalFlavorTests from localflavor.fi import FILocalFlavorTests
from localflavor.fr import FRLocalFlavorTests from localflavor.fr import FRLocalFlavorTests
from localflavor.generic import GenericLocalFlavorTests
from localflavor.il import ILLocalFlavorTests from localflavor.il import ILLocalFlavorTests
from localflavor.tr import TRLocalFlavorTests from localflavor.tr import TRLocalFlavorTests
__test__ = { __test__ = {
'localflavor_cz_tests': localflavor_cz_tests, 'localflavor_cz_tests': localflavor_cz_tests,
'localflavor_generic_tests': localflavor_generic_tests,
'localflavor_id_tests': localflavor_id_tests, 'localflavor_id_tests': localflavor_id_tests,
'localflavor_ie_tests': localflavor_ie_tests, 'localflavor_ie_tests': localflavor_ie_tests,
'localflavor_is_tests': localflavor_is_tests, 'localflavor_is_tests': localflavor_is_tests,

View File

@ -25,6 +25,7 @@ from regressiontests.forms.localflavortests import (
ESLocalFlavorTests, ESLocalFlavorTests,
FILocalFlavorTests, FILocalFlavorTests,
FRLocalFlavorTests, FRLocalFlavorTests,
GenericLocalFlavorTests,
ILLocalFlavorTests, ILLocalFlavorTests,
TRLocalFlavorTests, TRLocalFlavorTests,
) )