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.

125 lines
5.5 KiB

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