From 1b93eb19cb89aad083094182b0f285eb2e771ea7 Mon Sep 17 00:00:00 2001 From: vitor-aignosi Date: Mon, 27 Oct 2025 08:10:19 -0300 Subject: [PATCH] SIENTIAPDE-1314 Refactor OPC Class and Enhance Testing for Output Management - Removed the unused 'success' parameter from the manage_output_tags method in the OPC class to streamline its signature. - Commented out the error handling logic in the OpcRepository for clarity and future reference. - Added new tests for manage_output_tags to validate successful and failed write operations, ensuring accurate assertions for output data and metrics. --- laborious/activities/opc.py | 3 +- laborious/utils/repository/opc_repository.py | 2 +- tests/laborious/activities/test_opc.py | 80 +++++++++++++++++++- 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/laborious/activities/opc.py b/laborious/activities/opc.py index 2f1379c..d614433 100644 --- a/laborious/activities/opc.py +++ b/laborious/activities/opc.py @@ -199,7 +199,6 @@ class OPC(BaseActivity): config: dict[str, Any], data: DataFrame, metadata: dict[str, Any], - success: bool, ) -> tuple[bool, dict[str, float | None]]: """ Manage the writing of prediction and confidence data to OPC server tags. @@ -305,7 +304,7 @@ class OPC(BaseActivity): continue local_success, local_response_times = await self.manage_output_tags( - server_id, config, data, metadata, success + server_id, config, data, metadata ) metrics[server_id] = local_response_times local_count = len(local_response_times) diff --git a/laborious/utils/repository/opc_repository.py b/laborious/utils/repository/opc_repository.py index 1bd5684..95bfd77 100644 --- a/laborious/utils/repository/opc_repository.py +++ b/laborious/utils/repository/opc_repository.py @@ -263,7 +263,7 @@ class OpcRepository(BaseActivity): if self.client is None: return await self.connect() - # if self.error_count > 5: + # if self.error_count > 5: # NOSONAR # self.logger.custom_warning( # f'OPC server {self.id} will be disconnected due to multiple errors', self.metadata # ) diff --git a/tests/laborious/activities/test_opc.py b/tests/laborious/activities/test_opc.py index 76afed5..e9c9d74 100644 --- a/tests/laborious/activities/test_opc.py +++ b/tests/laborious/activities/test_opc.py @@ -257,6 +257,85 @@ async def test_write_data_exception(opc): raise AssertionError('Expected an exception to be raised') +@mark.asyncio +async def test_manage_output_tags_success(opc): + opc.write_data = AsyncMock(return_value=0.1) + + data = DataFrame({'prediction': [0.75], 'prediction_confidence': [0.95]}) + config = { + 'prediction_tags': {'tag1': {'data_type': 'float'}}, + 'confidence_tags': {'tag2': {'data_type': 'float'}}, + } + + output_data, opc_metrics = await opc.manage_output_tags( + server_id='server1', + config=config, + data=data, + metadata=metadata['metadata'], + ) + + assert output_data is True + assert opc_metrics == {'tag1': 0.1, 'tag2': 0.1} + opc.write_data.assert_has_calls( + [ + call( + server_id='server1', + tag='tag1', + data=0.75, + data_type='float', + tag_type='prediction', + metadata=metadata['metadata'], + ), + call( + server_id='server1', + tag='tag2', + data=0.95, + data_type='float', + tag_type='confidence', + metadata=metadata['metadata'], + ), + ] + ) + + +@mark.asyncio +async def test_manage_output_tags_failed(opc): + opc.write_data = AsyncMock(side_effect=[0.1, None]) + data = DataFrame({'prediction': [0.75], 'prediction_confidence': [0.95]}) + config = { + 'prediction_tags': {'tag1': {'data_type': 'float'}}, + 'confidence_tags': {'tag2': {'data_type': 'float'}}, + } + output_data, opc_metrics = await opc.manage_output_tags( + server_id='server1', + config=config, + data=data, + metadata=metadata['metadata'], + ) + assert output_data is False + assert opc_metrics == {'tag1': 0.1, 'tag2': None} + opc.write_data.assert_has_calls( + [ + call( + server_id='server1', + tag='tag1', + data=0.75, + data_type='float', + tag_type='prediction', + metadata=metadata['metadata'], + ), + call( + server_id='server1', + tag='tag2', + data=0.95, + data_type='float', + tag_type='confidence', + metadata=metadata['metadata'], + ), + ] + ) + + @mark.asyncio @patch('laborious.activities.opc.DataFrame') async def test_write_opc_data_success(mock_dataframe, opc): @@ -286,7 +365,6 @@ async def test_write_opc_data_success(mock_dataframe, opc): input_data['opc_output_config']['server1'], mock_dataframe.return_value, metadata['metadata'], - True, ) opc.process_confidence.assert_called_once_with( mock_dataframe.return_value,