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.

120 lines
5.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015-2017 Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo.tests import common
  5. from .common import ChangesetMixin
  6. class TestChangesetOrigin(ChangesetMixin, common.TransactionCase):
  7. """ Check that origin - old fields are stored as expected.
  8. 'origin' fields dynamically read fields from the partner when the state
  9. of the change is 'draft'. Once a change becomes 'done' or 'cancel', the
  10. 'old' field copies the value from the partner and then the 'origin' field
  11. displays the 'old' value.
  12. """
  13. def _setup_rules(self):
  14. ChangesetFieldRule = self.env['changeset.field.rule']
  15. ChangesetFieldRule.search([]).unlink()
  16. self.field_name = self.env.ref('base.field_res_partner_name')
  17. ChangesetFieldRule.create({
  18. 'field_id': self.field_name.id,
  19. 'action': 'validate',
  20. })
  21. def setUp(self):
  22. super(TestChangesetOrigin, self).setUp()
  23. self._setup_rules()
  24. self.partner = self.env['res.partner'].create({
  25. 'name': 'X',
  26. })
  27. # Add context for this test for compatibility with other modules' tests
  28. self.partner = self.partner.with_context(test_partner_changeset=True)
  29. def test_origin_value_of_change_with_apply(self):
  30. """ Origin field is read from the parter or 'old' - with apply
  31. According to the state of the change.
  32. """
  33. self.partner.write({
  34. 'name': 'Y',
  35. })
  36. changeset = self.partner.changeset_ids
  37. change = changeset.change_ids
  38. self.assertEqual(self.partner.name, 'X')
  39. self.assertEqual(change.origin_value_char, 'X')
  40. self.assertEqual(change.origin_value_display, 'X')
  41. self.partner.with_context(__no_changeset=True).write({'name': 'A'})
  42. # depends cannot trigger all fileds from partner. In real use case,
  43. # the user will probably be in different transaction, he will get the
  44. # new value of the field
  45. self.partner.invalidate_cache()
  46. self.assertEqual(change.origin_value_char, 'A')
  47. self.assertEqual(change.origin_value_display, 'A')
  48. change.apply()
  49. self.assertEqual(change.origin_value_char, 'A')
  50. self.assertEqual(change.origin_value_display, 'A')
  51. self.partner.with_context(__no_changeset=True).write({'name': 'B'})
  52. self.assertEqual(change.origin_value_char, 'A')
  53. self.assertEqual(change.origin_value_display, 'A')
  54. def test_origin_value_of_change_with_cancel(self):
  55. """ Origin field is read from the parter or 'old' - with cancel
  56. According to the state of the change.
  57. """
  58. self.partner.write({
  59. 'name': 'Y',
  60. })
  61. changeset = self.partner.changeset_ids
  62. change = changeset.change_ids
  63. self.assertEqual(self.partner.name, 'X')
  64. self.assertEqual(change.origin_value_char, 'X')
  65. self.assertEqual(change.origin_value_display, 'X')
  66. self.partner.with_context(__no_changeset=True).write({'name': 'A'})
  67. # depends cannot trigger all fileds from partner. In real use case,
  68. # the user will probably be in different transaction, he will get the
  69. # new value of the field
  70. self.partner.invalidate_cache()
  71. self.assertEqual(change.origin_value_char, 'A')
  72. self.assertEqual(change.origin_value_display, 'A')
  73. change.cancel()
  74. self.assertEqual(change.origin_value_char, 'A')
  75. self.assertEqual(change.origin_value_display, 'A')
  76. self.partner.with_context(__no_changeset=True).write({'name': 'B'})
  77. self.assertEqual(change.origin_value_char, 'A')
  78. self.assertEqual(change.origin_value_display, 'A')
  79. def test_old_field_of_change_with_apply(self):
  80. """ Old field is stored when the change is applied """
  81. self.partner.write({
  82. 'name': 'Y',
  83. })
  84. changeset = self.partner.changeset_ids
  85. change = changeset.change_ids
  86. self.assertEqual(self.partner.name, 'X')
  87. self.assertFalse(change.old_value_char)
  88. self.partner.with_context(__no_changeset=True).write({'name': 'A'})
  89. self.assertFalse(change.old_value_char)
  90. change.apply()
  91. self.assertEqual(change.old_value_char, 'A')
  92. self.partner.with_context(__no_changeset=True).write({'name': 'B'})
  93. self.assertEqual(change.old_value_char, 'A')
  94. def test_old_field_of_change_with_cancel(self):
  95. """ Old field is stored when the change is canceled """
  96. self.partner.write({
  97. 'name': 'Y',
  98. })
  99. changeset = self.partner.changeset_ids
  100. change = changeset.change_ids
  101. self.assertEqual(self.partner.name, 'X')
  102. self.assertFalse(change.old_value_char)
  103. self.partner.with_context(__no_changeset=True).write({'name': 'A'})
  104. self.assertFalse(change.old_value_char)
  105. change.cancel()
  106. self.assertEqual(change.old_value_char, 'A')
  107. self.partner.with_context(__no_changeset=True).write({'name': 'B'})
  108. self.assertEqual(change.old_value_char, 'A')