Skip to content

Commit

Permalink
WIP: Fixed some ExecutionEngine tests
Browse files Browse the repository at this point in the history
Some isolation issues exist with new function parse_plugins_list
parse_plugins_list is untested still
  has errors
    config_filepath does not exist in this function
    is still called from other tests, but does not cause failures
  • Loading branch information
asgibson committed Oct 13, 2023
1 parent 7c39d39 commit cd30ef0
Showing 1 changed file with 59 additions and 64 deletions.
123 changes: 59 additions & 64 deletions test/onair/src/run_scripts/test_execution_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ def test_ExecutionEngine_parse_configs_raises_KeyError_with_config_file_info_whe
'BenchmarkFiles':MagicMock(),
'BenchmarkIndices':MagicMock(),
'ParserFileName':MagicMock(),
'PluginList':MagicMock()
'KnowledgeRepPluginList':"{fake_name:fake_path}",
'LearnersPluginList':"{fake_name:fake_path}",
'PlannersPluginList':"{fake_name:fake_path}",
'ComplexPluginList':"{fake_name:fake_path}"
}
required_keys = [item for item in list(fake_default.keys()) if 'Benchmark' not in item]
missing_key = pytest.gen.choice(required_keys)
Expand Down Expand Up @@ -221,47 +224,6 @@ def test_ExecutionEngine_parse_configs_raises_ValueError_when_PluginList_from_co
assert execution_engine.isinstance.call_count == 1
assert execution_engine.isinstance.call_args_list[0].args == (fake_plugin_list.body, execution_engine.ast.Dict, )

def test_ExecutionEngine_parse_configs_raises_FileNotFoundError_when_given_plugin_path_is_not_valid(mocker):
# Arrange
arg_config_filepath = MagicMock()

fake_config = MagicMock()
fake_paths_and_filenames = str(MagicMock)
fake_plugin_list = MagicMock()
fake_plugin_list.body = MagicMock()
fake_plugin_list.body.keys = MagicMock()
fake_plugin_list.body.keys.__len__.return_value = 1
fake_temp_plugin_list = MagicMock()
fake_plugin_name = MagicMock()
fake_temp_iter = iter([fake_plugin_name])
fake_default_item = MagicMock()
fake_config.__getitem__.return_value = fake_default_item
fake_default_item.__getitem__.side_effect = [fake_paths_and_filenames] * 4 + [None] + [fake_plugin_list]
fake_config_read_result = MagicMock()
fake_config_read_result.__len__.return_value = 1


cut = ExecutionEngine.__new__(ExecutionEngine)

mocker.patch(execution_engine.__name__ + '.configparser.ConfigParser', return_value=fake_config)
mocker.patch.object(fake_config, 'read', return_value=fake_config_read_result)
mocker.patch.object(cut, 'ast_parse_eval', return_value=fake_plugin_list)
mocker.patch(execution_engine.__name__ + '.isinstance', return_value=True)
mocker.patch(execution_engine.__name__ + '.ast.literal_eval', return_value=fake_temp_plugin_list)
mocker.patch.object(fake_temp_plugin_list, 'values', return_value=fake_temp_iter)
mocker.patch(execution_engine.__name__ + '.os.path.exists', return_value=False)
# Act
with pytest.raises(FileNotFoundError) as e_info:
cut.parse_configs(arg_config_filepath)

# Assert
assert e_info.match(f"In config file '{arg_config_filepath}', path '{fake_plugin_name}' does not exist or is formatted incorrectly.")
assert cut.ast_parse_eval.call_count == 1
assert cut.ast_parse_eval.call_args_list[0].args == (fake_plugin_list,)
assert execution_engine.isinstance.call_count == 1
assert execution_engine.isinstance.call_args_list[0].args == (fake_plugin_list.body, execution_engine.ast.Dict, )


def test_ExecutionEngine_parse_configs_sets_all_items_without_error(mocker):
# Arrange
arg_config_filepath = MagicMock()
Expand All @@ -274,21 +236,29 @@ def test_ExecutionEngine_parse_configs_sets_all_items_without_error(mocker):
'BenchmarkFiles':MagicMock(),
'BenchmarkIndices':MagicMock(),
'ParserFileName':MagicMock(),
'PluginList':"{fake_name:fake_path}"
'KnowledgeRepPluginList':"{fake_name:fake_path}",
'LearnersPluginList':"{fake_name:fake_path}",
'PlannersPluginList':"{fake_name:fake_path}",
'ComplexPluginList':"{fake_name:fake_path}"
}
fake_run_flags = MagicMock()
fake_plugin_list = MagicMock()
fake_plugin_list.body = MagicMock()
fake_plugin_list.body.keys = MagicMock()
fake_plugin_list.body.keys.__len__.return_value = 1
fake_temp_plugin_list = MagicMock()
fake_plugin_name = MagicMock()
fake_temp_iter = iter([fake_plugin_name])
fake_dict_for_Config = {'DEFAULT':fake_default, 'RUN_FLAGS':fake_run_flags}
fake_config = MagicMock()
fake_config.__getitem__.side_effect = fake_dict_for_Config.__getitem__
fake_config_read_result = MagicMock()
fake_config_read_result.__len__.return_value = 1
fake_knowledge_rep_plugin_list = MagicMock()
fake_learners_plugin_list = MagicMock()
fake_planners_plugin_list = MagicMock()
fake_complex_plugin_list = MagicMock()
fake_plugins = [fake_knowledge_rep_plugin_list,
fake_learners_plugin_list,
fake_planners_plugin_list,
fake_complex_plugin_list]
fake_IO_flags = MagicMock()
fake_Dev_flags = MagicMock()
fake_Viz_flags = MagicMock()
Expand All @@ -304,11 +274,9 @@ def test_ExecutionEngine_parse_configs_sets_all_items_without_error(mocker):

mocker.patch(execution_engine.__name__ + '.configparser.ConfigParser', return_value=fake_config)
mocker.patch.object(fake_config, 'read', return_value=fake_config_read_result)
mocker.patch.object(cut, 'parse_plugins_list', side_effect=fake_plugins)
mocker.patch.object(fake_run_flags, 'getboolean', side_effect=[fake_IO_flags, fake_Dev_flags, fake_Viz_flags])
mocker.patch.object(cut, 'ast_parse_eval', return_value=fake_plugin_list)
mocker.patch(execution_engine.__name__ + '.isinstance', return_value=True)
mocker.patch(execution_engine.__name__ + '.ast.literal_eval', return_value=fake_temp_plugin_list)
mocker.patch.object(fake_temp_plugin_list, 'values', return_value=fake_temp_iter)
mocker.patch(execution_engine.__name__ + '.os.path.exists', return_value=True)
mocker.patch.object(fake_plugin_dict, 'keys', return_value=fake_keys)
mocker.patch.object(fake_plugin_dict, '__getitem__', return_value=fake_path)
Expand All @@ -327,7 +295,11 @@ def test_ExecutionEngine_parse_configs_sets_all_items_without_error(mocker):
assert cut.benchmarkFiles == fake_default['BenchmarkFiles']
assert cut.benchmarkIndices == fake_default['BenchmarkIndices']
assert cut.parser_file_name == fake_default['ParserFileName']
assert cut.plugin_list == fake_temp_plugin_list
assert cut.parse_plugins_list.call_count == 4
assert cut.knowledge_rep_plugin_list == fake_knowledge_rep_plugin_list
assert cut.learners_plugin_list == fake_learners_plugin_list
assert cut.planners_plugin_list == fake_planners_plugin_list
assert cut.complex_plugin_list == fake_complex_plugin_list
assert fake_run_flags.getboolean.call_count == 3
assert fake_run_flags.getboolean.call_args_list[0].args == ('IO_Flag', )
assert cut.IO_Flag == fake_IO_flags
Expand All @@ -346,14 +318,25 @@ def test_ExecutionEngine_parse_configs_bypasses_benchmarks_when_access_raises_er
'TelemetryMetadataFilePath':MagicMock(),
'MetaFile':MagicMock(),
'ParserFileName':MagicMock(),
'PluginList':"{fake_name:fake_path}"
'KnowledgeRepPluginList':"{fake_name:fake_path}",
'LearnersPluginList':"{fake_name:fake_path}",
'PlannersPluginList':"{fake_name:fake_path}",
'ComplexPluginList':"{fake_name:fake_path}"
}
fake_run_flags = MagicMock()
fake_dict_for_Config = {'DEFAULT':fake_default, 'RUN_FLAGS':fake_run_flags}
fake_config = MagicMock()
fake_config.__getitem__.side_effect = fake_dict_for_Config.__getitem__
fake_config_read_result = MagicMock()
fake_config_read_result.__len__.return_value = 1
fake_knowledge_rep_plugin_list = MagicMock()
fake_learners_plugin_list = MagicMock()
fake_planners_plugin_list = MagicMock()
fake_complex_plugin_list = MagicMock()
fake_plugins = [fake_knowledge_rep_plugin_list,
fake_learners_plugin_list,
fake_planners_plugin_list,
fake_complex_plugin_list]
fake_IO_flags = MagicMock()
fake_Dev_flags = MagicMock()
fake_Viz_flags = MagicMock()
Expand All @@ -369,6 +352,7 @@ def test_ExecutionEngine_parse_configs_bypasses_benchmarks_when_access_raises_er

mocker.patch(execution_engine.__name__ + '.configparser.ConfigParser', return_value=fake_config)
mocker.patch.object(fake_config, 'read', return_value=fake_config_read_result)
mocker.patch.object(cut, 'parse_plugins_list', side_effect=fake_plugins)
mocker.patch.object(fake_run_flags, 'getboolean', side_effect=[fake_IO_flags, fake_Dev_flags, fake_Viz_flags])
mocker.patch('ast.literal_eval',return_value=fake_plugin_dict)
mocker.patch.object(fake_plugin_dict, 'keys', return_value=fake_keys)
Expand Down Expand Up @@ -410,6 +394,7 @@ def test_ExecutionEngine_parse_configs_raises_KeyError_with_config_file_info_whe
cut = ExecutionEngine.__new__(ExecutionEngine)

mocker.patch(execution_engine.__name__ + '.configparser.ConfigParser', return_value=fake_config)
mocker.patch.object(cut, 'parse_plugins_list', return_value=None)
mocker.patch.object(fake_config, 'read', return_value=fake_config_read_result)

# Act
Expand Down Expand Up @@ -488,10 +473,14 @@ def __init__(self, data_file, meta_file, subsystems_breakdown):
# setup_sim tests
def test_ExecutionEngine_setup_sim_sets_self_sim_to_new_Simulator_and_sets_benchmark_data_when_no_exceptions_are_encountered(mocker):
# Arrange
cut = ExecutionEngine.__new__(ExecutionEngine)
cut = ExecutionEngine()
cut.simDataParser = MagicMock()
cut.knowledge_rep_plugin_list = MagicMock()
cut.learners_plugin_list = MagicMock()
cut.planners_plugin_list = MagicMock()
cut.complex_plugin_list = MagicMock()
cut.benchmarkFiles = MagicMock()
cut.benchmarkFilePath = MagicMock()
cut.benchmarkFilePath = str(MagicMock())
cut.benchmarkIndices = MagicMock()
cut.plugin_list = MagicMock()

Expand All @@ -501,8 +490,6 @@ def test_ExecutionEngine_setup_sim_sets_self_sim_to_new_Simulator_and_sets_bench
fake_bi = MagicMock()
fake__file__ = str(MagicMock())

print(fake__file__)

mocker.patch(execution_engine.__name__ + '.Simulator', return_value=fake_sim)
mocker.patch(execution_engine.__name__ + '.ast.literal_eval', side_effect=[fake_fls, fake_bi])
mocker.patch(execution_engine.__name__ + '.__file__', fake__file__)
Expand All @@ -515,32 +502,36 @@ def test_ExecutionEngine_setup_sim_sets_self_sim_to_new_Simulator_and_sets_bench

# Assert
assert execution_engine.Simulator.call_count == 1
assert execution_engine.Simulator.call_args_list[0].args == (cut.simDataParser, cut.plugin_list)
assert execution_engine.Simulator.call_args_list[0].args == (cut.simDataParser,
cut.knowledge_rep_plugin_list,
cut.learners_plugin_list,
cut.planners_plugin_list,
cut.complex_plugin_list)
assert cut.sim == fake_sim
assert execution_engine.ast.literal_eval.call_count == 2
assert execution_engine.ast.literal_eval.call_args_list[0].args == (cut.benchmarkFiles, )
assert execution_engine.ast.literal_eval.call_args_list[1].args == (cut.benchmarkIndices, )
assert execution_engine.os.path.realpath.call_count == 1
assert execution_engine.os.path.realpath.call_args_list[0].args == (fake__file__, )
assert execution_engine.os.path.dirname.call_count == 1
assert execution_engine.os.path.dirname.call_args_list[0].args == (fake_fp, )
assert execution_engine.ast.literal_eval.call_args_list[1].args == (cut.benchmarkIndices, )
assert fake_sim.set_benchmark_data.call_count == 1
assert fake_sim.set_benchmark_data.call_args_list[0].args == (fake_fp + '/../..' + cut.benchmarkFilePath, fake_fls, fake_bi, )

def test_ExecutionEngine_setup_sim_sets_self_sim_to_new_Simulator_but_does_not_set_bencmark_data_because_exception_is_encountered(mocker):
# Arrange
cut = ExecutionEngine.__new__(ExecutionEngine)
cut.simDataParser = MagicMock()
cut.knowledge_rep_plugin_list = MagicMock()
cut.learners_plugin_list = MagicMock()
cut.planners_plugin_list = MagicMock()
cut.complex_plugin_list = MagicMock()
cut.benchmarkFiles = MagicMock()
cut.benchmarkFilePath = MagicMock()
cut.benchmarkIndices = MagicMock()
cut.plugin_list = MagicMock()

fake_sim = MagicMock()
fake_fls = MagicMock()
fake_fp = str(MagicMock())
fake_bi = MagicMock()
fake__file__ = str(MagicMock())

print(fake__file__)

mocker.patch(execution_engine.__name__ + '.Simulator', return_value=fake_sim)
mocker.patch(execution_engine.__name__ + '.ast.literal_eval', side_effect=Exception)
Expand All @@ -553,7 +544,11 @@ def test_ExecutionEngine_setup_sim_sets_self_sim_to_new_Simulator_but_does_not_s

# Assert
assert execution_engine.Simulator.call_count == 1
assert execution_engine.Simulator.call_args_list[0].args == (cut.simDataParser, cut.plugin_list)
assert execution_engine.Simulator.call_args_list[0].args == (cut.simDataParser,
cut.knowledge_rep_plugin_list,
cut.learners_plugin_list,
cut.planners_plugin_list,
cut.complex_plugin_list)
assert cut.sim == fake_sim
assert execution_engine.ast.literal_eval.call_count == 1
assert execution_engine.ast.literal_eval.call_args_list[0].args == (cut.benchmarkFiles, )
Expand Down

0 comments on commit cd30ef0

Please sign in to comment.