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.

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