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.

113 lines
4.3 KiB

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