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 returns correct structure.""" response = client.post( "/calculate", json={ "total_flour": 500, "hydration": 70, "sourdough_percentage": 20, "sourdough_ratio": { "flour_parts": 1, "water_parts": 1 }, "salt": 2 } ) assert response.status_code == 200 data = response.json() # Check main structure assert "ingredients_to_add" in data assert "details" in data # Check ingredients_to_add fields ingredients = data["ingredients_to_add"] assert "flour" in ingredients assert "water" in ingredients assert "sourdough" in ingredients assert "salt" in ingredients def test_bread_proportions_values(): """Tests the calculated values are correct.""" response = client.post( "/calculate", json={ "total_flour": 1000, "hydration": 70, "sourdough_percentage": 20, "sourdough_ratio": { "flour_parts": 1, "water_parts": 1 }, "salt": 2 } ) assert response.status_code == 200 data = response.json() ingredients = data["ingredients_to_add"] details = data["details"] # Test calculated values assert ingredients["flour"] == 900.0 # 1000 - 100 (flour in sourdough) assert ingredients["water"] == 600.0 # 700 - 100 (water in sourdough) assert ingredients["sourdough"] == 200.0 # 1000 * 20% assert ingredients["salt"] == 20.0 # 1000 * 2% # Test details assert details["total_flour"] == 1000 assert details["total_water"] == 700.0 # 1000 * 70% assert details["flour_in_sourdough"] == 100.0 assert details["water_in_sourdough"] == 100.0 def test_validation_hydration_too_high(): """Tests that validation rejects hydration > 100%.""" response = client.post( "/calculate", json={ "total_flour": 500, "hydration": 150, # ❌ Too high "sourdough_percentage": 20, "sourdough_ratio": { "flour_parts": 1, "water_parts": 1 }, "salt": 2 } ) assert response.status_code == 422 # Validation error def test_validation_negative_flour(): """Tests that validation rejects negative flour.""" response = client.post( "/calculate", json={ "total_flour": -500, # ❌ Negative "hydration": 70, "sourdough_percentage": 20, "sourdough_ratio": { "flour_parts": 1, "water_parts": 1 }, "salt": 2 } ) assert response.status_code == 422 # Validation error