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.
This commit is contained in:
@@ -199,7 +199,6 @@ class OPC(BaseActivity):
|
|||||||
config: dict[str, Any],
|
config: dict[str, Any],
|
||||||
data: DataFrame,
|
data: DataFrame,
|
||||||
metadata: dict[str, Any],
|
metadata: dict[str, Any],
|
||||||
success: bool,
|
|
||||||
) -> tuple[bool, dict[str, float | None]]:
|
) -> tuple[bool, dict[str, float | None]]:
|
||||||
"""
|
"""
|
||||||
Manage the writing of prediction and confidence data to OPC server tags.
|
Manage the writing of prediction and confidence data to OPC server tags.
|
||||||
@@ -305,7 +304,7 @@ class OPC(BaseActivity):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
local_success, local_response_times = await self.manage_output_tags(
|
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
|
metrics[server_id] = local_response_times
|
||||||
local_count = len(local_response_times)
|
local_count = len(local_response_times)
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ class OpcRepository(BaseActivity):
|
|||||||
if self.client is None:
|
if self.client is None:
|
||||||
return await self.connect()
|
return await self.connect()
|
||||||
|
|
||||||
# if self.error_count > 5:
|
# if self.error_count > 5: # NOSONAR
|
||||||
# self.logger.custom_warning(
|
# self.logger.custom_warning(
|
||||||
# f'OPC server {self.id} will be disconnected due to multiple errors', self.metadata
|
# f'OPC server {self.id} will be disconnected due to multiple errors', self.metadata
|
||||||
# )
|
# )
|
||||||
|
|||||||
@@ -257,6 +257,85 @@ async def test_write_data_exception(opc):
|
|||||||
raise AssertionError('Expected an exception to be raised')
|
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
|
@mark.asyncio
|
||||||
@patch('laborious.activities.opc.DataFrame')
|
@patch('laborious.activities.opc.DataFrame')
|
||||||
async def test_write_opc_data_success(mock_dataframe, opc):
|
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'],
|
input_data['opc_output_config']['server1'],
|
||||||
mock_dataframe.return_value,
|
mock_dataframe.return_value,
|
||||||
metadata['metadata'],
|
metadata['metadata'],
|
||||||
True,
|
|
||||||
)
|
)
|
||||||
opc.process_confidence.assert_called_once_with(
|
opc.process_confidence.assert_called_once_with(
|
||||||
mock_dataframe.return_value,
|
mock_dataframe.return_value,
|
||||||
|
|||||||
Reference in New Issue
Block a user