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.

128 lines
5.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. #
  4. # Authors: Guewen Baconnier
  5. # Copyright 2015 Camptocamp SA
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. #
  21. from openerp.tests import common
  22. from .common import RevisionMixin
  23. class TestRevisionOrigin(RevisionMixin, common.TransactionCase):
  24. """ Check that origin - old fields are stored as expected.
  25. 'origin' fields dynamically read fields from the partner when the state
  26. of the change is 'draft'. Once a change becomes 'done' or 'cancel', the
  27. 'old' field copies the value from the partner and then the 'origin' field
  28. displays the 'old' value.
  29. """
  30. def _setup_rules(self):
  31. RevisionFieldRule = self.env['revision.field.rule']
  32. partner_model_id = self.env.ref('base.model_res_partner').id
  33. self.field_name = self.env.ref('base.field_res_partner_name')
  34. RevisionFieldRule.create({
  35. 'model_id': partner_model_id,
  36. 'field_id': self.field_name.id,
  37. 'action': 'validate',
  38. })
  39. def setUp(self):
  40. super(TestRevisionOrigin, self).setUp()
  41. self._setup_rules()
  42. self.partner = self.env['res.partner'].create({
  43. 'name': 'X',
  44. })
  45. def test_origin_value_of_change_with_apply(self):
  46. """ Origin field is read from the parter or 'old' - with apply
  47. According to the state of the change.
  48. """
  49. self.partner.with_context(__revision_rules=True).write({
  50. 'name': 'Y',
  51. })
  52. revision = self.partner.revision_ids
  53. change = revision.change_ids
  54. self.assertEqual(self.partner.name, 'X')
  55. self.assertEqual(change.origin_value_char, 'X')
  56. self.assertEqual(change.origin_value_display, 'X')
  57. self.partner.write({'name': 'A'})
  58. self.assertEqual(change.origin_value_char, 'A')
  59. self.assertEqual(change.origin_value_display, 'A')
  60. change.apply()
  61. self.assertEqual(change.origin_value_char, 'A')
  62. self.assertEqual(change.origin_value_display, 'A')
  63. self.partner.write({'name': 'B'})
  64. self.assertEqual(change.origin_value_char, 'A')
  65. self.assertEqual(change.origin_value_display, 'A')
  66. def test_origin_value_of_change_with_cancel(self):
  67. """ Origin field is read from the parter or 'old' - with cancel
  68. According to the state of the change.
  69. """
  70. self.partner.with_context(__revision_rules=True).write({
  71. 'name': 'Y',
  72. })
  73. revision = self.partner.revision_ids
  74. change = revision.change_ids
  75. self.assertEqual(self.partner.name, 'X')
  76. self.assertEqual(change.origin_value_char, 'X')
  77. self.assertEqual(change.origin_value_display, 'X')
  78. self.partner.write({'name': 'A'})
  79. self.assertEqual(change.origin_value_char, 'A')
  80. self.assertEqual(change.origin_value_display, 'A')
  81. change.cancel()
  82. self.assertEqual(change.origin_value_char, 'A')
  83. self.assertEqual(change.origin_value_display, 'A')
  84. self.partner.write({'name': 'B'})
  85. self.assertEqual(change.origin_value_char, 'A')
  86. self.assertEqual(change.origin_value_display, 'A')
  87. def test_old_field_of_change_with_apply(self):
  88. """ Old field is stored when the change is applied """
  89. self.partner.with_context(__revision_rules=True).write({
  90. 'name': 'Y',
  91. })
  92. revision = self.partner.revision_ids
  93. change = revision.change_ids
  94. self.assertEqual(self.partner.name, 'X')
  95. self.assertFalse(change.old_value_char)
  96. self.partner.write({'name': 'A'})
  97. self.assertFalse(change.old_value_char)
  98. change.apply()
  99. self.assertEqual(change.old_value_char, 'A')
  100. self.partner.write({'name': 'B'})
  101. self.assertEqual(change.old_value_char, 'A')
  102. def test_old_field_of_change_with_cancel(self):
  103. """ Old field is stored when the change is canceled """
  104. self.partner.with_context(__revision_rules=True).write({
  105. 'name': 'Y',
  106. })
  107. revision = self.partner.revision_ids
  108. change = revision.change_ids
  109. self.assertEqual(self.partner.name, 'X')
  110. self.assertFalse(change.old_value_char)
  111. self.partner.write({'name': 'A'})
  112. self.assertFalse(change.old_value_char)
  113. change.cancel()
  114. self.assertEqual(change.old_value_char, 'A')
  115. self.partner.write({'name': 'B'})
  116. self.assertEqual(change.old_value_char, 'A')