style: compacted program and reworked tests

ref: N25B-198
This commit is contained in:
JobvAlewijk
2025-11-12 18:04:39 +01:00
parent 79d3bfb3a6
commit 2ed2a84f13
2 changed files with 83 additions and 60 deletions

View File

@@ -5,8 +5,9 @@ import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from control_backend.api.v1.endpoints import program # <-- import your router
from control_backend.api.v1.endpoints import program
from control_backend.schemas.message import Message
from control_backend.schemas.program import Program
@pytest.fixture
@@ -23,30 +24,40 @@ def client(app):
return TestClient(app)
def make_valid_phase_dict():
"""Helper to create a valid Phase JSON structure."""
def make_valid_program_dict():
"""Helper to create a valid Program JSON structure."""
return {
"id": "phase1",
"name": "basephase",
"nextPhaseId": "phase2",
"phaseData": {
"norms": [{"id": "n1", "name": "norm", "value": "be nice"}],
"goals": [{"id": "g1", "name": "goal", "description": "test goal", "achieved": False}],
"triggers": [
{"id": "t1", "label": "trigger", "type": "keyword", "value": ["stop", "exit"]}
],
},
"phases": [
{
"id": "phase1",
"name": "basephase",
"nextPhaseId": "phase2",
"phaseData": {
"norms": [{"id": "n1", "name": "norm", "value": "be nice"}],
"goals": [
{"id": "g1", "name": "goal", "description": "test goal", "achieved": False}
],
"triggers": [
{
"id": "t1",
"label": "trigger",
"type": "keyword",
"value": ["stop", "exit"],
}
],
},
}
]
}
def test_receive_program_success(client):
"""Valid program JSON string should parse and be sent via the socket."""
# Arrange
"""Valid Program JSON should be parsed and sent through the socket."""
mock_pub_socket = AsyncMock()
client.app.state.endpoints_pub_socket = mock_pub_socket
phases_list = [make_valid_phase_dict()]
message_body = json.dumps(phases_list)
program_dict = make_valid_program_dict()
message_body = json.dumps(program_dict)
msg = Message(message=message_body)
# Act
@@ -54,38 +65,67 @@ def test_receive_program_success(client):
# Assert
assert response.status_code == 202
assert response.json() == {"status": "Program parsed", "phase_count": 1}
assert response.json() == {"status": "Program parsed"}
# Check the mocked socket
expected_body = json.dumps(phases_list).encode("utf-8")
mock_pub_socket.send_multipart.assert_awaited_once_with([b"program", expected_body])
# Verify socket call (don't compare raw JSON string)
mock_pub_socket.send_multipart.assert_awaited_once()
args, kwargs = mock_pub_socket.send_multipart.await_args
assert args[0][0] == b"program"
sent_bytes = args[0][1]
# Decode sent bytes and compare actual structures
sent_obj = json.loads(sent_bytes.decode())
expected_obj = Program.model_validate_json(message_body).model_dump()
assert sent_obj == expected_obj
def test_receive_program_invalid_json(client):
"""Malformed JSON string should return 400 with 'Undecodeable Json string'."""
"""Invalid JSON string (not parseable) should trigger HTTP 400."""
mock_pub_socket = AsyncMock()
client.app.state.endpoints_pub_socket = mock_pub_socket
# Not valid JSON
bad_message = Message(message="{not valid json}")
response = client.post("/program", json=bad_message.model_dump())
bad_json_str = "{invalid json}"
msg = Message(message=bad_json_str)
response = client.post("/program", json=msg.model_dump())
assert response.status_code == 400
assert response.json()["detail"] == "Undecodeable Json string"
assert response.json()["detail"] == "Not a valid program"
mock_pub_socket.send_multipart.assert_not_called()
def test_receive_program_invalid_phase(client):
"""Decodable JSON but invalid Phase structure should return 400 with 'Non-Phase String'."""
def test_receive_program_invalid_deep_structure(client):
"""Valid JSON shape but invalid deep nested data should still raise 400."""
mock_pub_socket = AsyncMock()
client.app.state.endpoints_pub_socket = mock_pub_socket
# Missing required Phase fields
invalid_phase = [{"id": "only_id"}]
bad_message = Message(message=json.dumps(invalid_phase))
# Structurally correct Program, but with missing elements
bad_program = {
"phases": [
{
"id": "phase1",
"name": "deepfail",
"nextPhaseId": "phase2",
"phaseData": {
"norms": [
{"id": "n1", "name": "norm"} # Missing "value"
],
"goals": [
{"id": "g1", "name": "goal", "description": "desc", "achieved": False}
],
"triggers": [
{"id": "t1", "label": "trigger", "type": "keyword", "value": ["start"]}
],
},
}
]
}
response = client.post("/program", json=bad_message.model_dump())
msg = Message(message=json.dumps(bad_program))
response = client.post("/program", json=msg.model_dump())
assert response.status_code == 400
assert response.json()["detail"] == "Non-Phase String"
assert response.json()["detail"] == "Not a valid program"
mock_pub_socket.send_multipart.assert_not_called()