|
| 1 | +# THis test file ensures the general functionality of the application by testing the API endpoints. |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from app.dao.configDataManager import ConfigManager |
| 8 | +from app.view.appView import AppView |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def client(): |
| 13 | + # Get the absolute path to the config file |
| 14 | + config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../storage/config/main_config.yaml')) |
| 15 | + # config_path = '../../storage/config/main_config.yaml' |
| 16 | + config_manager = ConfigManager(config_path) |
| 17 | + flask_app = AppView(config_manager).app |
| 18 | + with flask_app.test_client() as client: |
| 19 | + yield client |
| 20 | + |
| 21 | + |
| 22 | +def test_sanity_check(client): |
| 23 | + assert True |
| 24 | + |
| 25 | + |
| 26 | +# It takes me forever to generate an OpenAI API key for no apparent reason |
| 27 | +# Should work fine currently not passing without setting the key in main_config.yaml |
| 28 | +def test_ask_post(client): |
| 29 | + response = client.post('/ask', json={'content': 'test'}) |
| 30 | + assert response.status_code == 200 |
| 31 | + assert type(response.json['response']) == str and len(response.json['response']) > 0 |
| 32 | + |
| 33 | + |
| 34 | +def test_session_get(client): |
| 35 | + response = client.get('/session') |
| 36 | + assert response.status_code == 200 |
| 37 | + assert response.json['id'] is not None |
| 38 | + |
| 39 | + |
| 40 | +def test_session_put(client): |
| 41 | + response = client.put('/session', json={'content': 'test', 'role_name': 'user'}) |
| 42 | + assert response.status_code == 200 |
| 43 | + assert response.json['status'] == 'success' |
| 44 | + |
| 45 | + |
| 46 | +def test_session_put_failure(client): |
| 47 | + response = client.put('/session', json={'content': 'test'}) |
| 48 | + assert response.status_code == 400 |
| 49 | + assert response.json['status'] == 'failure' |
| 50 | + |
| 51 | + |
| 52 | +def test_session_delete(client): |
| 53 | + curr_session_id = client.get('/session').json['id'] |
| 54 | + response = client.delete('/session', json={'message_id': curr_session_id}) |
| 55 | + assert response.status_code == 200 |
| 56 | + assert response.json['status'] == 'success' |
| 57 | + |
| 58 | + |
| 59 | +def test_session_delete_failure(client): |
| 60 | + response = client.delete('/session', json={}) |
| 61 | + assert response.status_code == 400 |
| 62 | + assert response.json['status'] == 'failure' |
| 63 | + |
| 64 | + |
| 65 | +def test_history_get(client): |
| 66 | + response = client.get('/history') |
| 67 | + assert response.status_code == 200 |
| 68 | + assert isinstance(response.json, list) and len(response.json) > 0 |
| 69 | + |
| 70 | + |
| 71 | +def test_history_put(client): |
| 72 | + response = client.put('/history') |
| 73 | + curr_session_id = response.json |
| 74 | + session_list = client.get('/history').json |
| 75 | + assert response.status_code == 200 |
| 76 | + assert curr_session_id in session_list |
| 77 | + |
| 78 | + |
| 79 | +def test_history_delete(client): |
| 80 | + session_list = client.get('/history').json |
| 81 | + response = client.delete('/history', json={'session_id': session_list[0]}) |
| 82 | + assert response.status_code == 200 |
| 83 | + assert response.json['status'] == 'success' |
| 84 | + |
| 85 | + |
| 86 | +def test_history_delete_failure(client): |
| 87 | + response = client.delete('/history', json={}) |
| 88 | + assert response.status_code == 400 |
| 89 | + assert response.json['status'] == 'failure' |
| 90 | + |
| 91 | + |
| 92 | +def test_config_get(client): |
| 93 | + curr_session_id = client.get('/session').json['id'] |
| 94 | + response = client.get(f'/config/{curr_session_id}') |
| 95 | + assert response.status_code == 200 |
| 96 | + assert response.json[curr_session_id] is not None |
| 97 | + |
| 98 | + |
| 99 | +def test_config_get_failure(client): |
| 100 | + response = client.get('/config/test') |
| 101 | + assert response.status_code == 200 |
| 102 | + assert 'Attribute test not found in ConfigModel' in response.json['test'] |
| 103 | + |
| 104 | + |
| 105 | +def test_config_post(client): |
| 106 | + curr_session_id = client.get('/session').json['id'] |
| 107 | + response = client.post(f'/config/{curr_session_id}', json={'value': 'test'}) |
| 108 | + assert response.status_code == 200 |
| 109 | + assert response.json['status'] == 'updated' |
| 110 | + |
| 111 | + |
| 112 | +def test_bots_get(client): |
| 113 | + response = client.get('/bots') |
| 114 | + assert response.status_code == 200 |
| 115 | + assert isinstance(response.json, list) and len(response.json) > 0 |
0 commit comments