-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_schedule.py
More file actions
160 lines (140 loc) · 4.72 KB
/
test_schedule.py
File metadata and controls
160 lines (140 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from datetime import datetime, timedelta
from pathlib import Path
import pyproj
import pytest
from virtualship import Location
from virtualship.expedition.do_expedition import _load_input_data
from virtualship.expedition.schedule import Schedule, ScheduleError, Waypoint
from virtualship.utils import _get_ship_config
projection = pyproj.Geod(ellps="WGS84")
expedition_dir = Path("expedition_dir")
def test_import_export_schedule(tmpdir) -> None:
out_path = tmpdir.join("schedule.yaml")
# arbitrary time for testing
base_time = datetime.strptime("1950-01-01", "%Y-%m-%d")
schedule = Schedule(
waypoints=[
Waypoint(location=Location(0, 0), time=base_time, instrument=None),
Waypoint(
location=Location(1, 1),
time=base_time + timedelta(hours=1),
instrument=None,
),
]
)
schedule.to_yaml(out_path)
schedule2 = Schedule.from_yaml(out_path)
assert schedule == schedule2
def test_verify_schedule() -> None:
schedule = Schedule(
waypoints=[
Waypoint(location=Location(0, 0), time=datetime(2022, 1, 1, 1, 0, 0)),
Waypoint(location=Location(1, 0), time=datetime(2022, 1, 2, 1, 0, 0)),
]
)
ship_config = _get_ship_config(expedition_dir)
schedule.verify(ship_config.ship_speed_knots, None)
def test_get_instruments() -> None:
schedule = Schedule(
waypoints=[
Waypoint(location=Location(0, 0), instrument=["CTD"]),
Waypoint(location=Location(1, 0), instrument=["XBT", "ARGO_FLOAT"]),
Waypoint(location=Location(1, 0), instrument=["CTD"]),
]
)
assert set(instrument.name for instrument in schedule.get_instruments()) == {
"CTD",
"XBT",
"ARGO_FLOAT",
}
@pytest.mark.parametrize(
"schedule,check_space_time_region,error,match",
[
pytest.param(
Schedule(waypoints=[]),
False,
ScheduleError,
"At least one waypoint must be provided.",
id="NoWaypoints",
),
pytest.param(
Schedule(
waypoints=[
Waypoint(location=Location(0, 0)),
Waypoint(
location=Location(1, 0), time=datetime(2022, 1, 1, 1, 0, 0)
),
]
),
False,
ScheduleError,
"First waypoint must have a specified time.",
id="FirstWaypointHasTime",
),
pytest.param(
Schedule(
waypoints=[
Waypoint(
location=Location(0, 0), time=datetime(2022, 1, 2, 1, 0, 0)
),
Waypoint(location=Location(0, 0)),
Waypoint(
location=Location(1, 0), time=datetime(2022, 1, 1, 1, 0, 0)
),
]
),
False,
ScheduleError,
"Each waypoint should be timed after all previous waypoints",
id="SequentialWaypoints",
),
pytest.param(
Schedule(
waypoints=[
Waypoint(
location=Location(0, 0), time=datetime(2022, 1, 1, 1, 0, 0)
),
Waypoint(
location=Location(1, 0), time=datetime(2022, 1, 1, 1, 1, 0)
),
]
),
False,
ScheduleError,
"Waypoint planning is not valid: would arrive too late at waypoint number 2...",
id="NotEnoughTime",
),
pytest.param(
Schedule(
waypoints=[
Waypoint(
location=Location(0, 0), time=datetime(2022, 1, 1, 1, 0, 0)
),
Waypoint(
location=Location(1, 0), time=datetime(2022, 1, 2, 1, 1, 0)
),
]
),
True,
ScheduleError,
"space_time_region not found in schedule, please define it to fetch the data.",
id="NoSpaceTimeRegion",
),
],
)
def test_verify_schedule_errors(
schedule: Schedule, check_space_time_region: bool, error, match
) -> None:
ship_config = _get_ship_config(expedition_dir)
input_data = _load_input_data(
expedition_dir,
schedule,
ship_config,
input_data=Path("expedition_dir/input_data"),
)
with pytest.raises(error, match=match):
schedule.verify(
ship_config.ship_speed_knots,
input_data,
check_space_time_region=check_space_time_region,
)