Skip to content

Commit c4ddea1

Browse files
committed
refactor test cases to use Expedition object
1 parent efb53ca commit c4ddea1

File tree

1 file changed

+85
-55
lines changed

1 file changed

+85
-55
lines changed

tests/expedition/test_expedition.py

Lines changed: 85 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
import pyproj
55
import pytest
66

7-
from virtualship.errors import ConfigError, ScheduleError
7+
from virtualship.errors import InstrumentsConfigError, ScheduleError
88
from virtualship.expedition.do_expedition import _load_input_data
9-
from virtualship.models import Expedition, Location, Schedule, Waypoint
9+
from virtualship.models import (
10+
Expedition,
11+
Location,
12+
Schedule,
13+
Waypoint,
14+
)
1015
from virtualship.utils import EXPEDITION, _get_expedition, get_example_expedition
1116

1217
projection = pyproj.Geod(ellps="WGS84")
@@ -56,19 +61,29 @@ def test_verify_schedule() -> None:
5661

5762

5863
def test_get_instruments() -> None:
64+
get_expedition = _get_expedition(expedition_dir)
5965
schedule = Schedule(
6066
waypoints=[
6167
Waypoint(location=Location(0, 0), instrument=["CTD"]),
6268
Waypoint(location=Location(1, 0), instrument=["XBT", "ARGO_FLOAT"]),
6369
Waypoint(location=Location(1, 0), instrument=["CTD"]),
6470
]
6571
)
66-
67-
assert set(instrument.name for instrument in schedule.get_instruments()) == {
68-
"CTD",
69-
"XBT",
70-
"ARGO_FLOAT",
71-
}
72+
expedition = Expedition(
73+
schedule=schedule,
74+
instruments_config=get_expedition.instruments_config,
75+
ship_config=get_expedition.ship_config,
76+
)
77+
assert (
78+
set(instrument.name for instrument in expedition.get_instruments())
79+
== {
80+
"CTD",
81+
"UNDERWATER_ST", # not added above but underway instruments are auto present from instruments_config in expedition_dir/expedition.yaml
82+
"ADCP", # as above
83+
"ARGO_FLOAT",
84+
"XBT",
85+
}
86+
)
7287

7388

7489
@pytest.mark.parametrize(
@@ -165,15 +180,15 @@ def test_verify_schedule_errors(
165180

166181

167182
@pytest.fixture
168-
def schedule(tmp_file):
183+
def expedition(tmp_file):
169184
with open(tmp_file, "w") as file:
170185
file.write(get_example_expedition())
171-
return Expedition.from_yaml(tmp_file).schedule
186+
return Expedition.from_yaml(tmp_file)
172187

173188

174189
@pytest.fixture
175-
def schedule_no_xbt(schedule):
176-
for waypoint in schedule.waypoints:
190+
def expedition_no_xbt(expedition):
191+
for waypoint in expedition.schedule.waypoints:
177192
if waypoint.instrument and any(
178193
instrument.name == "XBT" for instrument in waypoint.instrument
179194
):
@@ -183,95 +198,110 @@ def schedule_no_xbt(schedule):
183198
if instrument.name != "XBT"
184199
]
185200

186-
return schedule
201+
return expedition
187202

188203

189204
@pytest.fixture
190-
def instruments_config(tmp_file):
191-
with open(tmp_file, "w") as file:
192-
file.write(get_example_expedition())
193-
return Expedition.from_yaml(tmp_file).instruments_config
205+
def instruments_config_no_xbt(expedition):
206+
delattr(expedition.instruments_config, "xbt_config")
207+
return expedition.instruments_config
194208

195209

196210
@pytest.fixture
197-
def instruments_config_no_xbt(instruments_config):
198-
delattr(instruments_config, "xbt_config")
199-
return instruments_config
211+
def instruments_config_no_ctd(expedition):
212+
delattr(expedition.instruments_config, "ctd_config")
213+
return expedition.instruments_config
200214

201215

202216
@pytest.fixture
203-
def instruments_config_no_ctd(instruments_config):
204-
delattr(instruments_config, "ctd_config")
205-
return instruments_config
217+
def instruments_config_no_ctd_bgc(expedition):
218+
delattr(expedition.instruments_config, "ctd_bgc_config")
219+
return expedition.instruments_config
206220

207221

208222
@pytest.fixture
209-
def instruments_config_no_ctd_bgc(instruments_config):
210-
delattr(instruments_config, "ctd_bgc_config")
211-
return instruments_config
223+
def instruments_config_no_argo_float(expedition):
224+
delattr(expedition.instruments_config, "argo_float_config")
225+
return expedition.instruments_config
212226

213227

214228
@pytest.fixture
215-
def instruments_config_no_argo_float(instruments_config):
216-
delattr(instruments_config, "argo_float_config")
217-
return instruments_config
229+
def instruments_config_no_drifter(expedition):
230+
delattr(expedition.instruments_config, "drifter_config")
231+
return expedition.instruments_config
218232

219233

220234
@pytest.fixture
221-
def instruments_config_no_drifter(instruments_config):
222-
delattr(instruments_config, "drifter_config")
223-
return instruments_config
235+
def instruments_config_no_adcp(expedition):
236+
delattr(expedition.instruments_config, "adcp_config")
237+
return expedition.instruments_config
224238

225239

226-
def test_verify_instruments_config(instruments_config, schedule) -> None:
227-
instruments_config.verify(schedule)
240+
@pytest.fixture
241+
def instruments_config_no_underwater_st(expedition):
242+
delattr(expedition.instruments_config, "ship_underwater_st_config")
243+
return expedition.instruments_config
228244

229245

230-
def test_verify_instruments_config_no_instrument(
231-
instruments_config, schedule_no_xbt
232-
) -> None:
233-
instruments_config.verify(schedule_no_xbt)
246+
def test_verify_instruments_config(expedition) -> None:
247+
expedition.instruments_config.verify(expedition)
248+
249+
250+
def test_verify_instruments_config_no_instrument(expedition, expedition_no_xbt) -> None:
251+
expedition.instruments_config.verify(expedition_no_xbt)
234252

235253

236254
@pytest.mark.parametrize(
237255
"instruments_config_fixture,error,match",
238256
[
239257
pytest.param(
240258
"instruments_config_no_xbt",
241-
ConfigError,
242-
"Schedule includes instrument 'XBT', but instruments_config does not provide configuration for it.",
243-
id="ShipConfigNoXBT",
259+
InstrumentsConfigError,
260+
"Expedition includes instrument 'XBT', but instruments_config does not provide configuration for it.",
261+
id="InstrumentsConfigNoXBT",
244262
),
245263
pytest.param(
246264
"instruments_config_no_ctd",
247-
ConfigError,
248-
"Schedule includes instrument 'CTD', but instruments_config does not provide configuration for it.",
249-
id="ShipConfigNoCTD",
265+
InstrumentsConfigError,
266+
"Expedition includes instrument 'CTD', but instruments_config does not provide configuration for it.",
267+
id="InstrumentsConfigNoCTD",
250268
),
251269
pytest.param(
252270
"instruments_config_no_ctd_bgc",
253-
ConfigError,
254-
"Schedule includes instrument 'CTD_BGC', but instruments_config does not provide configuration for it.",
255-
id="ShipConfigNoCTD_BGC",
271+
InstrumentsConfigError,
272+
"Expedition includes instrument 'CTD_BGC', but instruments_config does not provide configuration for it.",
273+
id="InstrumentsConfigNoCTD_BGC",
256274
),
257275
pytest.param(
258276
"instruments_config_no_argo_float",
259-
ConfigError,
260-
"Schedule includes instrument 'ARGO_FLOAT', but instruments_config does not provide configuration for it.",
261-
id="ShipConfigNoARGO_FLOAT",
277+
InstrumentsConfigError,
278+
"Expedition includes instrument 'ARGO_FLOAT', but instruments_config does not provide configuration for it.",
279+
id="InstrumentsConfigNoARGO_FLOAT",
262280
),
263281
pytest.param(
264282
"instruments_config_no_drifter",
265-
ConfigError,
266-
"Schedule includes instrument 'DRIFTER', but instruments_config does not provide configuration for it.",
267-
id="ShipConfigNoDRIFTER",
283+
InstrumentsConfigError,
284+
"Expedition includes instrument 'DRIFTER', but instruments_config does not provide configuration for it.",
285+
id="InstrumentsConfigNoDRIFTER",
286+
),
287+
pytest.param(
288+
"instruments_config_no_adcp",
289+
InstrumentsConfigError,
290+
r"Underway instrument config attribute\(s\) are missing from YAML\. Must be <Instrument>Config object or None\.",
291+
id="InstrumentsConfigNoADCP",
292+
),
293+
pytest.param(
294+
"instruments_config_no_underwater_st",
295+
InstrumentsConfigError,
296+
r"Underway instrument config attribute\(s\) are missing from YAML\. Must be <Instrument>Config object or None\.",
297+
id="InstrumentsConfigNoUNDERWATER_ST",
268298
),
269299
],
270300
)
271301
def test_verify_instruments_config_errors(
272-
request, schedule, instruments_config_fixture, error, match
302+
request, expedition, instruments_config_fixture, error, match
273303
) -> None:
274304
instruments_config = request.getfixturevalue(instruments_config_fixture)
275305

276306
with pytest.raises(error, match=match):
277-
instruments_config.verify(schedule)
307+
instruments_config.verify(expedition)

0 commit comments

Comments
 (0)