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.

107 lines
4.6 KiB

  1. # Copyright 2017 Tecnativa - Vicent Cubells
  2. # Copyright 2020 Tecnativa - João Marques
  3. # Copyright 2020 Tecnativa - Pedro M. Baeza
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo.exceptions import ValidationError
  6. from odoo.tests import common
  7. class TestResPartnerRefUnique(common.SavepointCase):
  8. @classmethod
  9. def setUpClass(cls):
  10. super(TestResPartnerRefUnique, cls).setUpClass()
  11. cls.partner_obj = cls.env["res.partner"]
  12. cls.company_obj = cls.env["res.company"]
  13. # look for possible already duplicated refs for being resilient
  14. cls.partner_obj.search([("ref", "!=", False)]).write({"ref": False})
  15. cls.company = cls.company_obj.create({"name": "Test company"})
  16. cls.env.user.write(
  17. {"company_id": cls.company.id, "company_ids": cls.company.ids}
  18. )
  19. cls.partner1 = cls.partner_obj.create({"name": "Partner1", "company_id": False})
  20. cls.partner2 = cls.partner_obj.create({"name": "Partner2", "company_id": False})
  21. def test_check_ref_company(self):
  22. (self.partner1 + self.partner2).write({"company_id": self.company.id})
  23. # Test that we can create/modify partners with same ref in current situation
  24. self.partner1.ref = "same_ref"
  25. partner = self.partner_obj.create({"name": "other", "ref": "same_ref"})
  26. # Try to activate restriction
  27. with self.assertRaises(ValidationError):
  28. self.company.partner_ref_unique = "all"
  29. # Let the situation without duplicate refs and apply global condition
  30. partner.unlink()
  31. self.company.partner_ref_unique = "all"
  32. with self.assertRaises(ValidationError):
  33. self.partner2.ref = "same_ref"
  34. with self.assertRaises(ValidationError):
  35. self.partner_obj.create(
  36. {"name": "other", "ref": "same_ref", "company_id": self.company.id}
  37. )
  38. # This one should also raise the constraint as the no-company contact
  39. # collapses with the company specific contact
  40. with self.assertRaises(ValidationError):
  41. self.partner_obj.create(
  42. {"name": "other", "ref": "same_ref", "company_id": False}
  43. )
  44. def test_partner1_wo_company_new_partner_w_company(self):
  45. self.company.partner_ref_unique = "all"
  46. self.partner1.write({"company_id": False, "ref": "same_ref"})
  47. with self.assertRaises(ValidationError):
  48. self.partner_obj.create(
  49. {"name": "other", "ref": "same_ref", "company_id": self.company.id}
  50. )
  51. self.partner1.unlink()
  52. def test_partner1_w_company_new_partner_wo_company(self):
  53. self.company.partner_ref_unique = "all"
  54. self.partner1.ref = "same_ref"
  55. with self.assertRaises(ValidationError):
  56. self.partner_obj.create(
  57. {"name": "other", "ref": "same_ref", "company_id": False}
  58. )
  59. self.partner1.unlink()
  60. def test_check_ref_companies(self):
  61. self.company.partner_ref_unique = (
  62. "none" # Ensure no constraint is applied at beginning
  63. )
  64. self.partner1.is_company = True
  65. self.partner2.is_company = True
  66. # Test that we can create/modify company partners
  67. # with same ref in current situation
  68. self.partner1.ref = "same_ref"
  69. partner3 = self.partner_obj.create(
  70. {"name": "Company3", "ref": "same_ref", "is_company": True}
  71. )
  72. # Try to activate restriction
  73. with self.assertRaises(ValidationError):
  74. self.company.partner_ref_unique = "companies"
  75. # Let the situation without duplicate refs and apply global condition
  76. partner3.unlink()
  77. self.company.partner_ref_unique = "companies"
  78. with self.assertRaises(ValidationError):
  79. self.partner2.ref = "same_ref"
  80. with self.assertRaises(ValidationError):
  81. self.partner_obj.create(
  82. {"is_company": True, "name": "other", "ref": "same_ref"}
  83. )
  84. # Here there shouldn't be any problem
  85. self.partner_obj.create(
  86. {"is_company": False, "name": "other", "ref": "same_ref"}
  87. )
  88. def test_merge(self):
  89. self.company.partner_ref_unique = "all"
  90. self.partner1.ref = "same_ref"
  91. wizard = self.env["base.partner.merge.automatic.wizard"].create(
  92. {
  93. "partner_ids": [(4, self.partner1.id), (4, self.partner2.id)],
  94. "dst_partner_id": self.partner2.id,
  95. "state": "selection",
  96. }
  97. )
  98. # this shouldn't raise error
  99. wizard.action_merge()