diff --git a/api/db/repositories.py b/api/db/repositories.py index 6608718..8a90014 100644 --- a/api/db/repositories.py +++ b/api/db/repositories.py @@ -16,4 +16,8 @@ def create_form(session: Session, form: FormSubmission) -> FormSubmission: session.add(form) session.commit() session.refresh(form) - return form \ No newline at end of file + return form + + +def get_form_submission(session: Session, submission_id: int) -> FormSubmission | None: + return session.get(FormSubmission, submission_id) \ No newline at end of file diff --git a/api/routes/forms.py b/api/routes/forms.py index f3430ed..b8a7fe7 100644 --- a/api/routes/forms.py +++ b/api/routes/forms.py @@ -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 from api.db.models import FormSubmission from api.errors.base import AppError from src.controller import Controller @@ -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 + + diff --git a/tests/test_forms.py b/tests/test_forms.py index 8f432bf..e114c99 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -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 , 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"}, + ) + 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"