Skip to content

Commit

Permalink
Fixed missing call to param update method - V1 Workers (#1045)
Browse files Browse the repository at this point in the history
* Add missing calls to method param func, this checks and aligns any deprecated param names

* Fix unit test
  • Loading branch information
sambles committed May 17, 2024
1 parent e184376 commit e950589
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
19 changes: 16 additions & 3 deletions src/model_execution_worker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ def start_analysis(analysis_settings, input_location, complex_data_files=None, *
"""
# Check that the input archive exists and is valid
filestore = get_filestore(settings)
from oasislmf.manager import OasisManager

logger.info("args: {}".format(str(locals())))
logger.info(str(get_worker_versions()))
tmpdir_persist = settings.getboolean('worker', 'KEEP_RUN_DIR', fallback=False)
Expand Down Expand Up @@ -330,7 +333,10 @@ def start_analysis(analysis_settings, input_location, complex_data_files=None, *

# Create and log params
run_params = {**config, **task_params}
params = paths_to_absolute_paths(run_params, config_path)
gen_losses_params = OasisManager()._params_generate_losses(**run_params)
post_hook_params = OasisManager()._params_post_analysis(**run_params)
params = paths_to_absolute_paths({**gen_losses_params, **post_hook_params}, config_path)

if debug_worker:
log_params(params, kwargs)

Expand Down Expand Up @@ -391,7 +397,10 @@ def generate_input(self,

# Start Oasis file generation
notify_api_status(analysis_pk, 'INPUTS_GENERATION_STARTED')
filestore.media_root = settings.get('worker', 'MEDIA_ROOT')
filestore = get_filestore(settings)
from oasislmf.manager import OasisManager

# filestore.media_root = settings.get('worker', 'MEDIA_ROOT')
tmpdir_persist = settings.getboolean('worker', 'KEEP_RUN_DIR', fallback=False)
tmpdir_base = settings.get('worker', 'BASE_RUN_DIR', fallback=None)

Expand Down Expand Up @@ -432,7 +441,11 @@ def generate_input(self,
config_path = get_oasislmf_config_path(settings)
config = get_json(config_path)
lookup_params = {**{k: v for k, v in config.items() if not k.startswith('oed_')}, **task_params}
params = paths_to_absolute_paths(lookup_params, config_path)

gen_files_params = OasisManager()._params_generate_files(**lookup_params)
pre_hook_params = OasisManager()._params_exposure_pre_analysis(**lookup_params)
params = paths_to_absolute_paths({**gen_files_params, **pre_hook_params}, config_path)

if debug_worker:
log_params(params, kwargs, exclude_keys=[
'profile_loc',
Expand Down
15 changes: 10 additions & 5 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,26 @@ def test_custom_model_runner_does_not_exist___generate_losses_is_called_output_f
def fake_run_dir(*args, **kwargs):
yield run_dir

with patch('src.model_execution_worker.tasks.OasisManager', Mock(return_value=cmd_instance)) as cmd_mock, \
with patch('oasislmf.manager.OasisManager.generate_oasis_losses', Mock(return_value='mocked result')) as cmd_mock, \
patch('src.model_execution_worker.tasks.get_worker_versions', Mock(return_value='')), \
patch('src.model_execution_worker.tasks.filestore.compress') as tarfile, \
patch('src.model_execution_worker.tasks.TASK_LOG_DIR', log_dir), \
patch('src.model_execution_worker.tasks.TemporaryDir', fake_run_dir):

cmd_instance.generate_oasis_losses.return_value = "mocked result" # Mock the return value
output_location, log_location, error_location, returncode = start_analysis(
os.path.join(media_root, 'analysis_settings.json'),
os.path.join(media_root, 'location.tar'),
log_filename=log_file,
)
expected_params = {**params, **{"analysis_settings_json": os.path.join(media_root, 'analysis_settings.json')}}
cmd_instance.generate_oasis_losses.assert_called_once_with(**expected_params)
tarfile.assert_called_once_with(os.path.join(media_root, output_location), os.path.join(run_dir, 'output'), 'output')

cmd_mock.assert_called_once()
called_args = cmd_mock.call_args.kwargs
self.assertEqual(called_args.get('oasis_files_dir', None), params.get('oasis_files_dir'))
self.assertEqual(called_args.get('model_run_dir', None), params.get('model_run_dir'))
self.assertEqual(called_args.get('ktools_fifo_relative', None), params.get('ktools_fifo_relative'))
self.assertEqual(called_args.get('verbose', None), params.get('verbose'))
self.assertEqual(called_args.get('analysis_settings.json', None), params.get('analysis_settings.json'))
tarfile.assert_called_once_with(mock.ANY, os.path.join(run_dir, 'output'), 'output')


class StartAnalysisTask(TestCase):
Expand Down

0 comments on commit e950589

Please sign in to comment.