Skip to content

Commit aa2454f

Browse files
authored
Merge pull request #239 from tisnik/lcore-303-fixed-pylint-issues-
LCORE-303: fixed pylint issues
2 parents cabe14e + d368e88 commit aa2454f

15 files changed

Lines changed: 166 additions & 67 deletions

tests/unit/app/endpoints/test_authorized.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Unit tests for the /authorized REST API endpoint."""
2+
13
from unittest.mock import AsyncMock
24

35
import pytest

tests/unit/app/endpoints/test_config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Unit tests for the /config REST API endpoint."""
2+
13
import pytest
24

35
from fastapi import HTTPException, status
@@ -20,7 +22,7 @@ def test_config_endpoint_handler_configuration_not_loaded(mocker):
2022
assert e.detail["response"] == "Configuration is not loaded"
2123

2224

23-
def test_config_endpoint_handler_configuration_loaded(mocker):
25+
def test_config_endpoint_handler_configuration_loaded():
2426
"""Test the config endpoint handler."""
2527
config_dict = {
2628
"name": "foo",

tests/unit/app/endpoints/test_feedback.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Unit tests for the /feedback REST API endpoint."""
2+
13
from fastapi import HTTPException, status
24
import pytest
35

@@ -123,7 +125,7 @@ def test_store_feedback(mocker):
123125
)
124126

125127

126-
def test_feedback_status(mocker):
128+
def test_feedback_status():
127129
"""Test that feedback_status returns the correct status response."""
128130
configuration.user_data_collection_configuration.feedback_disabled = False
129131

tests/unit/app/endpoints/test_health.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
"""Unit tests for the /health REST API endpoint."""
2+
13
from unittest.mock import Mock
24

5+
from llama_stack.providers.datatypes import HealthStatus
6+
37
from app.endpoints.health import (
48
readiness_probe_get_method,
59
liveness_probe_get_method,
610
get_providers_health_statuses,
711
)
812
from models.responses import ProviderHealthStatus, ReadinessResponse
9-
from llama_stack.providers.datatypes import HealthStatus
1013

1114

1215
def test_readiness_probe_fails_due_to_unhealthy_providers(mocker):

tests/unit/app/endpoints/test_info.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
"""Unit tests for the /info REST API endpoint."""
2+
13
from app.endpoints.info import info_endpoint_handler
24
from configuration import AppConfig
35

46

5-
def test_info_endpoint(mocker):
7+
def test_info_endpoint():
68
"""Test the info endpoint handler."""
79
config_dict = {
810
"name": "foo",

tests/unit/app/endpoints/test_models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import pytest
1+
"""Unit tests for the /models REST API endpoint."""
22

33
from unittest.mock import Mock
4+
5+
import pytest
6+
47
from fastapi import HTTPException, status
58

69
from app.endpoints.models import models_endpoint_handler
@@ -62,7 +65,7 @@ def test_models_endpoint_handler_improper_llama_stack_configuration(mocker):
6265
assert e.detail["response"] == "LLama stack is not configured"
6366

6467

65-
def test_models_endpoint_handler_configuration_loaded(mocker):
68+
def test_models_endpoint_handler_configuration_loaded():
6669
"""Test the models endpoint handler if configuration is loaded."""
6770
# configuration for tests
6871
config_dict = {

tests/unit/app/endpoints/test_query.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
"""Unit tests for the /query REST API endpoint."""
2+
3+
# pylint: disable=too-many-lines
4+
15
import json
26
from fastapi import HTTPException, status
37
import pytest
48

9+
from llama_stack_client import APIConnectionError
10+
from llama_stack_client.types import UserMessage # type: ignore
11+
512
from configuration import AppConfig
613
from app.endpoints.query import (
714
query_endpoint_handler,
@@ -15,16 +22,15 @@
1522
get_agent,
1623
_agent_cache,
1724
)
18-
from llama_stack_client import APIConnectionError
25+
1926
from models.requests import QueryRequest, Attachment
2027
from models.config import ModelContextProtocolServer
21-
from llama_stack_client.types import UserMessage # type: ignore
2228

2329
MOCK_AUTH = ("mock_user_id", "mock_username", "mock_token")
2430

2531

26-
@pytest.fixture(autouse=True)
27-
def setup_configuration():
32+
@pytest.fixture(name="setup_configuration")
33+
def setup_configuration_fixture():
2834
"""Set up configuration for tests."""
2935
config_dict = {
3036
"name": "test",
@@ -52,12 +58,13 @@ def setup_configuration():
5258
return cfg
5359

5460

55-
@pytest.fixture(autouse=True)
56-
def prepare_agent_mocks(mocker):
61+
@pytest.fixture(autouse=True, name="prepare_agent_mocks")
62+
def prepare_agent_mocks_fixture(mocker):
63+
"""Fixture that yields mock agent when called."""
5764
mock_client = mocker.Mock()
5865
mock_agent = mocker.Mock()
59-
"""Cleanup agent cache after tests."""
6066
yield mock_client, mock_agent
67+
# cleanup agent cache after tests
6168
_agent_cache.clear()
6269

6370

@@ -98,7 +105,7 @@ def test_is_transcripts_disabled(setup_configuration, mocker):
98105
assert is_transcripts_enabled() is False, "Transcripts should be disabled"
99106

100107

101-
def _test_query_endpoint_handler(mocker, store_transcript=False):
108+
def _test_query_endpoint_handler(mocker, store_transcript_to_file=False):
102109
"""Test the query endpoint handler."""
103110
mock_client = mocker.Mock()
104111
mock_lsc = mocker.patch("client.LlamaStackClientHolder.get_client")
@@ -110,7 +117,7 @@ def _test_query_endpoint_handler(mocker, store_transcript=False):
110117

111118
mock_config = mocker.Mock()
112119
mock_config.user_data_collection_configuration.transcripts_disabled = (
113-
not store_transcript
120+
not store_transcript_to_file
114121
)
115122
mocker.patch("app.endpoints.query.configuration", mock_config)
116123

@@ -124,7 +131,8 @@ def _test_query_endpoint_handler(mocker, store_transcript=False):
124131
)
125132
mocker.patch("app.endpoints.query.select_model_id", return_value="fake_model_id")
126133
mocker.patch(
127-
"app.endpoints.query.is_transcripts_enabled", return_value=store_transcript
134+
"app.endpoints.query.is_transcripts_enabled",
135+
return_value=store_transcript_to_file,
128136
)
129137
mock_transcript = mocker.patch("app.endpoints.query.store_transcript")
130138

@@ -137,7 +145,7 @@ def _test_query_endpoint_handler(mocker, store_transcript=False):
137145
assert response.conversation_id == conversation_id
138146

139147
# Assert the store_transcript function is called if transcripts are enabled
140-
if store_transcript:
148+
if store_transcript_to_file:
141149
mock_transcript.assert_called_once_with(
142150
user_id="user_id_placeholder",
143151
conversation_id=conversation_id,
@@ -155,12 +163,12 @@ def _test_query_endpoint_handler(mocker, store_transcript=False):
155163

156164
def test_query_endpoint_handler_transcript_storage_disabled(mocker):
157165
"""Test the query endpoint handler with transcript storage disabled."""
158-
_test_query_endpoint_handler(mocker, store_transcript=False)
166+
_test_query_endpoint_handler(mocker, store_transcript_to_file=False)
159167

160168

161169
def test_query_endpoint_handler_store_transcript(mocker):
162170
"""Test the query endpoint handler with transcript storage enabled."""
163-
_test_query_endpoint_handler(mocker, store_transcript=True)
171+
_test_query_endpoint_handler(mocker, store_transcript_to_file=True)
164172

165173

166174
def test_select_model_id(mocker):
@@ -368,9 +376,17 @@ def test_retrieve_response_one_available_shield(prepare_agent_mocks, mocker):
368376
"""Test the retrieve_response function."""
369377

370378
class MockShield:
379+
"""Mock for Llama Stack shield to be used."""
380+
371381
def __init__(self, identifier):
372382
self.identifier = identifier
373383

384+
def __str__(self):
385+
return "MockShield"
386+
387+
def __repr__(self):
388+
return "MockShield"
389+
374390
mock_client, mock_agent = prepare_agent_mocks
375391
mock_agent.create_turn.return_value.output_message.content = "LLM answer"
376392
mock_client.shields.list.return_value = [MockShield("shield1")]
@@ -407,9 +423,17 @@ def test_retrieve_response_two_available_shields(prepare_agent_mocks, mocker):
407423
"""Test the retrieve_response function."""
408424

409425
class MockShield:
426+
"""Mock for Llama Stack shield to be used."""
427+
410428
def __init__(self, identifier):
411429
self.identifier = identifier
412430

431+
def __str__(self):
432+
return "MockShield"
433+
434+
def __repr__(self):
435+
return "MockShield"
436+
413437
mock_client, mock_agent = prepare_agent_mocks
414438
mock_agent.create_turn.return_value.output_message.content = "LLM answer"
415439
mock_client.shields.list.return_value = [
@@ -832,7 +856,7 @@ def test_store_transcript(mocker):
832856
)
833857

834858

835-
def test_get_rag_toolgroups(mocker):
859+
def test_get_rag_toolgroups():
836860
"""Test get_rag_toolgroups function."""
837861
vector_db_ids = []
838862
result = get_rag_toolgroups(vector_db_ids)
@@ -864,7 +888,7 @@ def test_query_endpoint_handler_on_connection_error(mocker):
864888
query_endpoint_handler(query_request)
865889

866890

867-
def test_get_agent_cache_hit(prepare_agent_mocks, mocker):
891+
def test_get_agent_cache_hit(prepare_agent_mocks):
868892
"""Test get_agent function when agent exists in cache."""
869893
mock_client, mock_agent = prepare_agent_mocks
870894

tests/unit/app/endpoints/test_root.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
"""Unit tests for the / endpoint handler."""
2+
13
from app.endpoints.root import root_endpoint_handler
24

35

4-
def test_root_endpoint(mocker):
6+
def test_root_endpoint():
57
"""Test the root endpoint handler."""
68
request = None
79
response = root_endpoint_handler(request)

tests/unit/app/endpoints/test_streaming_query.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
import pytest
1+
"""Unit tests for the /streaming-query REST API endpoint."""
2+
3+
# pylint: disable=too-many-lines
4+
25
import json
36

7+
import pytest
8+
49
from fastapi import HTTPException, status
10+
from fastapi.responses import StreamingResponse
11+
12+
from llama_stack_client import APIConnectionError
13+
from llama_stack_client.types import UserMessage # type: ignore
514
from llama_stack_client.types.shared.interleaved_content_item import TextContentItem
615

716
from configuration import AppConfig
@@ -13,10 +22,8 @@
1322
get_agent,
1423
_agent_cache,
1524
)
16-
from llama_stack_client import APIConnectionError
1725
from models.requests import QueryRequest, Attachment
1826
from models.config import ModelContextProtocolServer
19-
from llama_stack_client.types import UserMessage # type: ignore
2027

2128
MOCK_AUTH = ("mock_user_id", "mock_username", "mock_token")
2229

@@ -47,8 +54,8 @@
4754
]
4855

4956

50-
@pytest.fixture(autouse=True)
51-
def setup_configuration():
57+
@pytest.fixture(autouse=True, name="setup_configuration")
58+
def setup_configuration_fixture():
5259
"""Set up configuration for tests."""
5360
config_dict = {
5461
"name": "test",
@@ -75,12 +82,13 @@ def setup_configuration():
7582
return cfg
7683

7784

78-
@pytest.fixture(autouse=True)
79-
def prepare_agent_mocks(mocker):
85+
@pytest.fixture(autouse=True, name="prepare_agent_mocks")
86+
def prepare_agent_mocks_fixture(mocker):
87+
"""Preparation for mock for the LLM agent."""
8088
mock_client = mocker.AsyncMock()
8189
mock_agent = mocker.AsyncMock()
82-
"""Cleanup agent cache after tests."""
8390
yield mock_client, mock_agent
91+
# cleanup agent cache after tests
8492
_agent_cache.clear()
8593

8694

@@ -205,9 +213,7 @@ async def _test_streaming_query_endpoint_handler(mocker, store_transcript=False)
205213
None, query_request, auth=MOCK_AUTH
206214
)
207215

208-
# Assert the response is a StreamingResponse
209-
from fastapi.responses import StreamingResponse
210-
216+
# assert the response is a StreamingResponse
211217
assert isinstance(response, StreamingResponse)
212218

213219
# Collect the streaming response content
@@ -288,7 +294,8 @@ async def test_retrieve_response_vector_db_available(prepare_agent_mocks, mocker
288294
mock_client, model_id, query_request, token
289295
)
290296

291-
# For streaming, the response should be the streaming object and conversation_id should be returned
297+
# For streaming, the response should be the streaming object and
298+
# conversation_id should be returned
292299
assert response is not None
293300
assert conversation_id == "test_conversation_id"
294301
mock_agent.create_turn.assert_called_once_with(
@@ -324,7 +331,8 @@ async def test_retrieve_response_no_available_shields(prepare_agent_mocks, mocke
324331
mock_client, model_id, query_request, token
325332
)
326333

327-
# For streaming, the response should be the streaming object and conversation_id should be returned
334+
# For streaming, the response should be the streaming object and
335+
# conversation_id should be returned
328336
assert response is not None
329337
assert conversation_id == "test_conversation_id"
330338
mock_agent.create_turn.assert_called_once_with(
@@ -340,11 +348,16 @@ async def test_retrieve_response_one_available_shield(prepare_agent_mocks, mocke
340348
"""Test the retrieve_response function."""
341349

342350
class MockShield:
351+
"""Mock for Llama Stack shield to be used."""
352+
343353
def __init__(self, identifier):
344354
self.identifier = identifier
345355

346-
def identifier(self):
347-
return self.identifier
356+
def __str__(self):
357+
return "MockShield"
358+
359+
def __repr__(self):
360+
return "MockShield"
348361

349362
mock_client, mock_agent = prepare_agent_mocks
350363
mock_agent.create_turn.return_value.output_message.content = "LLM answer"
@@ -383,11 +396,16 @@ async def test_retrieve_response_two_available_shields(prepare_agent_mocks, mock
383396
"""Test the retrieve_response function."""
384397

385398
class MockShield:
399+
"""Mock for Llama Stack shield to be used."""
400+
386401
def __init__(self, identifier):
387402
self.identifier = identifier
388403

389-
def identifier(self):
390-
return self.identifier
404+
def __str__(self):
405+
return "MockShield"
406+
407+
def __repr__(self):
408+
return "MockShield"
391409

392410
mock_client, mock_agent = prepare_agent_mocks
393411
mock_agent.create_turn.return_value.output_message.content = "LLM answer"

tests/unit/app/test_routers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ def include_router(self, router: Any, prefix: Optional[str] = None) -> None:
2929
self.routers.append((router, prefix))
3030

3131
def get_routers(self) -> list[Any]:
32+
"""Retrieve all routers defined in mocked REST API."""
3233
return [r[0] for r in self.routers]
3334

3435
def get_router_prefix(self, router: Any) -> Optional[str]:
36+
"""Retrieve router prefix configured for mocked REST API."""
3537
return list(filter(lambda r: r[0] == router, self.routers))[0][1]
3638

3739

0 commit comments

Comments
 (0)