|
| 1 | +import pytest |
| 2 | + |
| 3 | +from diffpy.cmi.packsmanager import PacksManager |
| 4 | + |
| 5 | + |
| 6 | +def paths_and_names_match(expected, actual, root): |
| 7 | + """Compare two tuples (example_name, path), ignoring temp dir |
| 8 | + differences.""" |
| 9 | + if len(expected) != len(actual): |
| 10 | + return False |
| 11 | + for (exp_name, exp_path), (act_name, act_path) in zip(expected, actual): |
| 12 | + if exp_name != act_name: |
| 13 | + return False |
| 14 | + actual_rel_path = str(act_path.relative_to(root)) |
| 15 | + if actual_rel_path != exp_path: |
| 16 | + return False |
| 17 | + return True |
| 18 | + |
| 19 | + |
| 20 | +example_params = [ |
| 21 | + # 1) pack with no examples. Expect {'empty_pack': []} |
| 22 | + # 2) pack with multiple examples. |
| 23 | + # Expect {'full_pack': [('example1`, path_to_1'), 'example2', path_to_2)] |
| 24 | + # 3) multiple packs. Expect dict with multiple pack:tuple pairs |
| 25 | + # 4) no pack found. Expect {} |
| 26 | + # case 1: pack with no examples. Expect {'empty_pack': []} |
| 27 | + # 5) multiple packs with the same example names |
| 28 | + # Expect dict with multiple pack:tuple pairs |
| 29 | + ( |
| 30 | + "case1", |
| 31 | + {"empty_pack": []}, |
| 32 | + ), |
| 33 | + # case 2: pack with multiple examples. |
| 34 | + # Expect {'full_pack': [('example1', path_to_1), |
| 35 | + # ('example2', path_to_2)]} |
| 36 | + ( |
| 37 | + "case2", |
| 38 | + { |
| 39 | + "full_pack": [ |
| 40 | + ("ex1", "case2/docs/examples/full_pack/ex1"), |
| 41 | + ("ex2", "case2/docs/examples/full_pack/ex2"), |
| 42 | + ] |
| 43 | + }, |
| 44 | + ), |
| 45 | + # case 3: multiple packs. Expect dict with multiple pack:tuple pairs |
| 46 | + ( |
| 47 | + "case3", |
| 48 | + { |
| 49 | + "packA": [ |
| 50 | + ("ex1", "case3/docs/examples/packA/ex1"), |
| 51 | + ("ex2", "case3/docs/examples/packA/ex2"), |
| 52 | + ], |
| 53 | + "packB": [("ex3", "case3/docs/examples/packB/ex3")], |
| 54 | + }, |
| 55 | + ), |
| 56 | + ( # case 4: no pack found. Expect {} |
| 57 | + "case4", |
| 58 | + {}, |
| 59 | + ), |
| 60 | + ( # case 5: multiple packs with duplicate example names |
| 61 | + # Expect dict with multiple pack:tuple pairs |
| 62 | + "case5", |
| 63 | + { |
| 64 | + "packA": [ |
| 65 | + ("ex1", "case5/docs/examples/packA/ex1"), |
| 66 | + ], |
| 67 | + "packB": [ |
| 68 | + ("ex1", "case5/docs/examples/packB/ex1"), |
| 69 | + ], |
| 70 | + }, |
| 71 | + ), |
| 72 | +] |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("input,expected", example_params) |
| 76 | +def test_available_examples(input, expected, example_cases): |
| 77 | + case_dir = example_cases / input |
| 78 | + pkmg = PacksManager(case_dir) |
| 79 | + actual = pkmg.available_examples() |
| 80 | + assert actual.keys() == expected.keys() |
| 81 | + for pack in expected: |
| 82 | + assert paths_and_names_match( |
| 83 | + expected[pack], actual[pack], example_cases |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize("input,expected", example_params) |
| 88 | +def test_tmp_file_structure(input, expected, example_cases): |
| 89 | + example_path = example_cases / input |
| 90 | + for path in example_path.rglob("*"): |
| 91 | + if path.suffix: |
| 92 | + assert path.is_file() |
| 93 | + else: |
| 94 | + assert path.is_dir() |
0 commit comments