Skip to content

Commit dc1163d

Browse files
committed
feat: POC automation test
JIRA: QA-23477 risk: nonprod
1 parent a576d4f commit dc1163d

File tree

7 files changed

+233
-0
lines changed

7 files changed

+233
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# (C) 2024 GoodData Corporation
2+
# env.py
3+
import os
4+
5+
# Define environment variables
6+
HOST = os.getenv("GOODDATA_HOST", "xxx")
7+
TOKEN = os.getenv("GOODDATA_TOKEN", "xxx")
8+
DATASOURCE_ID = os.getenv("DATASOURCE_ID", "xxx")
9+
WORKSPACE_ID = "xxx"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# (C) 2024 GoodData Corporation
2+
3+
import os
4+
import sys
5+
from pprint import pprint
6+
7+
import gooddata_api_client
8+
import pytest
9+
from gooddata_api_client.api import smart_functions_api
10+
from gooddata_api_client.model.chat_history_request import ChatHistoryRequest
11+
from gooddata_api_client.model.chat_history_result import ChatHistoryResult
12+
from gooddata_api_client.model.chat_request import ChatRequest
13+
from gooddata_api_client.model.chat_result import ChatResult
14+
15+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
16+
17+
from env import HOST, TOKEN, WORKSPACE_ID
18+
19+
20+
@pytest.fixture
21+
def api_client():
22+
configuration = gooddata_api_client.Configuration(host=HOST)
23+
configuration.access_token = TOKEN
24+
with gooddata_api_client.ApiClient(configuration) as api_client:
25+
yield api_client
26+
27+
28+
def test_ai_chat(api_client):
29+
question = "What is the number of Accounts?"
30+
31+
api_instance = smart_functions_api.SmartFunctionsApi(api_client)
32+
chat_request = ChatRequest(question=question)
33+
34+
try:
35+
api_response = api_instance.ai_chat(WORKSPACE_ID, chat_request)
36+
pprint(api_response)
37+
assert isinstance(api_response, ChatResult), "Response is not of type ChatResult"
38+
except gooddata_api_client.ApiException as e:
39+
print(f"Exception when calling SmartFunctionsApi->ai_chat: {e}")
40+
pytest.fail(f"Exception when calling SmartFunctionsApi->ai_chat: {e}\n")
41+
42+
43+
def test_ai_chat_history(api_client):
44+
api_instance = smart_functions_api.SmartFunctionsApi(api_client)
45+
chat_history_request = ChatHistoryRequest(
46+
chat_history_interaction_id=100,
47+
user_feedback="POSITIVE",
48+
)
49+
50+
try:
51+
api_response = api_instance.ai_chat_history(WORKSPACE_ID, chat_history_request)
52+
pprint(api_response)
53+
assert isinstance(api_response, ChatHistoryResult), "Response is not of type ChatHistoryResult"
54+
except gooddata_api_client.ApiException as e:
55+
print(f"Exception when calling SmartFunctionsApi->ai_chat_history: {e}")
56+
print(f"Status Code: {e.status}")
57+
print(f"Reason: {e.reason}")
58+
print(f"HTTP response headers: {e.headers}")
59+
print(f"HTTP response body: {e.body}")
60+
pytest.fail(f"Exception when calling SmartFunctionsApi->ai_chat_history: {e}\n")
61+
62+
63+
if __name__ == "__main__":
64+
pytest.main()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# (C) 2024 GoodData Corporation
2+
import os
3+
import sys
4+
5+
import pytest
6+
7+
# Add the root directory to sys.path
8+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9+
10+
from env import HOST, TOKEN, WORKSPACE_ID
11+
from gooddata_sdk import GoodDataSdk
12+
13+
14+
@pytest.fixture
15+
def test_config():
16+
return {"host": HOST, "token": TOKEN, "workspace_id": WORKSPACE_ID}
17+
18+
19+
questions = [
20+
"What is the number of Accounts?",
21+
"What is the total of Amount?",
22+
]
23+
24+
25+
@pytest.mark.parametrize("question", questions)
26+
def test_ask_ai(test_config, question):
27+
sdk = GoodDataSdk.create(host_=test_config["host"], token_=test_config["token"])
28+
workspace_id = test_config["workspace_id"]
29+
chat_ai_res = sdk.compute.ai_chat(workspace_id, question=question)
30+
31+
print(f"Chat AI response: {chat_ai_res}")
32+
assert chat_ai_res is not None, "Response should not be None"
33+
34+
35+
if __name__ == "__main__":
36+
pytest.main()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# (C) 2024 GoodData Corporation
2+
from env import DATASOURCE_ID, HOST, TOKEN, WORKSPACE_ID
3+
from workspace_manager import createWorkspace, getDataSource, update_env_file
4+
5+
if __name__ == "__main__":
6+
test_config = {"host": HOST, "token": TOKEN}
7+
8+
if WORKSPACE_ID:
9+
print(f"Workspace ID '{WORKSPACE_ID}' already exists. Skipping workspace creation.")
10+
else:
11+
workspace_id = createWorkspace(test_config)
12+
dataSource = getDataSource(DATASOURCE_ID, test_config)
13+
if workspace_id:
14+
update_env_file(workspace_id)
15+
else:
16+
print("Failed to create workspace.")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# (C) 2024 GoodData Corporation
2+
from env import HOST, TOKEN
3+
from workspace_manager import deleteWorkspace
4+
5+
if __name__ == "__main__":
6+
test_config = {"host": HOST, "token": TOKEN}
7+
8+
deleteWorkspace(test_config)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# (C) 2024 GoodData Corporation
2+
import os
3+
import sys
4+
import time
5+
import uuid
6+
7+
from gooddata_sdk import CatalogWorkspace, GoodDataSdk
8+
9+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10+
11+
try:
12+
from env import WORKSPACE_ID
13+
except ImportError:
14+
WORKSPACE_ID = None
15+
16+
17+
def createWorkspace(test_config):
18+
sdk = GoodDataSdk.create(host_=test_config["host"], token_=test_config["token"])
19+
20+
workspace_id = uuid.uuid4().hex
21+
timestamp = int(time.time())
22+
workspace_name = f"pysdk_test_{timestamp}"
23+
24+
workspace = CatalogWorkspace(workspace_id, workspace_name)
25+
try:
26+
sdk.catalog_workspace.create_or_update(workspace)
27+
workspace_o = sdk.catalog_workspace.get_workspace(workspace_id)
28+
assert workspace_o == workspace
29+
30+
print(f"Workspace '{workspace_name}' with ID '{workspace_id}' created successfully.")
31+
return workspace_id
32+
except Exception as e:
33+
print(f"An error occurred while creating the workspace: {e}")
34+
return None
35+
36+
37+
def deleteWorkspace(test_config):
38+
sdk = GoodDataSdk.create(host_=test_config["host"], token_=test_config["token"])
39+
try:
40+
workspaces = sdk.catalog_workspace.list_workspaces()
41+
for workspace in workspaces:
42+
if workspace.name.startswith("pysdk_test_"):
43+
sdk.catalog_workspace.delete_workspace(workspace.id)
44+
print(f"Workspace '{workspace.name}' with ID '{workspace.id}' deleted successfully.")
45+
remove_env_file()
46+
except Exception as e:
47+
print(f"An error occurred while deleting workspaces: {e}")
48+
49+
50+
def update_env_file(workspace_id):
51+
with open("env.py", "a") as f:
52+
f.write(f'\nWORKSPACE_ID = "{workspace_id}"\n')
53+
54+
55+
def remove_env_file():
56+
try:
57+
with open("env.py") as f: # Default mode is 'r'
58+
lines = f.readlines()
59+
with open("env.py", "w") as f:
60+
for line in lines:
61+
if "WORKSPACE_ID" not in line:
62+
f.write(line)
63+
print("Removed WORKSPACE_ID from env.py")
64+
except Exception as e:
65+
print(f"An error occurred while removing WORKSPACE_ID from env.py: {e}")
66+
67+
68+
def getDataSource(data_source_id, test_config):
69+
sdk = GoodDataSdk.create(host_=test_config["host"], token_=test_config["token"])
70+
data_source = sdk.catalog_data_source.get_data_source(data_source_id)
71+
data_source_schema = data_source.schema
72+
print(f"Data source schema: {data_source_schema}")
73+
return data_source_schema
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# (C) 2024 GoodData Corporation
2+
# import pytest
3+
# from workspace_manager import create_workspace, delete_workspace, get_data_source, update_env_file
4+
# from env import DATASOURCE_ID, HOST, TOKEN
5+
# @pytest.fixture
6+
# def test_config():
7+
# return {
8+
# 'host': HOST,
9+
# 'token': TOKEN
10+
# }
11+
12+
# def test_create_workspace(test_config):
13+
# workspace_id = create_workspace(test_config)
14+
# assert workspace_id is not None, "Workspace creation failed"
15+
# update_env_file(workspace_id)
16+
17+
# # def test_delete_workspace(test_config):
18+
# # delete_workspace(test_config)
19+
# # # Assuming the function prints the deletion message, we can check the output
20+
# # # Here we assume that the function works correctly if no exception is raised
21+
22+
# def test_get_data_source(test_config):
23+
# schema = get_data_source(DATASOURCE_ID, test_config)
24+
# assert schema is not None, "Failed to get data source schema"
25+
26+
# if __name__ == "__main__":
27+
# pytest.main()

0 commit comments

Comments
 (0)