|
| 1 | +# Copyright 2024 CamptoCamp SA |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +import mock |
| 5 | + |
| 6 | +from odoo.exceptions import UserError |
| 7 | + |
| 8 | +from odoo.addons.edi_oca.tests.common import EDIBackendCommonComponentRegistryTestCase |
| 9 | + |
| 10 | + |
| 11 | +class TestsPurchaseEDIConfiguration(EDIBackendCommonComponentRegistryTestCase): |
| 12 | + @classmethod |
| 13 | + def setUpClass(cls): |
| 14 | + super().setUpClass() |
| 15 | + cls._load_module_components(cls, "edi_purchase_oca") |
| 16 | + cls.edi_configuration = cls.env["edi.configuration"] |
| 17 | + cls.purchase_order = cls.env["purchase.order"] |
| 18 | + cls.product = cls.env["product.product"].create( |
| 19 | + { |
| 20 | + "name": "Product 1", |
| 21 | + "default_code": "1234567", |
| 22 | + } |
| 23 | + ) |
| 24 | + |
| 25 | + def setUp(self): |
| 26 | + super().setUp() |
| 27 | + self.confirm_conf = self.env.ref( |
| 28 | + "edi_purchase_oca.edi_conf_button_confirm_purchase_order" |
| 29 | + ) |
| 30 | + self.partner.write({"edi_purchase_conf_ids": [(4, self.confirm_conf.id)]}) |
| 31 | + |
| 32 | + def test_edi_configuration_snippet_before_do(self): |
| 33 | + order = self.purchase_order.create( |
| 34 | + { |
| 35 | + "partner_id": self.partner.id, |
| 36 | + "order_line": [ |
| 37 | + ( |
| 38 | + 0, |
| 39 | + 0, |
| 40 | + { |
| 41 | + "product_id": self.product.id, |
| 42 | + "product_qty": 10, |
| 43 | + "price_unit": 100.0, |
| 44 | + }, |
| 45 | + ) |
| 46 | + ], |
| 47 | + } |
| 48 | + ) |
| 49 | + self.assertTrue(order) |
| 50 | + self.assertEqual(order.state, "draft") |
| 51 | + |
| 52 | + # Replace snippet_before_do for test |
| 53 | + self.confirm_conf.snippet_before_do = "record.button_cancel()" |
| 54 | + order.button_confirm() |
| 55 | + # After purchase order is confirmed |
| 56 | + # it will be automatically canceled due to edi_configuration execution. |
| 57 | + self.assertEqual(order.state, "cancel") |
| 58 | + |
| 59 | + def test_edi_configuration_snippet_do(self): |
| 60 | + self.confirm_conf.write( |
| 61 | + { |
| 62 | + "backend_id": self.backend.id, |
| 63 | + "type_id": self.exchange_type_out.id, |
| 64 | + "snippet_do": "record._edi_send_via_edi(conf.type_id)", |
| 65 | + } |
| 66 | + ) |
| 67 | + with mock.patch.object( |
| 68 | + type(self.backend), "exchange_generate", return_value=True |
| 69 | + ): |
| 70 | + |
| 71 | + with self.assertRaises(UserError) as err: |
| 72 | + order = self.purchase_order.create( |
| 73 | + { |
| 74 | + "partner_id": self.partner.id, |
| 75 | + "order_line": [ |
| 76 | + ( |
| 77 | + 0, |
| 78 | + 0, |
| 79 | + { |
| 80 | + "product_id": self.product.id, |
| 81 | + "product_qty": 10, |
| 82 | + "price_unit": 100.0, |
| 83 | + }, |
| 84 | + ) |
| 85 | + ], |
| 86 | + } |
| 87 | + ) |
| 88 | + order.button_confirm() |
| 89 | + self.assertRegex( |
| 90 | + err.exception.args[0], r"Record ID=\d+ has no file to send!" |
| 91 | + ) |
0 commit comments