Skip to content

Commit

Permalink
refactor and clean up to reduce SonarQube issues
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemccabe committed Jan 24, 2025
1 parent 4efb2cc commit f6f82cf
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 253 deletions.
51 changes: 47 additions & 4 deletions internal/tests/pytests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import pytest

import sys
import os
import shlex
import subprocess
import pytest
import getpass
import shutil
from datetime import datetime, timedelta
from unittest import mock
from pathlib import Path
from netCDF4 import Dataset
Expand Down Expand Up @@ -33,6 +32,50 @@
print(f"ERROR: Cannot write to $METPLUS_TEST_OUTPUT_BASE: {output_base}")
sys.exit(2)

# used in tests like ioda2nc and ascii2nc to test runtime frequency options
# values correspond to missing, run, thresh, errors, allow_missing, runtime_freq
obs_to_nc_runtime_freq_test_params = [
(16, 24, 0.3, 0, True, 'RUN_ONCE_FOR_EACH'),
(16, 24, 0.7, 1, True, 'RUN_ONCE_FOR_EACH'),
(16, 24, 0.3, 16, False, 'RUN_ONCE_FOR_EACH'),
(2, 4, 0.4, 0, True, 'RUN_ONCE_PER_INIT_OR_VALID'),
(2, 4, 0.6, 1, True, 'RUN_ONCE_PER_INIT_OR_VALID'),
(2, 4, 0.6, 16, False, 'RUN_ONCE_PER_INIT_OR_VALID'),
(2, 5, 0.4, 0, True, 'RUN_ONCE_PER_LEAD'),
(2, 5, 0.7, 1, True, 'RUN_ONCE_PER_LEAD'),
(2, 5, 0.4, 17, False, 'RUN_ONCE_PER_LEAD'),
(0, 1, 0.4, 0, True, 'RUN_ONCE'),
(0, 1, 0.4, 16, False, 'RUN_ONCE'),
]


# used in tests like grid_stat and wavelet_stat to test runtime frequency options
# values correspond to once_per_field, missing, run, thresh, errors, allow_missing
stat_runtime_freq_test_params = [
(False, 6, 12, 0.5, 0, True),
(False, 6, 12, 0.6, 1, True),
(True, 12, 24, 0.5, 0, True),
(True, 12, 24, 0.6, 1, True),
(False, 6, 12, 0.5, 10, False),
(True, 12, 24, 0.5, 20, False),
]

@pytest.fixture
def fcst_and_obs_data():
fcst_name = 'APCP'
fcst_level = 'A03'
obs_name = 'APCP_03'
obs_level = '"(*,*)"'

inits = ['2005080700', '2005080712']
time_fmt = '%Y%m%d%H'
lead_hour = 12
valids = []
for init in inits:
valid = datetime.strptime(init, time_fmt) + timedelta(hours=lead_hour)
valid = valid.strftime(time_fmt)
valids.append(valid)
return fcst_name, fcst_level, obs_name, obs_level, inits, valids, lead_hour, time_fmt

@pytest.fixture(scope="session", autouse=True)
def custom_tmpdir():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import pytest

import os
from datetime import datetime

from metplus.wrappers.grid_stat_wrapper import GridStatWrapper
from internal.tests.pytests.conftest import stat_runtime_freq_test_params

fcst_dir = '/some/path/fcst'
obs_dir = '/some/path/obs'
Expand Down Expand Up @@ -59,14 +59,7 @@ def set_minimum_config_settings(config):


@pytest.mark.parametrize(
'once_per_field, missing, run, thresh, errors, allow_missing', [
(False, 6, 12, 0.5, 0, True),
(False, 6, 12, 0.6, 1, True),
(True, 12, 24, 0.5, 0, True),
(True, 12, 24, 0.6, 1, True),
(False, 6, 12, 0.5, 10, False),
(True, 12, 24, 0.5, 20, False),
]
'once_per_field, missing, run, thresh, errors, allow_missing', stat_runtime_freq_test_params
)
@pytest.mark.wrapper_b
def test_grid_stat_missing_inputs(metplus_config, get_test_data_dir,
Expand Down
15 changes: 2 additions & 13 deletions internal/tests/pytests/wrappers/ioda2nc/test_ioda2nc_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os

from metplus.wrappers.ioda2nc_wrapper import IODA2NCWrapper
from internal.tests.pytests.conftest import obs_to_nc_runtime_freq_test_params

time_fmt = '%Y%m%d%H'
run_times = ['2020031012', '2020031100']
Expand Down Expand Up @@ -31,19 +32,7 @@ def set_minimum_config_settings(config):


@pytest.mark.parametrize(
'missing, run, thresh, errors, allow_missing, runtime_freq', [
(16, 24, 0.3, 0, True, 'RUN_ONCE_FOR_EACH'),
(16, 24, 0.7, 1, True, 'RUN_ONCE_FOR_EACH'),
(16, 24, 0.3, 16, False, 'RUN_ONCE_FOR_EACH'),
(2, 4, 0.4, 0, True, 'RUN_ONCE_PER_INIT_OR_VALID'),
(2, 4, 0.6, 1, True, 'RUN_ONCE_PER_INIT_OR_VALID'),
(2, 4, 0.6, 16, False, 'RUN_ONCE_PER_INIT_OR_VALID'),
(2, 5, 0.4, 0, True, 'RUN_ONCE_PER_LEAD'),
(2, 5, 0.7, 1, True, 'RUN_ONCE_PER_LEAD'),
(2, 5, 0.4, 17, False, 'RUN_ONCE_PER_LEAD'),
(0, 1, 0.4, 0, True, 'RUN_ONCE'),
(0, 1, 0.4, 16, False, 'RUN_ONCE'),
]
'missing, run, thresh, errors, allow_missing, runtime_freq', obs_to_nc_runtime_freq_test_params
)
@pytest.mark.wrapper
def test_ioda2nc_missing_inputs(metplus_config, get_test_data_dir, missing,
Expand Down
33 changes: 11 additions & 22 deletions internal/tests/pytests/wrappers/pair_stat/test_pair_stat_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,14 @@
import pytest

import os
from datetime import datetime, timedelta

from metplus.wrappers.pair_stat_wrapper import PairStatWrapper

pairs_dir = '/some/path/pairs'
pair_stat_format = 'mpr'

fcst_name = 'APCP'
fcst_level = 'A03'
obs_name = 'APCP_03'
obs_level = '"(*,*)"'

inits = ['2005080700', '2005080712']
time_fmt = '%Y%m%d%H'
lead_hour = 12
lead_hour_str = str(lead_hour).zfill(3)
valids = []
for init in inits:
valid = datetime.strptime(init, time_fmt) + timedelta(hours=lead_hour)
valid = valid.strftime(time_fmt)
valids.append(valid)


def set_minimum_config_settings(config):
def set_minimum_config_settings(config, fcst_and_obs_data):
_, _, _, _, inits, _, lead_hour, time_fmt = fcst_and_obs_data
# set config variables to prevent command from running and bypass check
# if input files actually exist
config.set('config', 'DO_NOT_RUN_EXE', True)
Expand Down Expand Up @@ -65,9 +49,10 @@ def set_minimum_config_settings(config):
@pytest.mark.wrapper_a
def test_pair_stat_missing_inputs(metplus_config, get_test_data_dir,
once_per_field, missing, run, thresh, errors,
allow_missing):
allow_missing, fcst_and_obs_data):
fcst_name, fcst_level, obs_name, obs_level, _, _, _, _ = fcst_and_obs_data
config = metplus_config
set_minimum_config_settings(config)
set_minimum_config_settings(config, fcst_and_obs_data)
config.set('config', 'INPUT_MUST_EXIST', True)
config.set('config', 'PAIR_STAT_ALLOW_MISSING_INPUTS', allow_missing)
config.set('config', 'PAIR_STAT_INPUT_THRESH', thresh)
Expand Down Expand Up @@ -661,10 +646,14 @@ def test_pair_stat_missing_inputs(metplus_config, get_test_data_dir,
)
@pytest.mark.wrapper_a
def test_pair_stat_all_fields(metplus_config, config_overrides,
env_var_values, compare_command_and_env_vars):
env_var_values, compare_command_and_env_vars,
fcst_and_obs_data):
_, _, _, _, inits, valids, lead_hour, _ = fcst_and_obs_data
lead_hour_str = str(lead_hour).zfill(3)
level_no_quotes = '(*,*)'
level_with_quotes = f'"{level_no_quotes}"'


fcsts = [{'name': 'TMP',
'level': 'P750-900',
'thresh': '<=273,>273'},
Expand Down Expand Up @@ -698,7 +687,7 @@ def test_pair_stat_all_fields(metplus_config, config_overrides,
obs_fmts.append(obs_fmt)

config = metplus_config
set_minimum_config_settings(config)
set_minimum_config_settings(config, fcst_and_obs_data)

for index, (fcst, obs) in enumerate(zip(fcsts, obss)):
idx = index + 1
Expand Down
15 changes: 2 additions & 13 deletions internal/tests/pytests/wrappers/pb2nc/test_pb2nc_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import datetime

from metplus.wrappers.pb2nc_wrapper import PB2NCWrapper
from internal.tests.pytests.conftest import obs_to_nc_runtime_freq_test_params

from metplus.util import time_util
from metplus.util import do_string_sub
Expand All @@ -27,19 +28,7 @@ def pb2nc_wrapper(metplus_config):


@pytest.mark.parametrize(
'missing, run, thresh, errors, allow_missing, runtime_freq', [
(16, 24, 0.3, 0, True, 'RUN_ONCE_FOR_EACH'),
(16, 24, 0.7, 1, True, 'RUN_ONCE_FOR_EACH'),
(16, 24, 0.3, 16, False, 'RUN_ONCE_FOR_EACH'),
(2, 4, 0.4, 0, True, 'RUN_ONCE_PER_INIT_OR_VALID'),
(2, 4, 0.6, 1, True, 'RUN_ONCE_PER_INIT_OR_VALID'),
(2, 4, 0.6, 16, False, 'RUN_ONCE_PER_INIT_OR_VALID'),
(2, 5, 0.4, 0, True, 'RUN_ONCE_PER_LEAD'),
(2, 5, 0.7, 1, True, 'RUN_ONCE_PER_LEAD'),
(2, 5, 0.4, 17, False, 'RUN_ONCE_PER_LEAD'),
(0, 1, 0.4, 0, True, 'RUN_ONCE'),
(0, 1, 0.4, 16, False, 'RUN_ONCE'),
]
'missing, run, thresh, errors, allow_missing, runtime_freq', obs_to_nc_runtime_freq_test_params
)
@pytest.mark.wrapper
def test_pb2nc_missing_inputs(metplus_config, get_test_data_dir, missing,
Expand Down
Loading

0 comments on commit f6f82cf

Please sign in to comment.