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.

106 lines
4.2 KiB

  1. # Copyright 2016 Therp BV
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from odoo.tests import common
  4. class TestPartnerRelationCommon(common.TransactionCase):
  5. def setUp(self):
  6. super(TestPartnerRelationCommon, self).setUp()
  7. self.partner_model = self.env["res.partner"]
  8. self.category_model = self.env["res.partner.category"]
  9. self.type_model = self.env["res.partner.relation.type"]
  10. self.selection_model = self.env["res.partner.relation.type.selection"]
  11. self.relation_model = self.env["res.partner.relation"]
  12. self.relation_all_model = self.env["res.partner.relation.all"]
  13. self.partner_01_person = self.partner_model.create(
  14. {"name": "Test User 1", "is_company": False, "ref": "PR01"}
  15. )
  16. self.partner_02_company = self.partner_model.create(
  17. {"name": "Test Company", "is_company": True, "ref": "PR02"}
  18. )
  19. # Create partners with specific categories:
  20. self.category_01_ngo = self.category_model.create({"name": "NGO"})
  21. self.partner_03_ngo = self.partner_model.create(
  22. {
  23. "name": "Test NGO",
  24. "is_company": True,
  25. "ref": "PR03",
  26. "category_id": [(4, self.category_01_ngo.id)],
  27. }
  28. )
  29. self.category_02_volunteer = self.category_model.create({"name": "Volunteer"})
  30. self.partner_04_volunteer = self.partner_model.create(
  31. {
  32. "name": "Test Volunteer",
  33. "is_company": False,
  34. "ref": "PR04",
  35. "category_id": [(4, self.category_02_volunteer.id)],
  36. }
  37. )
  38. # Create a new relation type withouth categories:
  39. (
  40. self.type_company2person,
  41. self.selection_company2person,
  42. self.selection_person2company,
  43. ) = self._create_relation_type_selection(
  44. {
  45. "name": "mixed",
  46. "name_inverse": "mixed_inverse",
  47. "contact_type_left": "c",
  48. "contact_type_right": "p",
  49. }
  50. )
  51. # Create a new relation type with categories:
  52. (
  53. self.type_ngo2volunteer,
  54. self.selection_ngo2volunteer,
  55. self.selection_volunteer2ngo,
  56. ) = self._create_relation_type_selection(
  57. {
  58. "name": "NGO has volunteer",
  59. "name_inverse": "volunteer works for NGO",
  60. "contact_type_left": "c",
  61. "contact_type_right": "p",
  62. "partner_category_left": self.category_01_ngo.id,
  63. "partner_category_right": self.category_02_volunteer.id,
  64. }
  65. )
  66. def _create_relation_type_selection(self, vals):
  67. """Create relation type and return this with selection types."""
  68. assert "name" in vals, (
  69. "Name missing in vals to create relation type. Vals: %s." % vals
  70. )
  71. assert "name" in vals, (
  72. "Name_inverse missing in vals to create relation type. Vals: %s." % vals
  73. )
  74. new_type = self.type_model.create(vals)
  75. self.assertTrue(new_type, msg="No relation type created with vals %s." % vals)
  76. selection_types = self.selection_model.search([("type_id", "=", new_type.id)])
  77. for st in selection_types:
  78. if st.is_inverse:
  79. inverse_type_selection = st
  80. else:
  81. type_selection = st
  82. self.assertTrue(
  83. inverse_type_selection,
  84. msg="Failed to find inverse type selection based on"
  85. " relation type created with vals %s." % vals,
  86. )
  87. self.assertTrue(
  88. type_selection,
  89. msg="Failed to find type selection based on"
  90. " relation type created with vals %s." % vals,
  91. )
  92. return (new_type, type_selection, inverse_type_selection)
  93. def _create_company2person_relation(self):
  94. """Utility function to get a relation from company 2 partner."""
  95. return self.relation_all_model.create(
  96. {
  97. "type_selection_id": self.selection_company2person.id,
  98. "this_partner_id": self.partner_02_company.id,
  99. "other_partner_id": self.partner_01_person.id,
  100. }
  101. )