Skip to content

Commit

Permalink
starting rework of SBN adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
the-other-james committed Sep 22, 2023
1 parent 65e0a88 commit 9803b65
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
15 changes: 15 additions & 0 deletions onair/src/run_scripts/sbn_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import sbn_client as sbn
import message_headers as msg_hdr

from ...data_handling.parser_util import *

# Note: The double buffer does not clear between switching. If fresh data doesn't come in, stale data is returned (delayed by 1 frame)

# msgID_lookup_table format { msgID : [ "<APP NAME>" , msg_hdr.<data struct in message_headers.py> , "<data category>" ] }
Expand Down Expand Up @@ -78,6 +80,7 @@ def get_current_data(recv_msg, data_struct, app_name):
class AdapterDataSource(OnAirDataSource):
# Data structure
# TODO: Make init data structure better
# TODO: This should be in an __init__ function
currentData = []

for x in range(0,2):
Expand Down Expand Up @@ -123,6 +126,18 @@ def subscribe_message(self, msgid):
else:
sbn.subscribe(msgid)

# TODO: Same as csv_parser, may move to parser_utils
def parse_meta_data_file(self, meta_data_file, ss_breakdown):
print("HOOOOLLLLLLAAAA!")
parsed_meta_data = extract_meta_data(meta_data_file)
if ss_breakdown == False:
num_elements = len(parsed_meta_data['subsystem_assignments'])
parsed_meta_data['subsystem_assignments'] = [['MISSION'] for elem in range(num_elements)]
return parsed_meta_data

def process_data_file(self, data_file):
pass # Nothing to do since telemetry is live

def get_next(self):
"""Provides the latest data from SBN in a dictionary of lists structure.
Returned data is safe to use until the next get_next call.
Expand Down
85 changes: 84 additions & 1 deletion test/onair/src/run_scripts/test_sbn_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from mock import MagicMock, PropertyMock

import onair.src.run_scripts.sbn_adapter as sbn_adapter
from onair.src.run_scripts.sbn_adapter import AdapterDataSource

from importlib import reload
import sys
Expand All @@ -22,6 +23,7 @@ def setup_teardown():

print('setup')

pytest.cut = AdapterDataSource.__new__(AdapterDataSource)
yield 'setup_teardown'

print('teardown')
Expand Down Expand Up @@ -632,4 +634,85 @@ def test_sbn_adapter_AdapterDataSource_has_more_returns_true(setup_teardown):
result = cut.has_more()

# Assert
assert result == True
assert result == True

# sbn_adapter parse_config_data tests
# TODO: parse_meta_data_file implementation is common between sbn_adapter and csv_parser
def test_sbn_adapter_parse_meta_data_file_returns_call_to_extract_meta_data_file_given_metadata_file_when_given_ss_breakdown_does_not_resolve_to_False(mocker, setup_teardown):
# Arrange
AdapterDataSource = sbn_adapter.AdapterDataSource
cut = AdapterDataSource.__new__(AdapterDataSource)

arg_configFile = MagicMock()
arg_ss_breakdown = True if pytest.gen.randint(0, 1) else MagicMock()

expected_result = MagicMock()

mocker.patch(sbn_adapter.__name__ + '.extract_meta_data', return_value=expected_result)
mocker.patch(sbn_adapter.__name__ + '.len')

# Act
result = cut.parse_meta_data_file(arg_configFile, arg_ss_breakdown)

assert(False)
# Assert
assert sbn_adapter.extract_meta_data.call_count == 1
assert sbn_adapter.extract_meta_data.call_args_list[0].args == (arg_configFile, )
assert sbn_adapter.len.call_count == 0
assert result == expected_result

def test_sbn_adapter_parse_meta_data_file_returns_call_to_extract_meta_data_file_given_metadata_file_set_to_empty_list_when_len_of_call_value_dict_def_of_subsystem_assigments_when_given_ss_breakdown_evaluates_to_False(mocker, setup_teardown):
# Arrange
arg_configFile = MagicMock()
arg_ss_breakdown = False if pytest.gen.randint(0, 1) else 0

forced_return_extract_meta_data = {}
forced_return_len = 0
fake_empty_processed_filepath = MagicMock()
forced_return_extract_meta_data['subsystem_assignments'] = fake_empty_processed_filepath

expected_result = []

mocker.patch(sbn_adapter.__name__ + '.extract_meta_data', return_value=forced_return_extract_meta_data)
mocker.patch(sbn_adapter.__name__ + '.len', return_value=forced_return_len)

# Act
result = pytest.cut.parse_meta_data_file(arg_configFile, arg_ss_breakdown)

# Assert
assert sbn_adapter.extract_meta_data.call_count == 1
assert sbn_adapter.extract_meta_data.call_args_list[0].args == (arg_configFile, )
assert sbn_adapter.len.call_count == 1
assert sbn_adapter.len.call_args_list[0].args == (fake_empty_processed_filepath, )
assert result['subsystem_assignments'] == expected_result

def test_sbn_adapter_parse_meta_data_file_returns_call_to_extract_meta_data_given_metadata_file__with_dict_def_subsystem_assignments_def_of_call_set_to_single_item_list_str_MISSION_for_each_item_when_given_ss_breakdown_evaluates_to_False(mocker, setup_teardown):
# Arrange
arg_configFile = MagicMock()
arg_ss_breakdown = False if pytest.gen.randint(0, 1) else 0

forced_return_extract_meta_data = {}
forced_return_process_filepath = MagicMock()
fake_processed_filepath = []
num_fake_processed_filepaths = pytest.gen.randint(1,10) # arbitrary, from 1 to 10 (0 has own test)
for i in range(num_fake_processed_filepaths):
fake_processed_filepath.append(i)
forced_return_extract_meta_data['subsystem_assignments'] = fake_processed_filepath
forced_return_len = num_fake_processed_filepaths

expected_result = []
for i in range(num_fake_processed_filepaths):
expected_result.append(['MISSION'])

mocker.patch(sbn_adapter.__name__ + '.extract_meta_data', return_value=forced_return_extract_meta_data)
mocker.patch(sbn_adapter.__name__ + '.len', return_value=forced_return_len)

# Act
result = pytest.cut.parse_meta_data_file(arg_configFile, arg_ss_breakdown)

# Assert
assert sbn_adapter.extract_meta_data.call_count == 1
assert sbn_adapter.extract_meta_data.call_args_list[0].args == (arg_configFile, )
assert sbn_adapter.len.call_count == 1
assert sbn_adapter.len.call_args_list[0].args == (fake_processed_filepath, )
assert result['subsystem_assignments'] == expected_result

0 comments on commit 9803b65

Please sign in to comment.