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.

189 lines
8.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  4. from openerp.exceptions import AccessError, ValidationError
  5. from openerp.tests.common import TransactionCase
  6. class PartnerCase(TransactionCase):
  7. def setUp(self, *args, **kwargs):
  8. super(PartnerCase, self).setUp(*args, **kwargs)
  9. self.agrolait = self.env.ref("base.res_partner_2")
  10. self.tpl = self.env.ref("base_custom_info.tpl_smart")
  11. self.demouser = self.env.ref("base.user_demo")
  12. def set_custom_info_for_agrolait(self):
  13. """Used when you need to use some created custom info."""
  14. self.agrolait.custom_info_template_id = self.tpl
  15. self.agrolait.get_custom_info_value(
  16. self.env.ref("base_custom_info.prop_haters")).value_int = 5
  17. def test_access_granted(self):
  18. """Access to the model implies access to custom info."""
  19. # Demo user has contact creation permissions by default
  20. agrolait = self.agrolait.sudo(self.demouser)
  21. agrolait.custom_info_template_id = self.tpl
  22. prop_weaknesses = agrolait.env.ref("base_custom_info.prop_weaknesses")
  23. val_weaknesses = agrolait.get_custom_info_value(prop_weaknesses)
  24. opt_food = agrolait.env.ref("base_custom_info.opt_food")
  25. val_weaknesses.value_id = opt_food
  26. agrolait.custom_info_template_id.name = "Changed template name"
  27. opt_food.name = "Changed option name"
  28. prop_weaknesses.name = "Changed property name"
  29. def test_access_denied(self):
  30. """Forbidden access to the model forbids it to custom info."""
  31. # Remove permissions to demo user
  32. self.demouser.groups_id = self.env.ref("base.group_portal")
  33. agrolait = self.agrolait.sudo(self.demouser)
  34. with self.assertRaises(AccessError):
  35. agrolait.custom_info_template_id = self.tpl
  36. with self.assertRaises(AccessError):
  37. agrolait.env["custom.info.value"].create({
  38. "res_id": agrolait.id,
  39. "property_id":
  40. agrolait.env.ref("base_custom_info.prop_weaknesses").id,
  41. "value_id": agrolait.env.ref("base_custom_info.opt_food").id,
  42. })
  43. with self.assertRaises(AccessError):
  44. agrolait.custom_info_template_id.property_ids[0].name = "Changed!"
  45. with self.assertRaises(AccessError):
  46. agrolait.env.ref("base_custom_info.opt_food").name = "Changed!"
  47. def test_apply_unapply_template(self):
  48. """(Un)apply a template to a owner and it gets filled."""
  49. # Applying a template autofills the values
  50. self.agrolait.custom_info_template_id = self.tpl
  51. self.assertEqual(
  52. len(self.agrolait.custom_info_ids),
  53. len(self.tpl.property_ids))
  54. self.assertEqual(
  55. self.agrolait.custom_info_ids.mapped("property_id"),
  56. self.tpl.property_ids)
  57. # Unapplying a template empties the values
  58. self.agrolait.custom_info_template_id = False
  59. self.assertFalse(self.agrolait.custom_info_template_id)
  60. self.assertFalse(self.agrolait.custom_info_ids)
  61. def test_template_model_and_model_id_match(self):
  62. """Template's model and model_id fields match."""
  63. self.assertEqual(self.tpl.model, self.tpl.model_id.model)
  64. self.tpl.model = "res.users"
  65. self.assertEqual(self.tpl.model, self.tpl.model_id.model)
  66. def test_template_model_must_exist(self):
  67. """Cannot create templates for unexisting models."""
  68. with self.assertRaises(ValidationError):
  69. self.tpl.model = "yabadabaduu"
  70. def test_change_used_model_fails(self):
  71. """If a template's model is already used, you cannot change it."""
  72. self.set_custom_info_for_agrolait()
  73. with self.assertRaises(ValidationError):
  74. self.tpl.model = "res.users"
  75. def test_owners_selection(self):
  76. """Owners selection includes only the required matches."""
  77. choices = dict(self.env["custom.info.value"]._selection_owner_id())
  78. self.assertIn("res.partner", choices)
  79. self.assertNotIn("ir.model", choices)
  80. self.assertNotIn("custom.info.property", choices)
  81. self.assertNotIn("custom.info", choices)
  82. def test_owner_id(self):
  83. """Check the computed owner id for a value."""
  84. self.set_custom_info_for_agrolait()
  85. self.assertEqual(
  86. self.agrolait.mapped("custom_info_ids.owner_id"), self.agrolait)
  87. def test_get_custom_info_value(self):
  88. """Check the custom info getter helper works fine."""
  89. self.set_custom_info_for_agrolait()
  90. result = self.agrolait.get_custom_info_value(
  91. self.env.ref("base_custom_info.prop_haters"))
  92. self.assertEqual(result.field_type, "int")
  93. self.assertEqual(result.field_name, "value_int")
  94. self.assertEqual(result[result.field_name], 5)
  95. self.assertEqual(result.value_int, 5)
  96. self.assertEqual(result.value, "5")
  97. def test_default_values(self):
  98. """Default values get applied."""
  99. self.agrolait.custom_info_template_id = self.tpl
  100. val_weaknesses = self.agrolait.get_custom_info_value(
  101. self.env.ref("base_custom_info.prop_weaknesses"))
  102. opt_glasses = self.env.ref("base_custom_info.opt_glasses")
  103. self.assertEqual(val_weaknesses.value_id, opt_glasses)
  104. self.assertEqual(val_weaknesses.value, opt_glasses.name)
  105. def test_recursive_templates(self):
  106. """Recursive templates get loaded when required."""
  107. self.set_custom_info_for_agrolait()
  108. prop_weaknesses = self.env.ref("base_custom_info.prop_weaknesses")
  109. val_weaknesses = self.agrolait.get_custom_info_value(prop_weaknesses)
  110. val_weaknesses.value = "Needs videogames"
  111. tpl_gamer = self.env.ref("base_custom_info.tpl_gamer")
  112. self.agrolait.invalidate_cache()
  113. self.assertIn(tpl_gamer, self.agrolait.all_custom_info_templates())
  114. self.assertTrue(
  115. tpl_gamer.property_ids <
  116. self.agrolait.mapped("custom_info_ids.property_id"))
  117. cat_gaming = self.env.ref("base_custom_info.cat_gaming")
  118. self.assertIn(
  119. cat_gaming, self.agrolait.mapped("custom_info_ids.category_id"))
  120. def test_long_teacher_name(self):
  121. """Wow, your teacher cannot have such a long name!"""
  122. self.set_custom_info_for_agrolait()
  123. val = self.agrolait.get_custom_info_value(
  124. self.env.ref("base_custom_info.prop_teacher"))
  125. with self.assertRaises(ValidationError):
  126. val.value = (u"Don Walter Antonio José de la Cruz Hëisenberg de "
  127. u"Borbón Westley Jordy López Manuélez")
  128. def test_low_average_note(self):
  129. """Come on, you are supposed to be smart!"""
  130. self.set_custom_info_for_agrolait()
  131. val = self.agrolait.get_custom_info_value(
  132. self.env.ref("base_custom_info.prop_avg_note"))
  133. with self.assertRaises(ValidationError):
  134. val.value = "-1"
  135. def test_high_average_note(self):
  136. """Too smart!"""
  137. self.set_custom_info_for_agrolait()
  138. val = self.agrolait.get_custom_info_value(
  139. self.env.ref("base_custom_info.prop_avg_note"))
  140. with self.assertRaises(ValidationError):
  141. val.value = "11"
  142. def test_dirty_templates_setting_tpl(self):
  143. """If you set a template, it gets dirty."""
  144. with self.env.do_in_onchange():
  145. self.assertFalse(self.agrolait.dirty_templates)
  146. self.agrolait.custom_info_template_id = self.tpl
  147. self.assertTrue(self.agrolait.dirty_templates)
  148. def test_dirty_templates_removing_tpl(self):
  149. """If you remove a template, it gets dirty."""
  150. self.agrolait.custom_info_template_id = self.tpl
  151. with self.env.do_in_onchange():
  152. self.assertFalse(self.agrolait.dirty_templates)
  153. self.agrolait.custom_info_template_id = False
  154. self.assertTrue(self.agrolait.dirty_templates)
  155. def test_dirty_templates_choosing_option(self):
  156. """If you choose an option with an extra template, it gets dirty."""
  157. self.agrolait.custom_info_template_id = self.tpl
  158. with self.env.do_in_onchange():
  159. self.assertFalse(self.agrolait.dirty_templates)
  160. val = self.agrolait.get_custom_info_value(
  161. self.env.ref("base_custom_info.prop_weaknesses"))
  162. val.value_id = self.env.ref("base_custom_info.opt_videogames")
  163. self.assertTrue(self.agrolait.dirty_templates)