From bc27405fc563acaaef08f47662e6597e9e1751a0 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 18 Dec 2010 20:33:44 +0000 Subject: [PATCH] Fixed #14871, #14872 -- ZAIDField didn't handle alll EMPTY_VALUES correctly and ZAPostCodeField didn't respect *args or **kwargs (such as required=False). Also converted South African localflavor doctests into unittests. We have always been at war with doctests. Thanks to Idan Gazit. Fixing ZA localflavor clean() #14872 git-svn-id: http://code.djangoproject.com/svn/django/trunk@14956 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/za/forms.py | 8 +-- .../forms/localflavor/__init__.py | 1 - tests/regressiontests/forms/localflavor/za.py | 63 ++++++++----------- .../regressiontests/forms/localflavortests.py | 4 +- tests/regressiontests/forms/tests/__init__.py | 1 + 5 files changed, 32 insertions(+), 45 deletions(-) diff --git a/django/contrib/localflavor/za/forms.py b/django/contrib/localflavor/za/forms.py index 9a54f1ecb271f..4fb4203e455a8 100644 --- a/django/contrib/localflavor/za/forms.py +++ b/django/contrib/localflavor/za/forms.py @@ -22,14 +22,14 @@ class ZAIDField(Field): } def clean(self, value): - # strip spaces and dashes - value = value.strip().replace(' ', '').replace('-', '') - super(ZAIDField, self).clean(value) if value in EMPTY_VALUES: return u'' + # strip spaces and dashes + value = value.strip().replace(' ', '').replace('-', '') + match = re.match(id_re, value) if not match: @@ -57,4 +57,4 @@ class ZAPostCodeField(RegexField): def __init__(self, *args, **kwargs): super(ZAPostCodeField, self).__init__(r'^\d{4}$', - max_length=None, min_length=None) + max_length=None, min_length=None, *args, **kwargs) diff --git a/tests/regressiontests/forms/localflavor/__init__.py b/tests/regressiontests/forms/localflavor/__init__.py index 40a96afc6ff09..e69de29bb2d1d 100644 --- a/tests/regressiontests/forms/localflavor/__init__.py +++ b/tests/regressiontests/forms/localflavor/__init__.py @@ -1 +0,0 @@ -# -*- coding: utf-8 -*- diff --git a/tests/regressiontests/forms/localflavor/za.py b/tests/regressiontests/forms/localflavor/za.py index a948964b8d45d..c9124212202fe 100644 --- a/tests/regressiontests/forms/localflavor/za.py +++ b/tests/regressiontests/forms/localflavor/za.py @@ -1,40 +1,29 @@ -tests = r""" -# ZAIDField ################################################################# +from django.contrib.localflavor.za.forms import ZAIDField, ZAPostCodeField -ZAIDField validates that the date is a valid birthdate and that the value -has a valid checksum. It allows spaces and dashes, and returns a plain -string of digits. ->>> from django.contrib.localflavor.za.forms import ZAIDField ->>> f = ZAIDField() ->>> f.clean('0002290001003') -'0002290001003' ->>> f.clean('000229 0001 003') -'0002290001003' ->>> f.clean('0102290001001') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid South African ID number'] ->>> f.clean('811208') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid South African ID number'] ->>> f.clean('0002290001004') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid South African ID number'] +from utils import LocalFlavorTestCase -# ZAPostCodeField ########################################################### ->>> from django.contrib.localflavor.za.forms import ZAPostCodeField ->>> f = ZAPostCodeField() ->>> f.clean('abcd') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid South African postal code'] ->>> f.clean('0000') -u'0000' ->>> f.clean(' 7530') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid South African postal code'] -""" +class ZALocalFlavorTests(LocalFlavorTestCase): + def test_ZAIDField(self): + error_invalid = [u'Enter a valid South African ID number'] + valid = { + '0002290001003': '0002290001003', + '000229 0001 003': '0002290001003', + } + invalid = { + '0102290001001': error_invalid, + '811208': error_invalid, + '0002290001004': error_invalid, + } + self.assertFieldOutput(ZAIDField, valid, invalid) + + def test_ZAPostCodeField(self): + error_invalid = [u'Enter a valid South African postal code'] + valid = { + '0000': '0000', + } + invalid = { + 'abcd': error_invalid, + ' 7530': error_invalid, + } + self.assertFieldOutput(ZAPostCodeField, valid, invalid) diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index 10ddf9abc5691..66059383d3996 100644 --- a/tests/regressiontests/forms/localflavortests.py +++ b/tests/regressiontests/forms/localflavortests.py @@ -1,7 +1,5 @@ -# -*- coding: utf-8 -*- from localflavor.cz import tests as localflavor_cz_tests from localflavor.se import tests as localflavor_se_tests -from localflavor.za import tests as localflavor_za_tests from localflavor.ar import ARLocalFlavorTests from localflavor.at import ATLocalFlavorTests @@ -32,10 +30,10 @@ from localflavor.uk import UKLocalFlavorTests from localflavor.us import USLocalFlavorTests from localflavor.uy import UYLocalFlavorTests +from localflavor.za import ZALocalFlavorTests __test__ = { 'localflavor_cz_tests': localflavor_cz_tests, 'localflavor_se_tests': localflavor_se_tests, - 'localflavor_za_tests': localflavor_za_tests, } diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index 2cc725b4e0eb7..ba34c164f3fce 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -42,4 +42,5 @@ UKLocalFlavorTests, USLocalFlavorTests, UYLocalFlavorTests, + ZALocalFlavorTests, )