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.

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