File size: 1,987 Bytes
174138d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70b960d
174138d
 
 
550c7ec
ca919d4
174138d
 
 
 
70b960d
174138d
 
 
ca919d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pytest
from fastapi.testclient import TestClient
from everycure.app import app
import os
import tempfile

client = TestClient(app)

def create_test_pdf():
    # Create a temporary PDF file for testing
    with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp:
        tmp.write(b"%PDF-1.5\nTest PDF content")
        return tmp.name

@pytest.fixture
def test_pdf():
    pdf_path = create_test_pdf()
    yield pdf_path
    # Cleanup after test
    os.unlink(pdf_path)


def test_extract_entities_invalid_file():
    # Test with non-PDF file
    with tempfile.NamedTemporaryFile(suffix=".txt") as tmp:
        tmp.write(b"Not a PDF file")
        tmp.seek(0)
        response = client.post(
            "/api/v1/extract",
            files={"file": ("test.txt", tmp, "text/plain")}
        )
    
    assert response.status_code == 415
    assert "Unsupported file type" in response.json()["detail"]

def test_extract_entities_empty_file(test_pdf):
    with open(test_pdf, "rb") as f:
        response = client.post(
            "/api/v1/extract",
            files={}  # No file provided
        )
    
    assert response.status_code == 400  # Bad request error
    assert "Bad request, file not included or empty filename" in response.json()["detail"]

def test_extract_entities_server_error(monkeypatch):
    # Mock extract_entities_from_pdf to raise an exception
    def mock_extract(*args):
        raise Exception("Internal server error")
    
    monkeypatch.setattr("everycure.app.extract_entities_from_pdf", mock_extract)
    
    # Create a valid PDF file but force a server error
    with tempfile.NamedTemporaryFile(suffix=".pdf") as tmp:
        tmp.write(b"%PDF-1.5\nTest PDF content")
        tmp.seek(0)
        response = client.post(
            "/api/v1/extract",
            files={"file": ("test.pdf", tmp, "application/pdf")}
        )
    
    assert response.status_code == 500
    assert "Internal server error" in response.json()["detail"]