Skip to content

Commit 013f200

Browse files
committed
Type hints for tests/unit/app/endpoints/test_models.py
1 parent 6e2c199 commit 013f200

1 file changed

Lines changed: 28 additions & 15 deletions

File tree

tests/unit/app/endpoints/test_models.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
"""Unit tests for the /models REST API endpoint."""
22

3+
from typing import Any
34
import pytest
45

56
from fastapi import HTTPException, Request, status
7+
from pytest_mock import MockerFixture
68

79
from llama_stack_client import APIConnectionError
810

11+
from authentication.interface import AuthTuple
912
from app.endpoints.models import models_endpoint_handler
1013
from configuration import AppConfig
1114
from tests.unit.utils.auth_helpers import mock_authorization_resolvers
1215

1316

1417
@pytest.mark.asyncio
15-
async def test_models_endpoint_handler_configuration_not_loaded(mocker):
18+
async def test_models_endpoint_handler_configuration_not_loaded(
19+
mocker: MockerFixture,
20+
) -> None:
1621
"""Test the models endpoint handler if configuration is not loaded."""
1722
mock_authorization_resolvers(mocker)
1823

@@ -29,7 +34,7 @@ async def test_models_endpoint_handler_configuration_not_loaded(mocker):
2934
"headers": [(b"authorization", b"Bearer invalid-token")],
3035
}
3136
)
32-
auth = ("user_id", "user_name", "token")
37+
auth: AuthTuple = ("user_id", "user_name", "token")
3338

3439
with pytest.raises(HTTPException) as e:
3540
await models_endpoint_handler(request=request, auth=auth)
@@ -38,12 +43,14 @@ async def test_models_endpoint_handler_configuration_not_loaded(mocker):
3843

3944

4045
@pytest.mark.asyncio
41-
async def test_models_endpoint_handler_improper_llama_stack_configuration(mocker):
46+
async def test_models_endpoint_handler_improper_llama_stack_configuration(
47+
mocker: MockerFixture,
48+
) -> None:
4249
"""Test the models endpoint handler if Llama Stack configuration is not proper."""
4350
mock_authorization_resolvers(mocker)
4451

4552
# configuration for tests
46-
config_dict = {
53+
config_dict: dict[str, Any] = {
4754
"name": "test",
4855
"service": {
4956
"host": "localhost",
@@ -80,20 +87,22 @@ async def test_models_endpoint_handler_improper_llama_stack_configuration(mocker
8087
"headers": [(b"authorization", b"Bearer invalid-token")],
8188
}
8289
)
83-
auth = ("test_user", "token", {})
90+
auth: AuthTuple = ("test_user", "token", {})
8491
with pytest.raises(HTTPException) as e:
8592
await models_endpoint_handler(request=request, auth=auth)
8693
assert e.value.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
8794
assert e.detail["response"] == "Llama stack is not configured"
8895

8996

9097
@pytest.mark.asyncio
91-
async def test_models_endpoint_handler_configuration_loaded(mocker):
98+
async def test_models_endpoint_handler_configuration_loaded(
99+
mocker: MockerFixture,
100+
) -> None:
92101
"""Test the models endpoint handler if configuration is loaded."""
93102
mock_authorization_resolvers(mocker)
94103

95104
# configuration for tests
96-
config_dict = {
105+
config_dict: dict[str, Any] = {
97106
"name": "foo",
98107
"service": {
99108
"host": "localhost",
@@ -124,7 +133,7 @@ async def test_models_endpoint_handler_configuration_loaded(mocker):
124133
"headers": [(b"authorization", b"Bearer invalid-token")],
125134
}
126135
)
127-
auth = ("test_user", "token", {})
136+
auth: AuthTuple = ("test_user", "token", {})
128137

129138
with pytest.raises(HTTPException) as e:
130139
await models_endpoint_handler(request=request, auth=auth)
@@ -133,12 +142,14 @@ async def test_models_endpoint_handler_configuration_loaded(mocker):
133142

134143

135144
@pytest.mark.asyncio
136-
async def test_models_endpoint_handler_unable_to_retrieve_models_list(mocker):
145+
async def test_models_endpoint_handler_unable_to_retrieve_models_list(
146+
mocker: MockerFixture,
147+
) -> None:
137148
"""Test the models endpoint handler if configuration is loaded."""
138149
mock_authorization_resolvers(mocker)
139150

140151
# configuration for tests
141-
config_dict = {
152+
config_dict: dict[str, Any] = {
142153
"name": "foo",
143154
"service": {
144155
"host": "localhost",
@@ -177,18 +188,20 @@ async def test_models_endpoint_handler_unable_to_retrieve_models_list(mocker):
177188
"headers": [(b"authorization", b"Bearer invalid-token")],
178189
}
179190
)
180-
auth = ("test_user", "token", {})
191+
auth: AuthTuple = ("test_user", "token", {})
181192
response = await models_endpoint_handler(request=request, auth=auth)
182193
assert response is not None
183194

184195

185196
@pytest.mark.asyncio
186-
async def test_models_endpoint_llama_stack_connection_error(mocker):
197+
async def test_models_endpoint_llama_stack_connection_error(
198+
mocker: MockerFixture,
199+
) -> None:
187200
"""Test the model endpoint when LlamaStack connection fails."""
188201
mock_authorization_resolvers(mocker)
189202

190203
# configuration for tests
191-
config_dict = {
204+
config_dict: dict[str, Any] = {
192205
"name": "foo",
193206
"service": {
194207
"host": "localhost",
@@ -214,7 +227,7 @@ async def test_models_endpoint_llama_stack_connection_error(mocker):
214227
# mock AsyncLlamaStackClientHolder to raise APIConnectionError
215228
# when models.list() method is called
216229
mock_client = mocker.AsyncMock()
217-
mock_client.models.list.side_effect = APIConnectionError(request=None)
230+
mock_client.models.list.side_effect = APIConnectionError(request=None) # type: ignore
218231
mock_client_holder = mocker.patch(
219232
"app.endpoints.models.AsyncLlamaStackClientHolder"
220233
)
@@ -229,7 +242,7 @@ async def test_models_endpoint_llama_stack_connection_error(mocker):
229242
"headers": [(b"authorization", b"Bearer invalid-token")],
230243
}
231244
)
232-
auth = ("test_user", "token", {})
245+
auth: AuthTuple = ("test_user", "token", {})
233246

234247
with pytest.raises(HTTPException) as e:
235248
await models_endpoint_handler(request=request, auth=auth)

0 commit comments

Comments
 (0)