From ef5cb16d609cad70ea90cc0f1571edebc7f719c1 Mon Sep 17 00:00:00 2001 From: Ankit Chaurasia <8670962+sunank200@users.noreply.github.com> Date: Mon, 6 Nov 2023 13:55:57 +0545 Subject: [PATCH] Add test for on_submit_feedback --- .../rest/controllers/test_get_requests.py | 3 -- .../rest/controllers/test_submit_feedback.py | 47 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 tests/api/ask_astro/rest/controllers/test_submit_feedback.py diff --git a/tests/api/ask_astro/rest/controllers/test_get_requests.py b/tests/api/ask_astro/rest/controllers/test_get_requests.py index b185ff2f..c3ab009a 100644 --- a/tests/api/ask_astro/rest/controllers/test_get_requests.py +++ b/tests/api/ask_astro/rest/controllers/test_get_requests.py @@ -1,4 +1,3 @@ -import os from unittest.mock import AsyncMock, Mock, patch from uuid import uuid4 @@ -13,8 +12,6 @@ def app(): """Fixture that creates and configures a new Sanic application for testing.""" 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 diff --git a/tests/api/ask_astro/rest/controllers/test_submit_feedback.py b/tests/api/ask_astro/rest/controllers/test_submit_feedback.py new file mode 100644 index 00000000..2ec611d5 --- /dev/null +++ b/tests/api/ask_astro/rest/controllers/test_submit_feedback.py @@ -0,0 +1,47 @@ +import uuid +from unittest.mock import AsyncMock, patch + +import pytest +from httpx import Response +from pytest_sanic.utils import TestClient +from sanic import Sanic + + +def create_sanic_app(name: str) -> Sanic: + """Create a new instance of a Sanic application with the provided name.""" + Sanic.test_mode = True + return Sanic(name) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "request_data,expected_status,expected_response_text", + [ + ({"positive": True}, 200, None), + ({"positive": False}, 200, None), + ({}, 500, "An internal error occurred."), # Missing positive key + ], +) +async def test_on_submit_feedback(request_data, expected_status, expected_response_text): + """ + Test the behavior of the on_submit_feedback route. This test validates the responses of the feedback submission + route for various inputs. + """ + from ask_astro.rest.controllers.submit_feedback import on_submit_feedback + + app_name = f"test_sanic_app_{uuid.uuid4().hex}" + app = create_sanic_app(app_name) + app.add_route(on_submit_feedback, "/requests//feedback", methods=["POST"]) + + async def mock_post(*args, **kwargs): + """Mock the POST request by returning a dummy response.""" + return Response(status_code=expected_status, text=expected_response_text or "") + + with patch("pytest_sanic.utils.TestClient.post", new=mock_post): + test_manager = TestClient(app) + + with patch("ask_astro.rest.controllers.submit_feedback.submit_feedback", new_callable=AsyncMock): + response = await test_manager.post("/requests/test_request_id/feedback", json=request_data) + + assert response.status_code == expected_status + assert response.text == (expected_response_text or "")