fix(optimizer): implementar endpoints POST /api/genetic y proxy Express (v3.8.9)
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
"""
|
||||
Test suite validating FastAPI /api/genetic Heuristic Optimization endpoints.
|
||||
Tests preview-optimization, optimize, apply-optimization, commissions, and algorithm-status.
|
||||
"""
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from fastapi.testclient import TestClient
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
from main import app
|
||||
from app import db
|
||||
from app.models.user import User
|
||||
from app.models.classroom import Classroom
|
||||
from app.models.subject import Subject, Commission
|
||||
from app.models.reservation import Reservation, ReservationStatus
|
||||
from app.services.jwt_service import JWTService
|
||||
|
||||
|
||||
def test_genetic_endpoints_full_lifecycle(init_database):
|
||||
client = TestClient(app)
|
||||
|
||||
# 1. Crear usuario Administrador
|
||||
admin_user = User(
|
||||
email="bedelia.admin@unicaba.edu.ar",
|
||||
name="Admin Bedelia",
|
||||
password_hash=generate_password_hash("AdminPass123!"),
|
||||
role="ADMIN",
|
||||
is_active=True
|
||||
)
|
||||
init_database.session.add(admin_user)
|
||||
init_database.session.commit()
|
||||
|
||||
token = JWTService.create_access_token(admin_user)
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
# 2. Crear aulas de prueba
|
||||
room1 = Classroom(
|
||||
code="AULA-101",
|
||||
building="Central",
|
||||
capacity=40,
|
||||
is_active=True
|
||||
)
|
||||
room2 = Classroom(
|
||||
code="AULA-102",
|
||||
building="Anexo",
|
||||
capacity=50,
|
||||
is_active=True
|
||||
)
|
||||
init_database.session.add_all([room1, room2])
|
||||
init_database.session.commit()
|
||||
|
||||
# 3. Crear Asignatura y Comisión activa
|
||||
subject = Subject(
|
||||
code="INF-2026",
|
||||
name="Algoritmos y Estructuras",
|
||||
credits=4,
|
||||
is_active=True
|
||||
)
|
||||
init_database.session.add(subject)
|
||||
init_database.session.commit()
|
||||
|
||||
commission = Commission(
|
||||
subject_id=subject.id,
|
||||
code="COM-1A",
|
||||
semester="1C",
|
||||
year=2026,
|
||||
shift="Noche",
|
||||
schedule="Lunes 18:30 a 22:30",
|
||||
max_students=35,
|
||||
current_students=28,
|
||||
active=True
|
||||
)
|
||||
init_database.session.add(commission)
|
||||
init_database.session.commit()
|
||||
|
||||
comm_id = commission.id
|
||||
|
||||
# 4. Probar GET /api/genetic/commissions
|
||||
comm_res = client.get("/api/genetic/commissions", headers=headers)
|
||||
assert comm_res.status_code == 200
|
||||
comm_data = comm_res.json()
|
||||
assert comm_data["success"] is True
|
||||
assert len(comm_data["commissions"]) >= 1
|
||||
assert any(c["id"] == comm_id for c in comm_data["commissions"])
|
||||
|
||||
# 5. Probar GET /api/genetic/algorithm-status
|
||||
status_res = client.get("/api/genetic/algorithm-status", headers=headers)
|
||||
assert status_res.status_code == 200
|
||||
status_data = status_res.json()
|
||||
assert status_data["success"] is True
|
||||
assert "system_status" in status_data["data"]
|
||||
assert status_data["data"]["system_status"]["available_classrooms"] >= 2
|
||||
|
||||
# 6. Probar POST /api/genetic/preview-optimization con comisión válida
|
||||
preview_res = client.post(
|
||||
"/api/genetic/preview-optimization",
|
||||
headers=headers,
|
||||
json={"commission_ids": [comm_id], "dry_run": True}
|
||||
)
|
||||
assert preview_res.status_code == 200
|
||||
preview_data = preview_res.json()
|
||||
assert preview_data["success"] is True
|
||||
assert "data" in preview_data
|
||||
assert "commission_details" in preview_data["data"]
|
||||
assert len(preview_data["data"]["commission_details"]) == 1
|
||||
assert preview_data["data"]["commission_details"][0]["id"] == comm_id
|
||||
assert "optimization_result" in preview_data["data"]
|
||||
opt_res = preview_data["data"]["optimization_result"]
|
||||
assert "assigned_reservations" in opt_res
|
||||
assert "fitness_score" in opt_res
|
||||
|
||||
# 7. Probar POST /api/genetic/preview-optimization con comisión inexistente -> 404
|
||||
err_res = client.post(
|
||||
"/api/genetic/preview-optimization",
|
||||
headers=headers,
|
||||
json={"commission_ids": [99999], "dry_run": True}
|
||||
)
|
||||
assert err_res.status_code == 404
|
||||
|
||||
# 8. Probar POST /api/genetic/optimize
|
||||
now = datetime.now(timezone.utc).replace(tzinfo=None)
|
||||
optimize_res = client.post(
|
||||
"/api/genetic/optimize",
|
||||
headers=headers,
|
||||
json={
|
||||
"commission_ids": [comm_id],
|
||||
"start_date": now.isoformat(),
|
||||
"end_date": (now + timedelta(days=30)).isoformat()
|
||||
}
|
||||
)
|
||||
assert optimize_res.status_code == 200
|
||||
opt_payload = optimize_res.json()
|
||||
assert opt_payload["success"] is True
|
||||
data = opt_payload["data"]
|
||||
assert "reservations" in data
|
||||
assert len(data["reservations"]) >= 1
|
||||
|
||||
generated_reservations = data["reservations"]
|
||||
|
||||
# 9. Probar POST /api/genetic/apply-optimization
|
||||
apply_res = client.post(
|
||||
"/api/genetic/apply-optimization",
|
||||
headers=headers,
|
||||
json={"reservations": generated_reservations}
|
||||
)
|
||||
assert apply_res.status_code == 200
|
||||
apply_payload = apply_res.json()
|
||||
assert apply_payload["success"] is True
|
||||
assert apply_payload["data"]["applied_count"] >= 1
|
||||
assert len(apply_payload["data"]["reservations"]) >= 1
|
||||
|
||||
# Verificar que las reservas persistieron en la BD como CONFIRMED
|
||||
saved_res = Reservation.query.filter_by(commission_id=comm_id).first()
|
||||
assert saved_res is not None
|
||||
assert saved_res.status == ReservationStatus.CONFIRMED.value
|
||||
Reference in New Issue
Block a user