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 51f1b28
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
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.
54 changes: 54 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,54 @@
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:
mock_on_post_request.async_return_value = {}
mock_firestore.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 51f1b28

Please sign in to comment.