You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.8 KiB

  1. # Copyright 2017 David Vidal <david.vidal@tecnativa.com>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo.tests import common
  4. from odoo.exceptions import UserError
  5. class TestBaseLocationNuts(common.SavepointCase):
  6. @classmethod
  7. def setUpClass(cls):
  8. super(TestBaseLocationNuts, cls).setUpClass()
  9. cls.importer = cls.env['nuts.import']
  10. cls.importer.run_import() # loads nuts
  11. cls.country_1 = cls.env['res.country'].search([('code', '=', 'ES')])
  12. cls.country_2 = cls.env['res.country'].search([('code', '=', 'PT')])
  13. cls.nuts1_2 = cls.env[
  14. 'res.partner.nuts'].search([('code', '=', 'PT')])
  15. cls.nuts2_1 = cls.env[
  16. 'res.partner.nuts'].search([('code', '=', 'ES2')])
  17. cls.nuts3_1 = cls.env[
  18. 'res.partner.nuts'].search([('code', '=', 'ES24')])
  19. cls.nuts4_1 = cls.env[
  20. 'res.partner.nuts'].search([('code', '=', 'ES243')])
  21. cls.nuts4_2 = cls.env[
  22. 'res.partner.nuts'].search([('code', '=', 'ES300')])
  23. cls.partner = cls.env['res.partner'].create({
  24. 'name': 'Test partner',
  25. 'country_id': cls.country_1.id,
  26. })
  27. cls.state_1 = cls.env['res.country.state'].create({
  28. 'name': 'Zaragoza Test',
  29. 'code': 'ZT',
  30. 'country_id': cls.country_1.id,
  31. })
  32. cls.nuts4_1.state_id = cls.state_1
  33. cls.state_2 = cls.env['res.country.state'].create({
  34. 'name': 'Madrid Test',
  35. 'code': 'MT',
  36. 'country_id': cls.country_1.id,
  37. })
  38. cls.nuts4_2.state_id = cls.state_2
  39. cls.country_1.state_level = 4
  40. def test_onchange_nuts_country(self):
  41. self.partner.nuts1_id = self.nuts1_2
  42. self.partner._onchange_nuts1_id()
  43. self.assertEqual(self.partner.country_id, self.nuts1_2.country_id)
  44. def test_onchange_nuts(self):
  45. self.partner.country_id = self.country_2
  46. self.partner._onchange_country_id()
  47. self.assertEqual(self.partner.nuts1_id.country_id,
  48. self.partner.country_id)
  49. self.partner.nuts4_id = self.nuts4_1
  50. self.partner._onchange_nuts4_id()
  51. self.assertEqual(self.partner.country_id,
  52. self.country_1)
  53. self.assertEqual(self.partner.nuts3_id, self.nuts3_1)
  54. self.partner._onchange_nuts3_id()
  55. self.assertEqual(self.partner.nuts2_id, self.nuts2_1)
  56. self.partner._onchange_nuts2_id()
  57. self.assertEqual(self.partner.nuts1_id.country_id, self.country_1)
  58. self.partner.country_id = self.country_2
  59. self.partner._onchange_country_id()
  60. self.assertEqual(self.partner.country_id, self.nuts1_2.country_id)
  61. self.assertFalse(self.partner.nuts2_id)
  62. self.assertFalse(self.partner.nuts3_id)
  63. self.assertFalse(self.partner.nuts4_id)
  64. def test_onchange_states(self):
  65. self.partner.state_id = self.state_2
  66. self.partner.onchange_state_id_base_location_nuts()
  67. self.assertEqual(self.state_2, self.partner.nuts4_id.state_id)
  68. self.partner.state_id = self.state_1
  69. self.partner.onchange_state_id_base_location_nuts()
  70. self.assertEqual(self.state_1, self.partner.nuts4_id.state_id)
  71. self.partner._onchange_nuts4_id()
  72. self.assertEqual(self.partner.nuts3_id, self.nuts3_1)
  73. self.partner._onchange_nuts3_id()
  74. self.assertEqual(self.partner.nuts2_id, self.nuts2_1)
  75. self.partner._onchange_nuts2_id()
  76. self.assertEqual(self.partner.nuts1_id.country_id, self.country_1)
  77. def test_download_exceptions(self):
  78. """ Tests download exceptions """
  79. with self.assertRaises(UserError):
  80. self.importer._download_nuts(url_base='htttt://test.com')
  81. with self.assertRaises(UserError):
  82. self.importer._download_nuts(url_base='http://ec.europa.eu/_404')