dough-calc/tests/test_main.py

59 lines
1.5 KiB
Python

from fastapi.testclient import TestClient
from config import get_settings
from main import app
client = TestClient(app)
def test_cors_headers():
"""Tests that CORS is set."""
response = client.options(
"/",
headers={
"Origin": "http://localhost:5173",
"Access-Control-Request-Method": "GET"
}
)
assert "access-control-allow-origin" in response.headers
def test_settings_injection():
"""Tests that the settings are available."""
settings = get_settings()
assert settings is not None
assert hasattr(settings, "cors_origins")
def test_bread_proportions():
"""Tests the /calculate endpoint."""
response = client.post(
"/calculate",
json = {
"flour": 500,
"hydration": 63,
"sourdough_hydration": 100,
"salt_percent": 1.6
}
)
assert response.status_code == 200
data = response.json()
assert "flour" in data
assert "water" in data
assert "sourdough" in data
assert "salt" in data
def test_bread_proportions_values():
"""Tests the calculated values."""
response = client.post(
"/calculate",
json={
"flour": 1000,
"hydration": 62,
"sourdough_hydration": 50,
"sourdough_percent": 20
}
)
data = response.json()
assert data["flour"] == 900.0
assert data["water"] == 520.0
assert data["sourdough"] == 200.0
assert data["salt"] == 16.0