Skip to content

Commit dc6d773

Browse files
committed
Added unit test for 'suggest_path'
1 parent 56a511d commit dc6d773

1 file changed

Lines changed: 86 additions & 2 deletions

File tree

tests/server/api/test_file_io_instrument.py

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,89 @@
1-
def test_suggest_path():
2-
pass
1+
from pathlib import Path
2+
from unittest.mock import MagicMock
3+
4+
import pytest
5+
from pytest_mock import MockerFixture
6+
7+
from murfey.server.api.file_io_instrument import SuggestedPathParameters, suggest_path
8+
9+
10+
@pytest.mark.parametrize(
11+
"test_params",
12+
( # Touch | Extra directory | Has raw
13+
(True, "extra", False),
14+
(False, "extra", False),
15+
(True, "", False),
16+
(False, "", False),
17+
(True, "extra", True),
18+
(False, "extra", True),
19+
(True, "", True),
20+
(False, "", True),
21+
),
22+
)
23+
def test_suggest_path(
24+
mocker: MockerFixture,
25+
test_params: tuple[bool, str, bool],
26+
tmp_path: Path,
27+
):
28+
# Unpack test params
29+
touch, extra_dir, has_raw = test_params
30+
instrument_name = "test"
31+
year = "2026"
32+
visit_name = "visit"
33+
session_id = 1
34+
35+
rsync_basepath = tmp_path / "data"
36+
visit_dir = rsync_basepath / year / visit_name
37+
visit_dir.mkdir(parents=True, exist_ok=True)
38+
if has_raw:
39+
(visit_dir / "raw").mkdir(parents=True, exist_ok=True)
40+
41+
params = SuggestedPathParameters(
42+
base_path=visit_dir.relative_to(rsync_basepath) / "raw",
43+
touch=touch,
44+
extra_directory=extra_dir,
45+
)
46+
47+
# Mock the database call
48+
mock_session = MagicMock()
49+
mock_session.instrument_name = instrument_name
50+
mock_db = MagicMock()
51+
mock_db.exec.return_value.one.return_value = mock_session
52+
53+
# Mock 'get_machine_config'
54+
mock_machine_config = MagicMock()
55+
mock_machine_config.rsync_basepath = rsync_basepath
56+
mock_machine_config.mkdir_chmod = 0o775
57+
mocker.patch(
58+
"murfey.server.api.file_io_instrument.get_machine_config",
59+
return_value={
60+
instrument_name: mock_machine_config,
61+
},
62+
)
63+
64+
# Run the function and check outputs
65+
result = suggest_path(
66+
visit_name=visit_name,
67+
session_id=session_id,
68+
params=params,
69+
db=mock_db,
70+
)
71+
72+
# Check that the correct suggestion was returned
73+
dir_name = "raw" if not has_raw else "raw2"
74+
assert result["suggested_path"] == visit_dir.relative_to(rsync_basepath) / dir_name
75+
76+
# Check that folders are made only if 'touch' is set
77+
assert (
78+
(visit_dir / dir_name).exists()
79+
if touch
80+
else not (visit_dir / dir_name).exists()
81+
)
82+
assert (
83+
(visit_dir / dir_name / extra_dir).exists()
84+
if touch and extra_dir
85+
else not (visit_dir / dir_name / extra_dir).exists()
86+
)
387

488

589
def test_make_rsyncer_destination():

0 commit comments

Comments
 (0)