|
| 1 | +# Copyright 2026 Camptocamp SA |
| 2 | +# @author Simone Orsi <simone.orsi@camptocamp.com> |
| 3 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 4 | + |
| 5 | +from odoo.tests.common import TransactionCase |
| 6 | + |
| 7 | +from .common import OrderMixin, PurchaseEDIBackendTestMixin |
| 8 | + |
| 9 | + |
| 10 | +class TestGenerateViaConf(TransactionCase, PurchaseEDIBackendTestMixin, OrderMixin): |
| 11 | + """Verify that purchase EDI generation is driven by ``edi.configuration``. |
| 12 | +
|
| 13 | + No component / no fake handler: we simply assert that the snippets bound |
| 14 | + to the partner via ``partner_id.edi_purchase_conf_ids`` are executed by |
| 15 | + the state-change event dispatched by ``edi.exchange.consumer.mixin``. |
| 16 | +
|
| 17 | + Each snippet writes a marker on ``conf.description`` so we can verify |
| 18 | + which configurations actually ran. |
| 19 | + """ |
| 20 | + |
| 21 | + # Snippet writes the order's state on the conf description if it matches |
| 22 | + # the expected target state. |
| 23 | + _snippet_tpl = ( |
| 24 | + "if record.state == '{state}':\n" |
| 25 | + " conf.write({{'description': " |
| 26 | + "(conf.description or '') + '|' + record.state}})" |
| 27 | + ) |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def setUpClass(cls): |
| 31 | + super().setUpClass() |
| 32 | + cls._setup_env() |
| 33 | + cls._setup_records() |
| 34 | + |
| 35 | + cls.exc_type = cls._create_exchange_type( |
| 36 | + name="Demo Purchase Order out", |
| 37 | + code="demo_PurchaseOrder_out", |
| 38 | + direction="output", |
| 39 | + exchange_filename_pattern="{record_name}-{type.code}-{dt}", |
| 40 | + exchange_file_ext="xml", |
| 41 | + ) |
| 42 | + cls.state_change_trigger = cls.env.ref( |
| 43 | + "edi_purchase_oca.edi_conf_trigger_purchase_order_state_change" |
| 44 | + ) |
| 45 | + purchase_model_id = cls.env["ir.model"]._get_id("purchase.order") |
| 46 | + cls.edi_conf_confirmed = cls.env["edi.configuration"].create( |
| 47 | + { |
| 48 | + "name": "Demo Purchase Order - order confirmed", |
| 49 | + "type_id": cls.exc_type.id, |
| 50 | + "backend_id": cls.backend.id, |
| 51 | + "model_id": purchase_model_id, |
| 52 | + "trigger_id": cls.state_change_trigger.id, |
| 53 | + "snippet_do": cls._snippet_tpl.format(state="purchase"), |
| 54 | + } |
| 55 | + ) |
| 56 | + cls.edi_conf_cancelled = cls.env["edi.configuration"].create( |
| 57 | + { |
| 58 | + "name": "Demo Purchase Order - order cancelled", |
| 59 | + "type_id": cls.exc_type.id, |
| 60 | + "backend_id": cls.backend.id, |
| 61 | + "model_id": purchase_model_id, |
| 62 | + "trigger_id": cls.state_change_trigger.id, |
| 63 | + "snippet_do": cls._snippet_tpl.format(state="cancel"), |
| 64 | + } |
| 65 | + ) |
| 66 | + cls._setup_order() |
| 67 | + |
| 68 | + def test_new_order_no_conf_no_output(self): |
| 69 | + # No conf linked to the vendor -> no snippet executed. |
| 70 | + order = self._create_purchase_order() |
| 71 | + order.button_confirm() |
| 72 | + self.assertFalse(self.edi_conf_confirmed.description) |
| 73 | + self.assertFalse(self.edi_conf_cancelled.description) |
| 74 | + |
| 75 | + def test_new_order_1conf_output(self): |
| 76 | + self.vendor.edi_purchase_conf_ids = self.edi_conf_confirmed |
| 77 | + order = self._create_purchase_order() |
| 78 | + self.assertFalse(self.edi_conf_confirmed.description) |
| 79 | + order.button_confirm() |
| 80 | + self.assertEqual(self.edi_conf_confirmed.description, "|purchase") |
| 81 | + # The cancelled conf is not even attached to the vendor. |
| 82 | + self.assertFalse(self.edi_conf_cancelled.description) |
| 83 | + |
| 84 | + def test_new_order_2conf_output(self): |
| 85 | + self.vendor.edi_purchase_conf_ids = ( |
| 86 | + self.edi_conf_confirmed | self.edi_conf_cancelled |
| 87 | + ) |
| 88 | + order = self._create_purchase_order() |
| 89 | + # Confirm -> only the "confirmed" snippet matches |
| 90 | + order.button_confirm() |
| 91 | + self.assertEqual(self.edi_conf_confirmed.description, "|purchase") |
| 92 | + self.assertFalse(self.edi_conf_cancelled.description) |
| 93 | + # Cancel -> the "cancelled" snippet matches |
| 94 | + order.button_cancel() |
| 95 | + self.assertEqual(self.edi_conf_confirmed.description, "|purchase") |
| 96 | + self.assertEqual(self.edi_conf_cancelled.description, "|cancel") |
0 commit comments