Skip to content

Commit dbfc512

Browse files
committed
@pcrespov review: added more auto validation
1 parent b70892e commit dbfc512

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/models-library/src/models_library/projects_state.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ def check_not_null(v, values):
7171
raise ValueError("value cannot be None when project is locked")
7272
return v
7373

74+
@validator("status", always=True)
75+
@classmethod
76+
def check_status_compatible(v, values):
77+
if values["value"] is False and v not in ["CLOSED", "OPENED"]:
78+
raise ValueError(
79+
f"status is set to {v} and lock is set to {values['value']}!"
80+
)
81+
if values["value"] is True and v == "CLOSED":
82+
raise ValueError(
83+
f"status is set to {v} and lock is set to {values['value']}!"
84+
)
85+
return v
86+
7487

7588
class ProjectRunningState(BaseModel):
7689
value: RunningState = Field(

packages/models-library/tests/test_projects_state.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,18 @@ def test_projects_state_model_examples(model_cls, model_cls_examples):
1818
def test_project_locked_with_missing_owner_raises():
1919
with pytest.raises(ValueError):
2020
ProjectLocked(**{"value": True, "status": ProjectStatus.OPENED})
21+
ProjectLocked(**{"value": False, "status": ProjectStatus.OPENED})
22+
23+
24+
@pytest.mark.parametrize(
25+
"lock, status",
26+
[
27+
(False, x)
28+
for x in ProjectStatus
29+
if x not in [ProjectStatus.CLOSED, ProjectStatus.OPENED]
30+
]
31+
+ [(True, ProjectStatus.CLOSED)],
32+
)
33+
def test_project_locked_with_allowed_values(lock: bool, status: ProjectStatus):
34+
with pytest.raises(ValueError):
35+
ProjectLocked(**{"value": lock, "status": status})

0 commit comments

Comments
 (0)