Skip to content

Commit

Permalink
Attempt to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaPrahoveanu-SL committed Dec 17, 2024
1 parent 7bee6ca commit dc2790d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion backend/src/utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def handle_file_upload(file: LLMFile) -> FileUpload:
except Exception as text_error:
raise HTTPException(
status_code=400,
detail="File upload must be a supported type text or pdf)"
detail="File upload must be a supported type text or pdf"
) from text_error

session_file = FileUpload(
Expand Down
6 changes: 5 additions & 1 deletion backend/tests/agents/report_agent_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from src.llm.llm import LLMFile
from src.agents.report_agent import ReportAgent
from src.llm.factory import get_llm

Expand All @@ -14,6 +15,9 @@ async def test_invoke_calls_llm(mocker):

mock_llm.chat = mocker.AsyncMock(return_value=mock_response)

response = await report_agent.create_report("Test Document", materiality_topics={"abc": "123"})
response = await report_agent.create_report(
LLMFile(file_name="test", file=b"Sample text content"),
materiality_topics={"abc": "123"}
)

assert response == mock_response
30 changes: 14 additions & 16 deletions backend/tests/utils/file_utils_test.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
from io import BytesIO
from typing import BinaryIO
from unittest.mock import MagicMock
from fastapi import HTTPException, UploadFile
from fastapi.datastructures import Headers
from fastapi import HTTPException

import pytest

from src.llm.llm import LLMFile
from src.utils.file_utils import handle_file_upload


def test_handle_file_upload_size():
with pytest.raises(HTTPException) as err:
handle_file_upload(UploadFile(file=BinaryIO(), size=15*1024*1024))
large_file_content = b"x" * (15*1024*1024 + 1)
handle_file_upload(LLMFile(file_name="test", file=large_file_content))

assert err.value.status_code == 413
assert err.value.detail == 'File upload must be less than 10485760 bytes'


def test_handle_file_upload_unsupported_type():
headers = Headers({"content-type": "text/html"})
file_content = b"\x89PNG\r\n\x1a\n\x00\x00\x00IHDR"
with pytest.raises(HTTPException) as err:
handle_file_upload(UploadFile(file=BinaryIO(), size=15*1024, headers=headers, filename="test.txt"))
handle_file_upload(LLMFile(file_name="test.png", file=file_content ))

assert err.value.status_code == 400
assert err.value.detail == 'File upload must be supported type (text/plain or application/pdf)'
assert err.value.detail == 'File upload must be a supported type text or pdf'


def test_handle_file_upload_missing_file_name():
headers = Headers({"content-type": "text/html"})
file_content = b"Sample text content"
with pytest.raises(HTTPException) as err:
handle_file_upload(UploadFile(file=BytesIO(b"test content"), size=12, headers=headers))
handle_file_upload(LLMFile(file=file_content, file_name=""))

assert err.value.status_code == 400
assert err.value.detail == 'Filename missing from file upload'
Expand All @@ -37,20 +37,18 @@ def test_handle_file_upload_missing_file_name():
def test_handle_file_upload_text(mocker):
mock = mocker.patch("src.utils.file_utils.update_session_file_uploads", MagicMock())

headers = Headers({"content-type": "text/plain"})
file = BytesIO(b"test content")
session_file = handle_file_upload(UploadFile(file=file, size=12, headers=headers, filename="test.txt"))
file_content = b"Sample text content"
session_file = handle_file_upload(LLMFile(file_name="test.txt", file=file_content ))

mock.assert_called_with(session_file)


def test_handle_file_upload_pdf(mocker):
mock = mocker.patch("src.utils.file_utils.update_session_file_uploads", MagicMock())
pdf_mock = mocker.patch("src.utils.file_utils.PdfReader", MagicMock())
file_content = b"%PDF-1.4"


headers = Headers({"content-type": "application/pdf"})
session_file = handle_file_upload(UploadFile(file=BytesIO(), size=12, headers=headers, filename="test.pdf"))
session_file = handle_file_upload(LLMFile(file_name="test.pdf", file=file_content ))

pdf_mock.assert_called_once()
mock.assert_called_with(session_file)
Expand Down

0 comments on commit dc2790d

Please sign in to comment.