Skip to content

Commit

Permalink
Refactor test cases for DigiSync module
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jursimplicate committed Jun 13, 2024
1 parent 8b34b7c commit 51023a8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 38 deletions.
17 changes: 17 additions & 0 deletions product_digi_sync/tests/digi_sync_base_test_case.py
Original file line number Diff line number Diff line change
@@ -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)
43 changes: 21 additions & 22 deletions product_digi_sync/tests/test_product_category.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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()
27 changes: 11 additions & 16 deletions product_digi_sync/tests/test_product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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}
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 51023a8

Please sign in to comment.