Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion api/db/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ def create_form(session: Session, form: FormSubmission) -> FormSubmission:
session.add(form)
session.commit()
session.refresh(form)
return form
return form


def get_form_submission(session: Session, submission_id: int) -> FormSubmission | None:
return session.get(FormSubmission, submission_id)
12 changes: 10 additions & 2 deletions api/routes/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, HTTPException
from sqlmodel import Session
from api.deps import get_db
from api.schemas.forms import FormFill, FormFillResponse
from api.db.repositories import create_form, get_template
from api.db.repositories import create_form, get_template, get_form_submission
Comment on lines +1 to +5
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fill_form raises AppError, but api/main.py never registers register_exception_handlers, so AppError will surface as an unhandled exception (500) rather than the intended 404. Since this file now introduces HTTPException-based 404s, consider standardizing by switching the existing AppError("Template not found") path to HTTPException(status_code=404, detail=...), or alternatively register the AppError handler in api/main.py and align the error response shape (currently {"error": ...} vs {"detail": ...}).

Copilot uses AI. Check for mistakes.
from api.db.models import FormSubmission
from api.errors.base import AppError
from src.controller import Controller
Expand All @@ -23,3 +23,11 @@ def fill_form(form: FormFill, db: Session = Depends(get_db)):
return create_form(db, submission)


@router.get("/{submission_id}", response_model=FormFillResponse)
def get_form_submission_by_id(submission_id: int, db: Session = Depends(get_db)):
submission = get_form_submission(db, submission_id)
if submission is None:
raise HTTPException(status_code=404, detail="Form submission not found")
return submission


71 changes: 46 additions & 25 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
def test_submit_form(client):
pass
# First create a template
# form_payload = {
# "template_id": 3,
# "input_text": "Hi. The employee's name is John Doe. His job title is managing director. His department supervisor is Jane Doe. His phone number is 123456. His email is jdoe@ucsc.edu. The signature is <Mamañema>, and the date is 01/02/2005",
# }

# template_res = client.post("/templates/", json=template_payload)
# template_id = template_res.json()["id"]

# # Submit a form
# form_payload = {
# "template_id": template_id,
# "data": {"rating": 5, "comment": "Great service"},
# }

# response = client.post("/forms/", json=form_payload)

# assert response.status_code == 200

# data = response.json()
# assert data["id"] is not None
# assert data["template_id"] == template_id
# assert data["data"] == form_payload["data"]
from src.controller import Controller


def _template_payload(name: str):
return {
"name": name,
"pdf_path": "src/inputs/file.pdf",
"fields": {
"Employee's name": "string",
"Employee's job title": "string",
"Employee's department supervisor": "string",
"Employee's phone number": "string",
"Employee's email": "string",
"Signature": "string",
"Date": "string",
},
}


def test_get_form_submission_by_id(client, monkeypatch):
monkeypatch.setattr(Controller, "fill_form", lambda self, user_input, fields, pdf_form_path: "filled_test.pdf")

template_response = client.post("/templates/create", json=_template_payload("Form Template"))
template_id = template_response.json()["id"]

fill_response = client.post(
"/forms/fill",
json={"template_id": template_id, "input_text": "test transcript"},
)
Comment on lines +23 to +29
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test uses template_response.json()["id"] without asserting the template creation succeeded. Adding assert template_response.status_code == 200 (and similarly for fill_response) will make failures clearer and avoid misleading KeyError/JSONDecodeError when the request fails.

Suggested change
template_response = client.post("/templates/create", json=_template_payload("Form Template"))
template_id = template_response.json()["id"]
fill_response = client.post(
"/forms/fill",
json={"template_id": template_id, "input_text": "test transcript"},
)
template_response = client.post("/templates/create", json=_template_payload("Form Template"))
assert template_response.status_code == 200
template_id = template_response.json()["id"]
fill_response = client.post(
"/forms/fill",
json={"template_id": template_id, "input_text": "test transcript"},
)
assert fill_response.status_code == 200

Copilot uses AI. Check for mistakes.
submission_id = fill_response.json()["id"]

response = client.get(f"/forms/{submission_id}")

assert response.status_code == 200
data = response.json()
assert data["id"] == submission_id
assert data["template_id"] == template_id
assert data["input_text"] == "test transcript"
assert data["output_pdf_path"] == "filled_test.pdf"


def test_get_form_submission_by_id_not_found(client):
response = client.get("/forms/999999")

assert response.status_code == 404
assert response.json()["detail"] == "Form submission not found"
Loading