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.

281 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. # Check unlinking record sets with both derived relation records
  155. self.assertTrue(self.relation_all_model.search([]).unlink())
  156. def test_on_change_type_selection(self):
  157. """Test on_change_type_selection."""
  158. # 1. Test call with empty relation
  159. relation_empty = self._get_empty_relation()
  160. result = relation_empty.onchange_type_selection_id()
  161. self.assertTrue('domain' in result)
  162. self.assertFalse('warning' in result)
  163. self.assertTrue('this_partner_id' in result['domain'])
  164. self.assertFalse(result['domain']['this_partner_id'])
  165. self.assertTrue('other_partner_id' in result['domain'])
  166. self.assertFalse(result['domain']['other_partner_id'])
  167. # 2. Test call with company 2 person relation
  168. relation = self._create_company2person_relation()
  169. domain = relation.onchange_type_selection_id()['domain']
  170. self.assertTrue(
  171. ('is_company', '=', False) in domain['other_partner_id'])
  172. # 3. Test with relation needing categories,
  173. # take active partner from active_id:
  174. relation_ngo_volunteer = self.relation_all_model.with_context(
  175. active_id=self.partner_03_ngo.id).create({
  176. 'type_selection_id': self.selection_ngo2volunteer.id,
  177. 'other_partner_id': self.partner_04_volunteer.id})
  178. domain = relation_ngo_volunteer.onchange_type_selection_id()['domain']
  179. self.assertTrue(
  180. ('category_id', 'in', [self.category_01_ngo.id]) in
  181. domain['this_partner_id'])
  182. self.assertTrue(
  183. ('category_id', 'in', [self.category_02_volunteer.id]) in
  184. domain['other_partner_id'])
  185. # 4. Test with invalid or impossible combinations
  186. relation_nobody = self._get_empty_relation()
  187. with self.env.do_in_draft():
  188. relation_nobody.type_selection_id = self.selection_nobody
  189. warning = relation_nobody.onchange_type_selection_id()['warning']
  190. self.assertTrue('message' in warning)
  191. self.assertTrue('No this partner available' in warning['message'])
  192. with self.env.do_in_draft():
  193. relation_nobody.this_partner_id = self.partner_02_company
  194. warning = relation_nobody.onchange_type_selection_id()['warning']
  195. self.assertTrue('message' in warning)
  196. self.assertTrue('incompatible' in warning['message'])
  197. # Allow left partner and check message for other partner:
  198. self.type_nobody.write({'partner_category_left': False})
  199. self.selection_nobody.invalidate_cache(ids=self.selection_nobody.ids)
  200. warning = relation_nobody.onchange_type_selection_id()['warning']
  201. self.assertTrue('message' in warning)
  202. self.assertTrue('No other partner available' in warning['message'])
  203. def test_on_change_partner_id(self):
  204. """Test on_change_partner_id."""
  205. # 1. Test call with empty relation
  206. relation_empty = self._get_empty_relation()
  207. result = relation_empty.onchange_partner_id()
  208. self.assertTrue('domain' in result)
  209. self.assertFalse('warning' in result)
  210. self.assertTrue('type_selection_id' in result['domain'])
  211. self.assertFalse(result['domain']['type_selection_id'])
  212. # 2. Test call with company 2 person relation
  213. relation = self._create_company2person_relation()
  214. domain = relation.onchange_partner_id()['domain']
  215. self.assertTrue(
  216. ('contact_type_this', '=', 'c') in domain['type_selection_id'])
  217. # 3. Test with invalid or impossible combinations
  218. relation_nobody = self._get_empty_relation()
  219. with self.env.do_in_draft():
  220. relation_nobody.this_partner_id = self.partner_02_company
  221. relation_nobody.type_selection_id = self.selection_nobody
  222. warning = relation_nobody.onchange_partner_id()['warning']
  223. self.assertTrue('message' in warning)
  224. self.assertTrue('incompatible' in warning['message'])
  225. def test_write(self):
  226. """Test write. Special attention for changing type."""
  227. relation_company2person = self._create_company2person_relation()
  228. company_partner = relation_company2person.this_partner_id
  229. # First get another worker:
  230. partner_extra_person = self.partner_model.create({
  231. 'name': 'A new worker',
  232. 'is_company': False,
  233. 'ref': 'NW01'})
  234. relation_company2person.write({
  235. 'other_partner_id': partner_extra_person.id})
  236. self.assertEqual(
  237. relation_company2person.other_partner_id.name,
  238. partner_extra_person.name)
  239. # We will also change to a type going from person to company:
  240. (type_worker2company,
  241. selection_worker2company,
  242. selection_company2worker) = self._create_relation_type_selection({
  243. 'name': 'works for',
  244. 'name_inverse': 'has worker',
  245. 'contact_type_left': 'p',
  246. 'contact_type_right': 'c'})
  247. relation_company2person.write({
  248. 'this_partner_id': partner_extra_person.id,
  249. 'type_selection_id': selection_worker2company.id,
  250. 'other_partner_id': company_partner.id})
  251. self.assertEqual(
  252. relation_company2person.this_partner_id.id,
  253. partner_extra_person.id)
  254. self.assertEqual(
  255. relation_company2person.type_selection_id.id,
  256. selection_worker2company.id)
  257. self.assertEqual(
  258. relation_company2person.other_partner_id.id,
  259. company_partner.id)