From 470e4fbd5e304da20123b59ef4f75103c7874533 Mon Sep 17 00:00:00 2001 From: duongtq Date: Mon, 9 Oct 2023 15:31:57 +0700 Subject: [PATCH] [IMP] sale_order_import: Add tests for create/update order button --- sale_order_import/tests/test_order_import.py | 80 +++++++++++++++++++ sale_order_import/wizard/sale_order_import.py | 4 - 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/sale_order_import/tests/test_order_import.py b/sale_order_import/tests/test_order_import.py index 2972f7002a..30ae34156e 100644 --- a/sale_order_import/tests/test_order_import.py +++ b/sale_order_import/tests/test_order_import.py @@ -3,6 +3,11 @@ # @author: Simone Orsi # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import base64 +from unittest import mock + +from odoo.tests.common import Form + from .common import TestCommon @@ -65,3 +70,78 @@ def test_order_import_default_so_vals(self): sale_order_import__default_vals=dict(order=default) ).create_order(self.parsed_order, "pricelist") self.assertEqual(order.client_order_ref, "OVERRIDE") + + def test_with_order_buttons(self): + # Prepare test data + order_file_data = base64.b64encode( + b"baz" + ) + order_filename = "test_order.xml" + mock_parse_order = mock.patch.object(type(self.wiz_model), "parse_xml_order") + # Create a new form + with Form( + self.wiz_model.with_context( + default_order_filename=order_filename, + ) + ) as form: + with mock_parse_order as mocked: + # Return 'rfq' for doc_type + mocked.return_value = "rfq" + # Set values for the required fields + form.import_type = "xml" + form.order_file = order_file_data + mocked.assert_called() + # Test the button with the simulated values + mocked.return_value = self.parsed_order + action = form.save().import_order_button() + self.assertEqual(action["xml_id"], "sale.action_quotations") + self.assertEqual(action["view_mode"], "form,tree,calendar,graph") + self.assertEqual(action["view_id"], False) + mocked.assert_called() + so = self.env["sale.order"].browse(action["res_id"]) + self.assertEqual(so.partner_id.email, "deco.addict82@example.com") + self.assertEqual(so.client_order_ref, "TEST1242") + self.assertEqual(so.order_line.product_id.code, "FURN_8888") + self.assertEqual(so.state, "draft") + + # Create another form to update the above sale order + with Form( + self.wiz_model.with_context( + default_order_filename=order_filename, + ) + ) as form: + with mock_parse_order as mocked: + # Return 'rfq' for doc_type + mocked.return_value = "rfq" + # Set the required fields + form.import_type = "xml" + form.order_file = order_file_data + parsed_order_up = dict( + self.parsed_order, + lines=[ + { + "product": {"code": "FURN_8888"}, + "qty": 3, + "uom": {"unece_code": "C62"}, + "price_unit": 12.42, + }, + { + "product": {"code": "FURN_9999"}, + "qty": 1, + "uom": {"unece_code": "C62"}, + "price_unit": 1.42, + }, + ], + ) + mocked.return_value = parsed_order_up + action = form.save().import_order_button() + form = form.save() + self.assertEqual( + action["xml_id"], "sale_order_import.sale_order_import_action" + ) + self.assertEqual(form.state, "update") + self.assertEqual(form.sale_id, so) + form.update_order_button() + + self.assertEqual(len(so.order_line), 2) + self.assertEqual(so.order_line[0].product_uom_qty, 3) diff --git a/sale_order_import/wizard/sale_order_import.py b/sale_order_import/wizard/sale_order_import.py index 7c858dcea6..583f2b360b 100644 --- a/sale_order_import/wizard/sale_order_import.py +++ b/sale_order_import/wizard/sale_order_import.py @@ -354,7 +354,6 @@ def parse_order(self, order_file, order_filename, partner=False): ) return parsed_order - # TODO: add tests def import_order_button(self): self.ensure_one() bdio = self.env["business.document.import"] @@ -406,7 +405,6 @@ def create_order_button(self): ) return self.create_order_return_action(parsed_order, self.order_filename) - # TODO: add tests def create_order_return_action(self, parsed_order, order_filename): self.ensure_one() order = self.create_order(parsed_order, self.price_source, order_filename) @@ -443,7 +441,6 @@ def _prepare_update_order_vals(self, parsed_order, order, partner): vals["client_order_ref"] = parsed_order["order_ref"] return vals - # TODO: add tests @api.model def _prepare_create_order_line( self, product, uom, order, import_line, price_source @@ -612,7 +609,6 @@ def _prepare_update_order_line_vals(self, change_dict): # Allows other module to update some fields on the line return {} - # TODO: add tests def update_order_button(self): self.ensure_one() bdio = self.env["business.document.import"]