from django.test import TestCase from forms import MKPersonForm class MKLocalflavorTests(TestCase): def setUp(self): self.form = MKPersonForm({ 'first_name':'Someone', 'last_name':'Something', 'umcn': '2402983450006', 'municipality':'OD', 'municipality_req':'VE', 'id_number':'A1234567', }) def test_get_display_methods(self): """ Test that the get_*_display() methods are added to the model instances. """ person = self.form.save() self.assertEqual(person.get_municipality_display(), 'Ohrid') self.assertEqual(person.get_municipality_req_display(), 'Veles') def test_municipality_required(self): """ Test that required MKMunicipalityFields throw appropriate errors. """ form = MKPersonForm({ 'first_name':'Someone', 'last_name':'Something', 'umcn': '2402983450006', 'municipality':'OD', 'id_number':'A1234567', }) self.assertFalse(form.is_valid()) self.assertEqual( form.errors['municipality_req'], [u'This field is required.']) def test_umcn_invalid(self): """ Test that UMCNFields throw appropriate errors for invalid UMCNs. """ form = MKPersonForm({ 'first_name':'Someone', 'last_name':'Something', 'umcn': '2402983450007', 'municipality':'OD', 'municipality_req':'VE', 'id_number':'A1234567', }) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['umcn'], [u'The UMCN is not valid.']) form = MKPersonForm({ 'first_name':'Someone', 'last_name':'Something', 'umcn': '3002983450007', 'municipality':'OD', 'municipality_req':'VE', 'id_number':'A1234567', }) self.assertEqual(form.errors['umcn'], [u'The first 7 digits of the UMCN must represent a valid past date.']) def test_idnumber_invalid(self): """ Test that MKIdentityCardNumberFields throw appropriate errors for invalid values """ form = MKPersonForm({ 'first_name':'Someone', 'last_name':'Something', 'umcn': '2402983450007', 'municipality':'OD', 'municipality_req':'VE', 'id_number':'A123456a', }) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['id_number'], [u'Identity card numbers must contain either 4 to 7 ' 'digits or an uppercase letter and 7 digits.']) def test_field_blank_option(self): """ Test that the empty option is there. """ municipality_select_html = """\ """ self.assertEqual(str(self.form['municipality']), municipality_select_html)