Skip to content

Commit

Permalink
TST: Update unit tests for xtce creation
Browse files Browse the repository at this point in the history
  • Loading branch information
greglucas committed Aug 30, 2024
1 parent 8d922cf commit 6b8bff9
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 12 deletions.
Binary file not shown.
17 changes: 14 additions & 3 deletions imap_processing/tests/ccsds/test_data/expected_output.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<xtce:SizeInBits>
<xtce:DynamicValue>
<xtce:ParameterInstanceRef parameterRef="PKT_LEN" />
<xtce:LinearAdjustment slope="8" intercept="-70" />
<xtce:LinearAdjustment slope="8" intercept="-71" />
</xtce:DynamicValue>
</xtce:SizeInBits>
</xtce:BinaryDataEncoding>
Expand All @@ -59,6 +59,13 @@
<xtce:FloatParameterType name="TEST_PACKET.VAR_FLOAT">
<xtce:FloatDataEncoding sizeInBits="32" encoding="IEEE-754" />
</xtce:FloatParameterType>
<xtce:EnumeratedParameterType name="TEST_PACKET.VAR_STATE" signed="false">
<xtce:IntegerDataEncoding sizeInBits="1" encoding="unsigned" />
<xtce:EnumerationList>
<xtce:Enumeration value="0" label="OFF" />
<xtce:Enumeration value="1" label="ON" />
</xtce:EnumerationList>
</xtce:EnumeratedParameterType>
<xtce:IntegerParameterType name="TEST_PACKET2.SHCOARSE" signed="false">
<xtce:IntegerDataEncoding sizeInBits="32" encoding="unsigned" />
</xtce:IntegerParameterType>
Expand Down Expand Up @@ -109,11 +116,14 @@
<xtce:Parameter name="TEST_PACKET.VAR_FLOAT" parameterTypeRef="TEST_PACKET.VAR_FLOAT">
<xtce:LongDescription>Float data</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="TEST_PACKET.VAR_STATE" parameterTypeRef="TEST_PACKET.VAR_STATE">
<xtce:LongDescription>State data</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="TEST_PACKET2.SHCOARSE" parameterTypeRef="TEST_PACKET2.SHCOARSE">
<xtce:LongDescription>Mission elapsed time</xtce:LongDescription>
</xtce:Parameter>
<xtce:Parameter name="TEST_PACKET2.VAR1" parameterTypeRef="TEST_PACKET2.VAR1" shortDescription="Variable 1 short desc">
<xtce:LongDescription>Variable 1 - long desc</xtce:LongDescription>
<xtce:Parameter name="TEST_PACKET2.VAR1" parameterTypeRef="TEST_PACKET2.VAR1" shortDescription="Variable 1 short description">
<xtce:LongDescription>Variable 1 long description</xtce:LongDescription>
</xtce:Parameter>
</xtce:ParameterSet>
<xtce:ContainerSet>
Expand Down Expand Up @@ -142,6 +152,7 @@
<xtce:ParameterRefEntry parameterRef="TEST_PACKET.VAR_BYTE" />
<xtce:ParameterRefEntry parameterRef="TEST_PACKET.VAR_FILL" />
<xtce:ParameterRefEntry parameterRef="TEST_PACKET.VAR_FLOAT" />
<xtce:ParameterRefEntry parameterRef="TEST_PACKET.VAR_STATE" />
</xtce:EntryList>
</xtce:SequenceContainer>
<xtce:SequenceContainer name="TEST_PACKET2">
Expand Down
228 changes: 219 additions & 9 deletions imap_processing/tests/ccsds/test_excel_to_xtce.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
from unittest import mock

import pandas as pd
import pytest

from imap_processing.ccsds import excel_to_xtce
Expand All @@ -12,36 +13,245 @@


@pytest.fixture()
def excel_file():
p = Path(__file__).parent / "test_data" / "excel_to_xtce_test_file.xlsx"
return p
def xtce_excel_file(tmp_path):
"""Create an excel file for testing.
Dataframes for each tab of the spreadsheet that then get written to an excel file.
"""
# Create a pandas DataFrame for global attributes
subsystem = {
"infoField": ["subsystem", "sheetReleaseDate", "sheetReleaseRev"],
"infoValue": ["Test Instrument", "2024-07-26 00:00:00", "v1.2"],
}

packets = {"packetName": ["TEST_PACKET", "TEST_PACKET2"], "apIdHex": ["0x1", "0xF"]}

test_packet1 = {
"packetName": ["TEST_PACKET"] * 15,
"mnemonic": [
"PHVERNO",
"PHTYPE",
"PHSHF",
"PHAPID",
"PHGROUPF",
"PHSEQCNT",
"PHDLEN",
"SHCOARSE",
"VAR_UINT",
"VAR_INT",
"VAR_SINT",
"VAR_BYTE",
"VAR_FILL",
"VAR_FLOAT",
"VAR_STATE",
],
"lengthInBits": [3, 1, 1, 11, 2, 14, 16, 32, 2, 4, 5, 10000, 3, 32, 1],
"dataType": [
"UINT",
"UINT",
"UINT",
"UINT",
"UINT",
"UINT",
"UINT",
"UINT",
"UINT",
"INT",
"SINT",
"BYTE",
"FILL",
"FLOAT",
"UINT",
],
"convertAs": [
"NONE",
"NONE",
"NONE",
"NONE",
"NONE",
"NONE",
"NONE",
"NONE",
"ANALOG",
"NONE",
"NONE",
"NONE",
"NONE",
"NONE",
"STATE",
],
"units": [
"DN",
"DN",
"DN",
"DN",
"DN",
"DN",
"DN",
"DN",
"DN",
"DN",
"DN",
"DN",
"DN",
"DN",
"DN",
],
"longDescription": [
"CCSDS Packet Version Number",
"CCSDS Packet Type Indicator",
"CCSDS Packet Secondary Header Flag",
"CCSDS Packet Application Process ID",
"CCSDS Packet Grouping Flags",
"CCSDS Packet Sequence Count",
"CCSDS Packet Length",
"Mission elapsed time",
"Unsgned integer data with conversion",
"Integer data",
"Signed integer data",
"Binary data - variable length",
"Fill data",
"Float data",
"State data",
],
}

test_packet2 = {
"packetName": ["TEST_PACKET2"] * 9,
"mnemonic": [
"PHVERNO",
"PHTYPE",
"PHSHF",
"PHAPID",
"PHGROUPF",
"PHSEQCNT",
"PHDLEN",
"SHCOARSE",
"VAR1",
],
"lengthInBits": [3, 1, 1, 11, 2, 14, 16, 32, 2],
"dataType": [
"UINT",
"UINT",
"UINT",
"UINT",
"UINT",
"UINT",
"UINT",
"UINT",
"UINT",
],
"convertAs": [
"NONE",
"NONE",
"NONE",
"NONE",
"NONE",
"NONE",
"NONE",
"NONE",
"NONE",
],
"units": ["DN", "DN", "DN", "DN", "DN", "DN", "DN", "DN", "DN"],
"longDescription": [
"CCSDS Packet Version Number",
"CCSDS Packet Type Indicator",
"CCSDS Packet Secondary Header Flag",
"CCSDS Packet Application Process ID",
"CCSDS Packet Grouping Flags",
"CCSDS Packet Sequence Count",
"CCSDS Packet Length",
"Mission elapsed time",
"Variable 1 long description",
],
"shortDescription": [
"",
"",
"",
"",
"",
"",
"",
"",
"Variable 1 short description",
],
}

analog_conversions = {
"packetName": ["TEST_PACKET"],
"mnemonic": ["VAR_UINT"],
"convertAs": ["UNSEGMENTED_POLY"],
"segNumber": [1],
"lowValue": [0],
"highValue": [100],
"c0": [1.5],
"c1": [2.5],
"c2": [0],
"c3": [0],
"c4": [0],
"c5": [0],
"c6": [0],
"c7": [0],
}

states = {
"packetName": ["TEST_PACKET"] * 2,
"mnemonic": ["VAR_STATE"] * 2,
"value": [0, 1],
"state": ["OFF", "ON"],
}

# Write the DataFrame to an excel file
excel_path = tmp_path / "excel_to_xtce_test_file.xlsx"
excel_file = pd.ExcelWriter(excel_path, engine="openpyxl")

pd.DataFrame(subsystem).to_excel(excel_file, sheet_name="Subsystem", index=False)
pd.DataFrame(packets).to_excel(excel_file, sheet_name="Packets", index=False)
pd.DataFrame(test_packet1).to_excel(
excel_file, sheet_name="TEST_PACKET", index=False
)
# Test P_ version of sheet name as well
pd.DataFrame(test_packet2).to_excel(
excel_file, sheet_name="P_TEST_PACKET2", index=False
)
pd.DataFrame(analog_conversions).to_excel(
excel_file, sheet_name="AnalogConversions", index=False
)
pd.DataFrame(states).to_excel(excel_file, sheet_name="States", index=False)

# Write the file to disk
excel_file.close()

return excel_path


def test_generated_xml(excel_file, tmp_path):
def test_generated_xml(xtce_excel_file):
"""Make sure we are producing the expected contents within the XML file.
To produce a new expected output file the following command can be used.
imap_xtce imap_processing/tests/ccsds/test_data/excel_to_xtce_test_file.xlsx
--output imap_processing/tests/ccsds/test_data/expected_output.xml
"""
generator = excel_to_xtce.XTCEGenerator(excel_file)
output_file = tmp_path / "output.xml"
generator = excel_to_xtce.XTCEGenerator(xtce_excel_file)
output_file = xtce_excel_file.parent / "output.xml"
generator.to_xml(output_file)

expected_file = excel_file.parent / "expected_output.xml"
expected_file = Path(__file__).parent / "test_data/expected_output.xml"
# Uncomment this line if you want to re-create the expected output file
# generator.to_xml(expected_file)
with open(output_file) as f, open(expected_file) as f_expected:
assert f.read() == f_expected.read()


# General test
@mock.patch("imap_processing.ccsds.excel_to_xtce.XTCEGenerator")
def test_main_general(mock_input, excel_file):
def test_main_general(mock_input, xtce_excel_file):
"""Testing base main function."""
test_args = [
"test_script",
"--output",
"swe.xml",
f"{excel_file}",
f"{xtce_excel_file}",
]
with mock.patch.object(sys, "argv", test_args):
excel_to_xtce.main()
Expand Down

0 comments on commit 6b8bff9

Please sign in to comment.