some tests

This commit is contained in:
Alex
2023-09-27 17:54:57 +01:00
parent e6849b85d1
commit b8acb860aa
6 changed files with 114 additions and 41 deletions

3
.gitignore vendored
View File

@@ -169,4 +169,5 @@ application/vectors/
**/yarn.lock **/yarn.lock
node_modules/ node_modules/
.vscode/settings.json

View File

@@ -34,19 +34,10 @@ app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER = "inputs"
app.config["CELERY_BROKER_URL"] = settings.CELERY_BROKER_URL app.config["CELERY_BROKER_URL"] = settings.CELERY_BROKER_URL
app.config["CELERY_RESULT_BACKEND"] = settings.CELERY_RESULT_BACKEND app.config["CELERY_RESULT_BACKEND"] = settings.CELERY_RESULT_BACKEND
app.config["MONGO_URI"] = settings.MONGO_URI app.config["MONGO_URI"] = settings.MONGO_URI
#celery = Celery()
celery.config_from_object("application.celeryconfig") 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("/") @app.route("/")
def home(): def home():
""" """
@@ -63,9 +54,6 @@ def home():
# handling CORS # handling CORS
@app.after_request @app.after_request
def after_request(response): def after_request(response):

34
tests/llm/test_openai.py Normal file
View File

@@ -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"])

View File

@@ -1,32 +1,23 @@
from application.app import get_vectorstore, is_azure_configured from flask import Flask
import os
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(): assert app.config["UPLOAD_FOLDER"] == "inputs"
data = {"active_docs": "local/default"} assert app.config["CELERY_BROKER_URL"] == settings.CELERY_BROKER_URL
assert get_vectorstore(data) == os.path.join("application", "") assert app.config["CELERY_RESULT_BACKEND"] == settings.CELERY_RESULT_BACKEND
assert app.config["MONGO_URI"] == settings.MONGO_URI
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()

18
tests/test_celery.py Normal file
View File

@@ -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

41
tests/test_error.py Normal file
View File

@@ -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'}