From c90d7da02d4749b66850c38f601e526ff64d38eb Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Fri, 20 Sep 2024 17:28:16 -0700 Subject: [PATCH] add initial testing --- backend/danswer/configs/app_configs.py | 2 +- .../danswer/tools/custom/test_custom_tools.py | 84 +++++++++++++++++++ .../docker_compose/docker-compose.dev.yml | 2 +- .../docker_compose/docker-compose.gpu-dev.yml | 2 +- .../docker-compose.search-testing.yml | 2 +- 5 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 backend/tests/unit/danswer/tools/custom/test_custom_tools.py diff --git a/backend/danswer/configs/app_configs.py b/backend/danswer/configs/app_configs.py index 2dbe596b6..fe262c1f6 100644 --- a/backend/danswer/configs/app_configs.py +++ b/backend/danswer/configs/app_configs.py @@ -135,7 +135,7 @@ POSTGRES_PASSWORD = urllib.parse.quote_plus( os.environ.get("POSTGRES_PASSWORD") or "password" ) POSTGRES_HOST = os.environ.get("POSTGRES_HOST") or "localhost" -POSTGRES_PORT = os.environ.get("POSTGRES_PORT") or "5432" +POSTGRES_PORT = os.environ.get("POSTGRES_PORT") or "5433" POSTGRES_DB = os.environ.get("POSTGRES_DB") or "postgres" # defaults to False diff --git a/backend/tests/unit/danswer/tools/custom/test_custom_tools.py b/backend/tests/unit/danswer/tools/custom/test_custom_tools.py new file mode 100644 index 000000000..d651764a3 --- /dev/null +++ b/backend/tests/unit/danswer/tools/custom/test_custom_tools.py @@ -0,0 +1,84 @@ +from unittest.mock import MagicMock +from unittest.mock import patch + +import pytest + +from danswer.tools.custom.custom_tool import build_custom_tools_from_openapi_schema +from danswer.tools.custom.custom_tool import CustomTool +from danswer.tools.custom.openapi_parsing import MethodSpec +from danswer.tools.models import DynamicSchemaInfo + + +def test_custom_tool_run(): + # Mock MethodSpec + method_spec = MagicMock(MethodSpec) + method_spec.method = "POST" + method_spec.build_url.return_value = "http://test-url.com/api/endpoint" + method_spec.operation_id = "testOperation" # Add this line + method_spec.name = "test" + method_spec.summary = "test" + + # Create CustomTool instance + custom_tool = CustomTool(method_spec, "http://test-url.com") + + # Mock the requests.request function + with patch("requests.request") as mock_request: + # Set up the mock response + mock_response = MagicMock() + mock_response.json.return_value = {"result": "success"} + mock_request.return_value = mock_response + + # Run the tool + result = list(custom_tool.run(request_body={"key": "value"})) + + # Assert that the request was made with correct parameters + mock_request.assert_called_once_with( + "POST", "http://test-url.com/api/endpoint", json={"key": "value"} + ) + + # Check the result + assert len(result) == 1 + assert result[0].response.tool_result == {"result": "success"} + + +def test_build_custom_tools_with_dynamic_schema(): + openapi_schema = { + "openapi": "3.0.0", + "info": {"title": "Test API", "version": "1.0.0"}, + "servers": [{"url": "http://test-url.com/CHAT_SESSION_ID"}], + "paths": { + "/endpoint/CHAT_SESSION_ID": { + "post": { + "summary": "Create a new Assistant", + "operationId": "testEndpoint", + "parameters": [], + } + } + }, + } + + dynamic_schema_info = DynamicSchemaInfo(chat_session_id=123, message_id=456) + + tools = build_custom_tools_from_openapi_schema(openapi_schema, dynamic_schema_info) + print("tools") + print(tools) + + # assert len(tools) == 1 + # assert isinstance(tools[0], CustomTool) + + # # Test that the dynamic schema info was applied + # with patch('requests.request') as mock_request: + # mock_response = MagicMock() + # mock_response.json.return_value = {"result": "success"} + # mock_request.return_value = mock_response + + # list(tools[0].run()) + + # mock_request.assert_called_once() + # print(mock_request.call_args) + # # call_args = mock_request.call_args[0] + # assert 123 in call_args[1] # URL should contain the session ID + + +if __name__ == "__main__": + pytest.main([__file__]) diff --git a/deployment/docker_compose/docker-compose.dev.yml b/deployment/docker_compose/docker-compose.dev.yml index 5893fc296..0b58c5f5b 100644 --- a/deployment/docker_compose/docker-compose.dev.yml +++ b/deployment/docker_compose/docker-compose.dev.yml @@ -293,7 +293,7 @@ services: - POSTGRES_USER=${POSTGRES_USER:-postgres} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password} ports: - - "5432:5432" + - "5433:5432" volumes: - db_volume:/var/lib/postgresql/data diff --git a/deployment/docker_compose/docker-compose.gpu-dev.yml b/deployment/docker_compose/docker-compose.gpu-dev.yml index bc8bf10df..9063a23bb 100644 --- a/deployment/docker_compose/docker-compose.gpu-dev.yml +++ b/deployment/docker_compose/docker-compose.gpu-dev.yml @@ -303,7 +303,7 @@ services: - POSTGRES_USER=${POSTGRES_USER:-postgres} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password} ports: - - "5432:5432" + - "5433:5432" volumes: - db_volume:/var/lib/postgresql/data diff --git a/deployment/docker_compose/docker-compose.search-testing.yml b/deployment/docker_compose/docker-compose.search-testing.yml index a64b30f09..36b093fba 100644 --- a/deployment/docker_compose/docker-compose.search-testing.yml +++ b/deployment/docker_compose/docker-compose.search-testing.yml @@ -154,7 +154,7 @@ services: - POSTGRES_USER=${POSTGRES_USER:-postgres} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password} ports: - - "5432" + - "5433" volumes: - db_volume:/var/lib/postgresql/data