Skip to content

Commit 10771b1

Browse files
committed
add tests for copy_example
1 parent 81fa5cf commit 10771b1

File tree

1 file changed

+70
-18
lines changed

1 file changed

+70
-18
lines changed

tests/test_cli.py

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,100 @@
1+
import os
2+
13
import pytest
24

35
from diffpy.cmi import cli
46

57

68
@pytest.mark.parametrize(
7-
"structure, expected",
9+
"inputs, expected",
810
[
9-
# case: no packs, no examples
11+
# case: no packs, no examples, expect empty dict
1012
([], {}),
11-
# case: one pack with one example
13+
# case: one pack with one example,
14+
# expect dict of {"pack-name": ["example"]}
1215
([("packA", ["ex1"])], {"packA": ["ex1"]}),
13-
# case: one pack with multiple examples
16+
# case: one pack with multiple examples,
17+
# expect dict of {"pack-name": ["example1", "example2"]}
1418
([("packA", ["ex1", "ex2"])], {"packA": ["ex1", "ex2"]}),
15-
# case: multiple packs with one example each
19+
# case: multiple packs with one example each,
20+
# expect dict of {"pack-name": ["example"]}
1621
(
1722
[("packA", ["ex1"]), ("packB", ["ex2"])],
1823
{"packA": ["ex1"], "packB": ["ex2"]},
1924
),
20-
# case: multiple packs with multiple examples
25+
# case: multiple packs with multiple examples,
26+
# expect dict of {"pack-name": ["example1", "example2"]}
2127
(
2228
[("packA", ["ex1", "ex2"]), ("packB", ["ex3", "ex4"])],
2329
{"packA": ["ex1", "ex2"], "packB": ["ex3", "ex4"]},
2430
),
2531
],
2632
)
27-
def test_map_pack_to_examples(tmp_path, mocker, structure, expected):
33+
def test_map_pack_to_examples(tmp_path, mocker, inputs, expected):
2834
"""Finds examples directory and returns a dictionary mapping packs
2935
to examples."""
30-
# example input: build example structure
31-
for pack, exdirs in structure:
32-
packdir = tmp_path / pack
36+
for pack_name, example_list in inputs:
37+
packdir = tmp_path / pack_name
3338
packdir.mkdir()
34-
for ex in exdirs:
35-
exdir = packdir / ex
39+
for example in example_list:
40+
exdir = packdir / example
3641
exdir.mkdir()
37-
# check directory was created
38-
assert exdir.exists() and exdir.is_dir(), f"{exdir} not created"
3942
# patch _get_examples_dir to point to tmp_path
4043
mocker.patch.object(cli, "_get_examples_dir", return_value=tmp_path)
41-
# expected behavior: a dictionary mapping pack to lists of examples
4244
result = cli.map_pack_to_examples()
4345
assert result == expected
4446

4547

46-
def test_copy_examples():
47-
""""""
48-
return
48+
def test_copy_example_success(tmp_path, mocker):
49+
"""Tests successful copy of example from pack to cwd."""
50+
pack, ex = "pack1", "ex1"
51+
example_dir = tmp_path / pack / ex
52+
example_dir.mkdir(parents=True)
53+
# Patch _get_examples_dir to use tmp_path
54+
mocker.patch.object(cli, "_get_examples_dir", return_value=tmp_path)
55+
os.chdir(tmp_path)
56+
dest = cli.copy_example(f"{pack}/{ex}")
57+
expected_dest = tmp_path / ex
58+
assert dest == expected_dest
59+
assert dest.exists()
60+
61+
62+
@pytest.mark.parametrize("bad_input", ["pack1ex1", "pack1/", "/ex1"])
63+
def test_copy_example_invalid_format(tmp_path, mocker, bad_input):
64+
"""Tests invalid format ValueError."""
65+
mocker.patch.object(cli, "_get_examples_dir", return_value=tmp_path)
66+
os.chdir(tmp_path)
67+
with pytest.raises(ValueError):
68+
cli.copy_example(bad_input)
69+
70+
71+
@pytest.mark.parametrize(
72+
"pack, ex",
73+
[
74+
("pack1", "ex1"),
75+
("missing_pack", "ex1"),
76+
],
77+
)
78+
def test_copy_example_not_found(tmp_path, mocker, pack, ex):
79+
"""
80+
Test copy_example raises FileNotFoundError when:
81+
- the pack exists but example is missing
82+
- the pack itself is missing
83+
"""
84+
mocker.patch.object(cli, "_get_examples_dir", return_value=tmp_path)
85+
os.chdir(tmp_path)
86+
with pytest.raises(FileNotFoundError):
87+
cli.copy_example(f"{pack}/{ex}")
88+
89+
90+
def test_copy_example_destination_exists(tmp_path, mocker):
91+
"""Tests FileExistsError when destination directory already
92+
exists."""
93+
pack, ex = "pack1", "ex1"
94+
example_dir = tmp_path / pack / ex
95+
example_dir.mkdir(parents=True)
96+
mocker.patch.object(cli, "_get_examples_dir", return_value=tmp_path)
97+
os.chdir(tmp_path)
98+
(tmp_path / ex).mkdir(exist_ok=True)
99+
with pytest.raises(FileExistsError):
100+
cli.copy_example(f"{pack}/{ex}")

0 commit comments

Comments
 (0)