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.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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 ChangesetMixin(object):
  22. def assert_changeset(self, partner, expected_changes):
  23. """ Check if a changeset has been created according to expected values
  24. The partner should have no prior changeset than the one created in the
  25. test (so it has exactly 1 changeset).
  26. The expected changes are tuples with (field, origin_value,
  27. new_value, state)
  28. :param partner: record of partner having a changeset
  29. :param expected_changes: contains tuples with the changes
  30. :type expected_changes: list(tuple))
  31. """
  32. changeset = self.env['res.partner.changeset'].search(
  33. [('partner_id', '=', partner.id)],
  34. )
  35. self.assertEqual(len(changeset), 1,
  36. "1 changeset expected, got %s" % (changeset,))
  37. changes = changeset.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_changeset(self, partner, changes):
  64. """ Create a changeset and its associated changes
  65. :param partner: 'res.partner' record
  66. :param changes: list of changes [(field, new value, state)]
  67. :returns: 'res.partner.changeset' record
  68. """
  69. ChangesetChange = self.env['res.partner.changeset.change']
  70. get_field = ChangesetChange.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.changeset'].create(values)