| 1 | # -*- coding: utf-8 -*- |
| 2 | # Tests for the contrib/localflavor/ BE form fields. |
| 3 | |
| 4 | from django.test import TestCase |
| 5 | from django.forms import * |
| 6 | |
| 7 | from django.contrib.localflavor.be.forms import BEPostalCodeField, BEPhoneNumberField, BERegionSelect, BEProvinceSelect |
| 8 | |
| 9 | class BETests(TestCase): |
| 10 | """ |
| 11 | Test case to validate BE localflavor |
| 12 | """ |
| 13 | def assertRaisesErrorWithMessage(self, error, message, callable, *args, **kwargs): |
| 14 | self.assertRaises(error, callable, *args, **kwargs) |
| 15 | try: |
| 16 | callable(*args, **kwargs) |
| 17 | except error, e: |
| 18 | self.assertEqual(message, str(e)) |
| 19 | |
| 20 | def test_postal_code_field(self): |
| 21 | f = BEPostalCodeField() |
| 22 | self.assertEqual(u'1451', f.clean('1451')) |
| 23 | self.assertEqual(u'2540', f.clean('2540')) |
| 24 | err_message = "[u'Enter a valid postal code in the range and format 1XXX - 9XXX.']" |
| 25 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0287') |
| 26 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '14309') |
| 27 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '873') |
| 28 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '35 74') |
| 29 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '859A') |
| 30 | err_message = "[u'This field is required.']" |
| 31 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '') |
| 32 | f = BEPostalCodeField(required=False) |
| 33 | self.assertEqual(u'1451', f.clean('1451')) |
| 34 | self.assertEqual(u'2540', f.clean('2540')) |
| 35 | self.assertEqual(u'', f.clean('')) |
| 36 | err_message = "[u'Enter a valid postal code in the range and format 1XXX - 9XXX.']" |
| 37 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0287') |
| 38 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '14309') |
| 39 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '873') |
| 40 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '35 74') |
| 41 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '859A') |
| 42 | |
| 43 | def test_phone_number_field(self): |
| 44 | f = BEPhoneNumberField() |
| 45 | self.assertEqual(u'01 234 56 78', f.clean('01 234 56 78')) |
| 46 | self.assertEqual(u'01/234.56.78', f.clean('01/234.56.78')) |
| 47 | self.assertEqual(u'01.234.56.78', f.clean('01.234.56.78')) |
| 48 | self.assertEqual(u'012 34 56 78', f.clean('012 34 56 78')) |
| 49 | self.assertEqual(u'012/34.56.78', f.clean('012/34.56.78')) |
| 50 | self.assertEqual(u'012.34.56.78', f.clean('012.34.56.78')) |
| 51 | self.assertEqual(u'0412 34 56 78', f.clean('0412 34 56 78')) |
| 52 | self.assertEqual(u'0412/34.56.78', f.clean('0412/34.56.78')) |
| 53 | self.assertEqual(u'0412.34.56.78', f.clean('0412.34.56.78')) |
| 54 | self.assertEqual(u'012345678', f.clean('012345678')) |
| 55 | self.assertEqual(u'0412345678', f.clean('0412345678')) |
| 56 | err_message = "[u'Enter a valid phone number in one of the formats 0x xxx xx xx, 0xx xx xx xx, 04xx xx xx xx, 0x/xxx.xx.xx, 0xx/xx.xx.xx, 04xx/xx.xx.xx, 0xxxxxxxx, 04xxxxxxxx, 0x.xxx.xx.xx, 0xx.xx.xx.xx, 04xx.xx.xx.xx.']" |
| 57 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '01234567') |
| 58 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '12/345.67.89') |
| 59 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345.678.90') |
| 60 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34.56.789') |
| 61 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0123/45.67.89') |
| 62 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345 678 90') |
| 63 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34 56 789') |
| 64 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012.34 56 789') |
| 65 | err_message = "[u'This field is required.']" |
| 66 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '') |
| 67 | f = BEPhoneNumberField(required=False) |
| 68 | self.assertEqual(u'01 234 56 78', f.clean('01 234 56 78')) |
| 69 | self.assertEqual(u'01/234.56.78', f.clean('01/234.56.78')) |
| 70 | self.assertEqual(u'01.234.56.78', f.clean('01.234.56.78')) |
| 71 | self.assertEqual(u'012 34 56 78', f.clean('012 34 56 78')) |
| 72 | self.assertEqual(u'012/34.56.78', f.clean('012/34.56.78')) |
| 73 | self.assertEqual(u'012.34.56.78', f.clean('012.34.56.78')) |
| 74 | self.assertEqual(u'0412 34 56 78', f.clean('0412 34 56 78')) |
| 75 | self.assertEqual(u'0412/34.56.78', f.clean('0412/34.56.78')) |
| 76 | self.assertEqual(u'0412.34.56.78', f.clean('0412.34.56.78')) |
| 77 | self.assertEqual(u'012345678', f.clean('012345678')) |
| 78 | self.assertEqual(u'0412345678', f.clean('0412345678')) |
| 79 | self.assertEqual(u'', f.clean('')) |
| 80 | err_message = "[u'Enter a valid phone number in one of the formats 0x xxx xx xx, 0xx xx xx xx, 04xx xx xx xx, 0x/xxx.xx.xx, 0xx/xx.xx.xx, 04xx/xx.xx.xx, 0xxxxxxxx, 04xxxxxxxx, 0x.xxx.xx.xx, 0xx.xx.xx.xx, 04xx.xx.xx.xx.']" |
| 81 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '01234567') |
| 82 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '12/345.67.89') |
| 83 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345.678.90') |
| 84 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34.56.789') |
| 85 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '0123/45.67.89') |
| 86 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/345 678 90') |
| 87 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012/34 56 789') |
| 88 | self.assertRaisesErrorWithMessage(ValidationError, err_message, f.clean, '012.34 56 789') |
| 89 | |
| 90 | def test_phone_number_field(self): |
| 91 | w = BERegionSelect() |
| 92 | self.assertEqual(u'<select name="regions">\n<option value="BRU">Brussels Capital Region</option>\n<option value="VLG" selected="selected">Flemish Region</option>\n<option value="WAL">Wallonia</option>\n</select>', w.render('regions', 'VLG')) |
| 93 | |
| 94 | def test_phone_number_field(self): |
| 95 | w = BEProvinceSelect() |
| 96 | self.assertEqual(u'<select name="provinces">\n<option value="VAN">Antwerp</option>\n<option value="BRU">Brussels</option>\n<option value="VOV">East Flanders</option>\n<option value="VBR">Flemish Brabant</option>\n<option value="WHT">Hainaut</option>\n<option value="WLG" selected="selected">Liege</option>\n<option value="VLI">Limburg</option>\n<option value="WLX">Luxembourg</option>\n<option value="WNA">Namur</option>\n<option value="WBR">Walloon Brabant</option>\n<option value="VWV">West Flanders</option>\n</select>', w.render('provinces', 'WLG')) |
| 97 | |