- 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.
127 lines
3.9 KiB
Python
127 lines
3.9 KiB
Python
from typing import Any, Dict, List
|
|
from uuid import uuid4
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from config.database import Base, engine
|
|
from main import app
|
|
|
|
|
|
def setup_module(module: object) -> None:
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
def teardown_module(module: object) -> None:
|
|
Base.metadata.drop_all(bind=engine)
|
|
|
|
|
|
def _create_scenario(name: str | None = None) -> int:
|
|
payload: Dict[str, Any] = {
|
|
"name": name or f"ParamScenario-{uuid4()}",
|
|
"description": "Parameter test scenario",
|
|
}
|
|
response = TestClient(app).post("/api/scenarios/", json=payload)
|
|
assert response.status_code == 200
|
|
return response.json()["id"]
|
|
|
|
|
|
def _create_distribution() -> int:
|
|
payload: Dict[str, Any] = {
|
|
"name": f"NormalDist-{uuid4()}",
|
|
"distribution_type": "normal",
|
|
"parameters": {"mu": 10, "sigma": 2},
|
|
}
|
|
response = TestClient(app).post("/api/distributions/", json=payload)
|
|
assert response.status_code == 200
|
|
return response.json()["id"]
|
|
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def test_create_and_list_parameter():
|
|
scenario_id = _create_scenario()
|
|
distribution_id = _create_distribution()
|
|
parameter_payload: Dict[str, Any] = {
|
|
"scenario_id": scenario_id,
|
|
"name": f"param-{uuid4()}",
|
|
"value": 3.14,
|
|
"distribution_id": distribution_id,
|
|
}
|
|
|
|
create_response = client.post("/api/parameters/", json=parameter_payload)
|
|
assert create_response.status_code == 200
|
|
created = create_response.json()
|
|
assert created["scenario_id"] == scenario_id
|
|
assert created["name"] == parameter_payload["name"]
|
|
assert created["value"] == parameter_payload["value"]
|
|
assert created["distribution_id"] == distribution_id
|
|
assert created["distribution_type"] == "normal"
|
|
assert created["distribution_parameters"] == {"mu": 10, "sigma": 2}
|
|
|
|
list_response = client.get("/api/parameters/")
|
|
assert list_response.status_code == 200
|
|
params = list_response.json()
|
|
assert any(p["id"] == created["id"] for p in params)
|
|
|
|
|
|
def test_create_parameter_for_missing_scenario():
|
|
payload: Dict[str, Any] = {
|
|
"scenario_id": 0,
|
|
"name": "invalid",
|
|
"value": 1.0,
|
|
}
|
|
response = client.post("/api/parameters/", json=payload)
|
|
assert response.status_code == 404
|
|
assert response.json()["detail"] == "Scenario not found"
|
|
|
|
|
|
def test_multiple_parameters_listed():
|
|
scenario_id = _create_scenario()
|
|
payloads: List[Dict[str, Any]] = [
|
|
{"scenario_id": scenario_id, "name": f"alpha-{i}", "value": float(i)}
|
|
for i in range(2)
|
|
]
|
|
|
|
for payload in payloads:
|
|
resp = client.post("/api/parameters/", json=payload)
|
|
assert resp.status_code == 200
|
|
|
|
list_response = client.get("/api/parameters/")
|
|
assert list_response.status_code == 200
|
|
names = {item["name"] for item in list_response.json()}
|
|
for payload in payloads:
|
|
assert payload["name"] in names
|
|
|
|
|
|
def test_parameter_inline_distribution_metadata():
|
|
scenario_id = _create_scenario()
|
|
payload: Dict[str, Any] = {
|
|
"scenario_id": scenario_id,
|
|
"name": "inline-param",
|
|
"value": 7.5,
|
|
"distribution_type": "uniform",
|
|
"distribution_parameters": {"min": 5, "max": 10},
|
|
}
|
|
|
|
response = client.post("/api/parameters/", json=payload)
|
|
assert response.status_code == 200
|
|
created = response.json()
|
|
assert created["distribution_id"] is None
|
|
assert created["distribution_type"] == "uniform"
|
|
assert created["distribution_parameters"] == {"min": 5, "max": 10}
|
|
|
|
|
|
def test_parameter_with_missing_distribution_reference():
|
|
scenario_id = _create_scenario()
|
|
payload: Dict[str, Any] = {
|
|
"scenario_id": scenario_id,
|
|
"name": "missing-dist",
|
|
"value": 1.0,
|
|
"distribution_id": 9999,
|
|
}
|
|
|
|
response = client.post("/api/parameters/", json=payload)
|
|
assert response.status_code == 404
|
|
assert response.json()["detail"] == "Distribution not found"
|