|
14 | 14 | import pytest_asyncio |
15 | 15 |
|
16 | 16 | from pdfrest import AsyncPdfRestClient, PdfRestClient |
17 | | -from pdfrest.models import PdfRestFile |
| 17 | +from pdfrest.models import PdfRestFile, PdfRestFileID |
18 | 18 |
|
19 | 19 | from .resources import get_test_resource_path |
20 | 20 |
|
@@ -145,6 +145,45 @@ def live_async_file( |
145 | 145 | ) |
146 | 146 |
|
147 | 147 |
|
| 148 | +@pytest.mark.parametrize( |
| 149 | + "file_ref", |
| 150 | + [ |
| 151 | + pytest.param(PdfRestFileID.generate(), id="pdfrest-file-id"), |
| 152 | + pytest.param(str(uuid.uuid4()), id="raw-str"), |
| 153 | + ], |
| 154 | +) |
| 155 | +def test_files_get_fetches_info(file_ref: PdfRestFileID | str) -> None: |
| 156 | + file_id = str(file_ref) |
| 157 | + info_payload = _build_file_info_payload(file_id, "report.pdf") |
| 158 | + |
| 159 | + def handler(request: httpx.Request) -> httpx.Response: |
| 160 | + if request.method == "GET" and request.url.path == f"/resource/{file_id}": |
| 161 | + assert request.url.params["format"] == "info" |
| 162 | + return httpx.Response(200, json=info_payload) |
| 163 | + msg = f"Unexpected request: {request.method} {request.url}" |
| 164 | + raise AssertionError(msg) |
| 165 | + |
| 166 | + transport = httpx.MockTransport(handler) |
| 167 | + with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client: |
| 168 | + file_repr = client.files.get(file_ref) |
| 169 | + |
| 170 | + _assert_file_matches_payload(file_repr, info_payload) |
| 171 | + |
| 172 | + |
| 173 | +def test_files_get_rejects_invalid_id() -> None: |
| 174 | + transport = httpx.MockTransport( |
| 175 | + lambda request: (_ for _ in ()).throw( |
| 176 | + AssertionError("Request should not be sent for invalid IDs.") |
| 177 | + ) |
| 178 | + ) |
| 179 | + |
| 180 | + with ( |
| 181 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 182 | + pytest.raises(ValueError, match="Invalid PdfRestPrefixedUUID4"), |
| 183 | + ): |
| 184 | + client.files.get("not-a-valid-id") |
| 185 | + |
| 186 | + |
148 | 187 | def test_files_create_uses_upload_and_info() -> None: |
149 | 188 | uploaded_file_id = str(uuid.uuid4()) |
150 | 189 | info_payload = _build_file_info_payload(uploaded_file_id, "report.pdf") |
@@ -698,6 +737,45 @@ def handler(request: httpx.Request) -> httpx.Response: |
698 | 737 | _assert_file_matches_payload(response[0], info_payload) |
699 | 738 |
|
700 | 739 |
|
| 740 | +@pytest.mark.asyncio |
| 741 | +@pytest.mark.parametrize( |
| 742 | + "file_ref", |
| 743 | + [ |
| 744 | + pytest.param(PdfRestFileID.generate(), id="pdfrest-file-id"), |
| 745 | + pytest.param(str(uuid.uuid4()), id="raw-str"), |
| 746 | + ], |
| 747 | +) |
| 748 | +async def test_async_files_get_fetches_info(file_ref: PdfRestFileID | str) -> None: |
| 749 | + file_id = str(file_ref) |
| 750 | + info_payload = _build_file_info_payload(file_id, "report.pdf") |
| 751 | + |
| 752 | + def handler(request: httpx.Request) -> httpx.Response: |
| 753 | + if request.method == "GET" and request.url.path == f"/resource/{file_id}": |
| 754 | + assert request.url.params["format"] == "info" |
| 755 | + return httpx.Response(200, json=info_payload) |
| 756 | + msg = f"Unexpected request: {request.method} {request.url}" |
| 757 | + raise AssertionError(msg) |
| 758 | + |
| 759 | + transport = httpx.MockTransport(handler) |
| 760 | + async with AsyncPdfRestClient(api_key=VALID_API_KEY, transport=transport) as client: |
| 761 | + file_repr = await client.files.get(file_ref) |
| 762 | + |
| 763 | + _assert_file_matches_payload(file_repr, info_payload) |
| 764 | + |
| 765 | + |
| 766 | +@pytest.mark.asyncio |
| 767 | +async def test_async_files_get_rejects_invalid_id() -> None: |
| 768 | + transport = httpx.MockTransport( |
| 769 | + lambda request: (_ for _ in ()).throw( |
| 770 | + AssertionError("Request should not be sent for invalid IDs.") |
| 771 | + ) |
| 772 | + ) |
| 773 | + |
| 774 | + async with AsyncPdfRestClient(api_key=VALID_API_KEY, transport=transport) as client: |
| 775 | + with pytest.raises(ValueError, match="Invalid PdfRestPrefixedUUID4"): |
| 776 | + await client.files.get("not-a-valid-id") |
| 777 | + |
| 778 | + |
701 | 779 | @pytest.mark.asyncio |
702 | 780 | async def test_async_files_create_from_paths() -> None: |
703 | 781 | uploaded_ids = [str(uuid.uuid4()), str(uuid.uuid4())] |
|
0 commit comments