Skip to content

Commit

Permalink
fixup! fixup! fixup! edi_sale_ubl: split input/output
Browse files Browse the repository at this point in the history
  • Loading branch information
simahawk committed Dec 5, 2024
1 parent a6646c9 commit 086f9f9
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 66 deletions.
41 changes: 15 additions & 26 deletions edi_sale_ubl_input_oca/tests/test_order_in_full_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,25 @@ def setUpClass(cls):
def _get_backend(cls):
return cls.env.ref("edi_ubl_oca.edi_backend_ubl_demo")

def _create_order(self):
# Simulate order creation via incoming EDI exchange
order = self.env["sale.order"].create(
{
"client_order_ref": self.client_order_ref,
"partner_id": self.order_data.partner.id,
"origin_exchange_record_id": self.exc_record_in.id,
}
)
self.exc_record_in._set_related_record(order)
self.exc_type_in.edi_exchange_state = "input_processed"
return order

# No need to test sending data
@mock.patch("odoo.addons.edi_oca.models.edi_backend.EDIBackend._exchange_send")
@mock.patch("odoo.addons.edi_oca.models.edi_backend.EDIBackend._exchange_generate")
def test_new_order(self, mock_generate, mock_send):
order = self._find_order()
self.backend._check_input_exchange_sync()
self.assertEqual(self.exc_record_in.edi_exchange_state, "input_processed")
order = self._find_order()
def test_new_order(self, mock_send):
order = self._create_order()
order.partner_id.edi_sale_conf_ids = self.edi_conf
self.assertEqual(self.exc_record_in.record, order)
order_msg = order.message_ids[0]
self.assertIn("Exchange processed successfully", order_msg.body)
self.assertIn(self.exc_record_in.identifier, order_msg.body)
order.invalidate_cache()
# Test relations
self.assertEqual(len(order.exchange_record_ids), 1)
exc_record = order.exchange_record_ids.filtered(
lambda x: x.type_id == self.exc_type_in
)
self.assertEqual(exc_record, self.exc_record_in)
# Confirm the order
mock_generate.return_value = "<xml>fake</xml>"
order.action_confirm()
# Should give us a valid order response ack record
ack_exc_record = order.exchange_record_ids.filtered(
Expand All @@ -70,11 +67,3 @@ def test_new_order(self, mock_generate, mock_send):
file_content = ack_exc_record._get_file_content()
self.assertEqual(file_content, "<xml>fake</xml>")
self.assertEqual(ack_exc_record.edi_exchange_state, "output_sent")

def _find_order(self):
return self.env["sale.order"].search(
[
("client_order_ref", "=", self.order_data.client_order_ref),
("commercial_partner_id", "=", self.order_data.partner.parent_id.id),
]
)
2 changes: 1 addition & 1 deletion edi_sale_ubl_output_oca/demo/edi_exchange_type.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ components:
<record id="demo_edi_sale_ubl_output_so_in" model="edi.exchange.type">
<field name="backend_type_id" ref="edi_ubl_oca.edi_backend_type_ubl" />
<field name="backend_id" ref="edi_ubl_oca.edi_backend_ubl_demo" />
<field name="ack_type_id" ref="demo_edi_exc_type_order_response_out" />
<field name="ack_type_id" ref="demo_edi_sale_ubl_output_so_out" />
<field name="name">Demo UBL Sale Order</field>
<field name="code">demo_edi_sale_ubl_output_so_in</field>
<field name="direction">input</field>
Expand Down
2 changes: 1 addition & 1 deletion edi_sale_ubl_output_oca/demo/exc_templ_order_response.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<field name="code">ubl.generate.OrderResponse</field>
<field name="output_type">xml</field>
<field name="backend_type_id" ref="edi_ubl_oca.edi_backend_type_ubl" />
<field name="type_id" ref="demo_edi_exc_type_order_response_out" />
<field name="type_id" ref="demo_edi_sale_ubl_output_so_out" />
<field name="template_id" ref="qwb_tmpl_ubl_order_response_out" />
<field name="code_snippet">
seller = record.company_id.partner_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@
<cbc:SalesOrderID t-esc="record.name">SO1234</cbc:SalesOrderID>
</cac:OrderReference>
<cac:SellerSupplierParty>
<t t-call="edi_sale_ubl_oca.qweb_tmpl_ubl_party">
<t t-call="edi_sale_ubl_output_oca.qweb_tmpl_ubl_party">
<t t-set="party" t-value="seller_party" />
</t>
</cac:SellerSupplierParty>
<cac:BuyerCustomerParty>
<t t-call="edi_sale_ubl_oca.qweb_tmpl_ubl_party">
<t t-call="edi_sale_ubl_output_oca.qweb_tmpl_ubl_party">
<t t-set="party" t-value="buyer_party" />
</t>
</cac:BuyerCustomerParty>
Expand Down Expand Up @@ -88,7 +88,7 @@
t-esc="time_to_string(record.commitment_date)"
/>
<cac:DeliveryAddress t-if="delivery.partner_id">
<t t-call="edi_sale_ubl_oca.qweb_tmpl_ubl_address">
<t t-call="edi_sale_ubl_output_oca.qweb_tmpl_ubl_address">
<t t-set="partner" t-value="delivery.partner_id" />
</t>
</cac:DeliveryAddress>
Expand Down
74 changes: 45 additions & 29 deletions edi_sale_ubl_output_oca/tests/test_order_in_full_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,80 @@

from unittest import mock

from odoo.tests.common import SavepointCase
from odoo import fields

from odoo.addons.edi_oca.tests.common import EDIBackendTestMixin

from .common import OrderInboundTestMixin, get_xml_handler
from odoo.addons.edi_oca.tests.common import EDIBackendCommonComponentTestCase
from odoo.addons.edi_sale_ubl_oca.tests.common import (
OrderInboundTestMixin,
get_xml_handler,
)

# TODO: split in different tests w/ SingleTransaction


class TestOrderInboundFull(SavepointCase, EDIBackendTestMixin, OrderInboundTestMixin):
class TestOrderInboundFull(EDIBackendCommonComponentTestCase, OrderInboundTestMixin):

_schema_path = "base_ubl:data/xsd-2.2/maindoc/UBL-OrderResponse-2.2.xsd"

maxDiff = None

@classmethod
def setUpClass(cls):
super().setUpClass()
cls._setup_env()
cls.backend = cls._get_backend()
cls._setup_inbound_order(cls.backend)
cls.edi_conf = cls.env.ref("edi_sale_oca.demo_edi_configuration_confirmed")
cls.exc_type_out = cls.env.ref(
"edi_sale_ubl_output_oca.demo_edi_sale_ubl_output_so_out"
)
cls.exc_type_in = cls.env.ref(
"edi_sale_ubl_output_oca.demo_edi_sale_ubl_output_so_in"
)
cls._setup_inbound_order(cls.backend, cls.exc_type_in)
cls.edi_conf = cls.env.ref(
"edi_sale_oca.demo_edi_configuration_confirmed"
).copy(
{
"name": "UBL IN EDI Conf",
"type_id": cls.exc_type_out.id,
"backend_id": cls.backend.id,
}
)

@classmethod
def _get_backend(cls):
return cls.env.ref("edi_ubl_oca.edi_backend_ubl_demo")

def _create_order(self):
# Simulate order creation via incoming EDI exchange
order = self.env["sale.order"].create(
{
"client_order_ref": self.client_order_ref,
"partner_id": self.order_data.partner.id,
"origin_exchange_record_id": self.exc_record_in.id,
"commitment_date": fields.Date.today(),
}
)
self.exc_record_in._set_related_record(order)
self.exc_record_in.edi_exchange_state = "input_processed"
order.invalidate_cache()
return order

# No need to test sending data
@mock.patch("odoo.addons.edi_oca.models.edi_backend.EDIBackend._exchange_send")
def test_new_order(self, mock_send):
self.backend._check_input_exchange_sync()
self.assertEqual(self.exc_record_in.edi_exchange_state, "input_processed")
order = self._find_order()
order = self._create_order()
order.partner_id.edi_sale_conf_ids = self.edi_conf
self.assertEqual(self.exc_record_in.record, order)
order_msg = order.message_ids[0]
self.assertIn("Exchange processed successfully", order_msg.body)
self.assertIn(self.exc_record_in.identifier, order_msg.body)
order.invalidate_cache()
# Test relations
self.assertEqual(len(order.exchange_record_ids), 1)
exc_record = order.exchange_record_ids.filtered(
lambda x: x.type_id == self.exc_type_in
)
self.assertEqual(exc_record, self.exc_record_in)
# Confirm the order
order.action_confirm()
# Should give us a valid order response ack record
ack_exc_record = order.exchange_record_ids.filtered(
lambda x: x.type_id == self.exc_type_out
)
file_content = ack_exc_record._get_file_content()
self.assertTrue(file_content)
# TMP /
# path = "/tmp/order.response.test.xml"
# with open(path, "w") as out:
# out.write(file_content)
# / TMP
self.assertEqual(ack_exc_record.edi_exchange_state, "output_sent")
handler = get_xml_handler(self.backend, self._schema_path)
# Test is a valid file
err = handler.validate(file_content)
self.assertEqual(err, None, err)
# TODO: test data
data = handler.parse_xml(file_content)
# TODO: test all main data
self.assertEqual(data["cbc:OrderResponseCode"], "AP")
13 changes: 7 additions & 6 deletions edi_sale_ubl_output_oca/tests/test_order_response_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
from freezegun import freeze_time

from odoo.addons.edi_sale_oca.tests.common import OrderMixin
from odoo.addons.edi_sale_ubl_oca.tests.common import get_xml_handler
from odoo.addons.edi_xml_oca.tests.common import XMLComponentTestCase

from .common import get_xml_handler


class TestOrderResponseOutbound(XMLComponentTestCase, OrderMixin):

Expand All @@ -21,12 +20,14 @@ def setUpClass(cls):
super().setUpClass()
cls.backend = cls._get_backend()
cls.order = cls._setup_order()
cls.exc_type_in = cls.env.ref("edi_sale_ubl_oca.demo_edi_exc_type_order_in")
cls.exc_type_out = cls.env.ref(
"edi_sale_ubl_oca.demo_edi_exc_type_order_response_out"
"edi_sale_ubl_output_oca.demo_edi_sale_ubl_output_so_out"
)
cls.exc_type_in = cls.env.ref(
"edi_sale_ubl_output_oca.demo_edi_sale_ubl_output_so_in"
)
cls.exc_tmpl = cls.env.ref(
"edi_sale_ubl_oca.demo_edi_exc_template_order_response_out"
"edi_sale_ubl_output_oca.demo_edi_exc_template_order_response_out"
)
vals = {
"model": cls.order._name,
Expand All @@ -47,7 +48,7 @@ def test_get_template(self):
self.assertEqual(template, self.exc_tmpl)
self.assertEqual(
template.template_id.key,
"edi_sale_ubl_oca.qwb_tmpl_ubl_order_response_out",
"edi_sale_ubl_output_oca.qwb_tmpl_ubl_order_response_out",
)

def test_render_values(self):
Expand Down

0 comments on commit 086f9f9

Please sign in to comment.