main
 1from pathlib import Path
 2
 3import anyio
 4import pytest
 5from dirty_equals import IsDict, IsList, IsBytes, IsTuple
 6
 7from openai._files import to_httpx_files, async_to_httpx_files
 8
 9readme_path = Path(__file__).parent.parent.joinpath("README.md")
10
11
12def test_pathlib_includes_file_name() -> None:
13    result = to_httpx_files({"file": readme_path})
14    print(result)
15    assert result == IsDict({"file": IsTuple("README.md", IsBytes())})
16
17
18def test_tuple_input() -> None:
19    result = to_httpx_files([("file", readme_path)])
20    print(result)
21    assert result == IsList(IsTuple("file", IsTuple("README.md", IsBytes())))
22
23
24@pytest.mark.asyncio
25async def test_async_pathlib_includes_file_name() -> None:
26    result = await async_to_httpx_files({"file": readme_path})
27    print(result)
28    assert result == IsDict({"file": IsTuple("README.md", IsBytes())})
29
30
31@pytest.mark.asyncio
32async def test_async_supports_anyio_path() -> None:
33    result = await async_to_httpx_files({"file": anyio.Path(readme_path)})
34    print(result)
35    assert result == IsDict({"file": IsTuple("README.md", IsBytes())})
36
37
38@pytest.mark.asyncio
39async def test_async_tuple_input() -> None:
40    result = await async_to_httpx_files([("file", readme_path)])
41    print(result)
42    assert result == IsList(IsTuple("file", IsTuple("README.md", IsBytes())))
43
44
45def test_string_not_allowed() -> None:
46    with pytest.raises(TypeError, match="Expected file types input to be a FileContent type or to be a tuple"):
47        to_httpx_files(
48            {
49                "file": "foo",  # type: ignore
50            }
51        )