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.

112 lines
3.8 KiB

  1. # Copyright 2018 Eficent Business and IT Consulting Services S.L.
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  3. from odoo.tests import common
  4. from odoo.exceptions import UserError
  5. from .common import setup_test_model, teardown_test_model
  6. from .tier_validation_tester import TierValidationTester
  7. @common.at_install(False)
  8. @common.post_install(True)
  9. class TierTierValidation(common.SavepointCase):
  10. @classmethod
  11. def setUpClass(cls):
  12. super(TierTierValidation, cls).setUpClass()
  13. setup_test_model(cls.env, [TierValidationTester])
  14. cls.test_model = cls.env[TierValidationTester._name]
  15. cls.tester_model = cls.env['ir.model'].search([
  16. ('model', '=', 'tier.validation.tester')])
  17. # Access record:
  18. cls.env["ir.model.access"].create({
  19. 'name': "access.tester",
  20. 'model_id': cls.tester_model.id,
  21. 'perm_read': 1,
  22. 'perm_write': 1,
  23. 'perm_create': 1,
  24. 'perm_unlink': 1,
  25. })
  26. # Create users:
  27. group_ids = cls.env.ref('base.group_system').ids
  28. cls.test_user_1 = cls.env['res.users'].create({
  29. 'name': 'John',
  30. 'login': 'test1',
  31. 'groups_id': [(6, 0, group_ids)],
  32. })
  33. cls.test_user_2 = cls.env['res.users'].create({
  34. 'name': 'Mike',
  35. 'login': 'test2',
  36. })
  37. cls.test_user_3 = cls.env['res.users'].create({
  38. 'name': 'Mary',
  39. 'login': 'test3',
  40. })
  41. # Create tier definitions:
  42. cls.tier_def_obj = cls.env['tier.definition']
  43. cls.tier_def_obj.create({
  44. 'model_id': cls.tester_model.id,
  45. 'review_type': 'individual',
  46. 'reviewer_id': cls.test_user_1.id,
  47. 'definition_domain': "[('test_field', '>', 1.0)]",
  48. })
  49. cls.test_record = cls.test_model.create({
  50. 'test_field': 2.5,
  51. })
  52. @classmethod
  53. def tearDownClass(cls):
  54. teardown_test_model(cls.env,
  55. [TierValidationTester])
  56. super(TierTierValidation, cls).tearDownClass()
  57. def test_01_reviewer_from_python_expression(self):
  58. tier_definition = self.tier_def_obj.create({
  59. 'model_id': self.tester_model.id,
  60. 'review_type': 'individual',
  61. 'reviewer_id': self.test_user_1.id,
  62. 'definition_type': 'formula',
  63. 'python_code': 'rec.test_field > 1.0',
  64. })
  65. tier_definition.write({
  66. 'model_id': self.tester_model.id,
  67. 'review_type': 'expression',
  68. 'python_code': 'rec.test_field > 3.0',
  69. })
  70. tier_definition.onchange_review_type()
  71. tier_definition.write({
  72. 'reviewer_expression': 'rec.user_id',
  73. })
  74. self.test_record.write({
  75. 'test_field': 3.5,
  76. 'user_id': self.test_user_2.id,
  77. })
  78. reviews = self.test_record.sudo(
  79. self.test_user_3.id).request_validation()
  80. self.assertTrue(reviews)
  81. self.assertEqual(len(reviews), 2)
  82. record = self.test_record.sudo(self.test_user_1.id)
  83. self.assertIn(self.test_user_1, record.reviewer_ids)
  84. self.assertIn(self.test_user_2, record.reviewer_ids)
  85. res = self.test_model.search(
  86. [('reviewer_ids', 'in', self.test_user_2.id)])
  87. self.assertTrue(res)
  88. def test_02_wrong_reviewer_expression(self):
  89. """Error should raise with incorrect python expresions on
  90. tier definitions."""
  91. self.tier_def_obj.create({
  92. 'model_id': self.tester_model.id,
  93. 'review_type': 'expression',
  94. 'reviewer_expression': 'rec.test_field',
  95. 'python_code': 'rec.test_field > 1.0',
  96. })
  97. with self.assertRaises(UserError):
  98. self.test_record.sudo(self.test_user_3.id).request_validation()