Skip to content

Commit

Permalink
Fix type on RetractionMessage retract_latest property and rename
Browse files Browse the repository at this point in the history
  • Loading branch information
justinvasel committed Aug 16, 2024
1 parent 11a1729 commit e2a1fde
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
21 changes: 11 additions & 10 deletions snews/models/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
# Third-party modules
import numpy as np
from pydantic import (BaseModel, ConfigDict, Field, NonNegativeFloat,
ValidationError, field_validator, model_validator)
NonNegativeInt, ValidationError, field_validator,
model_validator)

# Local modules
from ..__version__ import schema_version
Expand Down Expand Up @@ -232,20 +233,20 @@ class RetractionMessage(DetectorMessageBase):

model_config = ConfigDict(validate_assignment=True)

retract_message_uid: Optional[str] = Field(
retract_message_uuid: Optional[str] = Field(
default=None,
title="Unique message ID",
description="Unique identifier for the message to retract"
)

retract_latest: bool = Field(
default=False,
retract_latest_n: NonNegativeInt = Field(
default=0,
title="Retract Latest Flag",
description="True if the latest message is being retracted",
)

retraction_reason: str = Field(
...,
retraction_reason: Optional[str] = Field(
default=None,
title="Retraction reason",
description="Reason for retraction",
)
Expand All @@ -257,11 +258,11 @@ def _set_tier(cls, values):

@model_validator(mode="after")
def _validate_model(self):
if self.retract_latest and self.retract_message_uid is not None:
raise ValueError("retract_message_uuid cannot be specified when retract_latest=True")
if self.retract_latest_n > 0 and self.retract_message_uuid is not None:
raise ValueError("retract_message_uuid cannot be specified when retract_latest_n > 0")

if not self.retract_latest and self.retract_message_uid is None:
raise ValueError("Must specify either retract_message_uuid or retract_latest=True")
if self.retract_latest_n == 0 and self.retract_message_uuid is None:
raise ValueError("Must specify either retract_message_uuid or retract_latest_n > 0")
return self


Expand Down
24 changes: 16 additions & 8 deletions snews/schema/RetractionMessage.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
"title": "Detector Name",
"type": "string"
},
"retract_message_uid": {
"retract_message_uuid": {
"anyOf": [
{
"type": "string"
Expand All @@ -153,22 +153,30 @@
"description": "Unique identifier for the message to retract",
"title": "Unique message ID"
},
"retract_latest": {
"default": false,
"retract_latest_n": {
"default": 0,
"description": "True if the latest message is being retracted",
"minimum": 0,
"title": "Retract Latest Flag",
"type": "boolean"
"type": "integer"
},
"retraction_reason": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "Reason for retraction",
"title": "Retraction reason",
"type": "string"
"title": "Retraction reason"
}
},
"required": [
"tier",
"detector_name",
"retraction_reason"
"detector_name"
],
"title": "RetractionMessage",
"type": "object"
Expand Down
12 changes: 6 additions & 6 deletions test/models/test_message_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
# Retraction message
strategy_required_fields_retraction = {
**strategy_required_fields_base,
"retract_message_uid": st.uuids(version=4).map(lambda x: str(x)),
"retract_message_uuid": st.uuids(version=4).map(lambda x: str(x)),
"retraction_reason": st.text(min_size=1),
}

Expand Down Expand Up @@ -165,10 +165,10 @@ def test_snews_message_model_retraction_required(**kwargs):
def test_snews_message_model_retraction_validation_both_indicators(**kwargs):
with pytest.raises(ValueError) as exc_info:
msg = RetractionMessage(**kwargs)
msg.retract_latest = True
msg.retract_latest_n = 3
msg.retract_message_uid = "1234567890"

assert "retract_message_uuid cannot be specified when retract_latest=True" in str(
assert "retract_message_uuid cannot be specified when retract_latest_n > 0" in str(
exc_info.value
)

Expand All @@ -177,9 +177,9 @@ def test_snews_message_model_retraction_validation_both_indicators(**kwargs):
def test_snews_message_model_retraction_validation_neither_indicator(**kwargs):
with pytest.raises(ValueError) as exc_info:
msg = RetractionMessage(**kwargs)
msg.retract_latest = False
msg.retract_message_uid = None
msg.retract_latest_n = 0
msg.retract_message_uuid = None

assert "Must specify either retract_message_uuid or retract_latest=True" in str(
assert "Must specify either retract_message_uuid or retract_latest_n > 0" in str(
exc_info.value
)

0 comments on commit e2a1fde

Please sign in to comment.