|
7 | 7 |
|
8 | 8 | from io import BytesIO |
9 | 9 |
|
10 | | -from lmstudio import AsyncClient, Chat, FileHandle, LMStudioServerError |
| 10 | +from lmstudio import AsyncClient, Chat, FileHandle, LMStudioServerError, LocalFileInput |
11 | 11 |
|
12 | 12 | from ..support import ( |
13 | 13 | EXPECTED_VLM_ID, |
|
17 | 17 | check_sdk_error, |
18 | 18 | ) |
19 | 19 |
|
| 20 | +_IMAGE_DATA = IMAGE_FILEPATH.read_bytes() |
| 21 | + |
| 22 | +_FILE_INPUT_CASES: list[tuple[str, LocalFileInput]] = [ |
| 23 | + ("filesystem path", IMAGE_FILEPATH), |
| 24 | + ("bytes IO stream", BytesIO(_IMAGE_DATA)), |
| 25 | + ("raw bytes", _IMAGE_DATA), |
| 26 | + ("mutable buffer", bytearray(_IMAGE_DATA)), |
| 27 | + ("memoryview", memoryview(_IMAGE_DATA)), |
| 28 | +] |
| 29 | +_FILE_INPUT_CASE_IDS = [case[0] for case in _FILE_INPUT_CASES] |
| 30 | + |
20 | 31 |
|
21 | 32 | @pytest.mark.asyncio |
22 | 33 | @pytest.mark.lmstudio |
23 | | -async def test_upload_from_pathlike_async(caplog: LogCap) -> None: |
| 34 | +@pytest.mark.parametrize( |
| 35 | + "input_kind,file_input", _FILE_INPUT_CASES, ids=_FILE_INPUT_CASE_IDS |
| 36 | +) |
| 37 | +async def test_prepare_async( |
| 38 | + caplog: LogCap, input_kind: str, file_input: LocalFileInput |
| 39 | +) -> None: |
24 | 40 | caplog.set_level(logging.DEBUG) |
25 | 41 | async with AsyncClient() as client: |
26 | 42 | session = client.files |
27 | | - file = await session._prepare_file(IMAGE_FILEPATH) |
| 43 | + file = await session._prepare_file(file_input) |
28 | 44 | assert file |
29 | 45 | assert isinstance(file, FileHandle) |
30 | | - logging.info(f"Uploaded file: {file}") |
31 | | - image = await session.prepare_image(IMAGE_FILEPATH) |
| 46 | + logging.info(f"Uploaded file from {input_kind}: {file}") |
| 47 | + image = await session.prepare_image(file_input) |
32 | 48 | assert image |
33 | 49 | assert isinstance(image, FileHandle) |
34 | | - logging.info(f"Uploaded image: {image}") |
| 50 | + logging.info(f"Uploaded image from {input_kind}: {image}") |
35 | 51 | # Even with the same data uploaded, assigned identifiers should differ |
36 | 52 | assert image != file |
37 | 53 |
|
38 | 54 |
|
39 | 55 | @pytest.mark.asyncio |
40 | 56 | @pytest.mark.lmstudio |
41 | | -async def test_upload_from_file_obj_async(caplog: LogCap) -> None: |
| 57 | +async def test_prepare_from_file_obj_async(caplog: LogCap) -> None: |
42 | 58 | caplog.set_level(logging.DEBUG) |
43 | 59 | async with AsyncClient() as client: |
44 | 60 | session = client.files |
45 | 61 | with open(IMAGE_FILEPATH, "rb") as f: |
46 | 62 | file = await session._prepare_file(f) |
47 | 63 | assert file |
48 | 64 | assert isinstance(file, FileHandle) |
49 | | - logging.info(f"Uploaded file: {file}") |
| 65 | + logging.info(f"Uploaded file from file object: {file}") |
50 | 66 | with open(IMAGE_FILEPATH, "rb") as f: |
51 | 67 | image = await session.prepare_image(f) |
52 | 68 | assert image |
53 | 69 | assert isinstance(image, FileHandle) |
54 | | - logging.info(f"Uploaded image: {image}") |
55 | | - # Even with the same data uploaded, assigned identifiers should differ |
56 | | - assert image != file |
57 | | - |
58 | | - |
59 | | -@pytest.mark.asyncio |
60 | | -@pytest.mark.lmstudio |
61 | | -async def test_upload_from_bytesio_async(caplog: LogCap) -> None: |
62 | | - caplog.set_level(logging.DEBUG) |
63 | | - async with AsyncClient() as client: |
64 | | - session = client.files |
65 | | - file = await session._prepare_file(BytesIO(IMAGE_FILEPATH.read_bytes())) |
66 | | - assert file |
67 | | - assert isinstance(file, FileHandle) |
68 | | - logging.info(f"Uploaded file: {file}") |
69 | | - image = await session.prepare_image(BytesIO(IMAGE_FILEPATH.read_bytes())) |
70 | | - assert image |
71 | | - assert isinstance(image, FileHandle) |
72 | | - logging.info(f"Uploaded image: {image}") |
| 70 | + logging.info(f"Uploaded image from file object: {image}") |
73 | 71 | # Even with the same data uploaded, assigned identifiers should differ |
74 | 72 | assert image != file |
75 | 73 |
|
|
0 commit comments