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. # -*- 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 psycopg2 import IntegrityError
  5. from odoo.exceptions import AccessError, ValidationError
  6. from odoo.tests.common import TransactionCase
  7. from odoo.tools import mute_logger
  8. class PartnerCase(TransactionCase):
  9. def setUp(self, *args, **kwargs):
  10. super(PartnerCase, self).setUp(*args, **kwargs)
  11. self.agrolait = self.env.ref("base.res_partner_2")
  12. self.tpl = self.env.ref("base_custom_info.tpl_smart")
  13. self.demouser = self.env.ref("base.user_demo")
  14. def set_custom_info_for_agrolait(self):
  15. """Used when you need to use some created custom info."""
  16. self.agrolait.custom_info_template_id = self.tpl
  17. self.agrolait._onchange_custom_info_template_id()
  18. self.agrolait.get_custom_info_value(
  19. self.env.ref("base_custom_info.prop_haters")).value_int = 5
  20. def test_access_granted(self):
  21. """Access to the model implies access to custom info."""
  22. # Demo user has contact creation permissions by default
  23. agrolait = self.agrolait.sudo(self.demouser)
  24. agrolait.custom_info_template_id = self.tpl
  25. agrolait._onchange_custom_info_template_id()
  26. prop_weaknesses = agrolait.env.ref("base_custom_info.prop_weaknesses")
  27. val_weaknesses = agrolait.get_custom_info_value(prop_weaknesses)
  28. opt_food = agrolait.env.ref("base_custom_info.opt_food")
  29. val_weaknesses.value_id = opt_food
  30. agrolait.custom_info_template_id.name = "Changed template name"
  31. opt_food.name = "Changed option name"
  32. prop_weaknesses.name = "Changed property name"
  33. def test_access_denied(self):
  34. """Forbidden access to the model forbids it to custom info."""
  35. # Remove permissions to demo user
  36. self.demouser.groups_id = self.env.ref("base.group_portal")
  37. agrolait = self.agrolait.sudo(self.demouser)
  38. with self.assertRaises(AccessError):
  39. agrolait.custom_info_template_id = self.tpl
  40. with self.assertRaises(AccessError):
  41. agrolait.env["custom.info.value"].create({
  42. "res_id": agrolait.id,
  43. "property_id":
  44. agrolait.env.ref("base_custom_info.prop_weaknesses").id,
  45. "value_id": agrolait.env.ref("base_custom_info.opt_food").id,
  46. })
  47. with self.assertRaises(AccessError):
  48. agrolait.custom_info_template_id.property_ids[0].name = "Changed!"
  49. with self.assertRaises(AccessError):
  50. agrolait.env.ref("base_custom_info.opt_food").name = "Changed!"
  51. def test_apply_unapply_template(self):
  52. """(Un)apply a template to a owner and it gets filled."""
  53. # Applying a template autofills the values
  54. self.agrolait.custom_info_template_id = self.tpl
  55. self.agrolait._onchange_custom_info_template_id()
  56. self.assertEqual(
  57. len(self.agrolait.custom_info_ids),
  58. len(self.tpl.property_ids))
  59. self.assertEqual(
  60. self.agrolait.custom_info_ids.mapped("property_id"),
  61. self.tpl.property_ids)
  62. # Unapplying a template empties the values
  63. self.agrolait.custom_info_template_id = False
  64. self.agrolait._onchange_custom_info_template_id()
  65. self.assertFalse(self.agrolait.custom_info_template_id)
  66. self.assertFalse(self.agrolait.custom_info_ids)
  67. def test_template_model_and_model_id_match(self):
  68. """Template's model and model_id fields match."""
  69. self.assertEqual(self.tpl.model, self.tpl.model_id.model)
  70. self.tpl.model = "res.users"
  71. self.assertEqual(self.tpl.model, self.tpl.model_id.model)
  72. @mute_logger('odoo.sql_db')
  73. def test_template_model_must_exist(self):
  74. """Cannot create templates for unexisting models."""
  75. with self.assertRaises(IntegrityError):
  76. self.tpl.model = "yabadabaduu"
  77. def test_change_used_model_fails(self):
  78. """If a template's model is already used, you cannot change it."""
  79. self.set_custom_info_for_agrolait()
  80. with self.assertRaises(ValidationError):
  81. self.tpl.model = "res.users"
  82. def test_owners_selection(self):
  83. """Owners selection includes only the required matches."""
  84. choices = dict(self.env["custom.info.value"]._selection_owner_id())
  85. self.assertIn("res.partner", choices)
  86. self.assertNotIn("ir.model", choices)
  87. self.assertNotIn("custom.info.property", choices)
  88. self.assertNotIn("custom.info", choices)
  89. def test_owner_id(self):
  90. """Check the computed owner id for a value."""
  91. self.set_custom_info_for_agrolait()
  92. self.assertEqual(
  93. self.agrolait.mapped("custom_info_ids.owner_id"), self.agrolait)
  94. def test_get_custom_info_value(self):
  95. """Check the custom info getter helper works fine."""
  96. self.set_custom_info_for_agrolait()
  97. result = self.agrolait.get_custom_info_value(
  98. self.env.ref("base_custom_info.prop_haters"))
  99. self.assertEqual(result.field_type, "int")
  100. self.assertEqual(result.field_name, "value_int")
  101. self.assertEqual(result[result.field_name], 5)
  102. self.assertEqual(result.value_int, 5)
  103. self.assertEqual(result.value, "5")
  104. def test_default_values(self):
  105. """Default values get applied."""
  106. self.agrolait.custom_info_template_id = self.tpl
  107. self.agrolait._onchange_custom_info_template_id()
  108. val_weaknesses = self.agrolait.get_custom_info_value(
  109. self.env.ref("base_custom_info.prop_weaknesses"))
  110. opt_glasses = self.env.ref("base_custom_info.opt_glasses")
  111. self.assertEqual(val_weaknesses.value_id, opt_glasses)
  112. self.assertEqual(val_weaknesses.value, opt_glasses.name)
  113. def test_recursive_templates(self):
  114. """Recursive templates get loaded when required."""
  115. self.set_custom_info_for_agrolait()
  116. prop_weaknesses = self.env.ref("base_custom_info.prop_weaknesses")
  117. val_weaknesses = self.agrolait.get_custom_info_value(prop_weaknesses)
  118. val_weaknesses.value = "Needs 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 = (u"Don Walter Antonio José de la Cruz Hëisenberg de "
  136. u"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 = "-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 = "11"