|
1 | | -import os |
| 1 | +import subprocess |
2 | 2 | from unittest.mock import Mock, patch |
3 | 3 |
|
4 | 4 | from backend.config.paths import DOCKER_DIR, RESOURCES_DIR |
5 | 5 | from backend.launch_service.app_setup import fetch_dataset, IMAGE_NAME, build_container |
6 | 6 |
|
7 | 7 |
|
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 = [] |
12 | 11 |
|
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 |
18 | 17 |
|
19 | | - result = fetch_dataset(url, data_file) |
| 18 | + def mock_subprocess_run(*args, **kwargs): |
| 19 | + commands_executed.append(args[0]) |
20 | 20 |
|
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"] |
27 | 37 |
|
28 | 38 |
|
29 | 39 | def test_successful_build(): |
|
0 commit comments