Add unit tests for RequestId and Exception Handling Middlewares - #128
Add unit tests for RequestId and Exception Handling Middlewares #128namidanam wants to merge 1 commit into
Conversation
📝 WalkthroughSummary by CodeRabbit
WalkthroughA new test module has been introduced to validate the behavior of two FastAPI middlewares: 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 (1)
🔇 Additional comments (7)
✨ Finishing Touches
🧪 Generate unit tests
🪧 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 (
|
Docstrings generation was requested by @NiveditJain. * #128 (comment) The following files were modified: * `api-server/tests/test_middlewares.py`
|
Note Generated docstrings for this pull request at #129 |
NiveditJain
left a comment
There was a problem hiding this comment.
Let's add a CI/CD pipeline to run these test while commit and pull request events.
We should probably add these in the same PR.
| app.add_middleware(UnhandledExceptionsMiddleware) | ||
| app.add_middleware(RequestIdMiddleware) |
There was a problem hiding this comment.
I think it's a good idea we test these middlewares separately instead of testing them together.
| assert new_rid != "invalid-id" # Confirm the invalid ID was replaced | ||
| uuid.UUID(new_rid) # Confirm replacement is a valid UUID | ||
|
|
||
| def test_unhandled_exception_caught(client): |
There was a problem hiding this comment.
Move this to a different file, let's keep the test per logical unit separately
| assert resp.status_code == 500 # Middleware converts error to HTTP 500 response | ||
|
|
||
| body = resp.json() | ||
| assert "error" in body or "detail" in body # Error info present in JSON response body |
There was a problem hiding this comment.
We have a fixed error body, I think it is better to assert that the whole body in asset equal condition.
This PR adds pytest-based unit tests for the RequestIdMiddleware and UnhandledExceptionsMiddleware. The tests verify correct request ID generation, preservation, replacement of invalid IDs, and proper JSON error responses on unhandled exceptions