LCORE-381: new unit tests for client.py and models endpoints#287
Conversation
WalkthroughNew unit tests were added to verify error handling and initialization logic in the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
tests/unit/app/endpoints/test_models.py (1)
162-210: Comprehensive test for API connection error handling.The new test effectively covers the scenario where LlamaStack client raises an
APIConnectionErrorand verifies that the endpoint handler properly converts it to an HTTP 500 exception. The test setup is thorough and the assertions are correct.However, there are a few minor improvements that could be made:
- The test configuration setup is duplicated across multiple tests - consider extracting it to a fixture
- The assertion statements after
pytest.raises(lines 209-210) will never execute due to the context manager behaviorwith pytest.raises(HTTPException) as e: models_endpoint_handler(request) - assert e.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR - assert e.detail["response"] == "Unable to connect to Llama Stack" + + assert e.value.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR + assert e.value.detail["response"] == "Unable to connect to Llama Stack"
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
tests/unit/app/endpoints/test_models.py(3 hunks)tests/unit/test_client.py(5 hunks)
🧰 Additional context used
🪛 GitHub Actions: Black
tests/unit/app/endpoints/test_models.py
[error] 1-1: Black formatting check failed. File would be reformatted. Run 'black --write' to fix code style issues.
🪛 GitHub Actions: Python linter
tests/unit/app/endpoints/test_models.py
[error] 10-10: pylint: third party import "llama_stack_client.APIConnectionError" should be placed before first party imports (wrong-import-order)
tests/unit/test_client.py
[error] 27-27: pylint: Line too long (103/100) (line-too-long)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build-pr
🔇 Additional comments (3)
tests/unit/app/endpoints/test_models.py (1)
145-145: Good consistency improvement.Using
mocker.Mock()instead ofMock()improves consistency with the pytest-mock pattern used throughout the test suite.tests/unit/test_client.py (2)
9-18: Excellent test for uninitialized client error handling.This test properly verifies that calling
get_client()on an uninitializedLlamaStackClientHolderraises aRuntimeErrorwith the expected message. The regex pattern correctly escapes the parentheses in the error message.
44-45: Good additions to verify client retrieval after initialization.These additions properly test that
get_client()returns a valid client instance after successful initialization. This complements the new tests for uninitialized clients and provides comprehensive coverage of theget_client()method behavior.Also applies to: 60-61, 93-94, 109-110
| def test_async_client_get_client_method() -> None: | ||
| """Test how get_client method works for unitialized client.""" | ||
| client = AsyncLlamaStackClientHolder() | ||
|
|
||
| with pytest.raises( | ||
| RuntimeError, | ||
| match="AsyncLlamaStackClient has not been initialised. Ensure 'load\\(..\\)' has been called.", | ||
| ): | ||
| client.get_client() |
There was a problem hiding this comment.
Fix line length and improve test documentation.
The test logic is correct, but there's a line length violation and a minor documentation issue.
def test_async_client_get_client_method() -> None:
- """Test how get_client method works for unitialized client."""
+ """Test how get_client method works for uninitialized async client."""
client = AsyncLlamaStackClientHolder()
with pytest.raises(
RuntimeError,
- match="AsyncLlamaStackClient has not been initialised. Ensure 'load\\(..\\)' has been called.",
+ match="AsyncLlamaStackClient has not been initialised. "
+ "Ensure 'load\\(..\\)' has been called.",
):
client.get_client()📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| def test_async_client_get_client_method() -> None: | |
| """Test how get_client method works for unitialized client.""" | |
| client = AsyncLlamaStackClientHolder() | |
| with pytest.raises( | |
| RuntimeError, | |
| match="AsyncLlamaStackClient has not been initialised. Ensure 'load\\(..\\)' has been called.", | |
| ): | |
| client.get_client() | |
| def test_async_client_get_client_method() -> None: | |
| """Test how get_client method works for uninitialized async client.""" | |
| client = AsyncLlamaStackClientHolder() | |
| with pytest.raises( | |
| RuntimeError, | |
| match="AsyncLlamaStackClient has not been initialised. " | |
| "Ensure 'load\\(..\\)' has been called.", | |
| ): | |
| client.get_client() |
🧰 Tools
🪛 GitHub Actions: Python linter
[error] 27-27: pylint: Line too long (103/100) (line-too-long)
🤖 Prompt for AI Agents
In tests/unit/test_client.py around lines 21 to 29, the test function's
docstring is brief and the line with the pytest.raises call exceeds the
recommended line length. Expand the docstring to better describe the test
purpose and split the long line into multiple shorter lines to comply with line
length standards.
baacb1d to
6494660
Compare
Description
LCORE-381: new unit tests for client.py and models endpoints
Type of change
Related Tickets & Documents
Summary by CodeRabbit
/modelsendpoint cannot connect to Llama Stack.