|
| 1 | +import logging |
| 2 | +from unittest.mock import AsyncMock |
| 3 | + |
| 4 | +import httpx |
| 5 | +import openai |
| 6 | +import pytest |
| 7 | + |
| 8 | +from pingpong.ai import poll_vector_store_files |
| 9 | + |
| 10 | +pytestmark = pytest.mark.asyncio |
| 11 | + |
| 12 | + |
| 13 | +def _not_found_error(file_id: str, vector_store_id: str) -> openai.NotFoundError: |
| 14 | + request = httpx.Request( |
| 15 | + "GET", |
| 16 | + f"https://api.openai.com/v1/vector_stores/{vector_store_id}/files/{file_id}", |
| 17 | + ) |
| 18 | + response = httpx.Response(404, request=request) |
| 19 | + return openai.NotFoundError( |
| 20 | + f"No file found with id '{file_id}' in vector store '{vector_store_id}'.", |
| 21 | + response=response, |
| 22 | + body={"error": {"message": "not found"}}, |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +async def test_poll_vector_store_files_skips_missing_files(caplog): |
| 27 | + cli = AsyncMock() |
| 28 | + |
| 29 | + async def fake_poll(*, file_id: str, vector_store_id: str): |
| 30 | + if file_id == "file-missing": |
| 31 | + raise _not_found_error(file_id, vector_store_id) |
| 32 | + return None |
| 33 | + |
| 34 | + cli.vector_stores.files.poll = AsyncMock(side_effect=fake_poll) |
| 35 | + |
| 36 | + with caplog.at_level(logging.WARNING): |
| 37 | + await poll_vector_store_files( |
| 38 | + cli, vector_store_id="vs-test", file_ids=["file-ok", "file-missing"] |
| 39 | + ) |
| 40 | + |
| 41 | + assert cli.vector_stores.files.poll.await_count == 2 |
| 42 | + assert "file-missing" in caplog.text |
| 43 | + assert "vs-test" in caplog.text |
| 44 | + |
| 45 | + |
| 46 | +async def test_poll_vector_store_files_noop_for_empty_file_list(): |
| 47 | + cli = AsyncMock() |
| 48 | + cli.vector_stores.files.poll = AsyncMock() |
| 49 | + |
| 50 | + await poll_vector_store_files(cli, vector_store_id="vs-test", file_ids=[]) |
| 51 | + |
| 52 | + assert cli.vector_stores.files.poll.await_count == 0 |
0 commit comments