Skip to content

Commit 2259ff3

Browse files
committed
feat add tests
1 parent b154e18 commit 2259ff3

File tree

13 files changed

+663
-14
lines changed

13 files changed

+663
-14
lines changed

backend/config/paths.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def get_tmp_thread_files(uid):
77

88
def get_thread_filepath(uid, filepath):
99
base_prefix = TMP_FILE_DIR + "/"
10-
new_prefix = f"{TMP_FILE_DIR}/{uid}_"
10+
new_prefix = f"{TMP_FILE_DIR}/{uid}"
1111
return filepath.replace(base_prefix, new_prefix)
1212

1313

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
3+
from backend.config.paths import (
4+
get_tmp_thread_files,
5+
RESULTS_DIR,
6+
UPLOAD_DIR,
7+
get_thread_filepath,
8+
TMP_FILE_DIR,
9+
)
10+
11+
12+
@pytest.mark.parametrize(
13+
"uid, filepath, expected",
14+
[
15+
("", RESULTS_DIR, RESULTS_DIR),
16+
("123", UPLOAD_DIR, TMP_FILE_DIR + "/123uploads"),
17+
],
18+
)
19+
def test_get_thread_filepath(uid, filepath, expected):
20+
assert get_thread_filepath(uid, filepath) == expected
21+
22+
23+
@pytest.mark.parametrize(
24+
"uid, expected",
25+
[
26+
("", [UPLOAD_DIR, RESULTS_DIR]),
27+
("123", [TMP_FILE_DIR + "/123uploads", TMP_FILE_DIR + "/123results"]),
28+
],
29+
)
30+
def test_get_created_tmp_files(uid, expected):
31+
assert get_tmp_thread_files(uid) == expected
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import shutil
2+
3+
import pytest, os, csv
4+
5+
from backend.config.paths import TMP_FILE_DIR
6+
from backend.file_utils.csv_methods_writer import write_launch_info_to_csv
7+
from backend.file_utils.files import reset_dirs, save_upload_file
8+
from unittest.mock import Mock
9+
from fastapi import UploadFile
10+
from io import BytesIO
11+
12+
13+
@pytest.mark.parametrize(
14+
"filepaths",
15+
[
16+
([TMP_FILE_DIR + "/test0"]),
17+
([TMP_FILE_DIR + "/test1", TMP_FILE_DIR + "/test2"]),
18+
],
19+
)
20+
def test_removing_tmp_files(filepaths):
21+
for path in filepaths:
22+
os.makedirs(path)
23+
reset_dirs(filepaths)
24+
for path in filepaths:
25+
assert not os.path.exists(path)
26+
27+
28+
def test_save_upload_file():
29+
test_content = b"Hello, World!" * 1000
30+
upload_dir = TMP_FILE_DIR + "/test_save_upload/"
31+
dest_path = upload_dir + "test.txt"
32+
33+
mock_file = BytesIO(test_content)
34+
upload_file = Mock(spec=UploadFile)
35+
upload_file.file = mock_file
36+
37+
save_upload_file(upload_file, dest_path)
38+
39+
assert os.path.exists(dest_path)
40+
with open(dest_path, "rb") as f:
41+
saved_content = f.read()
42+
shutil.rmtree(upload_dir)
43+
assert saved_content == test_content
44+
45+
46+
def test_write_launch_info_to_csv():
47+
upload_dir = TMP_FILE_DIR + "/test_write_launch_info_to_csv/"
48+
os.makedirs(upload_dir)
49+
output_file = upload_dir + "launch_info.csv"
50+
parsed_methods = [
51+
"ManuallyCollected.dll,BinSearchMain",
52+
"ManuallyCollected.dll,BellmanFord",
53+
"ManuallyCollected.dll,BinaryMaze1BFS",
54+
]
55+
56+
write_launch_info_to_csv(parsed_methods=parsed_methods, output_file=output_file)
57+
58+
assert os.path.exists(output_file)
59+
60+
with open(output_file, "r") as f:
61+
reader = csv.reader(f)
62+
rows = list(reader)
63+
64+
assert rows[0] == ["dll", "method"]
65+
66+
expected_data = [
67+
["ManuallyCollected.dll", "BinSearchMain"],
68+
["ManuallyCollected.dll", "BellmanFord"],
69+
["ManuallyCollected.dll", "BinaryMaze1BFS"],
70+
]
71+
shutil.rmtree(upload_dir)
72+
assert rows[1:] == expected_data

backend/launch_service/app_setup.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010
from backend.utils.methods_handler import Methods
1111

1212
IMAGE_NAME = "pysymgym-test"
13+
URL = "https://raw.githubusercontent.com/PySymGym/PySymGym/main/maps/DotNet/Maps/dataset.json"
1314

1415

15-
def fetch_dataset():
16-
url = "https://raw.githubusercontent.com/PySymGym/PySymGym/main/maps/DotNet/Maps/dataset.json"
16+
def fetch_dataset(url, data_upload_file):
1717
print(f"Downloading dataset from {url} ...")
1818
resp = requests.get(url)
1919
resp.raise_for_status()
2020

21-
with open(DATASET_FILE, "wb") as f:
21+
with open(data_upload_file, "wb") as f:
2222
f.write(resp.content)
2323

24-
print(f"Dataset saved to {DATASET_FILE}")
25-
return DATASET_FILE
24+
print(f"Dataset saved to {data_upload_file}")
25+
return data_upload_file
2626

2727

2828
def build_container():
@@ -35,13 +35,15 @@ def build_container():
3535
os.makedirs(RESOURCES_DIR, exist_ok=True)
3636

3737

38-
def update_frontend_selection_options():
38+
def update_frontend_selection_options(dataset_file, selection_options_file):
3939
print("Updating frontend selection options...")
40-
selection_tree = Methods.parse_dataset_file_for_front_selection(DATASET_FILE)
41-
Methods.write_selection_dataset_to_front_file(selection_tree, METHODS_TS_FILE)
40+
selection_tree = Methods.parse_dataset_file_for_front_selection(dataset_file)
41+
Methods.write_selection_dataset_to_front_file(
42+
selection_tree, selection_options_file
43+
)
4244

4345

4446
if __name__ == "__main__":
4547
build_container()
46-
fetch_dataset()
47-
update_frontend_selection_options()
48+
fetch_dataset(URL, DATASET_FILE)
49+
update_frontend_selection_options(DATASET_FILE, METHODS_TS_FILE)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import shutil
3+
from unittest.mock import Mock, patch
4+
5+
from backend.config.paths import TMP_FILE_DIR, DOCKER_DIR, RESOURCES_DIR
6+
from backend.launch_service.app_setup import fetch_dataset, IMAGE_NAME, build_container
7+
8+
9+
def test_fetch_dataset():
10+
url = "https://example.com/dataset.csv"
11+
test_content = b"dll,method\nManuallyCollected.dll,BinSearchMain"
12+
upload_dir = TMP_FILE_DIR + "/test_dataset_upload/"
13+
os.makedirs(upload_dir)
14+
data_file = upload_dir + "dataset.json"
15+
16+
with patch("requests.get") as mock_get:
17+
mock_response = Mock()
18+
mock_response.content = test_content
19+
mock_response.raise_for_status = Mock()
20+
mock_get.return_value = mock_response
21+
22+
result = fetch_dataset(url, data_file)
23+
24+
assert result == data_file
25+
assert os.path.exists(data_file)
26+
with open(data_file, "rb") as f:
27+
assert f.read() == test_content
28+
shutil.rmtree(upload_dir)
29+
mock_get.assert_called_once_with(url)
30+
mock_response.raise_for_status.assert_called_once()
31+
32+
33+
def test_successful_build():
34+
with patch("subprocess.run") as mock_run:
35+
mock_run.return_value = Mock(returncode=0)
36+
37+
with patch("os.makedirs") as mock_makedirs:
38+
build_container()
39+
40+
expected_command = ["docker", "build", "--no-cache", "-t", IMAGE_NAME, DOCKER_DIR]
41+
mock_run.assert_called_once_with(expected_command, check=True)
42+
mock_makedirs.assert_called_once_with(RESOURCES_DIR, exist_ok=True)

backend/utils/results_sender.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def send_folder_by_email(
1818
to_email: str,
1919
folder_path: str,
2020
experiment_name: str,
21-
model_name: str,
21+
model_file_name: str,
2222
):
2323
load_dotenv()
2424
from_email = require_env("EMAIL")
@@ -34,14 +34,15 @@ def send_folder_by_email(
3434
for file in folder.rglob("*"):
3535
zipf.write(file, file.relative_to(folder))
3636

37+
model_name = model_file_name[:-5]
3738
msg = EmailMessage()
3839
msg["From"] = from_email
3940
msg["To"] = to_email
40-
msg["Subject"] = f"Results of {experiment_name} with {model_name[:-5]}"
41+
msg["Subject"] = f"Results of {experiment_name} with {model_name}"
4142

4243
message = (
4344
f"\n\n"
44-
f"The results of the experiment '{experiment_name}' using the model '{model_name[:-5]}' are ready. "
45+
f"The results of the experiment '{experiment_name}' using the model '{model_name}' are ready. "
4546
f"Please find the details in the attached ZIP file.\n\n"
4647
f"Contents of the ZIP:\n"
4748
f" - Model run results: artifact_run_ai folder\n"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
method,tests,errors,coverage,total_time_sec
2+
BinSearchMain,1,1,100.0,0
3+
Switches1,29,0,100.0,96
4+
Switches2,1,0,15.0,97
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
method,tests,errors,coverage,total_time_sec
2+
BinSearchMain,1,1,100.0,0
3+
Switches1,30,0,100.0,3
4+
Switches2,2,0,38.0,96
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dll,method
2+
ManuallyCollected.dll,BinSearchMain
3+
ManuallyCollected.dll,Switches1
4+
ManuallyCollected.dll,Switches2
1.01 MB
Binary file not shown.

0 commit comments

Comments
 (0)