def test_get_basin_cyclone_from_bdeck(metplus_config, template, filename, other_cyclone, other_basin): fake_dir = '/fake/dir' expected_basin = other_basin if other_basin else 'al' expected_cyclone = other_cyclone if other_cyclone else '1009' time_info = {'date': datetime(2014, 12, 31, 18)} config = metplus_config() set_minimum_config_settings(config) wrapper = TCPairsWrapper(config) wrapper.c_dict['BDECK_DIR'] = fake_dir wrapper.c_dict['BDECK_TEMPLATE'] = template full_filename = os.path.join(fake_dir, filename) for wildcard_used in [True, False]: if wildcard_used: basin = wrapper.WILDCARDS['basin'] cyclone = wrapper.WILDCARDS['cyclone'] else: basin = expected_basin cyclone = expected_cyclone actual_basin, actual_cyclone = (wrapper._get_basin_cyclone_from_bdeck( full_filename, wildcard_used, basin, cyclone, time_info)) assert actual_basin == expected_basin assert actual_cyclone == expected_cyclone
def test_parse_storm_id(metplus_config, storm_id, basin, cyclone): """! Check that storm ID is parsed properly to get basin and cyclone. Check that it returns wildcard expressions basin and cyclone cannot be parsed from storm ID """ config = metplus_config() set_minimum_config_settings(config) wrapper = TCPairsWrapper(config) actual_basin, actual_cyclone = wrapper._parse_storm_id(storm_id) # get wildcard expression if test input is 'wildcard' if basin == 'wildcard': expected_basin = wrapper.WILDCARDS['basin'] else: expected_basin = basin if cyclone == 'wildcard': expected_cyclone = wrapper.WILDCARDS['cyclone'] else: expected_cyclone = cyclone assert actual_basin == expected_basin assert actual_cyclone == expected_cyclone
def test_get_bdeck(metplus_config, basin, cyclone, expected_files, expected_wildcard): """! Checks that the correct list of empty test files are found and the correct boolean to signify if wildcards were used for different combinations of basin/cyclone inputs """ time_info = {'date': datetime(2014, 12, 31, 18)} config = metplus_config() set_minimum_config_settings(config) test_data_dir = os.path.join(config.getdir('METPLUS_BASE'), 'internal_tests', 'data', 'tc_pairs') bdeck_dir = os.path.join(test_data_dir, 'bdeck') config.set('config', 'TC_PAIRS_BDECK_INPUT_DIR', bdeck_dir) get_bdeck_template = ( 'get_bdeck_b{basin?fmt=%s}q{date?fmt=%Y%m%d%H}.gfso.{cyclone?fmt=%s}') config.set('config', 'TC_PAIRS_BDECK_TEMPLATE', get_bdeck_template) wrapper = TCPairsWrapper(config) actual_files, actual_wildcard = wrapper._get_bdeck(basin, cyclone, time_info) assert (actual_wildcard == expected_wildcard) assert (len(actual_files) == len(expected_files)) for actual_file, expected_file in zip(sorted(actual_files), sorted(expected_files)): assert (os.path.basename(actual_file) == expected_file)
def test_tc_pairs_storm_id_lists(metplus_config, config_overrides, storm_type, values_to_check): config = metplus_config() set_minimum_config_settings(config) config.set('config', 'INIT_TIME_FMT', '%Y') config.set('config', 'INIT_BEG', '2019') config.set('config', 'INIT_END', '2019') test_data_dir = os.path.join(config.getdir('METPLUS_BASE'), 'internal_tests', 'data', 'tc_pairs') bdeck_dir = os.path.join(test_data_dir, 'bdeck') edeck_dir = os.path.join(test_data_dir, 'edeck') config.set('config', 'TC_PAIRS_BDECK_INPUT_DIR', bdeck_dir) config.set('config', 'TC_PAIRS_EDECK_INPUT_DIR', edeck_dir) config.set('config', 'TC_PAIRS_BDECK_TEMPLATE', 'storm_id_b{basin?fmt=%s}{cyclone?fmt=%s}{date?fmt=%Y}.dat') config.set('config', 'TC_PAIRS_EDECK_TEMPLATE', 'storm_id_e{basin?fmt=%s}{cyclone?fmt=%s}{date?fmt=%Y}.dat') # LOOP_ORDER processes runs once, times runs once per time config.set('config', 'LOOP_ORDER', 'processes') # set config variable overrides for key, value in config_overrides.items(): config.set('config', key, value) wrapper = TCPairsWrapper(config) assert wrapper.isOK all_cmds = wrapper.run_all_times() print(f"ALL COMMANDS:") for idx, (cmd, env_list) in enumerate(all_cmds): print(f"CMD{idx}: {cmd}") print(f"ENV{idx}: {env_list}") assert (len(all_cmds) == len(values_to_check)) for (cmd, env_vars), value_to_check in zip(all_cmds, values_to_check): env_var_key = f'METPLUS_{storm_type.upper()}' match = next( (item for item in env_vars if item.startswith(env_var_key)), None) assert (match is not None) print(f"Checking env var: {env_var_key}") actual_value = match.split('=', 1)[1] expected_value = f'{storm_type} = ["{value_to_check}"];' assert actual_value == expected_value
def test_get_config_file(metplus_config): fake_config_name = '/my/config/file' config = metplus_config() config.set('config', 'INIT_TIME_FMT', time_fmt) config.set('config', 'INIT_BEG', run_times[0]) default_config_file = os.path.join(config.getdir('PARM_BASE'), 'met_config', 'TCPairsConfig_wrapped') wrapper = TCPairsWrapper(config) assert wrapper.c_dict['CONFIG_FILE'] == default_config_file config.set('config', 'TC_PAIRS_CONFIG_FILE', fake_config_name) wrapper = TCPairsWrapper(config) assert wrapper.c_dict['CONFIG_FILE'] == fake_config_name
def test_read_storm_info(metplus_config, config_overrides, isOK): """! Check if error is thrown if storm_id and basin or cyclone are set """ config = metplus_config() set_minimum_config_settings(config) # set config variable overrides for key, value in config_overrides.items(): config.set('config', key, value) wrapper = TCPairsWrapper(config) assert wrapper.isOK == isOK
def test_tc_pairs_read_all_files(metplus_config, config_overrides, env_var_values): # run using init and valid time variables for loop_by in ['INIT', 'VALID']: config = metplus_config() set_minimum_config_settings(config, loop_by) test_data_dir = os.path.join(config.getdir('METPLUS_BASE'), 'internal_tests', 'data', 'tc_pairs') bdeck_dir = os.path.join(test_data_dir, 'bdeck') adeck_dir = os.path.join(test_data_dir, 'adeck') config.set('config', 'TC_PAIRS_BDECK_INPUT_DIR', bdeck_dir) config.set('config', 'TC_PAIRS_ADECK_INPUT_DIR', adeck_dir) # LOOP_ORDER processes runs once, times runs once per time config.set('config', 'LOOP_ORDER', 'processes') config.set('config', 'TC_PAIRS_READ_ALL_FILES', True) config.set('config', 'TC_PAIRS_OUTPUT_TEMPLATE', '') # set config variable overrides for key, value in config_overrides.items(): config.set('config', key, value) env_var_values[f'METPLUS_{loop_by}_BEG'] = ( f'{loop_by.lower()}_beg = "{run_times[0]}";') env_var_values[f'METPLUS_{loop_by}_END'] = ( f'{loop_by.lower()}_end = "{run_times[-1]}";') wrapper = TCPairsWrapper(config) assert wrapper.isOK 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') out_dir = wrapper.c_dict.get('OUTPUT_DIR') expected_cmds = [ (f"{app_path} {verbosity} " f"-bdeck {bdeck_dir} " f"-adeck {adeck_dir} " f"-config {config_file} " f"-out {out_dir}/tc_pairs"), ] all_cmds = wrapper.run_all_times() print(f"ALL COMMANDS: {all_cmds}") assert (len(all_cmds) == len(expected_cmds)) for (cmd, env_vars), expected_cmd in zip(all_cmds, expected_cmds): # check that environment variables were set properly for env_var_key in wrapper.WRAPPER_ENV_VAR_KEYS: match = next( (item for item in env_vars if item.startswith(env_var_key)), None) assert (match is not None) print(f'Checking env var: {env_var_key}') actual_value = match.split('=', 1)[1] assert (env_var_values.get(env_var_key, '') == actual_value) # unset begin and end for next loop del env_var_values[f'METPLUS_{loop_by}_BEG'] del env_var_values[f'METPLUS_{loop_by}_END']