SIENTIAPDE-994
Refactor and enhance the laborious workflow and utilities - Removed outdated test file `test_predictions_batch.py` from workflows. - Added `input_sample.json` for standardized input configuration. - Introduced `connectors_config.py` to manage database and service configurations. - Implemented a logging utility in `logger.py` for consistent logging across the application. - Created `policies.py` to define retry policies for workflows. - Developed comprehensive tests for `MLFlowRepository` in `test_model_repository.py`. - Added extensive tests for `OpcRepository` in `test_opc_repository.py`. - Updated `test_predictions_batch.py` to reflect new workflow structure and testing methodology.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from unittest.mock import MagicMock
|
||||
from laborious.activities.base import BaseActivity
|
||||
from pytest import fixture
|
||||
from pytest import fixture, mark
|
||||
from sientia_do.notifications.models import Notification
|
||||
|
||||
|
||||
@@ -12,7 +12,8 @@ def base_activity():
|
||||
)
|
||||
|
||||
|
||||
def test_prepare_activity(base_activity):
|
||||
@mark.asyncio
|
||||
async def test_prepare_activity(base_activity):
|
||||
base_activity.notification_handler.base_notification = Notification(
|
||||
project="project",
|
||||
pipeline="pipeline",
|
||||
@@ -21,12 +22,14 @@ def test_prepare_activity(base_activity):
|
||||
model_id="-",
|
||||
)
|
||||
|
||||
base_activity.prepare_activity(
|
||||
schedule_name="test_schedule",
|
||||
model_name="test_model",
|
||||
model_id="test_model_id",
|
||||
)
|
||||
await base_activity.prepare_activity({
|
||||
'workflow_name': 'test_workflow',
|
||||
'schedule_name': 'test_schedule',
|
||||
'model_name': 'test_model',
|
||||
'model_id': 'test_model_id'
|
||||
})
|
||||
|
||||
assert base_activity.notification_handler.base_notification.schedule_name == "test_schedule"
|
||||
assert base_activity.notification_handler.base_notification.model_name == "test_model"
|
||||
assert base_activity.notification_handler.base_notification.model_id == "test_model_id"
|
||||
assert base_activity.notification_handler.base_notification.pipeline_name == "test_workflow"
|
||||
|
||||
@@ -51,7 +51,7 @@ async def test_input_gate_specific_variables_null_values_with_stop_policy_only(
|
||||
}
|
||||
|
||||
result = await gates.input_gate(input_data)
|
||||
assert result == ('stop', -1)
|
||||
assert result == ('stop', -1, 'Input data with bad quality')
|
||||
|
||||
input_args = specific_variables_null_values_mock.call_args
|
||||
assert input_args[0][0].equals(DataFrame(
|
||||
@@ -98,7 +98,7 @@ async def test_input_gate_specific_variables_null_values_with_continue_policy_on
|
||||
}
|
||||
|
||||
result = await gates.input_gate(input_data)
|
||||
assert result == ('continue', 2)
|
||||
assert result == ('continue', 2, 'Input data with bad quality')
|
||||
|
||||
input_args = specific_variables_null_values_mock.call_args
|
||||
assert input_args[0][0].equals(DataFrame(
|
||||
@@ -145,7 +145,7 @@ async def test_input_gate_specific_variables_null_values_no_filtered(
|
||||
}
|
||||
|
||||
result = await gates.input_gate(input_data)
|
||||
assert result == (None, 0)
|
||||
assert result == (None, 0, '')
|
||||
|
||||
specific_variables_null_values_input_args = specific_variables_null_values_mock.call_args
|
||||
|
||||
@@ -197,7 +197,7 @@ async def test_input_gate_one_stop_policy(
|
||||
}
|
||||
|
||||
result = await gates.input_gate(input_data)
|
||||
assert result == ('stop', -1)
|
||||
assert result == ('stop', -1, 'Input data with bad quality')
|
||||
|
||||
specific_variables_null_values_input_args = specific_variables_null_values_mock.call_args
|
||||
assert specific_variables_null_values_input_args[0][0].equals(DataFrame(
|
||||
@@ -251,7 +251,7 @@ async def test_input_gate_one_continue_policy(
|
||||
}
|
||||
|
||||
result = await gates.input_gate(input_data)
|
||||
assert result == ('continue', 2)
|
||||
assert result == ('continue', 2, 'Input data with bad quality')
|
||||
|
||||
specific_variables_null_values_input_args = specific_variables_null_values_mock.call_args
|
||||
assert specific_variables_null_values_input_args[0][0].equals(DataFrame(
|
||||
@@ -305,7 +305,7 @@ async def test_input_gate_no_filtered(
|
||||
}
|
||||
|
||||
result = await gates.input_gate(input_data)
|
||||
assert result == (None, 0)
|
||||
assert result == (None, 0, '')
|
||||
|
||||
specific_variables_null_values_input_args = specific_variables_null_values_mock.call_args
|
||||
assert specific_variables_null_values_input_args[0][0].equals(DataFrame(
|
||||
@@ -340,7 +340,7 @@ async def test_input_gate_error(
|
||||
}
|
||||
|
||||
result = await gates.input_gate(input_data)
|
||||
assert result == (None, 0)
|
||||
assert result == (None, 0, '')
|
||||
|
||||
gates.notification_handler.build_and_send_notification.assert_called_once_with(
|
||||
notification_id='INTPUT_GATE_ERROR__SPECIFIC_VARIABLES_NULL_VALUES',
|
||||
@@ -389,7 +389,7 @@ async def test_mlflow_response_gate_no_filtered(
|
||||
}
|
||||
|
||||
result = await gates.mlflow_response_gate(input_data)
|
||||
assert result == (None, 0)
|
||||
assert result == (None, 0, '')
|
||||
|
||||
api_error_filter_mock.assert_called_once_with(
|
||||
input_data['data'],
|
||||
@@ -433,7 +433,7 @@ async def test_mlflow_response_gate_filtered(
|
||||
}
|
||||
|
||||
result = await gates.mlflow_response_gate(input_data)
|
||||
assert result == ('continue', 255)
|
||||
assert result == ('continue', 255, "Error")
|
||||
|
||||
api_error_filter_mock.assert_called_once_with(
|
||||
input_data['data'],
|
||||
@@ -480,7 +480,7 @@ async def test_mlflow_content_gate_no_filtered(
|
||||
}
|
||||
|
||||
result = await gates.mlflow_content_gate(input_data)
|
||||
assert result == (None, 0)
|
||||
assert result == (None, 0, '')
|
||||
|
||||
nan_values_filter_mock_args = nan_values_filter_mock.call_args
|
||||
assert nan_values_filter_mock_args[0][0].equals(DataFrame(
|
||||
@@ -504,7 +504,8 @@ async def test_mlflow_content_gate_filtered(
|
||||
if x == 'path_confidence':
|
||||
return transform_filter_path_confidence
|
||||
|
||||
mlflow_content_filter_functions_mock.__getitem__.side_effect = transform_filter_functions_side_effect
|
||||
mlflow_content_filter_functions_mock.__getitem__.side_effect = \
|
||||
transform_filter_functions_side_effect
|
||||
|
||||
input_data = {
|
||||
'filters': {
|
||||
@@ -521,7 +522,8 @@ async def test_mlflow_content_gate_filtered(
|
||||
}
|
||||
|
||||
result = await gates.mlflow_content_gate(input_data)
|
||||
assert result == ('repeat', -1)
|
||||
assert result == (
|
||||
'repeat', -1, "Transformed data not passed the content filter")
|
||||
|
||||
nan_values_filter_mock_args = nan_values_filter_mock.call_args
|
||||
assert nan_values_filter_mock_args[0][0].equals(DataFrame(
|
||||
|
||||
@@ -61,7 +61,7 @@ async def test_request_transform(mock_max, mock_dataframe, mlflow):
|
||||
mlflow.model_monitoring_repository.transform.return_value = expected_response
|
||||
|
||||
# Call the method
|
||||
response_data, timestamp = await mlflow.request_transform(input_data)
|
||||
response_data = await mlflow.request_transform(input_data)
|
||||
|
||||
# Verify the data was correctly transformed
|
||||
mock_dataframe.assert_called_once_with(input_data['data'])
|
||||
@@ -75,7 +75,6 @@ async def test_request_transform(mock_max, mock_dataframe, mlflow):
|
||||
|
||||
# Verify the response
|
||||
assert response_data == expected_response
|
||||
assert timestamp == '2024-01-02'
|
||||
|
||||
# Verify the repository was called with correct arguments
|
||||
mlflow.model_monitoring_repository.transform.assert_called_once_with(
|
||||
|
||||
@@ -1,111 +1,120 @@
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from unittest.mock import patch, MagicMock, ANY, call
|
||||
from pytest import fixture, mark
|
||||
from laborious.activities.opc import NotificationLevel
|
||||
|
||||
from laborious.activities.opc import OPC
|
||||
from sientia_do.notifications.models import NotificationLevel
|
||||
from unittest.mock import ANY
|
||||
|
||||
|
||||
@patch("laborious.activities.opc.OpcRepository")
|
||||
def test___init__(mock_opc_repository):
|
||||
mock_logger = MagicMock()
|
||||
server1 = MagicMock()
|
||||
server2 = MagicMock()
|
||||
mock_opc_repository.side_effect = [server1, server2]
|
||||
mock_notification_handler = MagicMock()
|
||||
servers = {
|
||||
'server1': {
|
||||
'url': 'http://localhost:8080',
|
||||
'server_uri': 'opc.tcp://localhost:4840',
|
||||
'cert_path': '',
|
||||
'private_key_path': '',
|
||||
'server_cert_path': '',
|
||||
'reconnection_interval': 60,
|
||||
},
|
||||
'server2': {
|
||||
'url': 'http://localhost:8080',
|
||||
'server_uri': 'opc.tcp://localhost:4840',
|
||||
'cert_path': '',
|
||||
'private_key_path': '',
|
||||
'server_cert_path': '',
|
||||
'reconnection_interval': 60,
|
||||
}
|
||||
}
|
||||
opc = OPC(
|
||||
name="test",
|
||||
url="http://localhost:8080",
|
||||
server_uri="opc.tcp://localhost:4840",
|
||||
cert_path="",
|
||||
private_key_path="",
|
||||
server_cert_path="",
|
||||
logger=MagicMock(),
|
||||
notification_handler=MagicMock()
|
||||
opc_servers=servers,
|
||||
logger=mock_logger,
|
||||
notification_handler=mock_notification_handler
|
||||
)
|
||||
|
||||
assert opc.name == "test"
|
||||
assert opc.url == "http://localhost:8080"
|
||||
assert opc.server_uri == "opc.tcp://localhost:4840"
|
||||
assert opc.cert_path == ""
|
||||
assert opc.private_key_path == ""
|
||||
assert opc.server_cert_path == ""
|
||||
assert opc.opc_repository == mock_opc_repository.return_value
|
||||
assert opc.opc_servers == servers
|
||||
assert opc.logger == mock_logger
|
||||
assert opc.notification_handler == mock_notification_handler
|
||||
assert opc.opc_repository['server1'] == server1
|
||||
assert opc.opc_repository['server2'] == server2
|
||||
|
||||
mock_opc_repository.assert_called_once_with(
|
||||
name="test",
|
||||
url="http://localhost:8080",
|
||||
server_uri="opc.tcp://localhost:4840",
|
||||
cert_path="",
|
||||
private_key_path="",
|
||||
server_cert_path="",
|
||||
logger=opc.logger,
|
||||
)
|
||||
mock_opc_repository.assert_has_calls([
|
||||
call(
|
||||
name="server1",
|
||||
url="http://localhost:8080",
|
||||
logger=mock_logger,
|
||||
server_uri="opc.tcp://localhost:4840",
|
||||
cert_path="",
|
||||
private_key_path="",
|
||||
server_cert_path="",
|
||||
notification_handler=mock_notification_handler,
|
||||
reconnection_interval=60,
|
||||
),
|
||||
])
|
||||
mock_opc_repository.assert_has_calls([
|
||||
call(
|
||||
name="server2",
|
||||
url="http://localhost:8080",
|
||||
logger=mock_logger,
|
||||
server_uri="opc.tcp://localhost:4840",
|
||||
cert_path="",
|
||||
private_key_path="",
|
||||
server_cert_path="",
|
||||
notification_handler=mock_notification_handler,
|
||||
reconnection_interval=60,
|
||||
)
|
||||
])
|
||||
|
||||
opc.opc_repository.connect.assert_called_once()
|
||||
server1.connect.assert_called_once()
|
||||
server2.connect.assert_called_once()
|
||||
|
||||
|
||||
@fixture
|
||||
@patch("laborious.activities.opc.OpcRepository")
|
||||
def opc(mock_opc_repository):
|
||||
def opc(_mock_opc_repository):
|
||||
servers = {
|
||||
'server1': {
|
||||
'url': 'http://localhost:8080',
|
||||
'server_uri': 'opc.tcp://localhost:4840',
|
||||
'cert_path': '',
|
||||
'private_key_path': '',
|
||||
'server_cert_path': '',
|
||||
'reconnection_interval': 60,
|
||||
}
|
||||
}
|
||||
return OPC(
|
||||
name="test",
|
||||
url="http://localhost:8080",
|
||||
server_uri="opc.tcp://localhost:4840",
|
||||
cert_path="",
|
||||
private_key_path="",
|
||||
server_cert_path="",
|
||||
opc_servers=servers,
|
||||
logger=MagicMock(),
|
||||
notification_handler=MagicMock()
|
||||
)
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_write_opc_data_success(opc):
|
||||
# Arrange
|
||||
input_data = {
|
||||
'data': {
|
||||
'prediction': [0.75],
|
||||
'prediction_confidence': [0.95]
|
||||
},
|
||||
'opc_servers': ['server1'],
|
||||
'opc_output_config': {
|
||||
'prediction_tags': {
|
||||
'tag1': {'data_type': 'float'}
|
||||
},
|
||||
'confidence_tags': {
|
||||
'tag2': {'data_type': 'float'}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Act
|
||||
await opc.write_opc_data(input_data)
|
||||
|
||||
# Assert
|
||||
opc.opc_repository.write_data.assert_any_call('tag1', 0.75, 'float')
|
||||
opc.opc_repository.write_data.assert_any_call('tag2', 0.95, 'float')
|
||||
assert opc.opc_repository.write_data.call_count == 2
|
||||
WRITE_DATA_CASES = [
|
||||
('tag1', 'int', 50),
|
||||
('tag2', 'float', 50.5),
|
||||
('tag3', 'bool', True),
|
||||
('tag4', 'string', 'test'),
|
||||
]
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_write_opc_data_prediction_error(opc):
|
||||
# Arrange
|
||||
input_data = {
|
||||
'data': {
|
||||
'prediction': [0.75],
|
||||
'prediction_confidence': [0.95]
|
||||
},
|
||||
'opc_servers': ['server1'],
|
||||
'opc_output_config': {
|
||||
'prediction_tags': {
|
||||
'tag1': {'data_type': 'float'}
|
||||
}
|
||||
}
|
||||
}
|
||||
@mark.parametrize('tag,data_type,data', WRITE_DATA_CASES)
|
||||
def test_write_data_success(opc, tag, data_type, data):
|
||||
opc.write_data(server='server1', tag=tag, data=data,
|
||||
data_type=data_type, tag_type='prediction')
|
||||
opc.opc_repository['server1'].write_data.assert_called_once_with(
|
||||
tag, data, data_type)
|
||||
|
||||
opc.opc_repository.write_data.side_effect = Exception("Test error")
|
||||
|
||||
# Act
|
||||
await opc.write_opc_data(input_data)
|
||||
|
||||
# Assert
|
||||
opc.notification_handler.build_and_send_notification.assert_called_with(
|
||||
def test_write_data_exception(opc):
|
||||
opc.opc_repository['server1'].write_data.side_effect = Exception(
|
||||
"Test error")
|
||||
opc.write_data(server='server1', tag='tag1', data=50,
|
||||
data_type='int', tag_type='prediction')
|
||||
opc.notification_handler.build_and_send_notification.assert_called_once_with(
|
||||
notification_id="WRITE_OPC_PREDICTION_ERROR",
|
||||
message="Error writing data to OPC server: Test error",
|
||||
block="write_opc_data",
|
||||
@@ -116,44 +125,48 @@ async def test_write_opc_data_prediction_error(opc):
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_write_opc_data_confidence_error(opc):
|
||||
async def test_write_opc_data_success(opc):
|
||||
# Arrange
|
||||
input_data = {
|
||||
'data': {
|
||||
'prediction': [0.75],
|
||||
'prediction_confidence': [0.95]
|
||||
},
|
||||
'opc_servers': ['server1'],
|
||||
'opc_output_config': {
|
||||
'prediction_tags': {
|
||||
'tag1': {'data_type': 'float'}
|
||||
},
|
||||
'confidence_tags': {
|
||||
'tag2': {'data_type': 'float'}
|
||||
'server1': {
|
||||
'prediction_tags': {
|
||||
'tag1': {'data_type': 'float'}
|
||||
},
|
||||
'confidence_tags': {
|
||||
'tag2': {'data_type': 'float'}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Make first call succeed but second fail
|
||||
def side_effect(*args, **kwargs):
|
||||
if args[0] == 'tag2':
|
||||
raise ValueError("Test error")
|
||||
return None
|
||||
|
||||
opc.opc_repository.write_data.side_effect = side_effect
|
||||
|
||||
# Act
|
||||
opc.write_data = MagicMock()
|
||||
await opc.write_opc_data(input_data)
|
||||
|
||||
# Assert
|
||||
opc.notification_handler.build_and_send_notification.assert_called_with(
|
||||
notification_id="WRITE_OPC_CONFIDENCE_ERROR",
|
||||
message="Error writing data to OPC server: Test error",
|
||||
block="write_opc_data",
|
||||
level=NotificationLevel.ERROR,
|
||||
attachment_content=ANY
|
||||
)
|
||||
opc.logger.error.assert_called_once()
|
||||
opc.write_data.assert_has_calls([
|
||||
call(
|
||||
server='server1',
|
||||
tag='tag1',
|
||||
data=0.75,
|
||||
data_type='float',
|
||||
tag_type='prediction'
|
||||
)])
|
||||
opc.write_data.assert_has_calls([
|
||||
call(
|
||||
server='server1',
|
||||
tag='tag2',
|
||||
data=0.95,
|
||||
data_type='float',
|
||||
tag_type='confidence'
|
||||
)
|
||||
])
|
||||
assert opc.write_data.call_count == 2
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
@@ -175,4 +188,4 @@ async def test_write_opc_data_empty_config(opc):
|
||||
await opc.write_opc_data(input_data)
|
||||
|
||||
# Assert
|
||||
opc.opc_repository.write_data.assert_not_called()
|
||||
opc.opc_repository['server1'].write_data.assert_not_called()
|
||||
|
||||
Reference in New Issue
Block a user