Skip to content

Commit 1b28c0f

Browse files
authored
Add test cases for more file input types (#152)
Also makes lmstudio.LocalFileData available for type hinting purposes. Follow to #148 to add #46 test cases
1 parent d12e757 commit 1b28c0f

File tree

3 files changed

+51
-53
lines changed

3 files changed

+51
-53
lines changed

src/lmstudio/history.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"FileHandleDict",
7979
"FileHandleInput",
8080
"FileType",
81+
"LocalFileInput",
8182
"SystemPrompt",
8283
"SystemPromptContent",
8384
"ToolCallRequest",

tests/async/test_images_async.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from io import BytesIO
99

10-
from lmstudio import AsyncClient, Chat, FileHandle, LMStudioServerError
10+
from lmstudio import AsyncClient, Chat, FileHandle, LMStudioServerError, LocalFileInput
1111

1212
from ..support import (
1313
EXPECTED_VLM_ID,
@@ -17,59 +17,57 @@
1717
check_sdk_error,
1818
)
1919

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+
2031

2132
@pytest.mark.asyncio
2233
@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:
2440
caplog.set_level(logging.DEBUG)
2541
async with AsyncClient() as client:
2642
session = client.files
27-
file = await session._prepare_file(IMAGE_FILEPATH)
43+
file = await session._prepare_file(file_input)
2844
assert file
2945
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)
3248
assert image
3349
assert isinstance(image, FileHandle)
34-
logging.info(f"Uploaded image: {image}")
50+
logging.info(f"Uploaded image from {input_kind}: {image}")
3551
# Even with the same data uploaded, assigned identifiers should differ
3652
assert image != file
3753

3854

3955
@pytest.mark.asyncio
4056
@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:
4258
caplog.set_level(logging.DEBUG)
4359
async with AsyncClient() as client:
4460
session = client.files
4561
with open(IMAGE_FILEPATH, "rb") as f:
4662
file = await session._prepare_file(f)
4763
assert file
4864
assert isinstance(file, FileHandle)
49-
logging.info(f"Uploaded file: {file}")
65+
logging.info(f"Uploaded file from file object: {file}")
5066
with open(IMAGE_FILEPATH, "rb") as f:
5167
image = await session.prepare_image(f)
5268
assert image
5369
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}")
7371
# Even with the same data uploaded, assigned identifiers should differ
7472
assert image != file
7573

tests/sync/test_images_sync.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from io import BytesIO
1616

17-
from lmstudio import Client, Chat, FileHandle, LMStudioServerError
17+
from lmstudio import Client, Chat, FileHandle, LMStudioServerError, LocalFileInput
1818

1919
from ..support import (
2020
EXPECTED_VLM_ID,
@@ -24,56 +24,55 @@
2424
check_sdk_error,
2525
)
2626

27+
_IMAGE_DATA = IMAGE_FILEPATH.read_bytes()
28+
29+
_FILE_INPUT_CASES: list[tuple[str, LocalFileInput]] = [
30+
("filesystem path", IMAGE_FILEPATH),
31+
("bytes IO stream", BytesIO(_IMAGE_DATA)),
32+
("raw bytes", _IMAGE_DATA),
33+
("mutable buffer", bytearray(_IMAGE_DATA)),
34+
("memoryview", memoryview(_IMAGE_DATA)),
35+
]
36+
_FILE_INPUT_CASE_IDS = [case[0] for case in _FILE_INPUT_CASES]
37+
2738

2839
@pytest.mark.lmstudio
29-
def test_upload_from_pathlike_sync(caplog: LogCap) -> None:
40+
@pytest.mark.parametrize(
41+
"input_kind,file_input", _FILE_INPUT_CASES, ids=_FILE_INPUT_CASE_IDS
42+
)
43+
def test_prepare_sync(
44+
caplog: LogCap, input_kind: str, file_input: LocalFileInput
45+
) -> None:
3046
caplog.set_level(logging.DEBUG)
3147
with Client() as client:
3248
session = client.files
33-
file = session._prepare_file(IMAGE_FILEPATH)
49+
file = session._prepare_file(file_input)
3450
assert file
3551
assert isinstance(file, FileHandle)
36-
logging.info(f"Uploaded file: {file}")
37-
image = session.prepare_image(IMAGE_FILEPATH)
52+
logging.info(f"Uploaded file from {input_kind}: {file}")
53+
image = session.prepare_image(file_input)
3854
assert image
3955
assert isinstance(image, FileHandle)
40-
logging.info(f"Uploaded image: {image}")
56+
logging.info(f"Uploaded image from {input_kind}: {image}")
4157
# Even with the same data uploaded, assigned identifiers should differ
4258
assert image != file
4359

4460

4561
@pytest.mark.lmstudio
46-
def test_upload_from_file_obj_sync(caplog: LogCap) -> None:
62+
def test_prepare_from_file_obj_sync(caplog: LogCap) -> None:
4763
caplog.set_level(logging.DEBUG)
4864
with Client() as client:
4965
session = client.files
5066
with open(IMAGE_FILEPATH, "rb") as f:
5167
file = session._prepare_file(f)
5268
assert file
5369
assert isinstance(file, FileHandle)
54-
logging.info(f"Uploaded file: {file}")
70+
logging.info(f"Uploaded file from file object: {file}")
5571
with open(IMAGE_FILEPATH, "rb") as f:
5672
image = session.prepare_image(f)
5773
assert image
5874
assert isinstance(image, FileHandle)
59-
logging.info(f"Uploaded image: {image}")
60-
# Even with the same data uploaded, assigned identifiers should differ
61-
assert image != file
62-
63-
64-
@pytest.mark.lmstudio
65-
def test_upload_from_bytesio_sync(caplog: LogCap) -> None:
66-
caplog.set_level(logging.DEBUG)
67-
with Client() as client:
68-
session = client.files
69-
file = session._prepare_file(BytesIO(IMAGE_FILEPATH.read_bytes()))
70-
assert file
71-
assert isinstance(file, FileHandle)
72-
logging.info(f"Uploaded file: {file}")
73-
image = session.prepare_image(BytesIO(IMAGE_FILEPATH.read_bytes()))
74-
assert image
75-
assert isinstance(image, FileHandle)
76-
logging.info(f"Uploaded image: {image}")
75+
logging.info(f"Uploaded image from file object: {image}")
7776
# Even with the same data uploaded, assigned identifiers should differ
7877
assert image != file
7978

0 commit comments

Comments
 (0)