Skip to content

Commit

Permalink
Add test for get request
Browse files Browse the repository at this point in the history
  • Loading branch information
sunank200 committed Nov 2, 2023
1 parent fa1704d commit 340028e
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 6 deletions.
3 changes: 2 additions & 1 deletion api/ask_astro/rest/controllers/post_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from ask_astro.clients.firestore import firestore_client
from ask_astro.config import FirestoreCollections
from ask_astro.models.request import AskAstroRequest
from ask_astro.services.questions import answer_question

logger = getLogger(__name__)

Expand Down Expand Up @@ -46,6 +45,8 @@ async def on_post_request(request: Request) -> json:
:param request: The Sanic request object.
"""
try:
from ask_astro.services.questions import answer_question

if "prompt" not in request.json:
return json({"error": "prompt is required"}, status=400)

Expand Down
152 changes: 147 additions & 5 deletions api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ uvicorn = "^0.23.2"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.2"
pytest-asyncio = "^0.21.1"
pytest-sanic = "^1.0.0"
sanic-testing = "^23.5.0"

[build-system]
requires = ["poetry-core"]
Expand Down
Empty file.
Empty file.
57 changes: 57 additions & 0 deletions tests/api/ask_astro/rest/controllers/test_get_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os
from unittest.mock import AsyncMock, Mock, patch
from uuid import uuid4

import pytest
from sanic import Sanic
from sanic_testing import TestManager

sanitized_name = __name__.replace(".", "_")


@pytest.fixture
def app():
app_instance = Sanic(sanitized_name)
os.environ["WEAVIATE_URL"] = "http://localhost:8080"
os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY"
TestManager(app_instance)

from ask_astro.rest.controllers.get_request import on_get_request

app_instance.add_route(on_get_request, "/requests/<request_id:uuid>")

return app_instance


@pytest.mark.asyncio
async def test_on_get_request_error(app):
with patch(
"ask_astro.rest.controllers.get_request.firestore_client", new_callable=AsyncMock
) as mock_firestore, patch(
"ask_astro.rest.controllers.on_post_request", new_callable=AsyncMock
) as mock_on_post_request, patch(
"google.cloud.firestore.AsyncClient"
) as mock_async_client:
mock_on_post_request.async_return_value = {}
mock_firestore.async_return_value = {}
mock_async_client.async_return_value = {}

request_id = uuid4()
mock_data = {"title": "Sample Question"}

mock_get = AsyncMock()
mock_get.exists = True
mock_get.to_dict.return_value = mock_data

mock_document = Mock()
mock_document.get.return_value = mock_get

mock_collection = Mock()
mock_collection.document.return_value = mock_document

mock_firestore.collection.return_value = mock_collection

request, response = await app.asgi_client.get(f"/requests/{request_id}")

assert response.status == 500
assert response.json == {"error": "Internal Server Error"}

0 comments on commit 340028e

Please sign in to comment.