|
1 | | -import os |
2 | | - |
3 | | -import pytest |
4 | | - |
5 | 1 | from diffpy.cmi import cli |
6 | 2 |
|
7 | 3 |
|
8 | | -@pytest.mark.parametrize( |
9 | | - "inputs, expected", |
10 | | - [ |
11 | | - # case: no packs, no examples, expect empty dict |
12 | | - ([], {}), |
13 | | - # case: one pack with one example, |
14 | | - # expect dict of {"pack-name": ["example"]} |
15 | | - ([("packA", ["ex1"])], {"packA": ["ex1"]}), |
16 | | - # case: one pack with multiple examples, |
17 | | - # expect dict of {"pack-name": ["example1", "example2"]} |
18 | | - ([("packA", ["ex1", "ex2"])], {"packA": ["ex1", "ex2"]}), |
19 | | - # case: multiple packs with one example each, |
20 | | - # expect dict of {"pack-name": ["example"]} |
21 | | - ( |
22 | | - [("packA", ["ex1"]), ("packB", ["ex2"])], |
23 | | - {"packA": ["ex1"], "packB": ["ex2"]}, |
24 | | - ), |
25 | | - # case: multiple packs with multiple examples, |
26 | | - # expect dict of {"pack-name": ["example1", "example2"]} |
27 | | - ( |
28 | | - [("packA", ["ex1", "ex2"]), ("packB", ["ex3", "ex4"])], |
29 | | - {"packA": ["ex1", "ex2"], "packB": ["ex3", "ex4"]}, |
30 | | - ), |
31 | | - ], |
32 | | -) |
33 | | -def test_map_pack_to_examples(tmp_path, mocker, inputs, expected): |
34 | | - """Finds examples directory and returns a dictionary mapping packs |
35 | | - to examples.""" |
36 | | - for pack_name, example_list in inputs: |
37 | | - packdir = tmp_path / pack_name |
38 | | - packdir.mkdir() |
39 | | - for example in example_list: |
40 | | - exdir = packdir / example |
41 | | - exdir.mkdir() |
42 | | - # patch _get_examples_dir to point to tmp_path |
43 | | - mocker.patch.object(cli, "_get_examples_dir", return_value=tmp_path) |
| 4 | +def test_map_pack_to_examples_structure(): |
| 5 | + """Test that map_pack_to_examples returns the right shape of |
| 6 | + data.""" |
44 | 7 | result = cli.map_pack_to_examples() |
45 | | - assert result == expected |
46 | | - |
47 | | - |
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}") |
| 8 | + assert isinstance(result, dict) |
| 9 | + for pack, exdirs in result.items(): |
| 10 | + assert isinstance(pack, str) |
| 11 | + assert isinstance(exdirs, list) |
| 12 | + for ex in exdirs: |
| 13 | + assert isinstance(ex, str) |
| 14 | + # Check for known packs |
| 15 | + assert "core" in result.keys() |
| 16 | + assert "pdf" in result.keys() |
| 17 | + # Check for known examples |
| 18 | + assert ["linefit"] in result.values() |
0 commit comments