-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from unittest.mock import AsyncMock, patch | ||
|
||
import pytest | ||
from sanic import Sanic | ||
from sanic_testing import TestManager | ||
|
||
sanitized_name = __name__.replace(".", "_") | ||
|
||
|
||
@pytest.fixture | ||
def app(): | ||
"""Fixture to create a new Sanic application for testing the POST request handler.""" | ||
app_instance = Sanic(sanitized_name) | ||
TestManager(app_instance) | ||
from ask_astro.rest.controllers import register_routes | ||
|
||
register_routes(app_instance) | ||
return app_instance | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize( | ||
"request_payload,expected_status,expected_response", | ||
[ | ||
({"prompt": "Tell me about space"}, 200, {"request_uuid": "test-uuid"}), | ||
({"prompt": "What is quantum mechanics?"}, 200, {"request_uuid": "test-uuid"}), | ||
({}, 400, {"error": "prompt is required"}), | ||
], | ||
) | ||
async def test_on_post_request(app, request_payload, expected_status, expected_response): | ||
"""Test the POST request endpoint behavior based on different input payloads.""" | ||
with patch("ask_astro.services.questions.answer_question") as mock_answer_question, patch( | ||
"ask_astro.clients.firestore.firestore.AsyncClient" | ||
) as mock_firestore: | ||
with patch("google.cloud.firestore_v1.Client", new=AsyncMock()): | ||
mock_firestore.collection.return_value.document.return_value.get.return_value = AsyncMock() | ||
mock_answer_question.return_value = AsyncMock() | ||
|
||
request, response = await app.asgi_client.post("/requests", json=request_payload) | ||
|
||
assert response.status == expected_status | ||
# If expecting a 200 status | ||
if expected_status == 200: | ||
assert "request_uuid" in response.json | ||
assert isinstance(response.json.get("request_uuid"), str) | ||
|
||
# If expecting a 400 status | ||
elif expected_status == 400: | ||
assert "error" in response.json | ||
assert response.json["error"] == expected_response["error"] |