|
| 1 | +import os |
| 2 | + |
1 | 3 | import pytest |
2 | 4 |
|
3 | 5 | from diffpy.cmi import cli |
4 | 6 |
|
5 | 7 |
|
6 | 8 | @pytest.mark.parametrize( |
7 | | - "structure, expected", |
| 9 | + "inputs, expected", |
8 | 10 | [ |
9 | | - # case: no packs, no examples |
| 11 | + # case: no packs, no examples, expect empty dict |
10 | 12 | ([], {}), |
11 | | - # case: one pack with one example |
| 13 | + # case: one pack with one example, |
| 14 | + # expect dict of {"pack-name": ["example"]} |
12 | 15 | ([("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"]} |
14 | 18 | ([("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"]} |
16 | 21 | ( |
17 | 22 | [("packA", ["ex1"]), ("packB", ["ex2"])], |
18 | 23 | {"packA": ["ex1"], "packB": ["ex2"]}, |
19 | 24 | ), |
20 | | - # case: multiple packs with multiple examples |
| 25 | + # case: multiple packs with multiple examples, |
| 26 | + # expect dict of {"pack-name": ["example1", "example2"]} |
21 | 27 | ( |
22 | 28 | [("packA", ["ex1", "ex2"]), ("packB", ["ex3", "ex4"])], |
23 | 29 | {"packA": ["ex1", "ex2"], "packB": ["ex3", "ex4"]}, |
24 | 30 | ), |
25 | 31 | ], |
26 | 32 | ) |
27 | | -def test_map_pack_to_examples(tmp_path, mocker, structure, expected): |
| 33 | +def test_map_pack_to_examples(tmp_path, mocker, inputs, expected): |
28 | 34 | """Finds examples directory and returns a dictionary mapping packs |
29 | 35 | 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 |
33 | 38 | packdir.mkdir() |
34 | | - for ex in exdirs: |
35 | | - exdir = packdir / ex |
| 39 | + for example in example_list: |
| 40 | + exdir = packdir / example |
36 | 41 | exdir.mkdir() |
37 | | - # check directory was created |
38 | | - assert exdir.exists() and exdir.is_dir(), f"{exdir} not created" |
39 | 42 | # patch _get_examples_dir to point to tmp_path |
40 | 43 | mocker.patch.object(cli, "_get_examples_dir", return_value=tmp_path) |
41 | | - # expected behavior: a dictionary mapping pack to lists of examples |
42 | 44 | result = cli.map_pack_to_examples() |
43 | 45 | assert result == expected |
44 | 46 |
|
45 | 47 |
|
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