From 51023a816b46f847839805d5aed402760ba81207 Mon Sep 17 00:00:00 2001 From: Jur de Vries Date: Thu, 13 Jun 2024 13:00:13 +0200 Subject: [PATCH] Refactor test cases for DigiSync module The test cases in the product_digi_sync module have been refactored for improved organization and separation of concerns. A base test case class, DigiSyncBaseTestCase, was introduced for encapsulating common functionality, reducing code duplication. Furthermore, increased the readability by structuring the unit test mock setups and cleanups. --- .../tests/digi_sync_base_test_case.py | 17 ++++++++ .../tests/test_product_category.py | 43 +++++++++---------- .../tests/test_product_template.py | 27 +++++------- 3 files changed, 49 insertions(+), 38 deletions(-) create mode 100644 product_digi_sync/tests/digi_sync_base_test_case.py diff --git a/product_digi_sync/tests/digi_sync_base_test_case.py b/product_digi_sync/tests/digi_sync_base_test_case.py new file mode 100644 index 0000000..54dabdd --- /dev/null +++ b/product_digi_sync/tests/digi_sync_base_test_case.py @@ -0,0 +1,17 @@ +from unittest.mock import patch + +from odoo.addons.base.models.ir_config_parameter import IrConfigParameter +from odoo.tests import TransactionCase + + +class DigiSyncBaseTestCase(TransactionCase): + def _patch_ir_config_parameter_for_get_param(self, client_id): + original_get_param = IrConfigParameter.get_param + + def patched_get_param(self, key, default=False): + if key == "digi_client_id": + return client_id # return a specific value for a particular key + else: + return original_get_param(self, key, default) + + return patch.object(IrConfigParameter, "get_param", patched_get_param) diff --git a/product_digi_sync/tests/test_product_category.py b/product_digi_sync/tests/test_product_category.py index e21b3ac..144468c 100644 --- a/product_digi_sync/tests/test_product_category.py +++ b/product_digi_sync/tests/test_product_category.py @@ -1,13 +1,11 @@ -from unittest.mock import patch - -from odoo.tests import TransactionCase +from unittest.mock import patch, Mock from odoo.addons.base.models.ir_config_parameter import IrConfigParameter from odoo.addons.product_digi_sync.models.digi_client import DigiClient from odoo.addons.queue_job.models.base import Base as QueueJobBase +from .digi_sync_base_test_case import DigiSyncBaseTestCase - -class ProductCategoryTestCase(TransactionCase): +class ProductCategoryTestCase(DigiSyncBaseTestCase): def setUp(self): super().setUp() @@ -59,21 +57,22 @@ def test_is_sends_the_category_to_digi_when_external_id_is_set(self): } ) - patch.object(IrConfigParameter, "get_param", digi_client.id) - - with patch.object( - DigiClient, "send_category_to_digi" - ) as mock_send_category_to_digi: - category = self.env["product.category"].create( - { - "name": "Test Category", - "external_digi_id": 1145, - } - ) - category.write( - { - "name": "Test Category altered", - } - ) + client_id = digi_client.id + patched_get_param = self._patch_ir_config_parameter_for_get_param(client_id) + patched_get_param.start() + send_category_to_digi = Mock() + mock_send_category_to_digi = patch.object(DigiClient, "send_category_to_digi", send_category_to_digi).start() + category = self.env["product.category"].create( + { + "name": "Test Category", + "external_digi_id": 1145, + } + ) + category.write( + { + "name": "Test Category altered", + } + ) - self.assertEqual(mock_send_category_to_digi.call_args[0][0], category) + self.assertEqual(mock_send_category_to_digi.call_args[0][0], category) + patched_get_param.stop() diff --git a/product_digi_sync/tests/test_product_template.py b/product_digi_sync/tests/test_product_template.py index 9b503f6..f5a69b5 100644 --- a/product_digi_sync/tests/test_product_template.py +++ b/product_digi_sync/tests/test_product_template.py @@ -9,9 +9,10 @@ from odoo.addons.base.models.ir_config_parameter import IrConfigParameter from odoo.addons.product_digi_sync.models.digi_client import DigiClient from odoo.addons.queue_job.models.base import Base as QueueJobBase +from .digi_sync_base_test_case import DigiSyncBaseTestCase -class ProductTemplateTestCase(TransactionCase): +class ProductTemplateTestCase(DigiSyncBaseTestCase): def setUp(self): super().setUp() @@ -29,7 +30,8 @@ def tearDown(self): def test_it_logs_an_error_when_plu_code_is_set_but_no_digi_client_is_provided( self, mock_logger ): - self._patch_ir_config_parameter_for_get_param("-1") + patched_get_param = self._patch_ir_config_parameter_for_get_param("-1") + patched_get_param.start() product = self.env["product.template"].create( {"name": "Test Product Template", "plu_code": 405} @@ -43,6 +45,7 @@ def test_it_logs_an_error_when_plu_code_is_set_but_no_digi_client_is_provided( mock_logger.assert_called_with( "Digi client requested, but no client was configured." ) + patched_get_param.stop() def test_it_sends_the_product_to_digi_when_plu_code_is_set(self): product1 = self.env["product.template"].create( @@ -56,7 +59,8 @@ def test_it_sends_the_product_to_digi_when_plu_code_is_set(self): digi_client = self._create_digi_client() - self._patch_ir_config_parameter_for_get_param(digi_client.id) + patched_get_param = self._patch_ir_config_parameter_for_get_param(digi_client.id) + patched_get_param.start() mock_send_product_to_digi = Mock() patch.object( DigiClient, "send_product_to_digi", mock_send_product_to_digi @@ -70,6 +74,7 @@ def test_it_sends_the_product_to_digi_when_plu_code_is_set(self): ) self.assertEqual(mock_send_product_to_digi.call_args[0][0], product1) + patched_get_param.stop() def _create_digi_client(self): digi_client = self.env["product_digi_sync.digi_client"].create( @@ -85,7 +90,8 @@ def test_it_sends_the_product_image_to_digi_when_the_image_is_set(self): digi_client = self._create_digi_client() client_id = digi_client.id - self._patch_ir_config_parameter_for_get_param(client_id) + patched_get_param = self._patch_ir_config_parameter_for_get_param(client_id) + patched_get_param.start() mock_send_product_image_to_digi = Mock() patch.object( DigiClient, "send_product_image_to_digi", mock_send_product_image_to_digi @@ -95,18 +101,7 @@ def test_it_sends_the_product_image_to_digi_when_the_image_is_set(self): product = self._create_product_with_image("Test Product Template", 400) self.assertEqual(mock_send_product_image_to_digi.call_args[0][0], product) - patch.object(IrConfigParameter, "get_param", digi_client.id).stop() - - def _patch_ir_config_parameter_for_get_param(self, client_id): - original_get_param = IrConfigParameter.get_param - - def patched_get_param(self, key, default=False): - if key == "digi_client_id": - return client_id # return a specific value for a particular key - else: - return original_get_param(self, key, default) - - patch.object(IrConfigParameter, "get_param", patched_get_param).start() + patched_get_param.stop() def _create_product_with_image(self, name, plu_code): product_with_image = self.env["product.template"].create(