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.

77 lines
3.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2015 Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. class ChangesetMixin(object):
  5. def assert_changeset(self, partner, expected_source, expected_changes):
  6. """ Check if a changeset has been created according to expected values
  7. The partner should have no prior changeset than the one created in the
  8. test (so it has exactly 1 changeset).
  9. The expected changes are tuples with (field, origin_value,
  10. new_value, state)
  11. :param partner: record of partner having a changeset
  12. :param expected_changes: contains tuples with the changes
  13. :type expected_changes: list(tuple))
  14. """
  15. changeset = self.env['res.partner.changeset'].search(
  16. [('partner_id', '=', partner.id)],
  17. )
  18. self.assertEqual(len(changeset), 1,
  19. "1 changeset expected, got %s" % (changeset,))
  20. self.assertEqual(changeset.source, expected_source)
  21. changes = changeset.change_ids
  22. missing = []
  23. for expected_change in expected_changes:
  24. for change in changes:
  25. if (change.field_id,
  26. change.get_origin_value(),
  27. change.get_new_value(),
  28. change.state) == expected_change:
  29. changes -= change
  30. break
  31. else:
  32. missing.append(expected_change)
  33. message = u''
  34. for field, origin_value, new_value, state in missing:
  35. message += ("- field: '%s', origin_value: '%s', "
  36. "new_value: '%s', state: '%s'\n" %
  37. (field.name, origin_value, new_value, state))
  38. for change in changes:
  39. message += ("+ field: '%s', origin_value: '%s', "
  40. "new_value: '%s', state: '%s'\n" %
  41. (change.field_id.name,
  42. change.get_origin_value(),
  43. change.get_new_value(),
  44. change.state))
  45. if message:
  46. raise AssertionError('Changes do not match\n\n:%s' % message)
  47. def _create_changeset(self, partner, changes):
  48. """ Create a changeset and its associated changes
  49. :param partner: 'res.partner' record
  50. :param changes: list of changes [(field, new value, state)]
  51. :returns: 'res.partner.changeset' record
  52. """
  53. ChangesetChange = self.env['res.partner.changeset.change']
  54. get_field = ChangesetChange.get_field_for_type
  55. change_values = []
  56. for field, value, state in changes:
  57. change = {
  58. 'field_id': field.id,
  59. # write in the field of the appropriate type for the
  60. # origin field (char, many2one, ...)
  61. get_field(field, 'new'): value,
  62. 'state': state,
  63. }
  64. change_values.append((0, 0, change))
  65. values = {
  66. 'partner_id': partner.id,
  67. 'change_ids': change_values,
  68. }
  69. return self.env['res.partner.changeset'].create(values)