|
9 | 9 | import unstructured_inference.models.detectron2 as detectron2 |
10 | 10 |
|
11 | 11 |
|
12 | | -@pytest.fixture |
13 | | -def sample_pdf_content(): |
14 | | - return """ |
15 | | - this is the content of a sample pdf file. |
16 | | - Title: ... |
17 | | - Author: ... |
18 | | - """ |
19 | | - |
20 | | - |
21 | 12 | class MockModel: |
22 | 13 | def __init__(self, *args, **kwargs): |
23 | 14 | self.args = args |
24 | 15 | self.kwargs = kwargs |
25 | 16 |
|
26 | 17 |
|
27 | | -def test_layout_parsing_pdf_api(sample_pdf_content, tmpdir, monkeypatch): |
| 18 | +@pytest.mark.parametrize("filetype, ext", [("pdf", "pdf"), ("image", "png")]) |
| 19 | +def test_layout_parsing_api(monkeypatch, filetype, ext): |
28 | 20 | monkeypatch.setattr(models, "load_model", lambda *args, **kwargs: MockModel(*args, **kwargs)) |
29 | 21 | monkeypatch.setattr(models, "hf_hub_download", lambda *args, **kwargs: "fake-path") |
30 | 22 | monkeypatch.setattr(detectron2, "is_detectron2_available", lambda *args: True) |
31 | 23 | monkeypatch.setattr( |
32 | 24 | DocumentLayout, "from_file", lambda *args, **kwargs: DocumentLayout.from_pages([]) |
33 | 25 | ) |
| 26 | + monkeypatch.setattr( |
| 27 | + DocumentLayout, "from_image_file", lambda *args, **kwargs: DocumentLayout.from_pages([]) |
| 28 | + ) |
34 | 29 |
|
35 | | - filename = os.path.join(tmpdir.dirname, "sample.pdf") |
36 | | - with open(filename, "w") as f: |
37 | | - f.write(sample_pdf_content) |
| 30 | + filename = os.path.join("sample-docs", f"loremipsum.{ext}") |
38 | 31 |
|
39 | 32 | client = TestClient(app) |
40 | | - response = client.post("/layout/pdf", files={"file": (filename, open(filename, "rb"))}) |
| 33 | + response = client.post(f"/layout/{filetype}", files={"file": (filename, open(filename, "rb"))}) |
41 | 34 | assert response.status_code == 200 |
42 | 35 |
|
43 | 36 | response = client.post( |
44 | | - "/layout/pdf", files={"file": (filename, open(filename, "rb"))}, data={"model": "checkbox"} |
| 37 | + f"/layout/{filetype}", |
| 38 | + files={"file": (filename, open(filename, "rb"))}, |
| 39 | + data={"model": "checkbox"}, |
45 | 40 | ) |
46 | 41 | assert response.status_code == 200 |
47 | 42 |
|
48 | 43 | response = client.post( |
49 | | - "/layout/pdf", |
| 44 | + f"/layout/{filetype}", |
50 | 45 | files={"file": (filename, open(filename, "rb"))}, |
51 | 46 | data={"model": "fake_model"}, |
52 | 47 | ) |
53 | 48 | assert response.status_code == 422 |
54 | 49 |
|
55 | 50 |
|
| 51 | +def test_bad_route_404(): |
| 52 | + client = TestClient(app) |
| 53 | + filename = os.path.join("sample-docs", "loremipsum.pdf") |
| 54 | + response = client.post("/layout/badroute", files={"file": (filename, open(filename, "rb"))}) |
| 55 | + assert response.status_code == 404 |
| 56 | + |
| 57 | + |
56 | 58 | def test_healthcheck(monkeypatch): |
57 | 59 | client = TestClient(app) |
58 | 60 | response = client.get("/healthcheck") |
|
0 commit comments