Skip to content

Commit

Permalink
Fixed Planner unit tests and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
asgibson committed Oct 20, 2023
1 parent 3b06568 commit 228b86d
Showing 1 changed file with 22 additions and 52 deletions.
74 changes: 22 additions & 52 deletions test/onair/src/ai_components/test_planners_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,81 +18,51 @@
from typing import Dict

# __init__ tests
def test_PlannersInterface__init__sets_instance_headers_to_given_headers_and_does_nothing_else_when_given__ai_plugins_is_empty(mocker):
def test_PlannersInterface__init__raises_AssertionError_when_given_headers_len_is_0():
# Arrange
arg_headers = []
arg__ai_plugins = {}
arg_headers = MagicMock()

num_fake_headers = pytest.gen.randint(1, 10) # arbitrary, from 1 to 10 headers (0 has own test)
for i in range(num_fake_headers):
arg_headers.append(MagicMock())
arg_headers.__len__.return_value = 0

cut = PlannersInterface.__new__(PlannersInterface)

# Act
cut.__init__(arg_headers, arg__ai_plugins)
with pytest.raises(AssertionError) as e_info:
cut.__init__(arg_headers)

# Assert
assert cut.headers == arg_headers
assert e_info.match('Headers are required')

def test_PlannersInterface__init__sets_instance_ai_constructs_to_a_list_of_the_calls_AIPlugIn_with_plugin_and_given_headers_for_each_item_in_given__ai_plugins_when_given__ai_plugins_is_occupied(mocker):
def test_PlannersInterface__init__sets_self_headers_to_given_headers_and_sets_self_ai_constructs_to_return_value_of_import_plugins(mocker):
# Arrange
arg_headers = []
arg__ai_plugins = {}
fake_spec_list = []
fake_module_list = []

num_fake_headers = pytest.gen.randint(1, 10) # arbitrary, from 1 to 10 headers (0 has own test)
for i in range(num_fake_headers):
arg_headers.append(MagicMock())
num_fake_ai_plugins = pytest.gen.randint(1, 10) # arbitrary, from 1 to 10 (0 has own test)
for i in range(num_fake_ai_plugins):
arg__ai_plugins[str(i)] = str(MagicMock())
fake_spec_list.append(MagicMock())
fake_module_list.append(MagicMock())

expected_ai_constructs = []
for i in range(num_fake_ai_plugins):
expected_ai_constructs.append(MagicMock())

# mocker.patch('importlib.import_module', return_value=fake_imported_module)
mocker.patch('importlib.util.spec_from_file_location',side_effect=fake_spec_list)
mocker.patch('importlib.util.module_from_spec',side_effect=fake_module_list)
for spec in fake_spec_list:
mocker.patch.object(spec,'loader.exec_module')
for i, module in enumerate(fake_module_list):
mocker.patch.object(module,'Plugin',return_value=expected_ai_constructs[i])
arg_headers = MagicMock()
arg__planner_plugins = MagicMock()

arg_headers.__len__.return_value = 1

forced_return_ai_constructs = MagicMock()

mocker.patch(planners_interface.__name__ + '.import_plugins', return_value=forced_return_ai_constructs)

cut = PlannersInterface.__new__(PlannersInterface)

# Act
cut.__init__(arg_headers, arg__ai_plugins)
cut.__init__(arg_headers, arg__planner_plugins)

# Assert
assert importlib.util.spec_from_file_location.call_count == len(arg__ai_plugins)
assert importlib.util.module_from_spec.call_count == len(fake_spec_list)

for i in range(num_fake_ai_plugins):
fake_name = list(arg__ai_plugins.keys())[i]
fake_path = arg__ai_plugins[fake_name]
assert importlib.util.spec_from_file_location.call_args_list[i].args == (fake_name,fake_path)
assert importlib.util.module_from_spec.call_args_list[i].args == (fake_spec_list[i],)
assert fake_spec_list[i].loader.exec_module.call_count == 1
assert fake_spec_list[i].loader.exec_module.call_args_list[0].args == (fake_module_list[i],)
assert fake_module_list[i].Plugin.call_count == 1
assert fake_module_list[i].Plugin.call_args_list[0].args == (fake_name,arg_headers)

assert cut.ai_constructs == expected_ai_constructs
assert cut.headers == arg_headers
assert planners_interface.import_plugins.call_count == 1
assert planners_interface.import_plugins.call_args_list[0].args == (arg_headers, arg__planner_plugins)
assert cut.ai_constructs == forced_return_ai_constructs

def test_update_does_nothing():
# Arrange
arg_curr_raw_tlm = MagicMock()
arg_status = MagicMock()
arg_high_level_data = MagicMock()

cut = PlannersInterface.__new__(PlannersInterface)

# Act
result = cut.update(arg_curr_raw_tlm, arg_status)
result = cut.update(arg_high_level_data)

# Assert
assert result == None
Expand Down

0 comments on commit 228b86d

Please sign in to comment.