from __future__ import absolute_import, unicode_literals from django.contrib.localflavor.mk.forms import ( MKIdentityCardNumberField, MKMunicipalitySelect, UMCNField) from django.test import SimpleTestCase from .forms import MKPersonForm class MKLocalFlavorTests(SimpleTestCase): 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'], ['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'], ['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'], ['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'], ['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 = b"""\ """ self.assertHTMLEqual(str(self.form['municipality']), municipality_select_html) def test_MKIdentityCardNumberField(self): error_invalid = ['Identity card numbers must contain either 4 to 7 ' 'digits or an uppercase letter and 7 digits.'] valid = { 'L0018077':'L0018077', 'A0078315' : 'A0078315', } invalid = { '123': error_invalid, 'abcdf': error_invalid, '234390a': error_invalid, } self.assertFieldOutput(MKIdentityCardNumberField, valid, invalid) def test_MKMunicipalitySelect(self): f = MKMunicipalitySelect() out='''''' self.assertHTMLEqual(f.render('municipality', 'DL' ), out) def test_UMCNField(self): error_invalid = ['This field should contain exactly 13 digits.'] error_checksum = ['The UMCN is not valid.'] error_date = ['The first 7 digits of the UMCN ' 'must represent a valid past date.'] valid = { '2402983450006': '2402983450006', '2803984430038': '2803984430038', '1909982045004': '1909982045004', } invalid = { '240298345': error_invalid, 'abcdefghj': error_invalid, '2402082450006': error_date, '3002982450006': error_date, '2402983450007': error_checksum, '2402982450006': error_checksum, } self.assertFieldOutput(UMCNField, valid, invalid)