Skip to content

Implemented the test suite structure and essential API tests #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,22 @@ pip install -e .
pip install -e .[dev]
pip install -e .[test]

```

# Run all tests
> Currently running on Python3.10

```bash
cd backend

python3.10 -m pytest -v
```

# Run individual test scripts
## Example: API functional tests

```bash
cd backend/tests/functional

pytest functional_api_test.py
```
44 changes: 30 additions & 14 deletions backend/app/view/appView.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,24 @@ def session():
content = request.json.get('content')
role_name = request.json.get('role_name')

success = self.history_manager.add_session_message(
MessageModel(role=RoleEnum.get_by_name(role_name), content=content),
self.current_session)
# Added a check to make sure both content and role_name are provided
success = False
if content and role_name:
success = self.history_manager.add_session_message(
MessageModel(role=RoleEnum.get_by_name(role_name), content=content),
self.current_session)

return jsonify({'status': 'success' if success else 'failure', 'message': 'Entry updated'})
return jsonify({'status': 'success' if success else 'failure',
'message': 'Entry updated' if success else 'Entry not updated'}), 200 if success else 400

elif request.method == 'DELETE':
message_id = request.json.get('message_id')
success = self.history_manager.delete_session_message(self.current_session, message_id)
return jsonify({'status': 'success' if success else 'failure', 'message': 'Entry deleted'})
# Added a check to make sure message_id is provided
success = False
if message_id:
success = self.history_manager.delete_session_message(self.current_session, message_id)
return jsonify({'status': 'success' if success else 'failure',
'message': 'Entry deleted' if success else 'Entry not deleted'}), 200 if success else 400

@self.app.route('/history', methods=['GET', 'PUT', 'DELETE'])
def history():
Expand All @@ -109,12 +117,17 @@ def history():
return jsonify(self.current_session.id)
elif request.method == 'DELETE':
session_id = request.json.get('session_id')
self.history_manager.delete_session(session_id)
# Added a check to make sure session_id is provided
success = False
if session_id:
success = self.history_manager.delete_session(session_id)
if session_id == self.current_session.id:
self.current_session = self.history_manager.create_session()
self.config_manager.config.runtime.current_session_id = self.current_session.id
return jsonify(self.current_session.id)

return jsonify({'status': 'success' if success else 'failure',
'message': 'Session deleted' if success else 'Session not deleted',
'current_session_id': self.current_session.id}), 200 if success else 400

@self.app.route('/config/<key>', methods=['GET', 'POST'])
def config(key):
if request.method == 'GET':
Expand All @@ -124,13 +137,16 @@ def config(key):
return jsonify({key: value})

elif request.method == 'POST':
success = False
value = request.json.get('value')
# How to make sure the config key exists before updating it? - Y.S.
if value:
# If session ID is updated, update the current session
if key == "runtime.current_session_id":
self.current_session = self.history_manager.get_session(value)
set_nested_attribute(self.config_manager.config, key, value)
self.config_manager.save()
return jsonify({key: value, 'status': 'updated'})
if key == "runtime.current_session_id":
self.current_session = self.history_manager.get_session(value)
set_nested_attribute(self.config_manager.config, key, value)
success = self.config_manager.save()
return jsonify({key: value, 'status': 'updated' if success else 'update failed'}), 200 if success else 400

@self.app.route('/bots', methods=['GET'])
def bots_list():
Expand Down
1 change: 1 addition & 0 deletions backend/tests/functional/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pass
115 changes: 115 additions & 0 deletions backend/tests/functional/functional_api_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# THis test file ensures the general functionality of the application by testing the API endpoints.

import os

import pytest

from app.dao.configDataManager import ConfigManager
from app.view.appView import AppView


@pytest.fixture
def client():
# Get the absolute path to the config file
config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../storage/config/main_config.yaml'))
# config_path = '../../storage/config/main_config.yaml'
config_manager = ConfigManager(config_path)
flask_app = AppView(config_manager).app
with flask_app.test_client() as client:
yield client


def test_sanity_check(client):
assert True


# It takes me forever to generate an OpenAI API key for no apparent reason
# Should work fine currently not passing without setting the key in main_config.yaml
def test_ask_post(client):
response = client.post('/ask', json={'content': 'test'})
assert response.status_code == 200
assert type(response.json['response']) == str and len(response.json['response']) > 0


def test_session_get(client):
response = client.get('/session')
assert response.status_code == 200
assert response.json['id'] is not None


def test_session_put(client):
response = client.put('/session', json={'content': 'test', 'role_name': 'user'})
assert response.status_code == 200
assert response.json['status'] == 'success'


def test_session_put_failure(client):
response = client.put('/session', json={'content': 'test'})
assert response.status_code == 400
assert response.json['status'] == 'failure'


def test_session_delete(client):
curr_session_id = client.get('/session').json['id']
response = client.delete('/session', json={'message_id': curr_session_id})
assert response.status_code == 200
assert response.json['status'] == 'success'


def test_session_delete_failure(client):
response = client.delete('/session', json={})
assert response.status_code == 400
assert response.json['status'] == 'failure'


def test_history_get(client):
response = client.get('/history')
assert response.status_code == 200
assert isinstance(response.json, list) and len(response.json) > 0


def test_history_put(client):
response = client.put('/history')
curr_session_id = response.json
session_list = client.get('/history').json
assert response.status_code == 200
assert curr_session_id in session_list


def test_history_delete(client):
session_list = client.get('/history').json
response = client.delete('/history', json={'session_id': session_list[0]})
assert response.status_code == 200
assert response.json['status'] == 'success'


def test_history_delete_failure(client):
response = client.delete('/history', json={})
assert response.status_code == 400
assert response.json['status'] == 'failure'


def test_config_get(client):
curr_session_id = client.get('/session').json['id']
response = client.get(f'/config/{curr_session_id}')
assert response.status_code == 200
assert response.json[curr_session_id] is not None


def test_config_get_failure(client):
response = client.get('/config/test')
assert response.status_code == 200
assert 'Attribute test not found in ConfigModel' in response.json['test']


def test_config_post(client):
curr_session_id = client.get('/session').json['id']
response = client.post(f'/config/{curr_session_id}', json={'value': 'test'})
assert response.status_code == 200
assert response.json['status'] == 'updated'


def test_bots_get(client):
response = client.get('/bots')
assert response.status_code == 200
assert isinstance(response.json, list) and len(response.json) > 0
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import shutil
import unittest

from app.dao import HistoryManager
from app.dao.historyDataManager import HistoryManager
from app.model.dataModel import MessageModel, RoleEnum


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import unittest

from app.control.bot import OpenAIBot # Adjust the import according to your project structure
from app.dao import ConfigManager, HistoryManager
from app.dao.configDataManager import ConfigManager
from app.dao.historyDataManager import HistoryManager
from app.model.dataModel import MessageModel, RoleEnum

config_manager = ConfigManager("../config.yaml")
Expand Down
129 changes: 0 additions & 129 deletions backend/tests/test.py

This file was deleted.

1 change: 1 addition & 0 deletions backend/tests/unit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pass
Loading