refactor: extract PROCEDURAL_TREE_TYPES constant and add negative tree_type test
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
"""Tree validation helper module for draft/published workflow."""
|
||||
from typing import Any
|
||||
|
||||
PROCEDURAL_TREE_TYPES = {"procedural", "maintenance"}
|
||||
|
||||
|
||||
class TreeValidationError(Exception):
|
||||
"""Custom exception for tree validation errors."""
|
||||
@@ -224,14 +226,14 @@ def can_publish_tree(
|
||||
errors.append({"field": "name", "message": "Tree must have a name to be published"})
|
||||
|
||||
# Validate structure based on tree type
|
||||
if tree_type in ("procedural", "maintenance"):
|
||||
if tree_type in PROCEDURAL_TREE_TYPES:
|
||||
structure_valid, structure_errors = validate_procedural_structure(tree_structure)
|
||||
else:
|
||||
structure_valid, structure_errors = validate_tree_structure(tree_structure)
|
||||
errors.extend(structure_errors)
|
||||
|
||||
# Validate intake form if present (procedural only)
|
||||
if intake_form and tree_type in ("procedural", "maintenance"):
|
||||
if intake_form and tree_type in PROCEDURAL_TREE_TYPES:
|
||||
form_valid, form_errors = _validate_intake_form(intake_form)
|
||||
errors.extend(form_errors)
|
||||
|
||||
|
||||
@@ -50,3 +50,24 @@ async def test_list_maintenance_trees_filter(client: AsyncClient, auth_headers:
|
||||
trees = resp.json()
|
||||
assert all(t["tree_type"] == "maintenance" for t in trees)
|
||||
assert len(trees) >= 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_invalid_tree_type_rejected(client: AsyncClient, auth_headers: dict):
|
||||
"""An unrecognized tree_type value is rejected with 422."""
|
||||
resp = await client.post(
|
||||
"/api/v1/trees",
|
||||
json={
|
||||
"name": "Bad Type",
|
||||
"tree_type": "garbage",
|
||||
"tree_structure": {
|
||||
"steps": [
|
||||
{"id": "s1", "type": "procedure_step", "title": "Step",
|
||||
"description": "Do it", "content_type": "action"},
|
||||
{"id": "end", "type": "procedure_end", "title": "Done"},
|
||||
]
|
||||
},
|
||||
},
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert resp.status_code == 422
|
||||
|
||||
Reference in New Issue
Block a user