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.

135 lines
5.2 KiB

  1. # Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  3. import logging
  4. from odoo.tests.common import TransactionCase
  5. _logger = logging.getLogger(__name__)
  6. class ValueConversionCase(TransactionCase):
  7. def setUp(self):
  8. super(ValueConversionCase, self).setUp()
  9. self.agrolait = self.env.ref("base.res_partner_2")
  10. self.tpl = self.env.ref("base_custom_info.tpl_smart")
  11. self.prop_str = self.env.ref("base_custom_info.prop_teacher")
  12. self.prop_int = self.env.ref("base_custom_info.prop_haters")
  13. self.prop_float = self.env.ref("base_custom_info.prop_avg_note")
  14. self.prop_bool = self.env.ref("base_custom_info.prop_smartypants")
  15. self.prop_id = self.env.ref("base_custom_info.prop_weaknesses")
  16. def fill_value(self, prop, value, field="value"):
  17. """Create a custom info value."""
  18. _logger.info(
  19. "Creating. prop: %s; value: %s; field: %s", prop, value, field)
  20. self.agrolait.custom_info_template_id = self.tpl
  21. self.agrolait._onchange_custom_info_template_id()
  22. if field == "value":
  23. field = "value_%s" % prop.field_type
  24. if prop.field_type == "id" and isinstance(value, str):
  25. value = self.env["custom.info.option"].search([
  26. ('property_ids', '=', prop.id),
  27. ('name', '=', value)
  28. ], limit=1).id
  29. self.assertTrue(value)
  30. self.value = self.agrolait.get_custom_info_value(prop)
  31. self.value[field] = value
  32. def creation_found(self, value):
  33. """Ensure you can search what you just created."""
  34. prop = self.value.property_id
  35. _logger.info(
  36. "Searching. prop: %s; value: %s", prop, value)
  37. self.assertEqual(
  38. self.value.search([
  39. ("property_id", "=", prop.id),
  40. ("value", "=", value)]),
  41. self.value)
  42. self.assertEqual(
  43. self.value.search([
  44. ("property_id", "=", prop.id),
  45. ("value", "in", [value])]),
  46. self.value)
  47. self.assertIs(
  48. self.value.search([
  49. ("property_id", "=", prop.id),
  50. ("value", "not in", [value])]).id,
  51. False)
  52. def test_to_str(self):
  53. """Conversion to text."""
  54. self.fill_value(self.prop_str, "Mr. Einstein")
  55. self.creation_found("Mr. Einstein")
  56. self.assertEqual(self.value.value, self.value.value_str)
  57. def test_from_str(self):
  58. """Conversion from text."""
  59. self.fill_value(self.prop_str, "Mr. Einstein", "value_str")
  60. self.creation_found("Mr. Einstein")
  61. self.assertEqual(self.value.value, self.value.value_str)
  62. def test_to_int(self):
  63. """Conversion to whole number."""
  64. self.fill_value(self.prop_int, 5)
  65. self.creation_found("5")
  66. self.assertEqual(int(self.value.value), self.value.value_int)
  67. def test_from_int(self):
  68. """Conversion from whole number."""
  69. self.fill_value(self.prop_int, 5, "value_int")
  70. self.creation_found("5")
  71. self.assertEqual(int(self.value.value), self.value.value_int)
  72. def test_to_float(self):
  73. """Conversion to decimal number."""
  74. self.fill_value(self.prop_float, 9.5)
  75. self.creation_found("9.5")
  76. self.assertEqual(float(self.value.value), self.value.value_float)
  77. def test_from_float(self):
  78. """Conversion from decimal number."""
  79. self.fill_value(self.prop_float, 9.5, "value_float")
  80. self.creation_found("9.5")
  81. self.assertEqual(float(self.value.value), self.value.value_float)
  82. def test_to_bool_true(self):
  83. """Conversion to yes."""
  84. self.fill_value(self.prop_bool, True)
  85. self.creation_found("True")
  86. self.assertEqual(self.value.with_context(lang="en_US").value, "Yes")
  87. self.assertIs(self.value.value_bool, True)
  88. def test_from_bool_true(self):
  89. """Conversion from yes."""
  90. self.fill_value(self.prop_bool, True, "value_bool")
  91. self.creation_found("True")
  92. self.assertEqual(self.value.with_context(lang="en_US").value, "Yes")
  93. self.assertIs(self.value.value_bool, True)
  94. def test_to_bool_false(self):
  95. """Conversion to no."""
  96. self.fill_value(self.prop_bool, False)
  97. self.assertEqual(self.value.with_context(lang="en_US").value, "No")
  98. self.assertIs(self.value.value_bool, False)
  99. def test_from_bool_false(self):
  100. """Conversion from no."""
  101. self.fill_value(self.prop_bool, False, "value_bool")
  102. self.assertEqual(self.value.with_context(lang="en_US").value, "No")
  103. self.assertIs(self.value.value_bool, False)
  104. def test_to_id(self):
  105. """Conversion to selection."""
  106. self.fill_value(self.prop_id, "Needs videogames")
  107. self.creation_found("Needs videogames")
  108. self.assertEqual(self.value.value, self.value.value_id.name)
  109. def test_from_id(self):
  110. """Conversion from selection."""
  111. self.fill_value(
  112. self.prop_id,
  113. self.env.ref("base_custom_info.opt_videogames").id,
  114. "value_id")
  115. self.creation_found("Needs videogames")
  116. self.assertEqual(self.value.value, self.value.value_id.name)