1 from __future__
import absolute_import
5 from django
.test
import SimpleTestCase
6 from django
.contrib
.localflavor
.au
.forms
import (AUPostCodeField
,
7 AUPhoneNumberField
, AUStateSelect
)
9 from .forms
import AustralianPlaceForm
12 SELECTED_OPTION_PATTERN
= r
'<option value="%s" selected="selected">'
13 BLANK_OPTION_PATTERN
= r
'<option value="">'
14 INPUT_VALUE_PATTERN
= r
'<input[^>]*value="%s"[^>]*>'
17 class AULocalflavorTests(SimpleTestCase
):
20 self
.form
= AustralianPlaceForm(
22 'state_required':'QLD',
25 'postcode_required':'4321',
28 def test_get_display_methods(self
):
29 """ Ensure get_*_display() methods are added to model instances. """
30 place
= self
.form
.save()
31 self
.assertEqual(place
.get_state_display(), 'Western Australia')
32 self
.assertEqual(place
.get_state_required_display(), 'Queensland')
34 def test_default_values(self
):
35 """ Ensure that default values are selected in forms. """
36 form
= AustralianPlaceForm()
37 self
.assertTrue(re
.search(SELECTED_OPTION_PATTERN
% 'NSW',
38 str(form
['state_default'])))
39 self
.assertTrue(re
.search(INPUT_VALUE_PATTERN
% '2500',
40 str(form
['postcode_default'])))
42 def test_required(self
):
43 """ Test that required AUStateFields throw appropriate errors. """
44 form
= AustralianPlaceForm({'state':'NSW', 'name':'Wollongong'})
45 self
.assertFalse(form
.is_valid())
47 form
.errors
['state_required'], [u
'This field is required.'])
49 form
.errors
['postcode_required'], [u
'This field is required.'])
51 def test_field_blank_option(self
):
52 """ Test that the empty option is there. """
53 self
.assertTrue(re
.search(BLANK_OPTION_PATTERN
,
54 str(self
.form
['state'])))
56 def test_selected_values(self
):
57 """ Ensure selected states match the initial values provided. """
58 self
.assertTrue(re
.search(SELECTED_OPTION_PATTERN
% 'WA',
59 str(self
.form
['state'])))
60 self
.assertTrue(re
.search(SELECTED_OPTION_PATTERN
% 'QLD',
61 str(self
.form
['state_required'])))
62 self
.assertTrue(re
.search(INPUT_VALUE_PATTERN
% '1234',
63 str(self
.form
['postcode'])))
64 self
.assertTrue(re
.search(INPUT_VALUE_PATTERN
% '4321',
65 str(self
.form
['postcode_required'])))
67 def test_AUStateSelect(self
):
69 out
= u
'''<select name="state">
70 <option value="ACT">Australian Capital Territory</option>
71 <option value="NSW" selected="selected">New South Wales</option>
72 <option value="NT">Northern Territory</option>
73 <option value="QLD">Queensland</option>
74 <option value="SA">South Australia</option>
75 <option value="TAS">Tasmania</option>
76 <option value="VIC">Victoria</option>
77 <option value="WA">Western Australia</option>
79 self
.assertHTMLEqual(f
.render('state', 'NSW'), out
)
81 def test_AUPostCodeField(self
):
82 error_format
= [u
'Enter a 4 digit postcode.']
89 '20001': [u
'Ensure this value has at most 4 characters (it has 5).'] + error_format
,
91 self
.assertFieldOutput(AUPostCodeField
, valid
, invalid
)
93 def test_AUPhoneNumberField(self
):
94 error_format
= [u
'Phone numbers must contain 10 digits.']
96 '1234567890': '1234567890',
97 '0213456789': '0213456789',
98 '02 13 45 67 89': '0213456789',
99 '(02) 1345 6789': '0213456789',
100 '(02) 1345-6789': '0213456789',
101 '(02)1345-6789': '0213456789',
102 '0408 123 456': '0408123456',
106 '1800DJANGO': error_format
,
108 self
.assertFieldOutput(AUPhoneNumberField
, valid
, invalid
)