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.

95 lines
3.5 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. from openerp.tests import common
  22. class TestChangesetFieldRule(common.TransactionCase):
  23. def setUp(self):
  24. super(TestChangesetFieldRule, self).setUp()
  25. self.company_model_id = self.env.ref('base.model_res_company').id
  26. self.field_name = self.env.ref('base.field_res_partner_name')
  27. self.field_street = self.env.ref('base.field_res_partner_street')
  28. def test_get_rules(self):
  29. ChangesetFieldRule = self.env['changeset.field.rule']
  30. ChangesetFieldRule.search([]).unlink()
  31. rule1 = ChangesetFieldRule.create({
  32. 'field_id': self.field_name.id,
  33. 'action': 'validate',
  34. })
  35. rule2 = ChangesetFieldRule.create({
  36. 'field_id': self.field_street.id,
  37. 'action': 'never',
  38. })
  39. get_rules = ChangesetFieldRule.get_rules(None)
  40. self.assertEqual(get_rules, {'name': rule1, 'street': rule2})
  41. def test_get_rules_source(self):
  42. ChangesetFieldRule = self.env['changeset.field.rule']
  43. ChangesetFieldRule.search([]).unlink()
  44. rule1 = ChangesetFieldRule.create({
  45. 'field_id': self.field_name.id,
  46. 'action': 'validate',
  47. })
  48. rule2 = ChangesetFieldRule.create({
  49. 'field_id': self.field_street.id,
  50. 'action': 'never',
  51. })
  52. rule3 = ChangesetFieldRule.create({
  53. 'source_model_id': self.company_model_id,
  54. 'field_id': self.field_street.id,
  55. 'action': 'never',
  56. })
  57. model = ChangesetFieldRule
  58. rules = model.get_rules(None)
  59. self.assertEqual(rules, {u'name': rule1, u'street': rule2})
  60. rules = model.get_rules('res.company')
  61. self.assertEqual(rules, {u'name': rule1, u'street': rule3})
  62. def test_get_rules_cache(self):
  63. ChangesetFieldRule = self.env['changeset.field.rule']
  64. ChangesetFieldRule.search([]).unlink()
  65. rule = ChangesetFieldRule.create({
  66. 'field_id': self.field_name.id,
  67. 'action': 'validate',
  68. })
  69. self.assertEqual(
  70. ChangesetFieldRule.get_rules(None)['name'].action,
  71. 'validate',
  72. )
  73. # Write on cursor to bypass the cache invalidation for the
  74. # matter of the test
  75. self.env.cr.execute("UPDATE changeset_field_rule "
  76. "SET action = 'never' "
  77. "WHERE id = %s", (rule.id,))
  78. self.assertEqual(
  79. ChangesetFieldRule.get_rules(None)['name'].action,
  80. 'validate',
  81. )
  82. rule.action = 'auto'
  83. self.assertEqual(
  84. ChangesetFieldRule.get_rules(None)['name'].action,
  85. 'auto',
  86. )
  87. rule.unlink()
  88. self.assertFalse(ChangesetFieldRule.get_rules(None))