SIENTIAPDE-1430: Refactor static threshold calculation logic and enhance test coverage

This commit is contained in:
Bruno Domingues
2025-12-19 16:19:10 -03:00
parent 06571011f2
commit df6bf1daba
4 changed files with 118 additions and 9 deletions

View File

@@ -493,6 +493,70 @@ def test_save_run_with_equation(
assert '/tmp/test_run/model_equation.json' in logged_artifacts # noqa: S108
@patch('model_manager.utils.repository.model_repository.ModelServing')
@patch('model_manager.utils.repository.model_repository.path.exists')
def test_save_run_with_static_threshold_value(
mock_exists, mock_model_serving_class, mock_logger, mock_train_result
):
"""Test _save_run logs static_threshold when rem_static_win is True and value is set."""
from model_manager.utils.repository.model_repository import ModelRepository
mock_model_serving_instance = MagicMock()
mock_model_serving_class.return_value = mock_model_serving_instance
repo = ModelRepository(
url='http://mlflow.test', username='user', password='pass', logger=mock_logger
)
# Set static_threshold to a specific value
mock_train_result.params.rem_static_win = True
mock_train_result.params.static_threshold = 500
mock_train_result.equation_path = None
# Mock all path.exists calls to return True
mock_exists.return_value = True
repo._save_run(mock_train_result)
# Verify static_threshold was logged with the correct value
log_param_calls = {
call[0][0]: call[0][1] for call in mock_model_serving_instance.log_param.call_args_list
}
assert log_param_calls['static_threshold'] == 500
@patch('model_manager.utils.repository.model_repository.ModelServing')
@patch('model_manager.utils.repository.model_repository.path.exists')
def test_save_run_with_rem_static_win_false(
mock_exists, mock_model_serving_class, mock_logger, mock_train_result
):
"""Test _save_run logs static_threshold as None when rem_static_win is False."""
from model_manager.utils.repository.model_repository import ModelRepository
mock_model_serving_instance = MagicMock()
mock_model_serving_class.return_value = mock_model_serving_instance
repo = ModelRepository(
url='http://mlflow.test', username='user', password='pass', logger=mock_logger
)
# Set rem_static_win to False
mock_train_result.params.rem_static_win = False
mock_train_result.params.static_threshold = 500 # Should be ignored
mock_train_result.equation_path = None
# Mock all path.exists calls to return True
mock_exists.return_value = True
repo._save_run(mock_train_result)
# Verify static_threshold was logged as None
log_param_calls = {
call[0][0]: call[0][1] for call in mock_model_serving_instance.log_param.call_args_list
}
assert log_param_calls['static_threshold'] is None
@patch('model_manager.utils.repository.model_repository.ModelServing')
@patch('model_manager.utils.repository.model_repository.path.exists')
def test_save_run_without_equation(

View File

@@ -261,6 +261,39 @@ class TestInitDataPreprocessor:
assert preprocessor.lag_transform == {'var1': 3, 'var2': 3, 'var3': 3}
class TestGetStaticThreshold:
"""Tests for _get_static_threshold method."""
def test_get_static_threshold_rem_static_win_false(self, training_repo, sample_params):
"""Test returns None when rem_static_win is False."""
sample_params.rem_static_win = False
sample_params.static_threshold = 500
result = training_repo._get_static_threshold(sample_params)
assert result is None
def test_get_static_threshold_rem_static_win_true_with_value(
self, training_repo, sample_params
):
"""Test returns static_threshold value when rem_static_win is True and value is set."""
sample_params.rem_static_win = True
sample_params.static_threshold = 500
result = training_repo._get_static_threshold(sample_params)
assert result == 500
def test_get_static_threshold_rem_static_win_true_with_none(self, training_repo, sample_params):
"""Test returns 1 when rem_static_win is True and static_threshold is None."""
sample_params.rem_static_win = True
sample_params.static_threshold = None
result = training_repo._get_static_threshold(sample_params)
assert result == 1
class TestInitScalerDict:
"""Tests for _init_scaler_dict method."""