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.

93 lines
3.6 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, origin_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,
  42. change.get_origin_value(),
  43. change.get_new_value(),
  44. change.state) == expected_change:
  45. changes -= change
  46. break
  47. else:
  48. missing.append(expected_change)
  49. message = u''
  50. for field, origin_value, new_value, state in missing:
  51. message += ("- field: '%s', origin_value: '%s', "
  52. "new_value: '%s', state: '%s'\n" %
  53. (field.name, origin_value, new_value, state))
  54. for change in changes:
  55. message += ("+ field: '%s', origin_value: '%s', "
  56. "new_value: '%s', state: '%s'\n" %
  57. (change.field_id.name,
  58. change.get_origin_value(),
  59. change.get_new_value(),
  60. change.state))
  61. if message:
  62. raise AssertionError('Changes do not match\n\n:%s' % message)
  63. def _create_revision(self, partner, changes):
  64. """ Create a revision and its associated changes
  65. :param partner: 'res.partner' record
  66. :param changes: list of changes [(field, new value, state)]
  67. :returns: 'res.partner.revision' record
  68. """
  69. RevisionChange = self.env['res.partner.revision.change']
  70. get_field = RevisionChange.get_field_for_type
  71. change_values = []
  72. for field, value, state in changes:
  73. change = {
  74. 'field_id': field.id,
  75. # write in the field of the appropriate type for the
  76. # origin field (char, many2one, ...)
  77. get_field(field, 'new'): value,
  78. 'state': state,
  79. }
  80. change_values.append((0, 0, change))
  81. values = {
  82. 'partner_id': partner.id,
  83. 'change_ids': change_values,
  84. }
  85. return self.env['res.partner.revision'].create(values)