from __future__ import absolute_import
from django.contrib.localflavor.us.forms import (USZipCodeField,
USPhoneNumberField, USStateField, USStateSelect,
USSocialSecurityNumberField)
from django.test import SimpleTestCase
from .forms import USPlaceForm
class USLocalFlavorTests(SimpleTestCase):
def setUp(self):
self.form = USPlaceForm({'state':'GA', 'state_req':'NC', 'postal_code': 'GA', 'name':'impossible'})
def test_get_display_methods(self):
"""Test that the get_*_display() methods are added to the model instances."""
place = self.form.save()
self.assertEqual(place.get_state_display(), 'Georgia')
self.assertEqual(place.get_state_req_display(), 'North Carolina')
def test_required(self):
"""Test that required USStateFields throw appropriate errors."""
form = USPlaceForm({'state':'GA', 'name':'Place in GA'})
self.assertFalse(form.is_valid())
self.assertEqual(form.errors['state_req'], [u'This field is required.'])
def test_field_blank_option(self):
"""Test that the empty option is there."""
state_select_html = """\
"""
self.assertEqual(str(self.form['state']), state_select_html)
def test_full_postal_code_list(self):
"""Test that the full USPS code field is really the full list."""
usps_select_html = """\
"""
self.assertEqual(str(self.form['postal_code']), usps_select_html)
def test_USStateSelect(self):
f = USStateSelect()
out = u''''''
self.assertEqual(f.render('state', 'IL'), out)
def test_USZipCodeField(self):
error_format = [u'Enter a zip code in the format XXXXX or XXXXX-XXXX.']
valid = {
'60606': '60606',
60606: '60606',
'04000': '04000',
'60606-1234': '60606-1234',
}
invalid = {
'4000': error_format,
'6060-1234': error_format,
'60606-': error_format,
}
self.assertFieldOutput(USZipCodeField, valid, invalid)
def test_USPhoneNumberField(self):
error_format = [u'Phone numbers must be in XXX-XXX-XXXX format.']
valid = {
'312-555-1212': '312-555-1212',
'3125551212': '312-555-1212',
'312 555-1212': '312-555-1212',
'(312) 555-1212': '312-555-1212',
'312 555 1212': '312-555-1212',
'312.555.1212': '312-555-1212',
'312.555-1212': '312-555-1212',
' (312) 555.1212 ': '312-555-1212',
}
invalid = {
'555-1212': error_format,
'312-55-1212': error_format,
}
self.assertFieldOutput(USPhoneNumberField, valid, invalid)
def test_USStateField(self):
error_invalid = [u'Enter a U.S. state or territory.']
valid = {
'il': 'IL',
'IL': 'IL',
'illinois': 'IL',
' illinois ': 'IL',
}
invalid = {
60606: error_invalid,
}
self.assertFieldOutput(USStateField, valid, invalid)
def test_USSocialSecurityNumberField(self):
error_invalid = [u'Enter a valid U.S. Social Security number in XXX-XX-XXXX format.']
valid = {
'987-65-4330': '987-65-4330',
'987654330': '987-65-4330',
}
invalid = {
'078-05-1120': error_invalid,
}
self.assertFieldOutput(USSocialSecurityNumberField, valid, invalid)