Ticket #7686: atlocalflavor2.diff

File atlocalflavor2.diff, 4.1 KB (added by bernd, 16 years ago)

add tests to the old version

  • django/contrib/localflavor/at/forms.py

     
     1"""
     2AT-specific Form helpers
     3"""
     4
     5from django.utils.translation import ugettext_lazy as _
     6from django.newforms.fields import Field, RegexField, Select
     7import re
     8
     9class ATZipCodeField(RegexField):
     10    """
     11    A form field that validates its input is a Austrian postcode.
     12   
     13    Accepts 4 digits
     14    """
     15    default_error_messages = {
     16        'invalid': _('Enter a zip code in the format XXXX.'),
     17    }
     18    def __init__(self, *args, **kwargs):
     19        super(ATZipCodeField, self).__init__(r'^\d{4}$',
     20            max_length=None, min_length=None, *args, **kwargs)
     21
     22class ATStateSelect(Select):
     23    """
     24    A Select widget that uses a list of AT states as its choices.
     25    """
     26    def __init__(self, attrs=None):
     27        from at_states import STATE_CHOICES
     28        super(ATStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
     29 No newline at end of file
  • django/contrib/localflavor/at/at_states.py

     
     1# -*- coding: utf-8 -*
     2from django.utils.translation import ugettext_lazy as _
     3
     4STATE_CHOICES = (
     5    ('BL', _('Burgenland')),
     6    ('KA', _('Carinthia')),
     7    ('NO', _('Lower Austria')),
     8    ('OO', _('Upper Austria')),
     9    ('SA', _('Salzburg')),
     10    ('ST', _('Styria')),
     11    ('TI', _('Tyrol')),
     12    ('VO', _('Vorarlberg')),
     13    ('WI', _('Vienna')),
     14)
     15 No newline at end of file
  • tests/regressiontests/forms/localflavor/at.py

     
     1# -*- coding: utf-8 -*-
     2# Tests for the contrib/localflavor/ AT form fields.
     3
     4tests = r"""
     5# ATZipCodeField ###########################################################
     6
     7>>> from django.contrib.localflavor.at.forms import ATZipCodeField
     8>>> f = ATZipCodeField()
     9>>> f.clean('1150')
     10u'1150'
     11>>> f.clean('4020')
     12u'4020'
     13>>> f.clean('8020')
     14u'8020'
     15>>> f.clean('111222')
     16Traceback (most recent call last):
     17...
     18ValidationError: [u'Enter a zip code in the format XXXX.']
     19>>> f.clean('eeffee')
     20Traceback (most recent call last):
     21...
     22ValidationError: [u'Enter a zip code in the format XXXX.']
     23>>> f.clean(u'')
     24Traceback (most recent call last):
     25...
     26ValidationError: [u'This field is required.']
     27>>> f.clean(None)
     28Traceback (most recent call last):
     29...
     30ValidationError: [u'This field is required.']
     31>>> f.clean('')
     32Traceback (most recent call last):
     33...
     34ValidationError: [u'This field is required.']
     35
     36
     37>>> f = ATZipCodeField(required=False)
     38>>> f.clean('1150')
     39u'1150'
     40>>> f.clean('4020')
     41u'4020'
     42>>> f.clean('8020')
     43u'8020'
     44>>> f.clean('111222')
     45Traceback (most recent call last):
     46...
     47ValidationError: [u'Enter a zip code in the format XXXX.']
     48>>> f.clean('eeffee')
     49Traceback (most recent call last):
     50...
     51ValidationError: [u'Enter a zip code in the format XXXX.']
     52>>> f.clean(None)
     53u''
     54>>> f.clean('')
     55u''
     56>>> f.clean(u'')
     57u''
     58
     59# ATStateSelect ##################################################################
     60
     61>>> from django.contrib.localflavor.at.forms import ATStateSelect
     62>>> f = ATStateSelect()
     63>>> f.render('bundesland', 'WI')
     64u'<select name="bundesland">\n<option value="BL">Burgenland</option>\n<option value="KA">Carinthia</option>\n<option value="NO">Lower Austria</option>\n<option value="OO">Upper Austria</option>\n<option value="SA">Salzburg</option>\n<option value="ST">Styria</option>\n<option value="TI">Tyrol</option>\n<option value="VO">Vorarlberg</option>\n<option value="WI" selected="selected">Vienna</option>\n</select>'
     65
     66"""
Back to Top