test_phonenumbers_patch.py 1.9 KB

123456789101112131415161718192021222324252627282930313233
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. try:
  4. import phonenumbers
  5. except ImportError:
  6. phonenumbers = None
  7. from odoo.tests.common import BaseCase
  8. from odoo.tools.parse_version import parse_version
  9. from odoo.addons.phone_validation.lib import phonenumbers_patch
  10. class TestPhonenumbersPatch(BaseCase):
  11. def test_region_CI_monkey_patch(self):
  12. """Test if the patch is apply on the good version of the lib
  13. And test some phonenumbers"""
  14. if not phonenumbers:
  15. self.skipTest('Cannot test without phonenumbers module installed.')
  16. # MONKEY PATCHING phonemetadata of Ivory Coast if phonenumbers is too old
  17. if parse_version('7.6.1') <= parse_version(phonenumbers.__version__) < parse_version('8.12.32'):
  18. # check that _local_load_region is set to `odoo.addons.phone_validation.lib.phonenumbers_patch._local_load_region`
  19. # check that you can load a new ivory coast phone number without error
  20. parsed_phonenumber_1 = phonenumbers.parse("20 25/35-51 ", region="CI", keep_raw_input=True)
  21. self.assertEqual(parsed_phonenumber_1.national_number, 20253551, "The national part of the phonenumber should be 22522586")
  22. self.assertEqual(parsed_phonenumber_1.country_code, 225, "The country code of Ivory Coast is 225")
  23. parsed_phonenumber_2 = phonenumbers.parse("+225 22 52 25 86 ", region="CI", keep_raw_input=True)
  24. self.assertEqual(parsed_phonenumber_2.national_number, 22522586, "The national part of the phonenumber should be 22522586")
  25. self.assertEqual(parsed_phonenumber_2.country_code, 225, "The country code of Ivory Coast is 225")
  26. else:
  27. self.assertFalse(hasattr(phonenumbers_patch, '_local_load_region'),
  28. "The code should not be monkey patched with phonenumbers > 8.12.32.")