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.

171 lines
7.6 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. 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. def test_template_model_must_exist(self):
  72. """Cannot create templates for unexisting models."""
  73. with self.assertRaises(IntegrityError):
  74. self.tpl.model = "yabadabaduu"
  75. def test_change_used_model_fails(self):
  76. """If a template's model is already used, you cannot change it."""
  77. self.set_custom_info_for_agrolait()
  78. with self.assertRaises(ValidationError):
  79. self.tpl.model = "res.users"
  80. def test_owners_selection(self):
  81. """Owners selection includes only the required matches."""
  82. choices = dict(self.env["custom.info.value"]._selection_owner_id())
  83. self.assertIn("res.partner", choices)
  84. self.assertNotIn("ir.model", choices)
  85. self.assertNotIn("custom.info.property", choices)
  86. self.assertNotIn("custom.info", choices)
  87. def test_owner_id(self):
  88. """Check the computed owner id for a value."""
  89. self.set_custom_info_for_agrolait()
  90. self.assertEqual(
  91. self.agrolait.mapped("custom_info_ids.owner_id"), self.agrolait)
  92. def test_get_custom_info_value(self):
  93. """Check the custom info getter helper works fine."""
  94. self.set_custom_info_for_agrolait()
  95. result = self.agrolait.get_custom_info_value(
  96. self.env.ref("base_custom_info.prop_haters"))
  97. self.assertEqual(result.field_type, "int")
  98. self.assertEqual(result.field_name, "value_int")
  99. self.assertEqual(result[result.field_name], 5)
  100. self.assertEqual(result.value_int, 5)
  101. self.assertEqual(result.value, "5")
  102. def test_default_values(self):
  103. """Default values get applied."""
  104. self.agrolait.custom_info_template_id = self.tpl
  105. self.agrolait._onchange_custom_info_template_id()
  106. val_weaknesses = self.agrolait.get_custom_info_value(
  107. self.env.ref("base_custom_info.prop_weaknesses"))
  108. opt_glasses = self.env.ref("base_custom_info.opt_glasses")
  109. self.assertEqual(val_weaknesses.value_id, opt_glasses)
  110. self.assertEqual(val_weaknesses.value, opt_glasses.name)
  111. def test_recursive_templates(self):
  112. """Recursive templates get loaded when required."""
  113. self.set_custom_info_for_agrolait()
  114. prop_weaknesses = self.env.ref("base_custom_info.prop_weaknesses")
  115. val_weaknesses = self.agrolait.get_custom_info_value(prop_weaknesses)
  116. val_weaknesses.value = "Needs videogames"
  117. tpl_gamer = self.env.ref("base_custom_info.tpl_gamer")
  118. self.agrolait.invalidate_cache()
  119. self.assertIn(tpl_gamer, self.agrolait.all_custom_info_templates())
  120. self.agrolait._onchange_custom_info_template_id()
  121. self.assertTrue(
  122. tpl_gamer.property_ids <
  123. self.agrolait.mapped("custom_info_ids.property_id"))
  124. cat_gaming = self.env.ref("base_custom_info.cat_gaming")
  125. self.assertIn(
  126. cat_gaming, self.agrolait.mapped("custom_info_ids.category_id"))
  127. def test_long_teacher_name(self):
  128. """Wow, your teacher cannot have such a long name!"""
  129. self.set_custom_info_for_agrolait()
  130. val = self.agrolait.get_custom_info_value(
  131. self.env.ref("base_custom_info.prop_teacher"))
  132. with self.assertRaises(ValidationError):
  133. val.value = (u"Don Walter Antonio José de la Cruz Hëisenberg de "
  134. u"Borbón Westley Jordy López Manuélez")
  135. def test_low_average_note(self):
  136. """Come on, you are supposed to be 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 = "-1"
  142. def test_high_average_note(self):
  143. """Too smart!"""
  144. self.set_custom_info_for_agrolait()
  145. val = self.agrolait.get_custom_info_value(
  146. self.env.ref("base_custom_info.prop_avg_note"))
  147. with self.assertRaises(ValidationError):
  148. val.value = "11"