From b8acb860aa697c15eeef2082902a30edc9ff9361 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 27 Sep 2023 17:54:57 +0100 Subject: [PATCH] some tests --- .gitignore | 3 ++- application/app.py | 12 ---------- tests/llm/test_openai.py | 34 +++++++++++++++++++++++++++++ tests/test_app.py | 47 ++++++++++++++++------------------------ tests/test_celery.py | 18 +++++++++++++++ tests/test_error.py | 41 +++++++++++++++++++++++++++++++++++ 6 files changed, 114 insertions(+), 41 deletions(-) create mode 100644 tests/llm/test_openai.py create mode 100644 tests/test_celery.py create mode 100644 tests/test_error.py diff --git a/.gitignore b/.gitignore index a1592a40..a896c299 100644 --- a/.gitignore +++ b/.gitignore @@ -169,4 +169,5 @@ application/vectors/ **/yarn.lock -node_modules/ \ No newline at end of file +node_modules/ +.vscode/settings.json diff --git a/application/app.py b/application/app.py index 5941ed6f..41b821b7 100644 --- a/application/app.py +++ b/application/app.py @@ -34,19 +34,10 @@ app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER = "inputs" app.config["CELERY_BROKER_URL"] = settings.CELERY_BROKER_URL app.config["CELERY_RESULT_BACKEND"] = settings.CELERY_RESULT_BACKEND app.config["MONGO_URI"] = settings.MONGO_URI -#celery = Celery() celery.config_from_object("application.celeryconfig") - - -# @celery.task(bind=True) -# def ingest(self, directory, formats, name_job, filename, user): -# resp = ingest_worker(self, directory, formats, name_job, filename, user) -# return resp - - @app.route("/") def home(): """ @@ -63,9 +54,6 @@ def home(): - - - # handling CORS @app.after_request def after_request(response): diff --git a/tests/llm/test_openai.py b/tests/llm/test_openai.py new file mode 100644 index 00000000..5baca037 --- /dev/null +++ b/tests/llm/test_openai.py @@ -0,0 +1,34 @@ +# FILEPATH: /Users/alextu/Documents/GitHub/DocsGPT/tests/llm/test_openai.py + +import unittest +from unittest.mock import patch, Mock +from application.llm.openai import OpenAILLM, AzureOpenAILLM + +class TestOpenAILLM(unittest.TestCase): + + def setUp(self): + self.api_key = "test_api_key" + self.llm = OpenAILLM(self.api_key) + + def test_init(self): + self.assertEqual(self.llm.api_key, self.api_key) + + @patch('application.llm.openai.openai.ChatCompletion.create') + def test_gen(self, mock_create): + model = "test_model" + engine = "test_engine" + messages = ["test_message"] + response = {"choices": [{"message": {"content": "test_response"}}]} + mock_create.return_value = response + result = self.llm.gen(model, engine, messages) + self.assertEqual(result, "test_response") + + @patch('application.llm.openai.openai.ChatCompletion.create') + def test_gen_stream(self, mock_create): + model = "test_model" + engine = "test_engine" + messages = ["test_message"] + response = [{"choices": [{"delta": {"content": "test_response"}}]}] + mock_create.return_value = response + result = list(self.llm.gen_stream(model, engine, messages)) + self.assertEqual(result, ["test_response"]) diff --git a/tests/test_app.py b/tests/test_app.py index a86234ae..96e88ea1 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,32 +1,23 @@ -from application.app import get_vectorstore, is_azure_configured -import os +from flask import Flask + +from application.api.answer.routes import answer +from application.api.internal.routes import internal +from application.api.user.routes import user +from application.core.settings import settings -# Test cases for get_vectorstore function -def test_no_active_docs(): - data = {} - assert get_vectorstore(data) == os.path.join("application", "") +def test_app_config(): + app = Flask(__name__) + app.register_blueprint(user) + app.register_blueprint(answer) + app.register_blueprint(internal) + app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER = "inputs" + app.config["CELERY_BROKER_URL"] = settings.CELERY_BROKER_URL + app.config["CELERY_RESULT_BACKEND"] = settings.CELERY_RESULT_BACKEND + app.config["MONGO_URI"] = settings.MONGO_URI -def test_local_default_active_docs(): - data = {"active_docs": "local/default"} - assert get_vectorstore(data) == os.path.join("application", "") - - -def test_local_non_default_active_docs(): - data = {"active_docs": "local/something"} - assert get_vectorstore(data) == os.path.join("application", "indexes/local/something") - - -def test_default_active_docs(): - data = {"active_docs": "default"} - assert get_vectorstore(data) == os.path.join("application", "") - - -def test_complex_active_docs(): - data = {"active_docs": "local/other/path"} - assert get_vectorstore(data) == os.path.join("application", "indexes/local/other/path") - - -def test_is_azure_configured(): - assert not is_azure_configured() + assert app.config["UPLOAD_FOLDER"] == "inputs" + assert app.config["CELERY_BROKER_URL"] == settings.CELERY_BROKER_URL + assert app.config["CELERY_RESULT_BACKEND"] == settings.CELERY_RESULT_BACKEND + assert app.config["MONGO_URI"] == settings.MONGO_URI diff --git a/tests/test_celery.py b/tests/test_celery.py new file mode 100644 index 00000000..d4b86b61 --- /dev/null +++ b/tests/test_celery.py @@ -0,0 +1,18 @@ +from celery import Celery +from unittest.mock import patch, MagicMock +from application.core.settings import settings +from application.celery import make_celery + + +@patch('application.celery.Celery') +def test_make_celery(mock_celery): + # Arrange + app_name = 'test_app_name' + + # Act + celery = make_celery(app_name) + + # Assert + mock_celery.assert_called_once_with(app_name, broker=settings.CELERY_BROKER_URL) + celery.conf.update.assert_called_once_with(settings) + assert celery == mock_celery.return_value \ No newline at end of file diff --git a/tests/test_error.py b/tests/test_error.py new file mode 100644 index 00000000..696a0fb3 --- /dev/null +++ b/tests/test_error.py @@ -0,0 +1,41 @@ +# FILEPATH: /Users/alextu/Documents/GitHub/DocsGPT/tests/test_error.py + +import pytest +from flask import Flask +from application.error import bad_request, response_error + + +@pytest.fixture +def app(): + app = Flask(__name__) + return app + + +def test_bad_request_with_message(app): + with app.app_context(): + message = "Invalid input" + response = bad_request(status_code=400, message=message) + assert response.status_code == 400 + assert response.json == {'error': 'Bad Request', 'message': message} + + +def test_bad_request_without_message(app): + with app.app_context(): + response = bad_request(status_code=400) + assert response.status_code == 400 + assert response.json == {'error': 'Bad Request'} + + +def test_response_error_with_message(app): + with app.app_context(): + message = "Something went wrong" + response = response_error(code_status=500, message=message) + assert response.status_code == 500 + assert response.json == {'error': 'Internal Server Error', 'message': message} + + +def test_response_error_without_message(app): + with app.app_context(): + response = response_error(code_status=500) + assert response.status_code == 500 + assert response.json == {'error': 'Internal Server Error'} \ No newline at end of file