Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.2.X] Converted generic localflavor doctests to unittests. We have …

…always been at war with doctests. Thanks to Idan Gazit. Backport of [14940].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14965 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 994ba4f3ada79deef9ed28b2e8059e28149a7176 1 parent 4aedbcb
@alex alex authored
View
237 tests/regressiontests/forms/localflavor/generic.py
@@ -1,163 +1,88 @@
-# -*- coding: utf-8 -*-
-# Tests for the contrib/localflavor/ generic form fields.
+import datetime
-tests = r"""
-## Generic DateField ##########################################################
+from django.contrib.localflavor.generic.forms import DateField, DateTimeField
->>> 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
->>> 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('25/10/2006')
-datetime.date(2006, 10, 25)
->>> f.clean('25/10/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('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.']
+class GenericLocalFlavorTests(LocalFlavorTestCase):
+ def test_GenericDateField(self):
+ error_invalid = [u'Enter a valid date.']
+ valid = {
+ datetime.date(2006, 10, 25): datetime.date(2006, 10, 25),
+ datetime.datetime(2006, 10, 25, 14, 30): datetime.date(2006, 10, 25),
+ datetime.datetime(2006, 10, 25, 14, 30, 59): datetime.date(2006, 10, 25),
+ datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.date(2006, 10, 25),
+ '2006-10-25': datetime.date(2006, 10, 25),
+ '25/10/2006': datetime.date(2006, 10, 25),
+ '25/10/06': datetime.date(2006, 10, 25),
+ 'Oct 25 2006': datetime.date(2006, 10, 25),
+ 'October 25 2006': datetime.date(2006, 10, 25),
+ 'October 25, 2006': datetime.date(2006, 10, 25),
+ '25 October 2006': datetime.date(2006, 10, 25),
+ '25 October, 2006': datetime.date(2006, 10, 25),
+ }
+ invalid = {
+ '2006-4-31': error_invalid,
+ '200a-10-25': error_invalid,
+ '10/25/06': error_invalid,
+ }
+ self.assertFieldOutput(DateField, valid, invalid, empty_value=None)
->>> f = DateField(required=False)
->>> f.clean(None)
->>> repr(f.clean(None))
-'None'
->>> f.clean('')
->>> repr(f.clean(''))
-'None'
+ # DateField with optional input_formats parameter
+ valid = {
+ datetime.date(2006, 10, 25): datetime.date(2006, 10, 25),
+ datetime.datetime(2006, 10, 25, 14, 30): datetime.date(2006, 10, 25),
+ '2006 10 25': datetime.date(2006, 10, 25),
+ }
+ 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:
->>> 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)
+ def test_GenericDateTimeField(self):
+ error_invalid = [u'Enter a valid date/time.']
+ valid = {
+ datetime.date(2006, 10, 25): datetime.datetime(2006, 10, 25, 0, 0),
+ 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),
+ datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.datetime(2006, 10, 25, 14, 30, 59, 200),
+ '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,
-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('25/10/2006')
-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.']
-
-## 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'
-
-"""
+ # DateTimeField with optional input_formats paramter
+ valid = {
+ datetime.date(2006, 10, 25): datetime.datetime(2006, 10, 25, 0, 0),
+ 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),
+ datetime.datetime(2006, 10, 25, 14, 30, 59, 200): datetime.datetime(2006, 10, 25, 14, 30, 59, 200),
+ '2006 10 25 2:30 PM': datetime.datetime(2006, 10, 25, 14, 30),
+ }
+ invalid = {
+ '2006-10-25 14:30:45': error_invalid,
+ }
+ kwargs = {'input_formats':['%Y %m %d %I:%M %p'],}
+ self.assertFieldOutput(DateTimeField,
+ valid, invalid, field_kwargs=kwargs, empty_value=None
+ )
View
3  tests/regressiontests/forms/localflavortests.py
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
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.ie import tests as localflavor_ie_tests
from localflavor.is_ import tests as localflavor_is_tests
@@ -30,11 +29,11 @@
from localflavor.es import ESLocalFlavorTests
from localflavor.fi import FILocalFlavorTests
from localflavor.fr import FRLocalFlavorTests
+from localflavor.generic import GenericLocalFlavorTests
__test__ = {
'localflavor_cz_tests': localflavor_cz_tests,
- 'localflavor_generic_tests': localflavor_generic_tests,
'localflavor_id_tests': localflavor_id_tests,
'localflavor_ie_tests': localflavor_ie_tests,
'localflavor_is_tests': localflavor_is_tests,
View
1  tests/regressiontests/forms/tests/__init__.py
@@ -24,4 +24,5 @@
ESLocalFlavorTests,
FILocalFlavorTests,
FRLocalFlavorTests,
+ GenericLocalFlavorTests,
)
Please sign in to comment.
Something went wrong with that request. Please try again.