From 1eee7757b79a13e386500aefbee8ab4939b9d88c Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 18 Dec 2010 20:30:50 +0000 Subject: [PATCH] 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 --- .../forms/localflavor/generic.py | 237 ++++++------------ .../regressiontests/forms/localflavortests.py | 3 +- tests/regressiontests/forms/tests/__init__.py | 1 + 3 files changed, 83 insertions(+), 158 deletions(-) diff --git a/tests/regressiontests/forms/localflavor/generic.py b/tests/regressiontests/forms/localflavor/generic.py index 0dbe30d709..f47fc91460 100644 --- a/tests/regressiontests/forms/localflavor/generic.py +++ b/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 + ) diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index 921148c854..8e4f65ce32 100644 --- a/tests/regressiontests/forms/localflavortests.py +++ b/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,13 +29,13 @@ from localflavor.de import DELocalFlavorTests from localflavor.es import ESLocalFlavorTests from localflavor.fi import FILocalFlavorTests from localflavor.fr import FRLocalFlavorTests +from localflavor.generic import GenericLocalFlavorTests from localflavor.il import ILLocalFlavorTests from localflavor.tr import TRLocalFlavorTests __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, diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index 99845d42be..a23290ee2a 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -25,6 +25,7 @@ from regressiontests.forms.localflavortests import ( ESLocalFlavorTests, FILocalFlavorTests, FRLocalFlavorTests, + GenericLocalFlavorTests, ILLocalFlavorTests, TRLocalFlavorTests, )