|
| 1 | +# (C) 2024 GoodData Corporation |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +import gooddata_api_client |
| 6 | +import pytest |
| 7 | +from gooddata_api_client.api import smart_functions_api |
| 8 | +from gooddata_api_client.model.chat_history_request import ChatHistoryRequest |
| 9 | +from gooddata_api_client.model.chat_history_result import ChatHistoryResult |
| 10 | +from gooddata_api_client.model.chat_request import ChatRequest |
| 11 | +from utils import load_json, normalize_metrics |
| 12 | + |
| 13 | +sys.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 14 | + |
| 15 | +from env import WORKSPACE_ID |
| 16 | + |
| 17 | +EXPECTED_OBJECTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "data_response") |
| 18 | +QUESTIONS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "fixtures") |
| 19 | + |
| 20 | +questions_list = load_json(os.path.join(QUESTIONS_DIR, "ai_questions.json")) |
| 21 | + |
| 22 | + |
| 23 | +class gooddataAiChatApp: |
| 24 | + def __init__(self, api_client, workspace_id): |
| 25 | + self.api_instance = smart_functions_api.SmartFunctionsApi(api_client) |
| 26 | + self.workspace_id = workspace_id |
| 27 | + |
| 28 | + async def ask_question(self, question: str): |
| 29 | + return self.api_instance.ai_chat(self.workspace_id, ChatRequest(question=question)) |
| 30 | + |
| 31 | + async def chat_history(self, interaction_id: int, feedback: str): |
| 32 | + return self.api_instance.ai_chat_history( |
| 33 | + self.workspace_id, ChatHistoryRequest(chat_history_interaction_id=interaction_id, user_feedback=feedback) |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def validate_response(actual_response, expected_response): |
| 38 | + actual_metrics = normalize_metrics( |
| 39 | + actual_response["created_visualizations"]["objects"][0]["metrics"], exclude_keys=["title"] |
| 40 | + ) |
| 41 | + expected_metrics = normalize_metrics(expected_response["metrics"], exclude_keys=["title"]) |
| 42 | + assert actual_metrics == expected_metrics, "Metrics do not match" |
| 43 | + assert ( |
| 44 | + actual_response["created_visualizations"]["objects"][0]["visualization_type"] |
| 45 | + == expected_response["visualizationType"] |
| 46 | + ), "Visualization type mismatch" |
| 47 | + assert ( |
| 48 | + actual_response["created_visualizations"]["objects"][0]["dimensionality"] == expected_response["dimensionality"] |
| 49 | + ), "Dimensionality mismatch" |
| 50 | + assert ( |
| 51 | + actual_response["created_visualizations"]["objects"][0]["filters"] == expected_response["filters"] |
| 52 | + ), "Filters mismatch" |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture(scope="module") |
| 56 | +def app(set_authorization_header): # Using the global fixture for Authorization header |
| 57 | + app_instance = gooddataAiChatApp(set_authorization_header, WORKSPACE_ID) |
| 58 | + return app_instance |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize( |
| 62 | + "question, expected_file", |
| 63 | + [(item["question"], item["expected_objects_file"]) for item in questions_list], |
| 64 | + ids=[item["question"] for item in questions_list], |
| 65 | +) |
| 66 | +@pytest.mark.asyncio |
| 67 | +async def test_ai_chat(app, question, expected_file): |
| 68 | + expected_objects = load_json(os.path.join(EXPECTED_OBJECTS_DIR, expected_file)) |
| 69 | + try: |
| 70 | + api_response = await app.ask_question(question) |
| 71 | + validate_response(api_response.to_dict(), expected_objects) |
| 72 | + |
| 73 | + interaction_id = api_response.chat_history_interaction_id |
| 74 | + user_feedback = await app.chat_history(interaction_id, "POSITIVE") |
| 75 | + assert isinstance(user_feedback, ChatHistoryResult), "Invalid response from chat history" |
| 76 | + except gooddata_api_client.ApiException as e: |
| 77 | + pytest.fail(f"API exception: {e}") |
| 78 | + except Exception as e: |
| 79 | + pytest.fail(f"Unexpected error: {e}") |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + pytest.main() |
0 commit comments