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.

85 lines
3.3 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. class RevisionMixin(object):
  22. def assert_revision(self, partner, expected_changes):
  23. """ Check if a revision has been created according to expected values
  24. The partner should have no prior revision than the one created in the
  25. test (so it has exactly 1 revision).
  26. The expected changes are tuples with (field, current_value,
  27. new_value, state)
  28. :param partner: record of partner having a revision
  29. :param expected_changes: contains tuples with the changes
  30. :type expected_changes: list(tuple))
  31. """
  32. revision = self.env['res.partner.revision'].search(
  33. [('partner_id', '=', partner.id)],
  34. )
  35. self.assertEqual(len(revision), 1,
  36. "1 revision expected, got %s" % (revision,))
  37. changes = revision.change_ids
  38. missing = []
  39. for expected_change in expected_changes:
  40. for change in changes:
  41. if (change.field_id, change.current_value, change.new_value,
  42. change.state) == expected_change:
  43. changes -= change
  44. break
  45. else:
  46. missing.append(expected_change)
  47. message = u''
  48. for field, current_value, new_value, state in missing:
  49. message += ("- field: '%s', current_value: '%s', "
  50. "new_value: '%s', state: '%s'\n" %
  51. (field.name, current_value, new_value, state))
  52. for change in changes:
  53. message += ("+ field: '%s', current_value: '%s', "
  54. "new_value: '%s', state: '%s'\n" %
  55. (change.field_id.name, change.current_value,
  56. change.new_value, change.state))
  57. if message:
  58. raise AssertionError('Changes do not match\n\n:%s' % message)
  59. def _create_revision(self, partner, changes):
  60. """ Create a revision and its associated changes
  61. :param partner: 'res.partner' record
  62. :param changes: list of changes [(field, new value, state)]
  63. :returns: 'res.partner.revision' record
  64. """
  65. change_values = [
  66. (0, 0, {
  67. 'field_id': field.id,
  68. 'current_value': partner[field.name],
  69. 'new_value': value,
  70. 'state': state,
  71. }) for field, value, state in changes
  72. ]
  73. values = {
  74. 'partner_id': partner.id,
  75. 'change_ids': change_values,
  76. }
  77. return self.env['res.partner.revision'].create(values)