Skip to content

Commit

Permalink
added a pytest fixture to handle comparison of use case commands and …
Browse files Browse the repository at this point in the history
…environment variable values to remove a lot of redundant logic in each wrapper test. Added fake madis data
  • Loading branch information
georgemccabe committed May 16, 2024
1 parent 39881e8 commit f6dae72
Show file tree
Hide file tree
Showing 22 changed files with 166 additions and 437 deletions.
Empty file.
Empty file.
32 changes: 32 additions & 0 deletions internal/tests/pytests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,35 @@ def get_test_data_path(subdir):
return os.path.join(internal_tests_dir, 'data', subdir)

return get_test_data_path


@pytest.fixture(scope="function")
def compare_command_and_env_vars():
def do_comparison(all_commands, expected_cmds, env_var_values, wrapper,
special_values=None):
print(f"ALL COMMANDS: {all_commands}")
assert len(all_commands) == len(expected_cmds)

missing_env = [item for item in env_var_values
if item not in wrapper.WRAPPER_ENV_VAR_KEYS
and item != 'DIAG_ARG']
env_var_keys = wrapper.WRAPPER_ENV_VAR_KEYS + missing_env

for (cmd, env_vars), expected_cmd in zip(all_commands, expected_cmds):
# ensure commands are generated as expected
assert cmd == expected_cmd

# check that environment variables were set properly
# including deprecated env vars (not in wrapper env var keys)
for env_var_key in env_var_keys:
print(f"ENV VAR: {env_var_key}")
match = next((item for item in env_vars if
item.startswith(env_var_key)), None)
assert match is not None
value = match.split('=', 1)[1]
if special_values is not None and env_var_key in special_values:
assert value == special_values[env_var_key]
else:
assert env_var_values.get(env_var_key, '') == value

return do_comparison
28 changes: 3 additions & 25 deletions internal/tests/pytests/wrappers/ascii2nc/test_ascii2nc_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ def test_ascii2nc_missing_inputs(metplus_config, get_test_data_dir,
]
)
@pytest.mark.wrapper
def test_ascii2nc_wrapper(metplus_config, config_overrides, env_var_values):
def test_ascii2nc_wrapper(metplus_config, config_overrides, env_var_values,
compare_command_and_env_vars):
wrapper = ascii2nc_wrapper(metplus_config, config_overrides)
assert wrapper.isOK

Expand Down Expand Up @@ -234,30 +235,7 @@ def test_ascii2nc_wrapper(metplus_config, config_overrides, env_var_values):
f"-config {config_file} {verbosity}"),
]

assert len(all_commands) == len(expected_cmds)
for (cmd, _), expected_cmd in zip(all_commands, expected_cmds):
# ensure commands are generated as expected
assert cmd == expected_cmd

env_vars = all_commands[0][1]

missing_env = [item for item in env_var_values
if item not in wrapper.WRAPPER_ENV_VAR_KEYS]
env_var_keys = wrapper.WRAPPER_ENV_VAR_KEYS + missing_env

# check that environment variables were set properly
# including deprecated env vars (not in wrapper env var keys)
for env_var_key in env_var_keys:
match = next((item for item in env_vars if
item.startswith(env_var_key)), None)
assert match is not None
value = match.split('=', 1)[1]

assert env_var_values.get(env_var_key, '') == value

output_base = wrapper.config.getdir('OUTPUT_BASE')
if output_base:
shutil.rmtree(output_base)
compare_command_and_env_vars(all_commands, expected_cmds, env_var_values, wrapper)


@pytest.mark.wrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ def test_ensemble_stat_field_info(metplus_config, config_overrides,
)
@pytest.mark.wrapper_c
def test_ensemble_stat_single_field(metplus_config, config_overrides,
env_var_values):
env_var_values, compare_command_and_env_vars):

config = metplus_config

Expand Down Expand Up @@ -687,31 +687,12 @@ def test_ensemble_stat_single_field(metplus_config, config_overrides,
]

all_cmds = wrapper.run_all_times()
print(f"ALL COMMANDS: {all_cmds}")
assert len(all_cmds) == len(expected_cmds)

missing_env = [item for item in env_var_values
if item not in wrapper.WRAPPER_ENV_VAR_KEYS]
env_var_keys = wrapper.WRAPPER_ENV_VAR_KEYS + missing_env

for (cmd, env_vars), expected_cmd in zip(all_cmds, expected_cmds):
# ensure commands are generated as expected
assert cmd == expected_cmd

# check that environment variables were set properly
# including deprecated env vars (not in wrapper env var keys)
for env_var_key in env_var_keys:
print(f"ENV VAR: {env_var_key}")
match = next((item for item in env_vars if
item.startswith(env_var_key)), None)
assert match is not None
actual_value = match.split('=', 1)[1]
if env_var_key == 'METPLUS_FCST_FIELD':
assert actual_value == fcst_fmt
elif env_var_key == 'METPLUS_OBS_FIELD':
assert actual_value == obs_fmt
else:
assert env_var_values.get(env_var_key, '') == actual_value
special_values = {
'METPLUS_FCST_FIELD': fcst_fmt,
'METPLUS_OBS_FIELD': obs_fmt,
}
compare_command_and_env_vars(all_cmds, expected_cmds, env_var_values,
wrapper, special_values)


@pytest.mark.wrapper_c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def test_gen_ens_prod_missing_inputs(metplus_config, get_test_data_dir, allow_mi
)
@pytest.mark.wrapper
def test_gen_ens_prod_single_field(metplus_config, config_overrides,
env_var_values):
env_var_values, compare_command_and_env_vars):

config = metplus_config

Expand Down Expand Up @@ -508,28 +508,11 @@ def test_gen_ens_prod_single_field(metplus_config, config_overrides,
]

all_cmds = wrapper.run_all_times()
print(f"ALL COMMANDS: {all_cmds}")
assert len(all_cmds) == len(expected_cmds)

missing_env = [item for item in env_var_values
if item not in wrapper.WRAPPER_ENV_VAR_KEYS]
env_var_keys = wrapper.WRAPPER_ENV_VAR_KEYS + missing_env

for (cmd, env_vars), expected_cmd in zip(all_cmds, expected_cmds):
# ensure commands are generated as expected
assert(cmd == expected_cmd)

# check that environment variables were set properly
# including deprecated env vars (not in wrapper env var keys)
for env_var_key in env_var_keys:
match = next((item for item in env_vars if
item.startswith(env_var_key)), None)
assert(match is not None)
actual_value = match.split('=', 1)[1]
if env_var_key == 'METPLUS_ENS_FIELD':
assert (actual_value == ens_fmt)
else:
assert(env_var_values.get(env_var_key, '') == actual_value)
special_values = {
'METPLUS_ENS_FIELD': ens_fmt,
}
compare_command_and_env_vars(all_cmds, expected_cmds, env_var_values,
wrapper, special_values)


@pytest.mark.parametrize(
Expand Down
30 changes: 7 additions & 23 deletions internal/tests/pytests/wrappers/grid_diag/test_grid_diag.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ def test_get_config_file(metplus_config):
]
)
@pytest.mark.wrapper
def test_grid_diag(metplus_config, config_overrides, env_var_values):
def test_grid_diag(metplus_config, config_overrides, env_var_values,
compare_command_and_env_vars):
config = metplus_config
set_minimum_config_settings(config)

Expand Down Expand Up @@ -385,25 +386,8 @@ def test_grid_diag(metplus_config, config_overrides, env_var_values):
]

all_cmds = wrapper.run_all_times()
print(f"ALL COMMANDS: {all_cmds}")
assert len(all_cmds) == len(expected_cmds)

missing_env = [item for item in env_var_values
if item not in wrapper.WRAPPER_ENV_VAR_KEYS]
env_var_keys = wrapper.WRAPPER_ENV_VAR_KEYS + missing_env

for (cmd, env_vars), expected_cmd in zip(all_cmds, expected_cmds):
# ensure commands are generated as expected
assert cmd == expected_cmd

# check that environment variables were set properly
# including deprecated env vars (not in wrapper env var keys)
for env_var_key in env_var_keys:
match = next((item for item in env_vars if
item.startswith(env_var_key)), None)
assert match is not None
actual_value = match.split('=', 1)[1]
if env_var_key == 'METPLUS_DATA_DICT':
assert actual_value == data_fmt
else:
assert env_var_values.get(env_var_key, '') == actual_value
special_values = {
'METPLUS_DATA_DICT': data_fmt,
}
compare_command_and_env_vars(all_cmds, expected_cmds, env_var_values,
wrapper, special_values)
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ def test_grid_stat_is_prob(metplus_config, config_overrides, expected_values):
)
@pytest.mark.wrapper_b
def test_grid_stat_single_field(metplus_config, config_overrides,
env_var_values):
env_var_values, compare_command_and_env_vars):

config = metplus_config

Expand All @@ -748,7 +748,6 @@ def test_grid_stat_single_field(metplus_config, config_overrides,
for index in range(0, len(run_times)):
extra_args[index] += f'-ugrid_config {ugrid_config_file} '


app_path = os.path.join(config.getdir('MET_BIN_DIR'), wrapper.app_name)
verbosity = f"-v {wrapper.c_dict['VERBOSITY']}"
config_file = wrapper.c_dict.get('CONFIG_FILE')
Expand All @@ -764,31 +763,12 @@ def test_grid_stat_single_field(metplus_config, config_overrides,
]

all_cmds = wrapper.run_all_times()
print(f"ALL COMMANDS: {all_cmds}")

missing_env = [item for item in env_var_values
if item not in wrapper.WRAPPER_ENV_VAR_KEYS]
env_var_keys = wrapper.WRAPPER_ENV_VAR_KEYS + missing_env

assert len(all_cmds) == len(expected_cmds)
for (cmd, env_vars), expected_cmd in zip(all_cmds, expected_cmds):
# ensure commands are generated as expected
assert cmd == expected_cmd

# check that environment variables were set properly
# including deprecated env vars (not in wrapper env var keys)
for env_var_key in env_var_keys:
print(f"ENV VAR: {env_var_key}")
match = next((item for item in env_vars if
item.startswith(env_var_key)), None)
assert match is not None
actual_value = match.split('=', 1)[1]
if env_var_key == 'METPLUS_FCST_FIELD':
assert actual_value == fcst_fmt
elif env_var_key == 'METPLUS_OBS_FIELD':
assert actual_value == obs_fmt
else:
assert env_var_values.get(env_var_key, '') == actual_value
special_values = {
'METPLUS_FCST_FIELD': fcst_fmt,
'METPLUS_OBS_FIELD': obs_fmt,
}
compare_command_and_env_vars(all_cmds, expected_cmds, env_var_values,
wrapper, special_values)


@pytest.mark.wrapper_b
Expand Down
22 changes: 2 additions & 20 deletions internal/tests/pytests/wrappers/ioda2nc/test_ioda2nc_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def test_ioda2nc_missing_inputs(metplus_config, get_test_data_dir, missing,
)
@pytest.mark.wrapper
def test_ioda2nc_wrapper(metplus_config, config_overrides,
env_var_values, extra_args):
env_var_values, extra_args, compare_command_and_env_vars):
config = metplus_config

set_minimum_config_settings(config)
Expand Down Expand Up @@ -273,25 +273,7 @@ def test_ioda2nc_wrapper(metplus_config, config_overrides,
]

all_cmds = wrapper.run_all_times()
print(f"ALL COMMANDS: {all_cmds}")
assert len(all_cmds) == len(expected_cmds)

missing_env = [item for item in env_var_values
if item not in wrapper.WRAPPER_ENV_VAR_KEYS]
env_var_keys = wrapper.WRAPPER_ENV_VAR_KEYS + missing_env

for (cmd, env_vars), expected_cmd in zip(all_cmds, expected_cmds):
# ensure commands are generated as expected
assert cmd == expected_cmd

# check that environment variables were set properly
# including deprecated env vars (not in wrapper env var keys)
for env_var_key in env_var_keys:
match = next((item for item in env_vars if
item.startswith(env_var_key)), None)
assert match is not None
actual_value = match.split('=', 1)[1]
assert env_var_values.get(env_var_key, '') == actual_value
compare_command_and_env_vars(all_cmds, expected_cmds, env_var_values, wrapper)


@pytest.mark.wrapper
Expand Down
Loading

0 comments on commit f6dae72

Please sign in to comment.