Skip to content

Commit

Permalink
Use pydantic alias for planned maintenance fields
Browse files Browse the repository at this point in the history
To have them match the field names in Zino1
  • Loading branch information
johannaengland committed Jul 30, 2024
1 parent 395ac8e commit 920b82b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.d/287.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use Zino1 field names for serialization of planned maintenance
2 changes: 1 addition & 1 deletion src/zino/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def dump_state_to_file(self, filename: str):
"""Dumps the full state to a file in JSON format"""
_log.debug("dumping state to %s", filename)
with open(filename, "w") as statefile:
statefile.write(self.model_dump_json(exclude_none=True, indent=2))
statefile.write(self.model_dump_json(exclude_none=True, indent=2, by_alias=True))

@classmethod
@log_time_spent()
Expand Down
13 changes: 8 additions & 5 deletions src/zino/statemodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Union,
)

from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field

from zino.compat import StrEnum
from zino.time import now
Expand Down Expand Up @@ -413,13 +413,16 @@ class PmType(StrEnum):


class PlannedMaintenance(BaseModel):
# Allow populating fields by name or alias
model_config = ConfigDict(populate_by_name=True)

id: Optional[int] = None
start_time: datetime.datetime
end_time: datetime.datetime
start_time: datetime.datetime = Field(alias="starttime")
end_time: datetime.datetime = Field(alias="endtime")
type: PmType
match_type: MatchType
match_device: Optional[str] = None
match_expression: str
match_device: Optional[str] = Field(default=None, alias="match_dev")
match_expression: str = Field(alias="match_expr")
log: List[LogEntry] = []
event_ids: List[int] = []

Expand Down
8 changes: 8 additions & 0 deletions tests/planned_maintenance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ def test_pms_should_be_parsed_as_correct_subclass_when_read_from_file(tmp_path,
assert isinstance(read_portstate_pm, PortStateMaintenance)


def test_model_dump_of_pm_should_use_aliases(state, active_portstate_pm, active_pm):
state_dump = state.model_dump_json(exclude_none=True, indent=2, by_alias=True)
assert "starttime" in state_dump
assert "endtime" in state_dump
assert "match_dev" in state_dump
assert "match_expr" in state_dump


@pytest.fixture
def pms():
return PlannedMaintenances()
Expand Down

0 comments on commit 920b82b

Please sign in to comment.