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.

241 lines
10 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.exceptions import ValidationError
  5. from .test_partner_relation_common import TestPartnerRelationCommon
  6. class TestPartnerRelation(TestPartnerRelationCommon):
  7. def setUp(self):
  8. super(TestPartnerRelation, self).setUp()
  9. # Create a new relation type which will not have valid relations:
  10. category_nobody = self.category_model.create({
  11. 'name': 'Nobody',
  12. })
  13. (self.type_nobody,
  14. self.selection_nobody,
  15. self.selection_nobody_inverse) = (
  16. self._create_relation_type_selection({
  17. 'name': 'has relation with nobody',
  18. 'name_inverse': 'nobody has relation with',
  19. 'contact_type_left': 'c',
  20. 'contact_type_right': 'p',
  21. 'partner_category_left': category_nobody.id,
  22. 'partner_category_right': category_nobody.id,
  23. })
  24. )
  25. def _get_empty_relation(self):
  26. """Get empty relation record for onchange tests."""
  27. # Need English, because we will compare text
  28. return self.relation_all_model.with_context(lang='en_US').new({})
  29. def test_create_with_active_id(self):
  30. """Test creation with this_partner_id from active_id."""
  31. # Check wether we can create connection from company to person,
  32. # taking the particular company from the active records:
  33. relation = self.relation_all_model.with_context(
  34. active_id=self.partner_02_company.id,
  35. active_ids=self.partner_02_company.ids,
  36. ).create({
  37. 'other_partner_id': self.partner_01_person.id,
  38. 'type_selection_id': self.selection_company2person.id,
  39. })
  40. self.assertTrue(relation)
  41. self.assertEqual(relation.this_partner_id, self.partner_02_company)
  42. # Partner should have one relation now:
  43. self.assertEqual(self.partner_01_person.relation_count, 1)
  44. def test_display_name(self):
  45. """Test display name"""
  46. relation = self._create_company2person_relation()
  47. self.assertEqual(
  48. relation.display_name, '%s %s %s' % (
  49. relation.this_partner_id.name,
  50. relation.type_selection_id.name,
  51. relation.other_partner_id.name,
  52. )
  53. )
  54. def test__regular_write(self):
  55. """Test write with valid data."""
  56. relation = self._create_company2person_relation()
  57. relation.write({
  58. 'date_start': '2014-09-01',
  59. })
  60. relation.invalidate_cache(ids=relation.ids)
  61. self.assertEqual(relation.date_start, '2014-09-01')
  62. def test_write_incompatible_dates(self):
  63. """Test write with date_end before date_start."""
  64. relation = self._create_company2person_relation()
  65. with self.assertRaises(ValidationError):
  66. relation.write({
  67. 'date_start': '2016-09-01',
  68. 'date_end': '2016-08-01',
  69. })
  70. def test_validate_overlapping_01(self):
  71. """Test create overlapping with no start / end dates."""
  72. relation = self._create_company2person_relation()
  73. with self.assertRaises(ValidationError):
  74. # New relation with no start / end should give error
  75. self.relation_all_model.create({
  76. 'this_partner_id': relation.this_partner_id.id,
  77. 'type_selection_id': relation.type_selection_id.id,
  78. 'other_partner_id': relation.other_partner_id.id,
  79. })
  80. def test_validate_overlapping_02(self):
  81. """Test create overlapping with start / end dates."""
  82. relation = self.relation_all_model.create({
  83. 'this_partner_id': self.partner_02_company.id,
  84. 'type_selection_id': self.selection_company2person.id,
  85. 'other_partner_id': self.partner_01_person.id,
  86. 'date_start': '2015-09-01',
  87. 'date_end': '2016-08-31',
  88. })
  89. # New relation with overlapping start / end should give error
  90. with self.assertRaises(ValidationError):
  91. self.relation_all_model.create({
  92. 'this_partner_id': relation.this_partner_id.id,
  93. 'type_selection_id': relation.type_selection_id.id,
  94. 'other_partner_id': relation.other_partner_id.id,
  95. 'date_start': '2016-08-01',
  96. 'date_end': '2017-07-30',
  97. })
  98. def test_validate_overlapping_03(self):
  99. """Test create not overlapping."""
  100. relation = self.relation_all_model.create({
  101. 'this_partner_id': self.partner_02_company.id,
  102. 'type_selection_id': self.selection_company2person.id,
  103. 'other_partner_id': self.partner_01_person.id,
  104. 'date_start': '2015-09-01',
  105. 'date_end': '2016-08-31',
  106. })
  107. relation_another_record = self.relation_all_model.create({
  108. 'this_partner_id': relation.this_partner_id.id,
  109. 'type_selection_id': relation.type_selection_id.id,
  110. 'other_partner_id': relation.other_partner_id.id,
  111. 'date_start': '2016-09-01',
  112. 'date_end': '2017-08-31',
  113. })
  114. self.assertTrue(relation_another_record)
  115. def test_inverse_record(self):
  116. """Test creation of inverse record."""
  117. relation = self._create_company2person_relation()
  118. inverse_relation = self.relation_all_model.search([
  119. ('this_partner_id', '=', relation.other_partner_id.id),
  120. ('other_partner_id', '=', relation.this_partner_id.id),
  121. ])
  122. self.assertEqual(len(inverse_relation), 1)
  123. self.assertEqual(
  124. inverse_relation.type_selection_id.name,
  125. self.selection_person2company.name
  126. )
  127. def test_inverse_creation(self):
  128. """Test creation of record through inverse selection."""
  129. relation = self.relation_all_model.create({
  130. 'this_partner_id': self.partner_01_person.id,
  131. 'type_selection_id': self.selection_person2company.id,
  132. 'other_partner_id': self.partner_02_company.id,
  133. })
  134. # Check wether display name is what we should expect:
  135. self.assertEqual(
  136. relation.display_name, '%s %s %s' % (
  137. self.partner_01_person.name,
  138. self.selection_person2company.name,
  139. self.partner_02_company.name,
  140. )
  141. )
  142. def test_unlink(self):
  143. """Unlinking derived relation should unlink base relation."""
  144. # Check wether underlying record is removed when record is removed:
  145. relation = self._create_company2person_relation()
  146. base_relation = relation.relation_id
  147. relation.unlink()
  148. self.assertFalse(base_relation.exists())
  149. def test_on_change_type_selection(self):
  150. """Test on_change_type_selection."""
  151. # 1. Test call with empty relation
  152. relation_empty = self._get_empty_relation()
  153. result = relation_empty.onchange_type_selection_id()
  154. self.assertTrue('domain' in result)
  155. self.assertFalse('warning' in result)
  156. self.assertTrue('this_partner_id' in result['domain'])
  157. self.assertFalse(result['domain']['this_partner_id'])
  158. self.assertTrue('other_partner_id' in result['domain'])
  159. self.assertFalse(result['domain']['other_partner_id'])
  160. # 2. Test call with company 2 person relation
  161. relation = self._create_company2person_relation()
  162. domain = relation.onchange_type_selection_id()['domain']
  163. self.assertTrue(
  164. ('is_company', '=', False) in domain['other_partner_id']
  165. )
  166. # 3. Test with relation needing categories.
  167. relation_ngo_volunteer = self.relation_all_model.create({
  168. 'this_partner_id': self.partner_03_ngo.id,
  169. 'type_selection_id': self.selection_ngo2volunteer.id,
  170. 'other_partner_id': self.partner_04_volunteer.id,
  171. })
  172. domain = relation_ngo_volunteer.onchange_type_selection_id()['domain']
  173. self.assertTrue(
  174. ('category_id', 'in', [self.category_01_ngo.id]) in
  175. domain['this_partner_id']
  176. )
  177. self.assertTrue(
  178. ('category_id', 'in', [self.category_02_volunteer.id]) in
  179. domain['other_partner_id']
  180. )
  181. # 4. Test with invalid or impossible combinations
  182. relation_nobody = self._get_empty_relation()
  183. with self.env.do_in_draft():
  184. relation_nobody.type_selection_id = self.selection_nobody
  185. warning = relation_nobody.onchange_type_selection_id()['warning']
  186. self.assertTrue('message' in warning)
  187. self.assertTrue('No this partner available' in warning['message'])
  188. with self.env.do_in_draft():
  189. relation_nobody.this_partner_id = self.partner_02_company
  190. warning = relation_nobody.onchange_type_selection_id()['warning']
  191. self.assertTrue('message' in warning)
  192. self.assertTrue('incompatible' in warning['message'])
  193. # Allow left partner and check message for other partner:
  194. self.type_nobody.write({
  195. 'partner_category_left': False,
  196. })
  197. self.selection_nobody.invalidate_cache(ids=self.selection_nobody.ids)
  198. warning = relation_nobody.onchange_type_selection_id()['warning']
  199. self.assertTrue('message' in warning)
  200. self.assertTrue('No other partner available' in warning['message'])
  201. def test_on_change_partner_id(self):
  202. """Test on_change_partner_id."""
  203. # 1. Test call with empty relation
  204. relation_empty = self._get_empty_relation()
  205. result = relation_empty.onchange_partner_id()
  206. self.assertTrue('domain' in result)
  207. self.assertFalse('warning' in result)
  208. self.assertTrue('type_selection_id' in result['domain'])
  209. self.assertFalse(result['domain']['type_selection_id'])
  210. # 2. Test call with company 2 person relation
  211. relation = self._create_company2person_relation()
  212. domain = relation.onchange_partner_id()['domain']
  213. self.assertTrue(
  214. ('contact_type_this', '=', 'c') in domain['type_selection_id']
  215. )
  216. # 3. Test with invalid or impossible combinations
  217. relation_nobody = self._get_empty_relation()
  218. with self.env.do_in_draft():
  219. relation_nobody.this_partner_id = self.partner_02_company
  220. relation_nobody.type_selection_id = self.selection_nobody
  221. warning = relation_nobody.onchange_partner_id()['warning']
  222. self.assertTrue('message' in warning)
  223. self.assertTrue('incompatible' in warning['message'])