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.

279 lines
13 KiB

  1. # Copyright 2016-2017 Therp BV
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from datetime import date
  4. from odoo.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. (self.type_nobody,
  13. self.selection_nobody,
  14. self.selection_nobody_inverse) = (
  15. self._create_relation_type_selection({
  16. 'name': 'has relation with nobody',
  17. 'name_inverse': 'nobody has relation with',
  18. 'contact_type_left': 'c',
  19. 'contact_type_right': 'p',
  20. 'partner_category_left': category_nobody.id,
  21. 'partner_category_right': category_nobody.id}))
  22. def _get_empty_relation(self):
  23. """Get empty relation record for onchange tests."""
  24. # Need English, because we will compare text
  25. return self.relation_all_model.with_context(lang='en_US').new({})
  26. def test_get_partner_types(self):
  27. """Partner types should contain at least 'c' and 'p'."""
  28. partner_types = self.selection_model.get_partner_types()
  29. type_codes = [ptype[0] for ptype in partner_types]
  30. self.assertTrue('c' in type_codes)
  31. self.assertTrue('p' in type_codes)
  32. def test_create_with_active_id(self):
  33. """Test creation with this_partner_id from active_id."""
  34. # Check wether we can create connection from company to person,
  35. # taking the particular company from the active records:
  36. relation = self.relation_all_model.with_context(
  37. active_id=self.partner_02_company.id,
  38. active_ids=self.partner_02_company.ids).create({
  39. 'other_partner_id': self.partner_01_person.id,
  40. 'type_selection_id': self.selection_company2person.id})
  41. self.assertTrue(relation)
  42. self.assertEqual(relation.this_partner_id, self.partner_02_company)
  43. # Partner should have one relation now:
  44. self.assertEqual(self.partner_01_person.relation_count, 1)
  45. # Test create without type_selection_id:
  46. with self.assertRaises(ValidationError):
  47. self.relation_all_model.create({
  48. 'this_partner_id': self.partner_02_company.id,
  49. 'other_partner_id': self.partner_01_person.id})
  50. def test_display_name(self):
  51. """Test display name"""
  52. relation = self._create_company2person_relation()
  53. self.assertEqual(
  54. relation.display_name, '%s %s %s' % (
  55. relation.this_partner_id.name,
  56. relation.type_selection_id.name,
  57. relation.other_partner_id.name))
  58. def test__regular_write(self):
  59. """Test write with valid data."""
  60. relation = self._create_company2person_relation()
  61. relation.write({'date_start': '2014-09-01'})
  62. relation.invalidate_cache(ids=relation.ids)
  63. self.assertEqual(relation.date_start, date(2014, 9, 1))
  64. def test_write_incompatible_dates(self):
  65. """Test write with date_end before date_start."""
  66. relation = self._create_company2person_relation()
  67. with self.assertRaises(ValidationError):
  68. relation.write({
  69. 'date_start': '2016-09-01',
  70. 'date_end': '2016-08-01'})
  71. def test_validate_overlapping_01(self):
  72. """Test create overlapping with no start / end dates."""
  73. relation = self._create_company2person_relation()
  74. with self.assertRaises(ValidationError):
  75. # New relation with no start / end should give error
  76. self.relation_all_model.create({
  77. 'this_partner_id': relation.this_partner_id.id,
  78. 'type_selection_id': relation.type_selection_id.id,
  79. 'other_partner_id': relation.other_partner_id.id})
  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. # New relation with overlapping start / end should give error
  89. with self.assertRaises(ValidationError):
  90. self.relation_all_model.create({
  91. 'this_partner_id': relation.this_partner_id.id,
  92. 'type_selection_id': relation.type_selection_id.id,
  93. 'other_partner_id': relation.other_partner_id.id,
  94. 'date_start': '2016-08-01',
  95. 'date_end': '2017-07-30'})
  96. def test_validate_overlapping_03(self):
  97. """Test create not overlapping."""
  98. relation = self.relation_all_model.create({
  99. 'this_partner_id': self.partner_02_company.id,
  100. 'type_selection_id': self.selection_company2person.id,
  101. 'other_partner_id': self.partner_01_person.id,
  102. 'date_start': '2015-09-01',
  103. 'date_end': '2016-08-31'})
  104. relation_another_record = self.relation_all_model.create({
  105. 'this_partner_id': relation.this_partner_id.id,
  106. 'type_selection_id': relation.type_selection_id.id,
  107. 'other_partner_id': relation.other_partner_id.id,
  108. 'date_start': '2016-09-01',
  109. 'date_end': '2017-08-31'})
  110. self.assertTrue(relation_another_record)
  111. def test_inverse_record(self):
  112. """Test creation of inverse record."""
  113. relation = self._create_company2person_relation()
  114. inverse_relation = self.relation_all_model.search([
  115. ('this_partner_id', '=', relation.other_partner_id.id),
  116. ('other_partner_id', '=', relation.this_partner_id.id)])
  117. self.assertEqual(len(inverse_relation), 1)
  118. self.assertEqual(
  119. inverse_relation.type_selection_id.name,
  120. self.selection_person2company.name)
  121. def test_inverse_creation(self):
  122. """Test creation of record through inverse selection."""
  123. relation = self.relation_all_model.create({
  124. 'this_partner_id': self.partner_01_person.id,
  125. 'type_selection_id': self.selection_person2company.id,
  126. 'other_partner_id': self.partner_02_company.id})
  127. # Check wether display name is what we should expect:
  128. self.assertEqual(
  129. relation.display_name, '%s %s %s' % (
  130. self.partner_01_person.name,
  131. self.selection_person2company.name,
  132. self.partner_02_company.name))
  133. def test_inverse_creation_type_id(self):
  134. """Test creation of record through inverse selection with type_id."""
  135. relation = self.relation_all_model.create({
  136. 'this_partner_id': self.partner_01_person.id,
  137. 'type_id': self.selection_person2company.type_id.id,
  138. 'is_inverse': True,
  139. 'other_partner_id': self.partner_02_company.id})
  140. # Check wether display name is what we should expect:
  141. self.assertEqual(
  142. relation.display_name, '%s %s %s' % (
  143. self.partner_01_person.name,
  144. self.selection_person2company.name,
  145. self.partner_02_company.name))
  146. def test_unlink(self):
  147. """Unlinking derived relation should unlink base relation."""
  148. # Check wether underlying record is removed when record is removed:
  149. relation = self._create_company2person_relation()
  150. base_model = self.env[relation.res_model]
  151. base_relation = base_model.browse([relation.res_id])
  152. relation.unlink()
  153. self.assertFalse(base_relation.exists())
  154. def test_on_change_type_selection(self):
  155. """Test on_change_type_selection."""
  156. # 1. Test call with empty relation
  157. relation_empty = self._get_empty_relation()
  158. result = relation_empty.onchange_type_selection_id()
  159. self.assertTrue('domain' in result)
  160. self.assertFalse('warning' in result)
  161. self.assertTrue('this_partner_id' in result['domain'])
  162. self.assertFalse(result['domain']['this_partner_id'])
  163. self.assertTrue('other_partner_id' in result['domain'])
  164. self.assertFalse(result['domain']['other_partner_id'])
  165. # 2. Test call with company 2 person relation
  166. relation = self._create_company2person_relation()
  167. domain = relation.onchange_type_selection_id()['domain']
  168. self.assertTrue(
  169. ('is_company', '=', False) in domain['other_partner_id'])
  170. # 3. Test with relation needing categories,
  171. # take active partner from active_id:
  172. relation_ngo_volunteer = self.relation_all_model.with_context(
  173. active_id=self.partner_03_ngo.id).create({
  174. 'type_selection_id': self.selection_ngo2volunteer.id,
  175. 'other_partner_id': self.partner_04_volunteer.id})
  176. domain = relation_ngo_volunteer.onchange_type_selection_id()['domain']
  177. self.assertTrue(
  178. ('category_id', 'in', [self.category_01_ngo.id]) in
  179. domain['this_partner_id'])
  180. self.assertTrue(
  181. ('category_id', 'in', [self.category_02_volunteer.id]) in
  182. domain['other_partner_id'])
  183. # 4. Test with invalid or impossible combinations
  184. relation_nobody = self._get_empty_relation()
  185. with self.env.do_in_draft():
  186. relation_nobody.type_selection_id = self.selection_nobody
  187. warning = relation_nobody.onchange_type_selection_id()['warning']
  188. self.assertTrue('message' in warning)
  189. self.assertTrue('No this partner available' in warning['message'])
  190. with self.env.do_in_draft():
  191. relation_nobody.this_partner_id = self.partner_02_company
  192. warning = relation_nobody.onchange_type_selection_id()['warning']
  193. self.assertTrue('message' in warning)
  194. self.assertTrue('incompatible' in warning['message'])
  195. # Allow left partner and check message for other partner:
  196. self.type_nobody.write({'partner_category_left': False})
  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. # 3. Test with invalid or impossible combinations
  216. relation_nobody = self._get_empty_relation()
  217. with self.env.do_in_draft():
  218. relation_nobody.this_partner_id = self.partner_02_company
  219. relation_nobody.type_selection_id = self.selection_nobody
  220. warning = relation_nobody.onchange_partner_id()['warning']
  221. self.assertTrue('message' in warning)
  222. self.assertTrue('incompatible' in warning['message'])
  223. def test_write(self):
  224. """Test write. Special attention for changing type."""
  225. relation_company2person = self._create_company2person_relation()
  226. company_partner = relation_company2person.this_partner_id
  227. # First get another worker:
  228. partner_extra_person = self.partner_model.create({
  229. 'name': 'A new worker',
  230. 'is_company': False,
  231. 'ref': 'NW01'})
  232. relation_company2person.write({
  233. 'other_partner_id': partner_extra_person.id})
  234. self.assertEqual(
  235. relation_company2person.other_partner_id.name,
  236. partner_extra_person.name)
  237. # We will also change to a type going from person to company:
  238. (type_worker2company,
  239. selection_worker2company,
  240. selection_company2worker) = self._create_relation_type_selection({
  241. 'name': 'works for',
  242. 'name_inverse': 'has worker',
  243. 'contact_type_left': 'p',
  244. 'contact_type_right': 'c'})
  245. relation_company2person.write({
  246. 'this_partner_id': partner_extra_person.id,
  247. 'type_selection_id': selection_worker2company.id,
  248. 'other_partner_id': company_partner.id})
  249. self.assertEqual(
  250. relation_company2person.this_partner_id.id,
  251. partner_extra_person.id)
  252. self.assertEqual(
  253. relation_company2person.type_selection_id.id,
  254. selection_worker2company.id)
  255. self.assertEqual(
  256. relation_company2person.other_partner_id.id,
  257. company_partner.id)