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.

118 lines
4.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015-2016 Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp.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. def test_origin_value_of_change_with_apply(self):
  28. """ Origin field is read from the parter or 'old' - with apply
  29. According to the state of the change.
  30. """
  31. self.partner.write({
  32. 'name': 'Y',
  33. })
  34. changeset = self.partner.changeset_ids
  35. change = changeset.change_ids
  36. self.assertEqual(self.partner.name, 'X')
  37. self.assertEqual(change.origin_value_char, 'X')
  38. self.assertEqual(change.origin_value_display, 'X')
  39. self.partner.with_context(__no_changeset=True).write({'name': 'A'})
  40. # depends cannot trigger all fileds from partner. In real use case,
  41. # the user will probably be in different transaction, he will get the
  42. # new value of the field
  43. self.partner.invalidate_cache()
  44. self.assertEqual(change.origin_value_char, 'A')
  45. self.assertEqual(change.origin_value_display, 'A')
  46. change.apply()
  47. self.assertEqual(change.origin_value_char, 'A')
  48. self.assertEqual(change.origin_value_display, 'A')
  49. self.partner.with_context(__no_changeset=True).write({'name': 'B'})
  50. self.assertEqual(change.origin_value_char, 'A')
  51. self.assertEqual(change.origin_value_display, 'A')
  52. def test_origin_value_of_change_with_cancel(self):
  53. """ Origin field is read from the parter or 'old' - with cancel
  54. According to the state of the change.
  55. """
  56. self.partner.write({
  57. 'name': 'Y',
  58. })
  59. changeset = self.partner.changeset_ids
  60. change = changeset.change_ids
  61. self.assertEqual(self.partner.name, 'X')
  62. self.assertEqual(change.origin_value_char, 'X')
  63. self.assertEqual(change.origin_value_display, 'X')
  64. self.partner.with_context(__no_changeset=True).write({'name': 'A'})
  65. # depends cannot trigger all fileds from partner. In real use case,
  66. # the user will probably be in different transaction, he will get the
  67. # new value of the field
  68. self.partner.invalidate_cache()
  69. self.assertEqual(change.origin_value_char, 'A')
  70. self.assertEqual(change.origin_value_display, 'A')
  71. change.cancel()
  72. self.assertEqual(change.origin_value_char, 'A')
  73. self.assertEqual(change.origin_value_display, 'A')
  74. self.partner.with_context(__no_changeset=True).write({'name': 'B'})
  75. self.assertEqual(change.origin_value_char, 'A')
  76. self.assertEqual(change.origin_value_display, 'A')
  77. def test_old_field_of_change_with_apply(self):
  78. """ Old field is stored when the change is applied """
  79. self.partner.write({
  80. 'name': 'Y',
  81. })
  82. changeset = self.partner.changeset_ids
  83. change = changeset.change_ids
  84. self.assertEqual(self.partner.name, 'X')
  85. self.assertFalse(change.old_value_char)
  86. self.partner.with_context(__no_changeset=True).write({'name': 'A'})
  87. self.assertFalse(change.old_value_char)
  88. change.apply()
  89. self.assertEqual(change.old_value_char, 'A')
  90. self.partner.with_context(__no_changeset=True).write({'name': 'B'})
  91. self.assertEqual(change.old_value_char, 'A')
  92. def test_old_field_of_change_with_cancel(self):
  93. """ Old field is stored when the change is canceled """
  94. self.partner.write({
  95. 'name': 'Y',
  96. })
  97. changeset = self.partner.changeset_ids
  98. change = changeset.change_ids
  99. self.assertEqual(self.partner.name, 'X')
  100. self.assertFalse(change.old_value_char)
  101. self.partner.with_context(__no_changeset=True).write({'name': 'A'})
  102. self.assertFalse(change.old_value_char)
  103. change.cancel()
  104. self.assertEqual(change.old_value_char, 'A')
  105. self.partner.with_context(__no_changeset=True).write({'name': 'B'})
  106. self.assertEqual(change.old_value_char, 'A')