openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/test_files.py
51lines · modecode
| 1 | from pathlib import Path |
| 2 | |
| 3 | import anyio |
| 4 | import pytest |
| 5 | from dirty_equals import IsDict, IsList, IsBytes, IsTuple |
| 6 | |
| 7 | from openai._files import to_httpx_files, async_to_httpx_files |
| 8 | |
| 9 | readme_path = Path(__file__).parent.parent.joinpath("README.md") |
| 10 | |
| 11 | |
| 12 | def 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 | |
| 18 | def 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 |
| 25 | async 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 |
| 32 | async 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 |
| 39 | async 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 | |
| 45 | def 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 | ) |
| 52 | |