Skip to content

Commit

Permalink
Adding test for pydantic serialiser converstion from minutes to seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvansebille committed Feb 20, 2025
1 parent 065bdb9 commit 99955d7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/virtualship/expedition/ship_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ADCPConfig(pydantic.BaseModel):

@pydantic.field_serializer("period")
def _serialize_period(self, value: timedelta, _info):
return value.total_seconds() / 60.0
return value.total_seconds() * 60.0


class CTDConfig(pydantic.BaseModel):
Expand All @@ -53,7 +53,7 @@ class CTDConfig(pydantic.BaseModel):

@pydantic.field_serializer("stationkeeping_time_minutes")
def _serialize_stationkeeping_time_minutes(self, value: timedelta, _info):
return value.total_seconds() / 60.0
return value.total_seconds() * 60.0


class ShipUnderwaterSTConfig(pydantic.BaseModel):
Expand All @@ -69,7 +69,7 @@ class ShipUnderwaterSTConfig(pydantic.BaseModel):

@pydantic.field_serializer("period")
def _serialize_period(self, value: timedelta, _info):
return value.total_seconds() / 60.0
return value.total_seconds() * 60.0


class DrifterConfig(pydantic.BaseModel):
Expand All @@ -91,7 +91,11 @@ class DrifterConfig(pydantic.BaseModel):

@pydantic.field_serializer("lifetime")
def _serialize_lifetime(self, value: timedelta, _info):
return value.total_seconds() / 60.0
return value.total_seconds() * 60.0

@pydantic.field_serializer("period")
def _serialize_period(self, value: timedelta, _info):
return value.total_seconds() * 60.0


class XBTConfig(pydantic.BaseModel):
Expand Down
9 changes: 9 additions & 0 deletions tests/expedition/test_simulate_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,12 @@ def test_simulate_schedule_too_far() -> None:
result = simulate_schedule(projection, ship_config, schedule)

assert isinstance(result, ScheduleProblem)


def test_time_in_minutes_in_ship_schedule() -> None:
"""Test whether the pydantic serializer picks up the time *in minutes* in the ship schedule."""
ship_config = ShipConfig.from_yaml("expedition_dir/ship_config.yaml")
assert ship_config.drifter_config.period == timedelta(minutes=60)
assert ship_config.adcp_config.period == timedelta(minutes=5)
assert ship_config.ctd_config.stationkeeping_time_minutes == timedelta(minutes=20)
assert ship_config.ship_underwater_st_config.period == timedelta(minutes=5)
2 changes: 2 additions & 0 deletions tests/instruments/test_drifter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_simulate_drifters(tmpdir) -> None:
),
depth=0.0,
lifetime=datetime.timedelta(hours=2),
period=60,
),
Drifter(
spacetime=Spacetime(
Expand All @@ -49,6 +50,7 @@ def test_simulate_drifters(tmpdir) -> None:
),
depth=0.0,
lifetime=None,
period=60,
),
]

Expand Down

0 comments on commit 99955d7

Please sign in to comment.