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.

78 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015-2016 Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp.tests import common
  5. class TestChangesetFieldRule(common.TransactionCase):
  6. def setUp(self):
  7. super(TestChangesetFieldRule, self).setUp()
  8. self.company_model_id = self.env.ref('base.model_res_company').id
  9. self.field_name = self.env.ref('base.field_res_partner_name')
  10. self.field_street = self.env.ref('base.field_res_partner_street')
  11. def test_get_rules(self):
  12. ChangesetFieldRule = self.env['changeset.field.rule']
  13. ChangesetFieldRule.search([]).unlink()
  14. rule1 = ChangesetFieldRule.create({
  15. 'field_id': self.field_name.id,
  16. 'action': 'validate',
  17. })
  18. rule2 = ChangesetFieldRule.create({
  19. 'field_id': self.field_street.id,
  20. 'action': 'never',
  21. })
  22. get_rules = ChangesetFieldRule.get_rules(None)
  23. self.assertEqual(get_rules, {'name': rule1, 'street': rule2})
  24. def test_get_rules_source(self):
  25. ChangesetFieldRule = self.env['changeset.field.rule']
  26. ChangesetFieldRule.search([]).unlink()
  27. rule1 = ChangesetFieldRule.create({
  28. 'field_id': self.field_name.id,
  29. 'action': 'validate',
  30. })
  31. rule2 = ChangesetFieldRule.create({
  32. 'field_id': self.field_street.id,
  33. 'action': 'never',
  34. })
  35. rule3 = ChangesetFieldRule.create({
  36. 'source_model_id': self.company_model_id,
  37. 'field_id': self.field_street.id,
  38. 'action': 'never',
  39. })
  40. model = ChangesetFieldRule
  41. rules = model.get_rules(None)
  42. self.assertEqual(rules, {u'name': rule1, u'street': rule2})
  43. rules = model.get_rules('res.company')
  44. self.assertEqual(rules, {u'name': rule1, u'street': rule3})
  45. def test_get_rules_cache(self):
  46. ChangesetFieldRule = self.env['changeset.field.rule']
  47. ChangesetFieldRule.search([]).unlink()
  48. rule = ChangesetFieldRule.create({
  49. 'field_id': self.field_name.id,
  50. 'action': 'validate',
  51. })
  52. self.assertEqual(
  53. ChangesetFieldRule.get_rules(None)['name'].action,
  54. 'validate',
  55. )
  56. # Write on cursor to bypass the cache invalidation for the
  57. # matter of the test
  58. self.env.cr.execute("UPDATE changeset_field_rule "
  59. "SET action = 'never' "
  60. "WHERE id = %s", (rule.id,))
  61. self.assertEqual(
  62. ChangesetFieldRule.get_rules(None)['name'].action,
  63. 'validate',
  64. )
  65. rule.action = 'auto'
  66. self.assertEqual(
  67. ChangesetFieldRule.get_rules(None)['name'].action,
  68. 'auto',
  69. )
  70. rule.unlink()
  71. self.assertFalse(ChangesetFieldRule.get_rules(None))