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.

182 lines
6.7 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 TestRevisionFlow(RevisionMixin, common.TransactionCase):
  24. """ Check how revision are generated and applied based on the rules.
  25. We do not really care about the types of the fields in this test
  26. suite, so we only use 'char' fields. We have to ensure that the
  27. general revision flows work as expected, that is:
  28. * create a 'done' revision when a manual/system write is made on partner
  29. * create a revision according to the revision rules when the key
  30. '__revision_rules' is passed in the context
  31. * apply a revision change writes the value on the partner
  32. * apply a whole revision writes all the changes' values on the partner
  33. * changes in state 'cancel' or 'done' do not write on the partner
  34. """
  35. def _setup_behavior(self):
  36. RevisionBehavior = self.env['revision.behavior']
  37. partner_model_id = self.env.ref('base.model_res_partner').id
  38. self.field_name = self.env.ref('base.field_res_partner_name')
  39. self.field_street = self.env.ref('base.field_res_partner_street')
  40. self.field_street2 = self.env.ref('base.field_res_partner_street2')
  41. RevisionBehavior.create({
  42. 'model_id': partner_model_id,
  43. 'field_id': self.field_name.id,
  44. 'default_behavior': 'auto',
  45. })
  46. RevisionBehavior.create({
  47. 'model_id': partner_model_id,
  48. 'field_id': self.field_street.id,
  49. 'default_behavior': 'validate',
  50. })
  51. RevisionBehavior.create({
  52. 'model_id': partner_model_id,
  53. 'field_id': self.field_street2.id,
  54. 'default_behavior': 'never',
  55. })
  56. def setUp(self):
  57. super(TestRevisionFlow, self).setUp()
  58. self._setup_behavior()
  59. self.partner = self.env['res.partner'].create({
  60. 'name': 'X',
  61. 'street': 'street X',
  62. 'street2': 'street2 X',
  63. })
  64. def test_new_revision(self):
  65. """ Add a new revision on a partner """
  66. self.partner.with_context(__revision_rules=True).write({
  67. 'name': 'Y',
  68. 'street': 'street Y',
  69. 'street2': 'street2 Y',
  70. })
  71. self.assert_revision(
  72. self.partner,
  73. [(self.field_name, 'X', 'Y', 'done'),
  74. (self.field_street, 'street X', 'street Y', 'draft'),
  75. (self.field_street2, 'street2 X', 'street2 Y', 'cancel'),
  76. ],
  77. )
  78. self.assertEqual(self.partner.name, 'Y')
  79. self.assertEqual(self.partner.street, 'street X')
  80. self.assertEqual(self.partner.street2, 'street2 X')
  81. def test_new_revision_empty_value(self):
  82. """ Create a revision change that empty a value """
  83. self.partner.with_context(__revision_rules=True).write({
  84. 'street': False,
  85. })
  86. self.assert_revision(
  87. self.partner,
  88. [(self.field_street, 'street X', False, 'draft')]
  89. )
  90. def test_manual_edition(self):
  91. """ A manual edition of a partner should always be applied
  92. But should create a 'done' revision
  93. """
  94. self.partner.write({
  95. 'name': 'Y',
  96. 'street': 'street Y',
  97. 'street2': 'street2 Y',
  98. })
  99. self.assert_revision(
  100. self.partner,
  101. [(self.field_name, 'X', 'Y', 'done'),
  102. (self.field_street, 'street X', 'street Y', 'done'),
  103. (self.field_street2, 'street2 X', 'street2 Y', 'done'),
  104. ],
  105. )
  106. self.assertEqual(self.partner.name, 'Y')
  107. self.assertEqual(self.partner.street, 'street Y')
  108. self.assertEqual(self.partner.street2, 'street2 Y')
  109. def test_apply_change(self):
  110. """ Apply a revision change on a partner """
  111. changes = [
  112. (self.field_name, 'Y', 'draft'),
  113. ]
  114. revision = self._create_revision(self.partner, changes)
  115. revision.change_ids.apply()
  116. self.assertEqual(self.partner.name, 'Y')
  117. def test_apply_done_change(self):
  118. """ Done changes do not apply (already applied) """
  119. changes = [
  120. (self.field_name, 'Y', 'done'),
  121. ]
  122. revision = self._create_revision(self.partner, changes)
  123. revision.change_ids.apply()
  124. self.assertEqual(self.partner.name, 'X')
  125. def test_apply_cancel_change(self):
  126. """ Cancel changes do not apply """
  127. changes = [
  128. (self.field_name, 'Y', 'cancel'),
  129. ]
  130. revision = self._create_revision(self.partner, changes)
  131. revision.change_ids.apply()
  132. self.assertEqual(self.partner.name, 'X')
  133. def test_apply_empty_value(self):
  134. """ Apply a change that empty a value """
  135. changes = [
  136. (self.field_street, False, 'draft'),
  137. ]
  138. revision = self._create_revision(self.partner, changes)
  139. revision.change_ids.apply()
  140. self.assertFalse(self.partner.street)
  141. def test_apply_change_loop(self):
  142. """ Test @api.multi on the changes """
  143. changes = [
  144. (self.field_name, 'Y', 'draft'),
  145. (self.field_street, 'street Y', 'draft'),
  146. (self.field_street2, 'street2 Y', 'draft'),
  147. ]
  148. revision = self._create_revision(self.partner, changes)
  149. revision.change_ids.apply()
  150. self.assertEqual(self.partner.name, 'Y')
  151. self.assertEqual(self.partner.street, 'street Y')
  152. self.assertEqual(self.partner.street2, 'street2 Y')
  153. def test_apply(self):
  154. """ Apply a full revision on a partner """
  155. changes = [
  156. (self.field_name, 'Y', 'draft'),
  157. (self.field_street, 'street Y', 'draft'),
  158. (self.field_street2, 'street2 Y', 'draft'),
  159. ]
  160. revision = self._create_revision(self.partner, changes)
  161. revision.apply()
  162. self.assertEqual(self.partner.name, 'Y')
  163. self.assertEqual(self.partner.street, 'street Y')
  164. self.assertEqual(self.partner.street2, 'street2 Y')