-
Notifications
You must be signed in to change notification settings - Fork 5
Test: test for copy_examples
#60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
eb85d97
918c559
6a26c9e
3ab2efb
678d0b9
15fd82a
d57e7fa
79fa679
a1fcf3b
fc1d0b0
a50c442
a04de5a
aa453b7
e75a339
f750453
49e745e
14fac59
909da70
e66eb5f
42553c1
ef792d2
4dc1538
5820dd3
84e54a8
6c38885
3405e17
b8e4336
1324185
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| **Added:** | ||
|
|
||
| * Add test for ``copy_examples`` function. | ||
|
|
||
| **Changed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Deprecated:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Removed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Fixed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Security:** | ||
|
|
||
| * <news item> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,14 +26,18 @@ | |
|
|
||
|
|
||
| def copy_examples( | ||
| examples_dict: Dict[str, List[Tuple[str, Path]]], target_dir: Path = None | ||
| examples_dict: Dict[str, List[Tuple[str, Path]]], | ||
| user_input: List[str], | ||
| target_dir: Path = None, | ||
| ) -> None: | ||
| """Copy an example into the the target or current working directory. | ||
| Parameters | ||
| ---------- | ||
| examples_dict : dict | ||
| Dictionary mapping pack name -> list of (example, path) tuples. | ||
| user_input : list of str | ||
| List of user-specified pack(s) or example(s) to copy. | ||
|
||
| target_dir : pathlib.Path, optional | ||
| Target directory to copy examples into. Defaults to current | ||
| working directory. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,63 +3,172 @@ | |
|
|
||
| import pytest | ||
|
|
||
| from diffpy.cmi import cli | ||
| from diffpy.cmi.cli import copy_examples | ||
| from diffpy.cmi.packsmanager import PacksManager | ||
|
|
||
| # Test copy_examples for various structural cases and copy scenarios. | ||
sbillinge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # In total, 5 structural cases x 6 copy scenarios + 2 path scenarios = 32 tests | ||
| # The copy scenarios are: | ||
| # a) copy to cwd | ||
| # b) copy to target dir | ||
|
|
||
| def test_map_pack_to_examples_structure(): | ||
| """Test that map_pack_to_examples returns the right shape of | ||
| data.""" | ||
| actual = cli.map_pack_to_examples() | ||
| assert isinstance(actual, dict) | ||
| for pack, exdirs in actual.items(): | ||
| assert isinstance(pack, str) | ||
| assert isinstance(exdirs, list) | ||
| for ex in exdirs: | ||
| assert isinstance(ex, str) | ||
| # Check for known packs | ||
| assert "core" in actual.keys() | ||
| assert "pdf" in actual.keys() | ||
| # Check for known examples | ||
| assert ["linefit"] in actual.values() | ||
| # 1) copy one example | ||
| # 2) copy list of examples from same pack | ||
| # 3) copy list of examples from different packs | ||
| # 4) copy all examples from a pack | ||
| # 5) copy all examples from list of packs | ||
| # 6) copy all examples from all packs | ||
|
|
||
| # Case params: List of tuples | ||
| # input: str - name of the test case directory | ||
| # user_inputs: List[List[str]] - Possible user inputs for the given case | ||
| # expected: List[List[Path]] - The expected paths that should be copied for | ||
| # each user input | ||
| case_params = [ | ||
| ( | ||
| "case1", # case with empty pack | ||
| [ | ||
| ["empty_pack"], # 4) all examples from a pack (but pack is empty) | ||
| ["all"], # 6) all examples from all packs (but pack is empty) | ||
| ], | ||
| [ | ||
| [], | ||
| [], | ||
| ], | ||
| ), | ||
| ( | ||
| "case2", # case with one pack with multiple examples | ||
| [ | ||
| ["ex1"], # 1) single example | ||
| ["ex1", "ex2"], # 2) multiple examples from same pack | ||
| ["full_pack"], # 4) all examples from a pack | ||
| ["all"], # 6) all examples from all packs | ||
| ], | ||
| [ | ||
| [Path("full_pack/ex1/solution/diffpy-cmi/script1.py")], | ||
sbillinge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| [ | ||
| Path("full_pack/ex1/solution/diffpy-cmi/script1.py"), | ||
| Path("full_pack/ex2/random/path/script1.py"), | ||
| Path("full_pack/ex2/random/path/script2.py"), | ||
| ], | ||
| [ | ||
| Path("full_pack/ex1/solution/diffpy-cmi/script1.py"), | ||
| Path("full_pack/ex2/random/path/script1.py"), | ||
| Path("full_pack/ex2/random/path/script2.py"), | ||
| ], | ||
| [ | ||
| Path("full_pack/ex1/solution/diffpy-cmi/script1.py"), | ||
| Path("full_pack/ex2/random/path/script1.py"), | ||
| Path("full_pack/ex2/random/path/script2.py"), | ||
| ], | ||
| ], | ||
| ), | ||
| ( | ||
| "case3", # case with multiple packs with multiple examples | ||
| [ | ||
| ["ex1"], # 1) single example from packA | ||
| ["ex1", "ex2"], # 2) list of examples from same pack | ||
| ["ex2", "ex3"], # 3) list of examples from different packs | ||
| ["packA"], # 4) all examples from a pack | ||
| ["packA", "packB"], # 5) all examples from multiple packs | ||
| ["all"], # 6) all examples from all packs | ||
| ], | ||
| [ | ||
| [Path("packA/ex1/script1.py")], | ||
| [ | ||
| Path("packA/ex1/script1.py"), | ||
| Path("packA/ex2/solutions/script2.py"), | ||
| ], | ||
| [ | ||
| Path("packA/ex2/solutions/script2.py"), | ||
| Path("packB/ex3/more/random/path/script3.py"), | ||
| Path("packB/ex3/more/random/path/script4.py"), | ||
| ], | ||
| [ | ||
| Path("packA/ex1/script1.py"), | ||
| Path("packA/ex2/solutions/script2.py"), | ||
| ], | ||
| [ | ||
| Path("packA/ex1/script1.py"), | ||
| Path("packA/ex2/solutions/script2.py"), | ||
| Path("packB/ex3/more/random/path/script3.py"), | ||
| Path("packB/ex3/more/random/path/script4.py"), | ||
| ], | ||
| [ | ||
| Path("packA/ex1/script1.py"), | ||
| Path("packA/ex2/solutions/script2.py"), | ||
| Path("packB/ex3/more/random/path/script3.py"), | ||
| Path("packB/ex3/more/random/path/script4.py"), | ||
| ], | ||
| ], | ||
| ), | ||
| ( | ||
| "case4", # case with no packs (empty examples directory) | ||
| [ | ||
| ["all"], # 6) all examples from all packs (but examples exist) | ||
| ], | ||
| [ | ||
| [], | ||
| ], | ||
| ), | ||
| ( | ||
| "case5", # case with multiple packs with same example names | ||
| [ | ||
| ["ex1"], # 1) single example (ambiguous, should get both) | ||
| [ | ||
| "ex1", | ||
| "ex1", | ||
| ], # 3) list of ex from diff packs (ambiguous, should get both) | ||
| ["packA"], # 4) all examples from a pack | ||
| ["packA", "packB"], # 5) all examples from a list of packs | ||
| ["all"], # 6) all examples from all packs | ||
| ], | ||
| [ | ||
| [ | ||
| Path("packA/ex1/path1/script1.py"), | ||
| Path("packB/ex1/path2/script2.py"), | ||
| ], | ||
| [ | ||
| Path("packA/ex1/path1/script1.py"), | ||
| Path("packB/ex1/path2/script2.py"), | ||
| ], | ||
| [Path("packA/ex1/path1/script1.py")], | ||
| [ | ||
| Path("packA/ex1/path1/script1.py"), | ||
| Path("packB/ex1/path2/script2.py"), | ||
| ], | ||
| [ | ||
| Path("packA/ex1/path1/script1.py"), | ||
| Path("packA/ex1/path2/script2.py"), | ||
| ], | ||
| ], | ||
| ), | ||
| ] | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_valid_str", | ||
| [ | ||
| "core/linefit", | ||
| "pdf/ch03NiModelling", | ||
| ], | ||
| ) | ||
| def test_copy_example_success(tmp_path, input_valid_str): | ||
| """Given a valid example format (<pack>/<ex>), test that its copied | ||
| to the temp dir.""" | ||
| os.chdir(tmp_path) | ||
| actual = cli.copy_example(input_valid_str) | ||
| expected = tmp_path / Path(input_valid_str).name | ||
| assert expected.exists() and expected.is_dir() | ||
| assert actual == expected | ||
|
|
||
|
|
||
| def test_copy_example_fnferror(): | ||
| """Test that FileNotFoundError is raised when the example does not | ||
| exist.""" | ||
| with pytest.raises(FileNotFoundError): | ||
| cli.copy_example("pack/example1") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_bad_str", | ||
| [ | ||
| "", # empty string | ||
| "/", # missing pack and example | ||
| "corelinefit", # missing slash | ||
| "linefit", # missing pack and slash | ||
| "core/", # missing example | ||
| "/linefit", # missing pack | ||
| "core/linefit/extra", # too many slashes | ||
| ], | ||
| ) | ||
| def test_copy_example_valueerror(input_bad_str): | ||
| """Test that ValueError is raised when the format is invalid.""" | ||
| with pytest.raises(ValueError): | ||
| cli.copy_example(input_bad_str) | ||
| @pytest.mark.parametrize("target", [None, "user_target"]) | ||
| @pytest.mark.parametrize("case,user_inputs,expected", case_params) | ||
| def test_copy_examples(case, user_inputs, expected, target, example_cases): | ||
| cwd = example_cases / "cwd" | ||
| os.chdir(cwd) | ||
sbillinge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case_dir = example_cases / case | ||
| pm = PacksManager(root_path=case_dir) | ||
| examples_dict = pm.available_examples() | ||
| if target is None: | ||
| target_dir = cwd | ||
| else: | ||
| target_dir = case_dir / "user_target" | ||
| for command in user_inputs: | ||
| copy_examples( | ||
| examples_dict, | ||
| user_input=command, | ||
| target_dir=None if target is None else target_dir, | ||
| ) | ||
| if expected: | ||
| for exp_paths in expected: | ||
| for path in exp_paths: | ||
| dest_file = target_dir / path | ||
| assert dest_file.exists() | ||
| else: | ||
| empty_dir = list(target_dir.rglob("*")) | ||
| assert not empty_dir, f"Expected nothing, but found: {empty_dir}" | ||
|
||
Uh oh!
There was an error while loading. Please reload this page.