from fastapi import FastAPI, Depends, HTTPException, status from sqlalchemy.orm import Session from . import models, schemas, crud, database models.Base.metadata.create_all(bind=database.engine) app = FastAPI(title="Test API") def get_db(): db = database.SessionLocal() try: yield db finally: db.close() @app.post("/tests/", response_model=schemas.Test, status_code=status.HTTP_201_CREATED) def create_test_endpoint(test: schemas.TestCreate, db: Session = Depends(get_db)): return crud.create_test(db, test) @app.get("/tests/{test_id}", response_model=schemas.Test) def read_test(test_id: int, db: Session = Depends(get_db)): db_test = crud.get_test(db, test_id) if not db_test: raise HTTPException(status_code=404, detail="Test not found") return db_test @app.get("/tests/", response_model=list[schemas.Test]) def list_tests(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): return crud.get_tests(db, skip=skip, limit=limit) @app.put("/tests/{test_id}", response_model=schemas.Test) def update_test_endpoint(test_id: int, updates: schemas.TestUpdate, db: Session = Depends(get_db)): db_test = crud.get_test(db, test_id) if not db_test: raise HTTPException(status_code=404, detail="Test not found") return crud.update_test(db, db_test, updates) @app.delete("/tests/{test_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_test_endpoint(test_id: int, db: Session = Depends(get_db)): db_test = crud.get_test(db, test_id) if not db_test: raise HTTPException(status_code=404, detail="Test not found") crud.delete_test(db, db_test) return None