- 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.
78 lines
2.2 KiB
Python
78 lines
2.2 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(client: TestClient) -> int:
|
|
payload = {
|
|
"name": f"Consumption Scenario {uuid4()}",
|
|
"description": "Scenario for consumption tests",
|
|
}
|
|
response = client.post("/api/scenarios/", json=payload)
|
|
assert response.status_code == 200
|
|
return response.json()["id"]
|
|
|
|
|
|
def test_create_consumption(client: TestClient) -> None:
|
|
scenario_id = _create_scenario(client)
|
|
payload = {
|
|
"scenario_id": scenario_id,
|
|
"amount": 125.5,
|
|
"description": "Fuel usage baseline",
|
|
"unit_name": "Liters",
|
|
"unit_symbol": "L",
|
|
}
|
|
|
|
response = client.post("/api/consumption/", json=payload)
|
|
assert response.status_code == 201
|
|
body = response.json()
|
|
assert body["id"] > 0
|
|
assert body["scenario_id"] == scenario_id
|
|
assert body["amount"] == pytest.approx(125.5)
|
|
assert body["description"] == "Fuel usage baseline"
|
|
assert body["unit_symbol"] == "L"
|
|
|
|
|
|
def test_list_consumption_returns_created_items(client: TestClient) -> None:
|
|
scenario_id = _create_scenario(client)
|
|
values = [50.0, 80.75]
|
|
for amount in values:
|
|
response = client.post(
|
|
"/api/consumption/",
|
|
json={
|
|
"scenario_id": scenario_id,
|
|
"amount": amount,
|
|
"description": f"Consumption {amount}",
|
|
"unit_name": "Tonnes",
|
|
"unit_symbol": "t",
|
|
},
|
|
)
|
|
assert response.status_code == 201
|
|
|
|
list_response = client.get("/api/consumption/")
|
|
assert list_response.status_code == 200
|
|
items = [
|
|
item
|
|
for item in list_response.json()
|
|
if item["scenario_id"] == scenario_id
|
|
]
|
|
assert {item["amount"] for item in items} == set(values)
|
|
|
|
|
|
def test_create_consumption_rejects_negative_amount(client: TestClient) -> None:
|
|
scenario_id = _create_scenario(client)
|
|
payload = {
|
|
"scenario_id": scenario_id,
|
|
"amount": -10,
|
|
"description": "Invalid negative amount",
|
|
}
|
|
|
|
response = client.post("/api/consumption/", json=payload)
|
|
assert response.status_code == 422
|