-
Notifications
You must be signed in to change notification settings - Fork 55
Use proper Request object to test REST API endpoints handlers #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tisnik
merged 2 commits into
lightspeed-core:main
from
tisnik:use-proper-request-object-to-test-endpoints
Aug 10, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,16 @@ | ||
| """Unit tests for the / endpoint handler.""" | ||
|
|
||
| from fastapi import Request | ||
|
|
||
| from app.endpoints.root import root_endpoint_handler | ||
|
|
||
|
|
||
| def test_root_endpoint(): | ||
| """Test the root endpoint handler.""" | ||
| request = None | ||
| request = Request( | ||
| scope={ | ||
| "type": "http", | ||
| } | ||
| ) | ||
| response = root_endpoint_handler(request) | ||
| assert response is not None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
|
|
||
| import pytest | ||
|
|
||
| from fastapi import HTTPException, status | ||
| from fastapi import HTTPException, Request, status | ||
| from fastapi.responses import StreamingResponse | ||
|
|
||
| from llama_stack_client import APIConnectionError | ||
|
|
@@ -118,9 +118,14 @@ async def test_streaming_query_endpoint_handler_configuration_not_loaded(mocker) | |
| query = "What is OpenStack?" | ||
| query_request = QueryRequest(query=query) | ||
|
|
||
| request = Request( | ||
| scope={ | ||
| "type": "http", | ||
| } | ||
| ) | ||
| # await the async function | ||
| with pytest.raises(HTTPException) as e: | ||
| await streaming_query_endpoint_handler(None, query_request, auth=MOCK_AUTH) | ||
| await streaming_query_endpoint_handler(request, query_request, auth=MOCK_AUTH) | ||
| assert e.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR | ||
| assert e.detail["response"] == "Configuration is not loaded" | ||
|
|
||
|
|
@@ -145,9 +150,14 @@ async def test_streaming_query_endpoint_on_connection_error(mocker): | |
| mock_async_lsc = mocker.patch("client.AsyncLlamaStackClientHolder.get_client") | ||
| mock_async_lsc.return_value = mock_client | ||
|
|
||
| request = Request( | ||
| scope={ | ||
| "type": "http", | ||
| } | ||
| ) | ||
| # await the async function | ||
| with pytest.raises(HTTPException) as e: | ||
| await streaming_query_endpoint_handler(None, query_request, auth=MOCK_AUTH) | ||
| await streaming_query_endpoint_handler(request, query_request, auth=MOCK_AUTH) | ||
| assert e.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR | ||
| assert e.detail["response"] == "Configuration is not loaded" | ||
|
|
||
|
Comment on lines
159
to
163
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simulate connection error at the correct call and fix assertions and expected message Two issues:
# raise the error when listing models
mock_client.models.list.side_effect = APIConnectionError(request=query_request)
with pytest.raises(HTTPException) as e:
await streaming_query_endpoint_handler(request, query_request, auth=MOCK_AUTH)
assert e.value.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
assert e.value.detail["response"] == "Unable to connect to Llama Stack"Also, you can remove one of the duplicate patches: # Keep just one
mocker.patch("client.AsyncLlamaStackClientHolder.get_client", return_value=mock_client)🤖 Prompt for AI Agents |
||
|
|
@@ -256,9 +266,14 @@ async def _test_streaming_query_endpoint_handler(mocker, store_transcript=False) | |
|
|
||
| query_request = QueryRequest(query=query) | ||
|
|
||
| request = Request( | ||
| scope={ | ||
| "type": "http", | ||
| } | ||
| ) | ||
| # Await the async function | ||
| response = await streaming_query_endpoint_handler( | ||
| None, query_request, auth=MOCK_AUTH | ||
| request, query_request, auth=MOCK_AUTH | ||
| ) | ||
|
|
||
| # assert the response is a StreamingResponse | ||
|
|
@@ -1259,8 +1274,13 @@ async def test_auth_tuple_unpacking_in_streaming_query_endpoint_handler(mocker): | |
| "app.endpoints.streaming_query.is_transcripts_enabled", return_value=False | ||
| ) | ||
|
|
||
| request = Request( | ||
| scope={ | ||
| "type": "http", | ||
| } | ||
| ) | ||
| await streaming_query_endpoint_handler( | ||
| None, | ||
| request, | ||
| QueryRequest(query="test query"), | ||
| auth=("user123", "username", "auth_token_123"), | ||
| mcp_headers=None, | ||
|
|
@@ -1301,8 +1321,13 @@ async def test_streaming_query_endpoint_handler_no_tools_true(mocker): | |
|
|
||
| query_request = QueryRequest(query="What is OpenStack?", no_tools=True) | ||
|
|
||
| request = Request( | ||
| scope={ | ||
| "type": "http", | ||
| } | ||
| ) | ||
| response = await streaming_query_endpoint_handler( | ||
| None, query_request, auth=MOCK_AUTH | ||
| request, query_request, auth=MOCK_AUTH | ||
| ) | ||
|
|
||
| # Assert the response is a StreamingResponse | ||
|
|
@@ -1341,8 +1366,13 @@ async def test_streaming_query_endpoint_handler_no_tools_false(mocker): | |
|
|
||
| query_request = QueryRequest(query="What is OpenStack?", no_tools=False) | ||
|
|
||
| request = Request( | ||
| scope={ | ||
| "type": "http", | ||
| } | ||
| ) | ||
| response = await streaming_query_endpoint_handler( | ||
| None, query_request, auth=MOCK_AUTH | ||
| request, query_request, auth=MOCK_AUTH | ||
| ) | ||
|
|
||
| # Assert the response is a StreamingResponse | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix pytest.raises assertions (must assert outside the context and use e.value)
Asserting on e.status_code/e.detail inside the with-block and without using e.value is incorrect and can raise AttributeError. Move assertions outside and use e.value.
🤖 Prompt for AI Agents