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.

131 lines
5.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 David Vidal <david.vidal@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo.tests import common
  5. from odoo.exceptions import UserError
  6. class TestBaseLocationNuts(common.SavepointCase):
  7. @classmethod
  8. def setUpClass(cls):
  9. super(TestBaseLocationNuts, cls).setUpClass()
  10. cls.importer = cls.env['nuts.import']
  11. cls.importer.run_import() # loads nuts
  12. cls.country_1 = cls.env['res.country'].search([('code', '=', 'ES')])
  13. cls.country_2 = cls.env['res.country'].search([('code', '=', 'PT')])
  14. cls.nuts_model = cls.env['res.partner.nuts']
  15. cls.nuts1_2 = cls.nuts_model.search([('code', '=', 'PT')])
  16. cls.nuts2_1 = cls.nuts_model.search([('code', '=', 'ES2')])
  17. cls.nuts3_1 = cls.nuts_model.search([('code', '=', 'ES24')])
  18. cls.nuts4_1 = cls.nuts_model.search([('code', '=', 'ES243')])
  19. cls.nuts4_2 = cls.nuts_model.search([('code', '=', 'ES300')])
  20. cls.partner = cls.env['res.partner'].create({
  21. 'name': 'Test partner',
  22. 'country_id': cls.country_1.id,
  23. })
  24. cls.state_1 = cls.env['res.country.state'].create({
  25. 'name': 'Zaragoza Test',
  26. 'code': 'ZT',
  27. 'country_id': cls.country_1.id,
  28. })
  29. cls.nuts4_1.state_id = cls.state_1
  30. cls.state_2 = cls.env['res.country.state'].create({
  31. 'name': 'Madrid Test',
  32. 'code': 'MT',
  33. 'country_id': cls.country_1.id,
  34. })
  35. cls.nuts4_2.state_id = cls.state_2
  36. cls.country_1.state_level = 4
  37. def test_onchange_nuts_country(self):
  38. self.partner.nuts1_id = self.nuts1_2
  39. self.partner._onchange_nuts1_id()
  40. self.assertEqual(self.partner.country_id, self.nuts1_2.country_id)
  41. def test_onchange_nuts(self):
  42. self.partner.country_id = self.country_2
  43. self.partner._onchange_country_id()
  44. self.assertEqual(self.partner.nuts1_id.country_id,
  45. self.partner.country_id)
  46. self.partner.nuts4_id = self.nuts4_1
  47. self.partner._onchange_nuts4_id()
  48. self.assertEqual(self.partner.country_id,
  49. self.country_1)
  50. self.assertEqual(self.partner.nuts3_id, self.nuts3_1)
  51. self.partner._onchange_nuts3_id()
  52. self.assertEqual(self.partner.nuts2_id, self.nuts2_1)
  53. self.partner._onchange_nuts2_id()
  54. self.assertEqual(self.partner.nuts1_id.country_id, self.country_1)
  55. self.partner.country_id = self.country_2
  56. self.partner._onchange_country_id()
  57. self.assertEqual(self.partner.country_id, self.nuts1_2.country_id)
  58. self.assertFalse(self.partner.nuts2_id)
  59. self.assertFalse(self.partner.nuts3_id)
  60. self.assertFalse(self.partner.nuts4_id)
  61. def test_onchange_states(self):
  62. self.partner.state_id = self.state_2
  63. self.partner.onchange_state_id_base_location_nuts()
  64. self.assertEqual(self.state_2, self.partner.nuts4_id.state_id)
  65. self.partner.state_id = self.state_1
  66. self.partner.onchange_state_id_base_location_nuts()
  67. self.assertEqual(self.state_1, self.partner.nuts4_id.state_id)
  68. self.partner._onchange_nuts4_id()
  69. self.assertEqual(self.partner.nuts3_id, self.nuts3_1)
  70. self.partner._onchange_nuts3_id()
  71. self.assertEqual(self.partner.nuts2_id, self.nuts2_1)
  72. self.partner._onchange_nuts2_id()
  73. self.assertEqual(self.partner.nuts1_id.country_id, self.country_1)
  74. def test_download_exceptions(self):
  75. """ Tests download exceptions """
  76. with self.assertRaises(UserError):
  77. self.importer._download_nuts(url_base='htttt://test.com')
  78. with self.assertRaises(UserError):
  79. self.importer._download_nuts(url_base='http://ec.europa.eu/_404')
  80. def create_new_parent(self, orig_parent):
  81. new_parent = self.nuts_model.create({
  82. 'level': orig_parent.level,
  83. 'code': 'NEW' + orig_parent.code,
  84. 'name': 'New parent',
  85. 'country_id': orig_parent.country_id.id,
  86. 'not_updatable': False
  87. })
  88. return new_parent
  89. def test_no_update(self):
  90. # Update a NUTS field
  91. orig_name = self.nuts4_2.name
  92. new_name = 2 * orig_name
  93. self.assertNotEqual(orig_name, new_name)
  94. # Update hierarchy creating a new parent
  95. orig_parent = self.nuts4_2.parent_id
  96. new_parent = self.create_new_parent(orig_parent)
  97. self.assertNotEqual(orig_parent, new_parent)
  98. # If the flag is False (default), updates will be overwritten
  99. # and the new parent deleted
  100. self.assertFalse(self.nuts4_2.not_updatable)
  101. self.assertFalse(new_parent.not_updatable)
  102. self.nuts4_2.name = new_name
  103. self.nuts4_2.parent_id = new_parent
  104. self.importer.run_import()
  105. self.assertEqual(self.nuts4_2.name, orig_name)
  106. self.assertEqual(self.nuts4_2.parent_id, orig_parent)
  107. self.assertFalse(new_parent.exists())
  108. # New parent has been deleted by the import
  109. new_parent = self.create_new_parent(orig_parent)
  110. # If the flag is True, creation and updates will not be overwritten
  111. self.nuts4_2.not_updatable = True
  112. new_parent.not_updatable = True
  113. self.nuts4_2.name = new_name
  114. self.nuts4_2.parent_id = new_parent
  115. self.importer.run_import()
  116. self.assertEqual(self.nuts4_2.name, new_name)
  117. self.assertEqual(self.nuts4_2.parent_id, new_parent)
  118. self.assertTrue(new_parent.exists())