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.

173 lines
7.7 KiB

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