- Updated test functions in various test files to enhance code clarity by formatting long lines and improving indentation. - Adjusted assertions to use multi-line formatting for better readability. - Added new test cases for theme settings API to ensure proper functionality. - Ensured consistent use of line breaks and spacing across test files for uniformity.
126 lines
4.1 KiB
Python
126 lines
4.1 KiB
Python
from uuid import uuid4
|
|
|
|
import pytest
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
@pytest.fixture
|
|
def client(api_client: TestClient) -> TestClient:
|
|
return api_client
|
|
|
|
|
|
def _create_scenario_and_equipment(client: TestClient):
|
|
scenario_payload = {
|
|
"name": f"Test Scenario {uuid4()}",
|
|
"description": "Scenario for maintenance tests",
|
|
}
|
|
scenario_response = client.post("/api/scenarios/", json=scenario_payload)
|
|
assert scenario_response.status_code == 200
|
|
scenario_id = scenario_response.json()["id"]
|
|
|
|
equipment_payload = {
|
|
"scenario_id": scenario_id,
|
|
"name": f"Test Equipment {uuid4()}",
|
|
"description": "Equipment linked to maintenance",
|
|
}
|
|
equipment_response = client.post("/api/equipment/", json=equipment_payload)
|
|
assert equipment_response.status_code == 200
|
|
equipment_id = equipment_response.json()["id"]
|
|
return scenario_id, equipment_id
|
|
|
|
|
|
def _create_maintenance_payload(
|
|
equipment_id: int, scenario_id: int, description: str
|
|
):
|
|
return {
|
|
"equipment_id": equipment_id,
|
|
"scenario_id": scenario_id,
|
|
"maintenance_date": "2025-10-20",
|
|
"description": description,
|
|
"cost": 100.0,
|
|
}
|
|
|
|
|
|
def test_create_and_list_maintenance(client: TestClient):
|
|
scenario_id, equipment_id = _create_scenario_and_equipment(client)
|
|
payload = _create_maintenance_payload(
|
|
equipment_id, scenario_id, "Create maintenance"
|
|
)
|
|
|
|
response = client.post("/api/maintenance/", json=payload)
|
|
assert response.status_code == 201
|
|
created = response.json()
|
|
assert created["equipment_id"] == equipment_id
|
|
assert created["scenario_id"] == scenario_id
|
|
assert created["description"] == "Create maintenance"
|
|
|
|
list_response = client.get("/api/maintenance/")
|
|
assert list_response.status_code == 200
|
|
items = list_response.json()
|
|
assert any(item["id"] == created["id"] for item in items)
|
|
|
|
|
|
def test_get_maintenance(client: TestClient):
|
|
scenario_id, equipment_id = _create_scenario_and_equipment(client)
|
|
payload = _create_maintenance_payload(
|
|
equipment_id, scenario_id, "Retrieve maintenance"
|
|
)
|
|
create_response = client.post("/api/maintenance/", json=payload)
|
|
assert create_response.status_code == 201
|
|
maintenance_id = create_response.json()["id"]
|
|
|
|
response = client.get(f"/api/maintenance/{maintenance_id}")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["id"] == maintenance_id
|
|
assert data["equipment_id"] == equipment_id
|
|
assert data["description"] == "Retrieve maintenance"
|
|
|
|
|
|
def test_update_maintenance(client: TestClient):
|
|
scenario_id, equipment_id = _create_scenario_and_equipment(client)
|
|
create_response = client.post(
|
|
"/api/maintenance/",
|
|
json=_create_maintenance_payload(
|
|
equipment_id, scenario_id, "Maintenance before update"
|
|
),
|
|
)
|
|
assert create_response.status_code == 201
|
|
maintenance_id = create_response.json()["id"]
|
|
|
|
update_payload = {
|
|
"equipment_id": equipment_id,
|
|
"scenario_id": scenario_id,
|
|
"maintenance_date": "2025-11-01",
|
|
"description": "Maintenance after update",
|
|
"cost": 250.0,
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/maintenance/{maintenance_id}", json=update_payload
|
|
)
|
|
assert response.status_code == 200
|
|
updated = response.json()
|
|
assert updated["maintenance_date"] == "2025-11-01"
|
|
assert updated["description"] == "Maintenance after update"
|
|
assert updated["cost"] == 250.0
|
|
|
|
|
|
def test_delete_maintenance(client: TestClient):
|
|
scenario_id, equipment_id = _create_scenario_and_equipment(client)
|
|
create_response = client.post(
|
|
"/api/maintenance/",
|
|
json=_create_maintenance_payload(
|
|
equipment_id, scenario_id, "Delete maintenance"
|
|
),
|
|
)
|
|
assert create_response.status_code == 201
|
|
maintenance_id = create_response.json()["id"]
|
|
|
|
delete_response = client.delete(f"/api/maintenance/{maintenance_id}")
|
|
assert delete_response.status_code == 204
|
|
|
|
get_response = client.get(f"/api/maintenance/{maintenance_id}")
|
|
assert get_response.status_code == 404
|