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.

113 lines
4.2 KiB

7 years ago
  1. # Copyright 2016 Akretion Mourad EL HADJ MIMOUNE
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from odoo.tests import common
  4. from odoo.exceptions import ValidationError
  5. from odoo import fields
  6. from .common import setup_test_model
  7. from .purchase_test import PurchaseTest, LineTest, ExceptionRule
  8. import logging
  9. _logger = logging.getLogger(__name__)
  10. @common.at_install(False)
  11. @common.post_install(True)
  12. class TestBaseException(common.SavepointCase):
  13. @classmethod
  14. def setUpClass(cls):
  15. super(TestBaseException, cls).setUpClass()
  16. setup_test_model(cls.env, [PurchaseTest, LineTest, ExceptionRule])
  17. cls.base_exception = cls.env["base.exception"]
  18. cls.exception_rule = cls.env["exception.rule"]
  19. if "test_purchase_ids" not in cls.exception_rule._fields:
  20. field = fields.Many2many("base.exception.test.purchase")
  21. cls.exception_rule._add_field("test_purchase_ids", field)
  22. cls.exception_confirm = cls.env["exception.rule.confirm"]
  23. cls.exception_rule._fields["model"].selection.append(
  24. ("base.exception.test.purchase", "Purchase Order")
  25. )
  26. cls.exception_rule._fields["model"].selection.append(
  27. ("base.exception.test.purchase.line", "Purchase Order Line")
  28. )
  29. cls.partner = cls.env.ref("base.res_partner_1")
  30. cls.partner.zip = False
  31. cls.potest1 = cls.env["base.exception.test.purchase"].create(
  32. {
  33. "name": "Test base exception to basic purchase",
  34. "partner_id": cls.partner.id,
  35. "line_ids": [
  36. (0, 0, {"name": "line test", "amount": 120.0, "qty": 1.5})
  37. ],
  38. }
  39. )
  40. def test_valid(self):
  41. self.potest1.button_confirm()
  42. self.assertFalse(self.potest1.exception_ids)
  43. def test_fail_by_py(self):
  44. self.exception_amount_total = self.env["exception.rule"].create(
  45. {
  46. "name": "Min order except",
  47. "sequence": 10,
  48. "model": "base.exception.test.purchase",
  49. "code": "if obj.amount_total <= 200.0: failed=True",
  50. "exception_type": "by_py_code",
  51. }
  52. )
  53. with self.assertRaises(ValidationError):
  54. self.potest1.button_confirm()
  55. self.assertTrue(self.potest1.exception_ids)
  56. def test_fail_by_domain(self):
  57. self.exception_partner_no_zip = self.env["exception.rule"].create(
  58. {
  59. "name": "No ZIP code on destination",
  60. "sequence": 10,
  61. "model": "base.exception.test.purchase",
  62. "domain": "[('partner_id.zip', '=', False)]",
  63. "exception_type": "by_domain",
  64. }
  65. )
  66. with self.assertRaises(ValidationError):
  67. self.potest1.button_confirm()
  68. self.assertTrue(self.potest1.exception_ids)
  69. def test_fail_by_method(self):
  70. self.exception_no_name = self.env["exception.rule"].create(
  71. {
  72. "name": "No name",
  73. "sequence": 10,
  74. "model": "base.exception.test.purchase",
  75. "method": "exception_method_no_zip",
  76. "exception_type": "by_method",
  77. }
  78. )
  79. with self.assertRaises(ValidationError):
  80. self.potest1.button_confirm()
  81. self.assertTrue(self.potest1.exception_ids)
  82. def test_ignore_exception(self):
  83. # same as 1st test
  84. self.exception_amount_total = self.env["exception.rule"].create(
  85. {
  86. "name": "Min order except",
  87. "sequence": 10,
  88. "model": "base.exception.test.purchase",
  89. "code": "if obj.amount_total <= 200.0: failed=True",
  90. "exception_type": "by_py_code",
  91. }
  92. )
  93. # Block because of exception during validation
  94. with self.assertRaises(ValidationError):
  95. self.potest1.button_confirm()
  96. # Test that we have linked exceptions
  97. self.assertTrue(self.potest1.exception_ids)
  98. # Test ignore exeception make possible for the po to validate
  99. self.potest1.action_ignore_exceptions()
  100. self.potest1.button_confirm()
  101. self.assertTrue(self.potest1.state == "purchase")