|
| 1 | +# (C) 2024 GoodData Corporation |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +from pprint import pprint |
| 6 | + |
| 7 | +import gooddata_api_client |
| 8 | +import pytest |
| 9 | +from dotenv import load_dotenv |
| 10 | +from gooddata_api_client.api import smart_functions_api |
| 11 | +from gooddata_api_client.model.chat_history_request import ChatHistoryRequest |
| 12 | +from gooddata_api_client.model.chat_request import ChatRequest |
| 13 | + |
| 14 | +from integration_tests.scenarios.utils import compare_and_print_diff, load_json, normalize_metrics |
| 15 | + |
| 16 | +_current_dir = Path(__file__).parent.absolute() |
| 17 | +parent_dir = _current_dir.parent |
| 18 | +expected_object_dir = parent_dir / "expected" |
| 19 | +questions_list_dir = parent_dir / "fixtures" / "ai_questions.json" |
| 20 | + |
| 21 | +# Load environment variables from the .env file |
| 22 | +load_dotenv() |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope="module") |
| 26 | +def test_config(): |
| 27 | + return { |
| 28 | + "host": os.getenv("HOST"), |
| 29 | + "token": os.getenv("TOKEN"), |
| 30 | + "workspace_id": os.getenv("WORKSPACE_ID"), |
| 31 | + "llm_token": os.getenv("LLM_TOKEN"), |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture(scope="module") |
| 36 | +def api_client(test_config): |
| 37 | + configuration = gooddata_api_client.Configuration(host=test_config["host"]) |
| 38 | + api_client = gooddata_api_client.ApiClient(configuration) |
| 39 | + api_client.default_headers["Authorization"] = f"Bearer {test_config['token']}" |
| 40 | + return api_client |
| 41 | + |
| 42 | + |
| 43 | +def validate_response(actual_response, expected_response): |
| 44 | + actual_metrics = normalize_metrics( |
| 45 | + actual_response["created_visualizations"]["objects"][0]["metrics"], exclude_keys=["title"] |
| 46 | + ) |
| 47 | + expected_metrics = normalize_metrics(expected_response["metrics"], exclude_keys=["title"]) |
| 48 | + compare_and_print_diff(actual_metrics, expected_metrics, "Metrics") |
| 49 | + actual_visualization_type = actual_response["created_visualizations"]["objects"][0]["visualization_type"] |
| 50 | + expected_visualization_type = expected_response["visualizationType"] |
| 51 | + compare_and_print_diff(actual_visualization_type, expected_visualization_type, "Visualization type") |
| 52 | + actual_dimensionality = actual_response["created_visualizations"]["objects"][0]["dimensionality"] |
| 53 | + expected_dimensionality = expected_response["dimensionality"] |
| 54 | + compare_and_print_diff(actual_dimensionality, expected_dimensionality, "Dimensionality") |
| 55 | + actual_filters = actual_response["created_visualizations"]["objects"][0]["filters"] |
| 56 | + expected_filters = expected_response["filters"] |
| 57 | + compare_and_print_diff(actual_filters, expected_filters, "Filters") |
| 58 | + |
| 59 | + |
| 60 | +def test_ai_chat_history_reset(api_client, test_config): |
| 61 | + api_instance = smart_functions_api.SmartFunctionsApi(api_client) |
| 62 | + chat_history_request = ChatHistoryRequest(reset=True) |
| 63 | + try: |
| 64 | + api_response = api_instance.ai_chat_history(test_config["workspace_id"], chat_history_request) |
| 65 | + pprint(api_response) |
| 66 | + except gooddata_api_client.ApiException as e: |
| 67 | + pytest.fail(f"API exception: {e}") |
| 68 | + except Exception as e: |
| 69 | + pytest.fail(f"Unexpected error: {e}") |
| 70 | + |
| 71 | + |
| 72 | +questions_list = load_json(questions_list_dir) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize( |
| 76 | + "question, expected_file", |
| 77 | + [(item["question"], item["expected_objects_file"]) for item in questions_list], |
| 78 | + ids=[item["question"] for item in questions_list], |
| 79 | +) |
| 80 | +def test_ai_chat(api_client, test_config, question, expected_file): |
| 81 | + expected_objects = load_json(os.path.join(expected_object_dir, expected_file)) |
| 82 | + api_instance = smart_functions_api.SmartFunctionsApi(api_client) |
| 83 | + try: |
| 84 | + api_response = api_instance.ai_chat(test_config["workspace_id"], ChatRequest(question=question)) |
| 85 | + print("\napi_response", api_response.created_visualizations.objects[0]) |
| 86 | + print("\nexpected_file", expected_objects) |
| 87 | + |
| 88 | + validate_response(api_response.to_dict(), expected_objects) |
| 89 | + |
| 90 | + except gooddata_api_client.ApiException as e: |
| 91 | + pytest.fail(f"API exception: {e}") |
| 92 | + except Exception as e: |
| 93 | + pytest.fail(f"Unexpected error: {e}") |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + pytest.main(["-s", __file__]) |
0 commit comments