Skip to content

Commit da2337a

Browse files
committed
improve test_slurm_submit(), add tests/test_init.py
1 parent f25d91f commit da2337a

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

tests/test_init.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
from typing import Any
3+
4+
from mb_discovery import as_dict_handler, chunks
5+
6+
7+
def test_has_root_pkg_dir() -> None:
8+
9+
from mb_discovery import PKG_DIR, ROOT
10+
11+
assert os.path.isdir(ROOT)
12+
assert os.path.isdir(PKG_DIR)
13+
14+
15+
def test_chunks() -> None:
16+
17+
assert list(chunks([], 1)) == []
18+
assert list(chunks([1], 1)) == [[1]]
19+
assert list(chunks([1, 2], 1)) == [[1], [2]]
20+
assert list(chunks([1, 2, 3], 1)) == [[1], [2], [3]]
21+
assert list(chunks([1, 2, 3], 2)) == [[1, 2], [3]]
22+
assert list(chunks(range(1, 4), 2)) == [range(1, 3), range(3, 4)]
23+
assert list(chunks(range(1, 5), 2)) == [range(1, 3), range(3, 5)]
24+
assert list(chunks(range(1, 5), 3)) == [range(1, 4), range(4, 5)]
25+
26+
27+
def test_as_dict_handler() -> None:
28+
class C:
29+
def as_dict(self) -> dict[str, Any]:
30+
return {"foo": "bar"}
31+
32+
assert as_dict_handler(C()) == {"foo": "bar"}
33+
assert as_dict_handler(1) is None
34+
assert as_dict_handler("foo") is None
35+
assert as_dict_handler([1, 2, 3]) is None
36+
assert as_dict_handler({"foo": "bar"}) is None

tests/test_slurm.py

+29-11
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,42 @@
22
from unittest.mock import patch
33

44
import pytest
5+
from pytest import CaptureFixture
56

67
from mb_discovery.slurm import _get_calling_file_path, slurm_submit_python
78

89

9-
def test_slurm_submit() -> None:
10+
def test_slurm_submit(capsys: CaptureFixture[str]) -> None:
11+
# check slurm_submit_python() does nothing in normal mode
1012

13+
kwargs = dict(
14+
job_name="test_job",
15+
log_dir="test_log_dir",
16+
time="0:0:1",
17+
slurm_flags=("--test-flag",),
18+
)
19+
slurm_submit_python(**kwargs) # type: ignore
20+
21+
stdout, stderr = capsys.readouterr()
22+
assert stderr == stderr == ""
23+
24+
# check slurm_submit_python() does prints cmd calls subprocess.run() in submit mode
1125
sys.argv += ["slurm-submit"]
12-
with pytest.raises(SystemExit) as exc_info, patch(
26+
27+
with pytest.raises(SystemExit), patch(
1328
"mb_discovery.slurm.subprocess.run"
14-
) as mock_run:
15-
slurm_submit_python(
16-
job_name="test_job",
17-
log_dir="test_log_dir",
18-
time="0:0:1",
19-
slurm_flags=("--test_flag",),
20-
)
21-
assert exc_info.value.code == 0
22-
assert mock_run.call_count == 1
29+
) as mock_subprocess_run:
30+
slurm_submit_python(**kwargs) # type: ignore
31+
32+
assert mock_subprocess_run.call_count == 1
33+
34+
sbatch_cmd = (
35+
"sbatch --partition=icelake --account=LEE-SL3-CPU --time=0:0:1 --job-name "
36+
"test_job --output test_log_dir/slurm-%A-%a.out --test-flag --wrap python"
37+
)
38+
stdout, stderr = capsys.readouterr()
39+
assert sbatch_cmd in stdout
40+
assert stderr == ""
2341

2442

2543
def test_get_calling_file_path() -> None:

0 commit comments

Comments
 (0)