Skip to content

Commit 5233bbb

Browse files
committed
fetch_dataset function uploads dataset from docker image, ruff format
1 parent 785f136 commit 5233bbb

File tree

3 files changed

+53
-30
lines changed

3 files changed

+53
-30
lines changed

backend/launch_service/app_setup.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import subprocess
3-
import requests
43
from backend.config.paths import (
54
RESOURCES_DIR,
65
DATASET_FILE,
@@ -10,19 +9,33 @@
109
from backend.utils.methods_handler import Methods
1110

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

1513

16-
def fetch_dataset(url, data_upload_file):
17-
print(f"Downloading dataset from {url} ...")
18-
resp = requests.get(url)
19-
resp.raise_for_status()
14+
def fetch_dataset(data_upload_file):
15+
container_name = "temp-fetch-dataset"
2016

21-
with open(data_upload_file, "wb") as f:
22-
f.write(resp.content)
17+
try:
18+
subprocess.run(
19+
["docker", "create", "--name", container_name, IMAGE_NAME],
20+
check=True,
21+
capture_output=True,
22+
)
2323

24-
print(f"Dataset saved to {data_upload_file}")
25-
return data_upload_file
24+
subprocess.run(
25+
[
26+
"docker",
27+
"cp",
28+
f"{container_name}:/workspace/PySymGym/maps/DotNet/Maps/dataset.json",
29+
data_upload_file,
30+
],
31+
check=True,
32+
)
33+
34+
print(f"Dataset copied to {data_upload_file}")
35+
return data_upload_file
36+
37+
finally:
38+
subprocess.run(["docker", "rm", container_name], capture_output=True)
2639

2740

2841
def build_container():
@@ -43,5 +56,5 @@ def update_frontend_selection_options(dataset_file, selection_options_file):
4356

4457
if __name__ == "__main__":
4558
build_container()
46-
fetch_dataset(URL, DATASET_FILE)
59+
fetch_dataset(DATASET_FILE)
4760
update_frontend_selection_options(DATASET_FILE, METHODS_TS_FILE)

backend/launch_service/tests/test_setup_functions.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1-
import os
1+
import subprocess
22
from unittest.mock import Mock, patch
33

44
from backend.config.paths import DOCKER_DIR, RESOURCES_DIR
55
from backend.launch_service.app_setup import fetch_dataset, IMAGE_NAME, build_container
66

77

8-
def test_fetch_dataset(tmp_path):
9-
url = "https://example.com/dataset.csv"
10-
test_content = b"dll,method\nManuallyCollected.dll,BinSearchMain"
11-
data_file = tmp_path / "dataset.json"
8+
def test_fetch_dataset_success(tmp_path, monkeypatch):
9+
test_file = tmp_path / "dataset.json"
10+
commands_executed = []
1211

13-
with patch("requests.get") as mock_get:
14-
mock_response = Mock()
15-
mock_response.content = test_content
16-
mock_response.raise_for_status = Mock()
17-
mock_get.return_value = mock_response
12+
class MockResult:
13+
def __init__(self, returncode=0, stdout=b"", stderr=b""):
14+
self.returncode = returncode
15+
self.stdout = stdout
16+
self.stderr = stderr
1817

19-
result = fetch_dataset(url, data_file)
18+
def mock_subprocess_run(*args, **kwargs):
19+
commands_executed.append(args[0])
2020

21-
assert result == data_file
22-
assert os.path.exists(data_file)
23-
with open(data_file, "rb") as f:
24-
assert f.read() == test_content
25-
mock_get.assert_called_once_with(url)
26-
mock_response.raise_for_status.assert_called_once()
21+
if args[0][0] == "docker" and args[0][1] == "cp":
22+
test_file.write_text('{"test": "data"}')
23+
24+
return MockResult()
25+
26+
monkeypatch.setattr(subprocess, "run", mock_subprocess_run)
27+
28+
result = fetch_dataset(str(test_file))
29+
30+
assert result == str(test_file)
31+
assert test_file.exists()
32+
assert test_file.read_text() == '{"test": "data"}'
33+
assert len(commands_executed) == 3
34+
assert commands_executed[0][:3] == ["docker", "create", "--name"]
35+
assert commands_executed[1][:2] == ["docker", "cp"]
36+
assert commands_executed[2][:2] == ["docker", "rm"]
2737

2838

2939
def test_successful_build():

backend/utils/tests/test_methods_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_write_selection_dataset_to_front_file():
1818
content = temp_file.read()
1919

2020
assert content.startswith("export const METHODS = ")
21-
json_str = content[len("export const METHODS = "):]
21+
json_str = content[len("export const METHODS = ") :]
2222
written_data = json.loads(json_str)
2323
assert written_data == test_data
2424

@@ -29,7 +29,7 @@ def test_write_selection_dataset_to_front_file_empty():
2929

3030
temp_file.seek(0)
3131
content = temp_file.read()
32-
json_str = content[len("export const METHODS = "):]
32+
json_str = content[len("export const METHODS = ") :]
3333
assert json.loads(json_str) == []
3434

3535

0 commit comments

Comments
 (0)