Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 8681544

Browse files
LeVinhGithubHarry Le
andauthored
task: Expand e2e test for section "files" #2065
* test: add e2e files --------- Co-authored-by: Harry Le <[email protected]>
1 parent 6716266 commit 8681544

File tree

8 files changed

+384
-0
lines changed

8 files changed

+384
-0
lines changed

.github/workflows/cortex-cpp-quality-gate.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ jobs:
247247
cd engine
248248
make package
249249
250+
- name: Upload E2E Log
251+
if: failure()
252+
uses: actions/upload-artifact@v4
253+
with:
254+
name: e2e-log-${{ matrix.os }}-${{ matrix.name }}
255+
path: ./engine/e2e-test/logs
256+
250257
- name: Upload Artifact
251258
uses: actions/upload-artifact@v4
252259
with:

engine/e2e-test/api/files/blank.txt

Whitespace-only changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import pytest
2+
import requests
3+
from utils.test_runner import start_server, stop_server
4+
import os
5+
import platform
6+
import jsonschema
7+
from utils.logger import log_response
8+
from utils.assertion import assert_equal
9+
import fnmatch
10+
11+
12+
class TestApiCreateFile:
13+
14+
@pytest.fixture(autouse=True)
15+
def setup_and_teardown(self):
16+
# Setup
17+
success = start_server()
18+
if not success:
19+
raise Exception("Failed to start server")
20+
21+
yield
22+
23+
# Teardown
24+
stop_server()
25+
26+
@pytest.mark.skipif(platform.system() != "Linux", reason="Todo: fix later on Mac and Window")
27+
def test_api_create_file_successfully(self):
28+
# Define file path
29+
file_path_rel = os.path.join("e2e-test", "api", "files", "blank.txt")
30+
file_path = os.path.join(os.getcwd(), file_path_rel)
31+
log_response(file_path, "test_api_create_file_successfully")
32+
33+
post_file_url = "http://127.0.0.1:3928/v1/files"
34+
with open(file_path, "rb") as file:
35+
files = {"file": ("blank.txt", file, "text/plain")}
36+
data = {"purpose": "assistants"}
37+
response = requests.post(post_file_url, files=files, data=data)
38+
log_response(response.text, "test_api_create_file_successfully")
39+
log_response(response.status_code, "test_api_create_file_successfully")
40+
41+
json_data = response.json()
42+
log_response(json_data, "test_api_create_file_successfully")
43+
assert_equal(response.status_code, 200)
44+
45+
# Schema to validate
46+
schema = {
47+
"type": "object",
48+
"properties": {
49+
"bytes": {"type": "integer"},
50+
"created_at": {"type": "integer"},
51+
"filename": {"type": "string"},
52+
"id": {"type": "string"},
53+
"object": {"type": "string"},
54+
"purpose": {"type": "string"}
55+
},
56+
"required": ["bytes", "created_at", "filename", "id", "object", "purpose"]
57+
}
58+
59+
# Validate response schema
60+
jsonschema.validate(instance=json_data, schema=schema)
61+
62+
# Assert content
63+
assert (fnmatch.fnmatch(json_data["filename"], "blank_*.txt") or json_data["filename"] == "blank.txt"), f"Filename {json_data['filename']} does not match pattern blank_*.txt or blank.txt"
64+
assert_equal(json_data["purpose"], "assistants")
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import pytest
2+
import requests
3+
from utils.test_runner import start_server, stop_server
4+
import os
5+
import jsonschema
6+
from utils.logger import log_response
7+
from utils.assertion import assert_equal
8+
import platform
9+
10+
11+
class TestApiDeleteFile:
12+
13+
@pytest.fixture(autouse=True)
14+
def setup_and_teardown(self):
15+
# Setup
16+
success = start_server()
17+
if not success:
18+
raise Exception("Failed to start server")
19+
20+
yield
21+
22+
# Teardown
23+
stop_server()
24+
25+
@pytest.mark.skipif(platform.system() != "Linux", reason="Todo: fix later on Mac and Window")
26+
def test_api_del_file_successfully(self):
27+
# Define file path
28+
file_path = os.path.join("e2e-test", "api", "files", "blank.txt")
29+
30+
# Upload file first
31+
files = {
32+
"file": ("blank.txt", open(file_path, "rb"), "text/plain")
33+
}
34+
data = {
35+
"purpose": "assistants"
36+
}
37+
38+
file_url = "http://127.0.0.1:3928/v1/files"
39+
response = requests.post(file_url, files=files, data=data)
40+
41+
json_data = response.json()
42+
log_response(json_data, "test_api_del_file_successfully")
43+
assert_equal(response.status_code, 200)
44+
45+
file_id=json_data["id"]
46+
47+
# Delete message with id
48+
file_id_url = f"http://127.0.0.1:3928/v1/files/{file_id}"
49+
file_response = requests.delete(file_id_url)
50+
json_data_file = file_response.json()
51+
log_response(json_data_file, "test_api_del_file_successfully")
52+
assert_equal(file_response.status_code,200)
53+
54+
55+
# Schema to validate
56+
schema = {
57+
"properties": {
58+
"deleted": {
59+
"description": "Indicates if the file was successfully deleted",
60+
"type": "boolean"
61+
},
62+
"id": {
63+
"description": "The ID of the deleted file",
64+
"type": "string"
65+
},
66+
"object": {
67+
"description": "Type of object, always 'file'",
68+
"type": "string"
69+
}
70+
},
71+
"required": [
72+
"deleted",
73+
"id",
74+
"object"
75+
],
76+
"type": "object"
77+
}
78+
79+
# Validate response schema
80+
jsonschema.validate(instance=json_data_file, schema=schema)
81+
82+
# Assert content
83+
assert_equal(json_data_file["deleted"], True)
84+
assert_equal(json_data_file["id"], file_id)
85+
assert_equal(json_data_file["object"], "file")
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import pytest
2+
import requests
3+
from utils.test_runner import start_server, stop_server
4+
import platform
5+
import os
6+
import jsonschema
7+
from utils.logger import log_response
8+
from utils.assertion import assert_equal
9+
import fnmatch
10+
11+
12+
class TestApiGetFile:
13+
14+
@pytest.fixture(autouse=True)
15+
def setup_and_teardown(self):
16+
# Setup
17+
success = start_server()
18+
if not success:
19+
raise Exception("Failed to start server")
20+
21+
yield
22+
23+
# Teardown
24+
stop_server()
25+
26+
@pytest.mark.skipif(platform.system() != "Linux", reason="Todo: fix later on Mac and Window")
27+
def test_api_get_file_successfully(self):
28+
# Define file path
29+
file_path = os.path.join("e2e-test", "api", "files", "blank.txt")
30+
31+
# Upload file first
32+
files = {
33+
"file": ("blank.txt", open(file_path, "rb"), "text/plain")
34+
}
35+
data = {
36+
"purpose": "assistants"
37+
}
38+
39+
file_url = "http://127.0.0.1:3928/v1/files"
40+
response = requests.post(file_url, files=files, data=data)
41+
log_response(response.text, "test_api_get_file_successfully")
42+
43+
json_data = response.json()
44+
log_response(json_data, "test_api_get_file_successfully")
45+
assert_equal(response.status_code, 200)
46+
47+
file_id=json_data["id"]
48+
49+
# Get message with id
50+
file_id_url = f"http://127.0.0.1:3928/v1/files/{file_id}"
51+
file_response = requests.get(file_id_url)
52+
json_data_file = file_response.json()
53+
log_response(json_data_file, "test_api_get_file_successfully")
54+
assert_equal(file_response.status_code,200)
55+
56+
57+
# Schema to validate
58+
schema = {
59+
"properties": {
60+
"bytes": {
61+
"type": "integer",
62+
"examples": [
63+
3211109
64+
]
65+
},
66+
"created_at": {
67+
"type": "integer",
68+
"examples": [
69+
1733942093
70+
]
71+
},
72+
"filename": {
73+
"type": "string",
74+
"examples": [
75+
"Enterprise_Application_Infrastructure_v2_20140903_toCTC_v1.0.pdf"
76+
]
77+
},
78+
"id": {
79+
"type": "string",
80+
"examples": [
81+
"file-0001KNKPTDDAQSDVEQGRBTCTNJ"
82+
]
83+
},
84+
"object": {
85+
"type": "string",
86+
"examples": [
87+
"file"
88+
]
89+
},
90+
"purpose": {
91+
"type": "string",
92+
"examples": [
93+
"assistants"
94+
]
95+
}
96+
},
97+
"type": "object"
98+
}
99+
100+
# Validate response schema
101+
jsonschema.validate(instance=json_data_file, schema=schema)
102+
103+
# Assert content
104+
assert (fnmatch.fnmatch(json_data["filename"], "blank_*.txt") or json_data["filename"] == "blank.txt"), f"Filename {json_data['filename']} does not match pattern blank_*.txt or blank.txt"
105+
assert_equal(json_data_file["id"], file_id)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import pytest
2+
import requests
3+
from utils.test_runner import start_server, stop_server
4+
import os
5+
import platform
6+
import jsonschema
7+
from utils.logger import log_response
8+
from utils.assertion import assert_equal
9+
import fnmatch
10+
11+
12+
class TestApiGetListFile:
13+
14+
@pytest.fixture(autouse=True)
15+
def setup_and_teardown(self):
16+
# Setup
17+
success = start_server()
18+
if not success:
19+
raise Exception("Failed to start server")
20+
21+
yield
22+
23+
# Teardown
24+
stop_server()
25+
26+
@pytest.mark.skipif(platform.system() != "Linux", reason="Todo: fix later on Mac and Window")
27+
def test_api_get_list_file_successfully(self):
28+
# Define file path
29+
file_path = os.path.join("e2e-test", "api", "files", "blank.txt")
30+
31+
# Upload file first
32+
files = {
33+
"file": ("blank.txt", open(file_path, "rb"), "text/plain")
34+
}
35+
data = {
36+
"purpose": "assistants"
37+
}
38+
39+
file_url = "http://127.0.0.1:3928/v1/files"
40+
response = requests.post(file_url, files=files, data=data)
41+
log_response(response.text, "test_api_get_list_file_successfully")
42+
43+
json_data = response.json()
44+
log_response(json_data, "test_api_get_list_file_successfully")
45+
assert_equal(response.status_code, 200)
46+
47+
# Get list message
48+
list_file_response = requests.get(file_url)
49+
json_data_list_file = list_file_response.json()
50+
log_response(json_data_list_file, "test_api_get_list_file_successfully")
51+
assert_equal(list_file_response.status_code,200)
52+
53+
54+
# Schema to validate
55+
schema = {
56+
"properties": {
57+
"data": {
58+
"items": {
59+
"properties": {
60+
"bytes": {
61+
"type": "integer",
62+
"examples": [
63+
3211109
64+
]
65+
},
66+
"created_at": {
67+
"type": "integer",
68+
"examples": [
69+
1733942093
70+
]
71+
},
72+
"filename": {
73+
"type": "string",
74+
"examples": [
75+
"Enterprise_Application_Infrastructure_v2_20140903_toCTC_v1.0.pdf"
76+
]
77+
},
78+
"id": {
79+
"type": "string",
80+
"examples": [
81+
"file-0001KNKPTDDAQSDVEQGRBTCTNJ"
82+
]
83+
},
84+
"object": {
85+
"type": "string",
86+
"examples": [
87+
"file"
88+
]
89+
},
90+
"purpose": {
91+
"type": "string",
92+
"examples": [
93+
"assistants"
94+
]
95+
}
96+
},
97+
"type": "object"
98+
},
99+
"type": "array"
100+
},
101+
"object": {
102+
"type": "string",
103+
"examples": [
104+
"list"
105+
]
106+
}
107+
},
108+
"type": "object"
109+
}
110+
111+
# Validate response schema
112+
jsonschema.validate(instance=json_data_list_file, schema=schema)
113+
114+
# Assert content
115+
assert (fnmatch.fnmatch(json_data["filename"], "blank_*.txt") or json_data["filename"] == "blank.txt"), f"Filename {json_data['filename']} does not match pattern blank_*.txt or blank.txt"

0 commit comments

Comments
 (0)