Files
template-workflow-extract-b…/tests/testing_utils/test_parse.py
T
Clelia (Astra) Bertelli adda5d6b32 feat: template with new sdk (#178)
* feat: template with new sdk

* chore: more test updates

* chore: more tests

* chore: update system prompt

* fix: generate_value for nested pydantic models

* chore: template validation

* chore: add ExtractedData

* chore: last tweaks to new sdk template

* feat: add --template flag to bundle-coder.sh (#181)

* feat: add --template flag to bundle-coder.sh

Add a -T/--template flag to select which template to bundle instead of
creating a separate script. This consolidates bundle-coder.sh and
bundle-coder-new-sdk.sh into a single script.

- Default template remains 'extract-basic'
- Use --template extract-basic-new to bundle the new SDK template
- Template name suffix is included in the e2b template name

* Update bundle-coder.sh

---------

Co-authored-by: Claude <noreply@anthropic.com>

* chore: pr suggestions

* chore: package versioning

* chore: move extract-basic-new to extract-basic

* chore: prompt tweaks

* fix: typo

* fix: typo pt2

* chore: pr suggestions

---------

Co-authored-by: Adrian Lyjak <adrianlyjak@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
2026-01-23 16:12:00 +01:00

180 lines
5.8 KiB
Python

"""Tests for the FakeParseNamespace mock implementation."""
import pytest
import respx
from extraction_review.testing_utils import FakeLlamaCloudServer
from llama_cloud import APIStatusError, AsyncLlamaCloud
@pytest.fixture
def server():
"""Provide a server with parse namespace enabled."""
with FakeLlamaCloudServer() as srv:
yield srv
@pytest.fixture()
def client() -> AsyncLlamaCloud:
return AsyncLlamaCloud(api_key="fake-api-key")
@pytest.fixture()
def data() -> tuple[str, bytes, str]:
with open("tests/files/test.pdf", "rb") as f:
content = f.read()
return ("tests/files/test.pdf", content, "application/pdf")
@pytest.mark.asyncio
async def test_parse_with_upload_file(
server: FakeLlamaCloudServer, client: AsyncLlamaCloud, data: tuple[str, bytes, str]
) -> None:
job_create = await client.parsing.create(
tier="fast",
version="latest",
upload_file=data,
)
assert job_create.error_message is None
assert job_create.status == "COMPLETED"
assert job_create.project_id == server.default_project_id
job_response = await client.parsing.get(
job_id=job_create.id, expand=["text", "markdown", "items"]
)
assert job_response.job.id == job_create.id
assert job_response.job.status == job_create.status
assert job_response.job.project_id == job_create.project_id
assert job_response.items is not None
assert job_response.markdown is not None
assert job_response.text is not None
@pytest.mark.asyncio
async def test_parse_with_different_expand(
server: FakeLlamaCloudServer, client: AsyncLlamaCloud, data: tuple[str, bytes, str]
) -> None:
job_create = await client.parsing.create(
tier="fast",
version="latest",
upload_file=data,
)
job_response = await client.parsing.get(job_id=job_create.id, expand=["text"])
assert job_response.items is None
assert job_response.markdown is None
assert job_response.text is not None
job_response = await client.parsing.get(job_id=job_create.id, expand=["markdown"])
assert job_response.items is None
assert job_response.markdown is not None
assert job_response.text is None
job_response = await client.parsing.get(job_id=job_create.id, expand=["items"])
assert job_response.items is not None
assert job_response.markdown is None
assert job_response.text is None
# no expands -> defaul to items
job_response = await client.parsing.get(job_id=job_create.id)
assert job_response.items is not None
assert job_response.markdown is None
assert job_response.text is None
@pytest.mark.asyncio
async def test_parse_with_file_id(
server: FakeLlamaCloudServer, client: AsyncLlamaCloud, data: tuple[str, bytes, str]
) -> None:
file_name, _, _ = data
file_obj = await client.files.create(
file=file_name,
purpose="parse",
external_file_id=file_name,
)
job_create = await client.parsing.create(
tier="fast",
version="latest",
file_id=file_obj.id,
)
assert job_create.error_message is None
assert job_create.status == "COMPLETED"
assert job_create.project_id == server.default_project_id
job_response = await client.parsing.get(
job_id=job_create.id, expand=["text", "markdown", "items"]
)
assert job_response.job.id == job_create.id
assert job_response.job.status == job_create.status
assert job_response.job.project_id == job_create.project_id
assert job_response.items is not None
assert job_response.markdown is not None
assert job_response.text is not None
@pytest.mark.asyncio
async def test_parse_with_file_id_file_not_found(
server: FakeLlamaCloudServer,
client: AsyncLlamaCloud,
) -> None:
with pytest.raises(APIStatusError) as exc_info:
await client.parsing.create(
tier="fast",
version="latest",
file_id="does-not-exist",
)
assert exc_info.value.status_code == 404
@pytest.mark.asyncio
async def test_parse_without_fileid_or_sourceurl(
server: FakeLlamaCloudServer,
client: AsyncLlamaCloud,
) -> None:
with pytest.raises(APIStatusError) as exc_info:
await client.parsing.create(
tier="fast",
version="latest",
)
assert exc_info.value.status_code == 400
@pytest.mark.asyncio
@respx.mock(assert_all_mocked=False)
async def test_parse_with_source_url(
server: FakeLlamaCloudServer,
client: AsyncLlamaCloud,
) -> None:
job_create = await client.parsing.create(
tier="fast",
version="latest",
source_url="https://pdfobject.com/pdf/sample.pdf",
)
assert job_create.error_message is None
assert job_create.status == "COMPLETED"
assert job_create.project_id == server.default_project_id
job_response = await client.parsing.get(
job_id=job_create.id, expand=["text", "markdown", "items"]
)
assert job_response.job.id == job_create.id
assert job_response.job.status == job_create.status
assert job_response.job.project_id == job_create.project_id
assert job_response.items is not None
assert job_response.markdown is not None
assert job_response.text is not None
@pytest.mark.asyncio
async def test_parse_e2e(
server: FakeLlamaCloudServer, client: AsyncLlamaCloud, data: tuple[str, bytes, str]
) -> None:
file_name, _, _ = data
file_obj = await client.files.create(
file=file_name,
purpose="parse",
external_file_id=file_name,
)
result = await client.parsing.parse(
file_id=file_obj.id,
expand=["markdown"],
tier="agentic",
version="latest",
)
assert result.markdown is not None
assert len(result.markdown.pages) == 1
assert hasattr(result.markdown.pages[0], "markdown")
assert isinstance(result.markdown.pages[0].markdown, str) # type: ignore