1import pytest
  2from datetime import datetime
  3from chartepssg.alg.ASH_reader import (
  4    AutomationScheduleHistory,
  5    AutomationScheduleHistoryFileNotFound,
  6    AutomationScheduleHistoryFileEmpty,
  7    Parameter,
  8    Action,
  9)
 10
 11
 12def test_automation_history_non_existent_file():
 13    with pytest.raises(AutomationScheduleHistoryFileNotFound):
 14        AutomationScheduleHistory("doesnotexist.xml")
 15
 16
 17def test_automation_history_file_exists_but_empty(empty_file):
 18    with pytest.raises(AutomationScheduleHistoryFileEmpty):
 19        AutomationScheduleHistory(empty_file)
 20
 21
 22def test_number_of_activities(example_file):
 23    output = len(list(AutomationScheduleHistory(example_file).activities()))
 24    expected = 1
 25    assert expected == output
 26
 27
 28def test_number_of_timeline_actions(example_file):
 29    output = len(list(AutomationScheduleHistory(example_file).timeline_actions()))
 30    expected = 3
 31    assert expected == output
 32
 33
 34@pytest.mark.parametrize(
 35    "attrib,expected",
 36    [
 37        ("name", "CALIB_A"),
 38        ("ssid", 2),
 39        ("atype", "Orbital Angle tagged Sequence"),
 40        ("absolute_orbit", 363),
 41        ("deg_from_anx", 137.795),
 42        ("start_time", datetime(2015, 1, 1, 9, 48, 12, 59000)),
 43        ("status", "Verified"),
 44        ("parameters", [Parameter("TYPE", "LR"), Parameter("ANGLE", "270.234")]),
 45        (
 46            "actions",
 47            [
 48                Action(
 49                    datetime(2015, 1, 1, 9, 48, 12, 59000), "Activity paused by User"
 50                ),
 51                Action(
 52                    datetime(2015, 1, 1, 9, 49, 12, 59000), "Activity resumed by User"
 53                ),
 54                Action(
 55                    datetime(2015, 1, 1, 9, 49, 12, 59000), "Activity released to PE"
 56                ),
 57                Action(
 58                    datetime(2015, 1, 1, 9, 48, 12, 59000), "Status changed to Pending"
 59                ),
 60                Action(
 61                    datetime(2015, 1, 1, 9, 48, 12, 59000), "Status changed to Running"
 62                ),
 63                Action(
 64                    datetime(2015, 1, 1, 9, 58, 12, 59000), "Status changed to Verified"
 65                ),
 66            ],
 67        ),
 68    ],
 69)
 70def test_activity(attrib, expected, example_file):
 71    output = next(
 72        AutomationScheduleHistory(example_file).activities()
 73    ).__getattribute__(attrib)
 74    assert expected == output
 75
 76
 77def test_timeline(example_file):
 78    actions = list(AutomationScheduleHistory(example_file).timeline_actions())
 79    assert actions[0].timestamp == datetime(2015, 1, 2, 9, 48, 12, 59000)
 80    assert actions[0].description == "Anticipation time set to 12h"
 81    assert actions[1].timestamp == datetime(2015, 1, 2, 9, 48, 12, 59000)
 82    assert actions[1].description == "Timeline paused by User"
 83    assert actions[2].timestamp == datetime(2015, 1, 2, 9, 48, 12, 59000)
 84    assert actions[2].description == "Timeline resumed by User"
 85
 86
 87@pytest.fixture
 88def empty_file(tmp_path):
 89    f = tmp_path / "empty.xml"
 90    f.write_text("")
 91    return f.resolve()
 92
 93
 94@pytest.fixture
 95def example_file(tmp_path):
 96    f = tmp_path / "example.xml"
 97    f.write_text(
 98        """<?xml version="1.0" encoding="UTF-8"?>
 99        <Schedule_History domain="SGA1">
100            <List_of_Activities count="1">
101                <Activity>
102                    <Activity_Name>CALIB_A</Activity_Name>
103                    <SSID>2</SSID>
104                    <Activity_Type>Orbital Angle tagged Sequence</Activity_Type>
105                    <Activity_Absolute_orbit>363</Activity_Absolute_orbit>
106                    <Activity_Deg_from_ANX>137.795</Activity_Deg_from_ANX>
107                    <Activity_Start_Time>UTC=2015-01-01T09:48:12.059</Activity_Start_Time>
108                    <Activity_Status>Verified</Activity_Status>
109                    <List_of_Parameters count="2">
110                        <Parameter>
111                            <Name>TYPE</Name>
112                            <Value>LR</Value>
113                        </Parameter>
114                        <Parameter>
115                            <Name>ANGLE</Name>
116                            <Value>270.234</Value>
117                        </Parameter>
118                    </List_of_Parameters>
119                    <List_of_Actions count="6">
120                        <Action>
121                            <Timestamp>UTC=2015-01-01T09:48:12.059</Timestamp>
122                            <Description>Activity paused by User</Description>
123                        </Action>
124                        <Action>
125                            <Timestamp>UTC=2015-01-01T09:49:12.059</Timestamp>
126                            <Description>Activity resumed by User</Description>
127                        </Action>
128                        <Action>
129                            <Timestamp>UTC=2015-01-01T09:49:12.059</Timestamp>
130                            <Description>Activity released to PE</Description>
131                        </Action>
132                        <Action>
133                            <Timestamp>UTC=2015-01-01T09:48:12.059</Timestamp>
134                            <Description>Status changed to Pending</Description>
135                        </Action>
136                        <Action>
137                            <Timestamp>UTC=2015-01-01T09:48:12.059</Timestamp>
138                            <Description>Status changed to Running</Description>
139                        </Action>
140                        <Action>
141                            <Timestamp>UTC=2015-01-01T09:58:12.059</Timestamp>
142                            <Description>Status changed to Verified</Description>
143                        </Action>
144                    </List_of_Actions>
145                </Activity>
146            </List_of_Activities>
147            <List_of_Timeline_Actions count="3">
148                <TML_Action>
149                    <Timestamp>UTC=2015-01-02T09:48:12.059</Timestamp>
150                    <Description>Anticipation time set to 12h</Description>
151                </TML_Action>
152                <TML_Action>
153                    <Timestamp>UTC=2015-01-02T09:48:12.059</Timestamp>
154                    <Description>Timeline paused by User</Description>
155                </TML_Action>
156                <TML_Action>
157                    <Timestamp>UTC=2015-01-02T09:48:12.059</Timestamp>
158                    <Description>Timeline resumed by User</Description>
159                </TML_Action>
160            </List_of_Timeline_Actions>
161        </Schedule_History>"""
162    )
163    return f.resolve()