-
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 18 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 |
|---|---|---|
| @@ -1,65 +0,0 @@ | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from diffpy.cmi import cli | ||
|
|
||
|
|
||
| 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() | ||
|
|
||
|
|
||
| @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) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from diffpy.cmi.packsmanager import PacksManager | ||
|
|
@@ -63,9 +66,12 @@ def paths_and_names_match(expected, actual, root): | |
| { | ||
| "packA": [ | ||
| ("ex1", "case5/docs/examples/packA/ex1"), | ||
| ("ex2", "case5/docs/examples/packA/ex2"), | ||
| ], | ||
| "packB": [ | ||
| ("ex1", "case5/docs/examples/packB/ex1"), | ||
| ("ex3", "case5/docs/examples/packB/ex3"), | ||
| ("ex4", "case5/docs/examples/packB/ex4"), | ||
| ], | ||
| }, | ||
| ), | ||
|
|
@@ -92,3 +98,189 @@ def test_tmp_file_structure(input, expected, example_cases): | |
| assert path.is_file() | ||
| else: | ||
| assert path.is_dir() | ||
|
|
||
|
|
||
| copy_params = [ | ||
| # Test various use cases to copy_examples on case5 | ||
| # 1) copy one example (ambiguous) | ||
| # 2) copy list of examples from same pack (ambiguous) | ||
| # 3) copy list of examples from different pack (ambiguous) | ||
| # 4) copy one example (unambiguous) | ||
| # 5) copy list of examples from same pack (unambiguous) | ||
| # 6) copy list of examples from different pack (unambiguous) | ||
| # 7) copy all examples from a pack | ||
| # 8) copy all examples from list of packs | ||
| # 9) copy all examples from all packs | ||
| ( # 1) copy one example, (ambiguous) | ||
| "ex1", | ||
sbillinge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| [ | ||
| Path("packA/ex1/path1/script1.py"), | ||
| Path("packB/ex1/path2/script2.py"), | ||
| ], | ||
| ), | ||
| ( # 2) copy list of examples from same pack (ambiguous) | ||
| ["ex1", "ex2"], | ||
| [ | ||
| Path("packA/ex1/path1/script1.py"), | ||
| Path("packB/ex1/path2/script2.py"), | ||
| Path("packA/ex2/script3.py"), | ||
| ], | ||
| ), | ||
| ( # 3) copy list of examples from different packs (ambiguous) | ||
| ["ex1", "ex1"], | ||
| [ | ||
| Path("packA/ex1/path1/script1.py"), | ||
| Path("packB/ex1/path2/script2.py"), | ||
| ], | ||
| ), | ||
| ( # 4) copy one example (unambiguous) | ||
| "ex2", | ||
| [ | ||
| Path("packA/ex2/script3.py"), | ||
| ], | ||
| ), | ||
| ( # 5) copy list of examples from same pack (unambiguous) | ||
| ["ex3", "ex4"], | ||
| [ | ||
| Path("packB/ex3/script4.py"), | ||
| Path("packB/ex4/script5.py"), | ||
| ], | ||
| ), | ||
| ( # 6) copy list of examples from different packs (unambiguous) | ||
| ["ex2", "ex3"], | ||
| [ | ||
| Path("packA/ex2/script3.py"), | ||
| Path("packB/ex3/script4.py"), | ||
| ], | ||
| ), | ||
| ( # 7) copy all examples from a pack | ||
| "packA", | ||
sbillinge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| [ | ||
| Path("packA/ex1/path1/script1.py"), | ||
| Path("packA/ex2/script3.py"), | ||
| ], | ||
| ), | ||
| ( # 8) copy all examples from list of packs | ||
| ["packA", "packB"], | ||
| [ | ||
| Path("packA/ex1/path1/script1.py"), | ||
| Path("packA/ex2/script3.py"), | ||
| Path("packB/ex1/path2/script2.py"), | ||
| Path("packB/ex3/script4.py"), | ||
| Path("packB/ex4/script5.py"), | ||
| ], | ||
| ), | ||
| ( # 9) copy all examples from all packs | ||
| "all", | ||
| [ | ||
| Path("packA/ex1/path1/script1.py"), | ||
| Path("packA/ex2/script3.py"), | ||
| Path("packB/ex1/path2/script2.py"), | ||
| Path("packB/ex3/script4.py"), | ||
| Path("packB/ex4/script5.py"), | ||
| ], | ||
| ), | ||
| ] | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| # input: list of str - cli input(s) to copy_examples | ||
| # expected_paths: list of Path - expected relative paths to copied examples | ||
| @pytest.mark.parametrize("input,expected_paths", copy_params) | ||
| def test_copy_examples(input, expected_paths, example_cases): | ||
| examples_dir = example_cases / "case5" | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pm = PacksManager(root_path=examples_dir) | ||
| target_dir = example_cases / "user_target" | ||
| actual = pm.copy_examples(user_input=input, target_dir=target_dir) | ||
sbillinge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| expected = [] | ||
| for path in expected_paths: | ||
| root_path = target_dir / path | ||
|
||
| expected.append(root_path) | ||
| assert actual == expected | ||
|
|
||
|
|
||
| # Test default and targeted copy_example location on case5 | ||
| # input: str or None - path arg to copy_examples | ||
| # expected: Path - expected relative path to copied example | ||
| @pytest.mark.parametrize( | ||
| "input,expected_path", | ||
| [ | ||
| (None, Path("cwd/packA/ex1/path1/script1.py")), | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ("user_target", Path("user_target/packA/ex1/path1/script1.py")), | ||
| ], | ||
| ) | ||
| def test_copy_examples_location(input, expected_path, example_cases): | ||
| examples_dir = example_cases / "case5" | ||
| os.chdir(example_cases / "cwd") | ||
| pm = PacksManager(root_path=examples_dir) | ||
| paths = pm.copy_examples(user_input="packA", target_dir=input) | ||
| actual = paths[0] | ||
| expected = example_cases / expected_path | ||
| assert actual == expected | ||
|
|
||
|
|
||
| # Test bad inputs to copy_examples on case3 | ||
| # These include: | ||
| # 1) input not found (example or pack) | ||
| # 2) mixed good and bad inputs | ||
| # 3) Path to directory already exists | ||
| @pytest.mark.parametrize( | ||
| "bad_inputs,expected,path", | ||
| [ | ||
| ( | ||
| "bad_example", | ||
| ValueError, | ||
|
||
| None, | ||
| ), # input not found (example or pack) | ||
sbillinge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ( | ||
| ["ex1", "bad_example"], | ||
| ValueError, | ||
| None, | ||
| ), # mixed good ex and bad inputs | ||
| ( | ||
| ["packA", "bad_example"], | ||
| ValueError, | ||
| None, | ||
| ), # mixed good pack and bad inputs | ||
| ( | ||
| "ex1", | ||
| FileExistsError, | ||
| Path("docs/examples/"), | ||
| ), # path to dir already exists | ||
| ], | ||
| ) | ||
| def test_copy_examples_bad(bad_inputs, expected, path, example_cases): | ||
| examples_dir = example_cases / "case3" | ||
| pm = PacksManager(root_path=examples_dir) | ||
| with pytest.raises(expected): | ||
| pm.copy_examples( | ||
| user_input=bad_inputs, | ||
| target_dir=examples_dir / path if path is not None else None, | ||
| ) | ||
|
|
||
|
|
||
| # Test bad target_dir to copy_examples on case3 | ||
| # 1) target doesn't exist | ||
| # 2) target is a file | ||
| # 3) target is nested in a file | ||
| @pytest.mark.parametrize( | ||
| "bad_inputs,expected", | ||
| [ | ||
| (Path("nonexistent/path/target"), FileNotFoundError), # doesn't exist | ||
|
||
| ( | ||
| Path("docs/examples/packA/ex1/script4.py"), | ||
| NotADirectoryError, | ||
| ), # target is a file | ||
| ( | ||
| Path("docs/examples/packA/ex1/script4.py/nested"), | ||
| NotADirectoryError, | ||
| ), # nested in file | ||
| ], | ||
| ) | ||
| def test_copy_examples_bad_target(bad_inputs, expected, example_cases): | ||
| examples_dir = example_cases / "case3" | ||
| pm = PacksManager(root_path=examples_dir) | ||
| with pytest.raises(expected): | ||
| pm.copy_examples( | ||
| user_input="packA", | ||
| target_dir=bad_inputs, | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.