-
Couldn't load subscription status.
- Fork 5
feat: build examples dict for cli commands #53
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 all commits
54974ba
c9cf238
f9a14c3
71423d5
a1ee907
b16c90b
15c8111
db2ac9a
9fcef71
c70ca27
d32201a
fbbf88c
a458c5e
736b519
0bd26fc
4be38f6
280390c
99c4951
aa65d02
4aa15b0
cfb2273
fdf09b2
53ac9a2
ce1efa7
c39dfae
c2e8076
d1038b7
85bb747
0d08dbe
fcbb1a2
52aa4e7
e8c8ee5
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 |
|---|---|---|
|
|
@@ -22,6 +22,84 @@ def tmp_examples(tmp_path_factory): | |
| yield tmp_examples | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def example_cases(tmp_path_factory): | ||
| """Copy the entire examples tree into a temp directory once per test | ||
| session. | ||
|
|
||
| Returns the path to that copy. | ||
| """ | ||
| root_temp_dir = tmp_path_factory.mktemp("temp") | ||
|
|
||
| # case 1: pack with no examples | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the tree structure here: #53 (comment). This is what the temp dir looks like. let me know if this looks okay. I believe everything is being correctly and there is no magic |
||
| case1ex_dir = root_temp_dir / "case1" / "docs" / "examples" | ||
| case1 = case1ex_dir / "empty_pack" # empty_pack | ||
| case1.mkdir(parents=True, exist_ok=True) | ||
| case1req_dir = root_temp_dir / "case1" / "requirements" / "packs" | ||
| case1req_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # Case 2: pack with multiple examples | ||
| case2ex_dir = root_temp_dir / "case2" / "docs" / "examples" | ||
| case2a = ( | ||
| case2ex_dir / "full_pack" / "ex1" / "solution" / "diffpy-cmi" | ||
| ) # full_pack, ex1 | ||
| case2a.mkdir(parents=True, exist_ok=True) | ||
| (case2a / "script1.py").touch() | ||
| case2b = ( | ||
| case2ex_dir / "full_pack" / "ex2" / "random" / "path" | ||
| ) # full_pack, ex2 | ||
| case2b.mkdir(parents=True, exist_ok=True) | ||
| (case2b / "script1.py").touch() | ||
| (case2b / "script2.py").touch() | ||
| case2req_dir = root_temp_dir / "case2" / "requirements" / "packs" | ||
| case2req_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # Case 3: multiple packs with multiple examples | ||
| case3ex_dir = root_temp_dir / "case3" / "docs" / "examples" | ||
| case3a = case3ex_dir / "packA" / "ex1" # packA, ex1 | ||
| case3a.mkdir(parents=True, exist_ok=True) | ||
| (case3a / "script1.py").touch() | ||
| case3b = case3ex_dir / "packA" / "ex2" / "solutions" # packA, ex2 | ||
| case3b.mkdir(parents=True, exist_ok=True) | ||
| (case3b / "script2.py").touch() | ||
| case3c = ( | ||
| case3ex_dir / "packB" / "ex3" / "more" / "random" / "path" | ||
| ) # packB, ex3 | ||
| case3c.mkdir(parents=True, exist_ok=True) | ||
| (case3c / "script3.py").touch() | ||
| (case3c / "script4.py").touch() | ||
| case3req_dir = root_temp_dir / "case3" / "requirements" / "packs" | ||
| case3req_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # Case 4: no pack found (empty directory) | ||
| case4ex_dir = root_temp_dir / "case4" / "docs" / "examples" | ||
| case4 = case4ex_dir | ||
| case4.mkdir(parents=True, exist_ok=True) | ||
| case4req_dir = root_temp_dir / "case4" / "requirements" / "packs" | ||
| case4req_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # Case 5: multiple packs with the same example names | ||
| case5ex_dir = root_temp_dir / "case5" / "docs" / "examples" | ||
| case5a = case5ex_dir / "packA" / "ex1" / "path1" # packA, ex1 | ||
| case5a.mkdir(parents=True, exist_ok=True) | ||
| (case5a / "script1.py").touch() | ||
| case5b = case5ex_dir / "packB" / "ex1" / "path2" # packB, ex1 | ||
| case5b.mkdir(parents=True, exist_ok=True) | ||
| (case5b / "script2.py").touch() | ||
| case5req_dir = root_temp_dir / "case5" / "requirements" / "packs" | ||
| case5req_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| yield root_temp_dir | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def target_dir(tmp_path_factory): | ||
cadenmyers13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Create a temporary directory to serve as the target for copying | ||
| examples.""" | ||
| target_directory = tmp_path_factory.mktemp("copy_target") | ||
| yield target_directory | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session", autouse=True) | ||
| def use_headless_matplotlib(): | ||
| """Force matplotlib to use a headless backend during tests.""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,60 @@ | ||
| import os | ||
| from pathlib import Path | ||
| from shutil import copytree | ||
|
|
||
| import pytest | ||
|
|
||
| from diffpy.cmi import cli | ||
| from diffpy.cmi.packsmanager import PacksManager | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input,to_cwd,expected", | ||
| [ | ||
| # PARAMS: | ||
| # input: list - list of example(s) and/or pack(s) to copy | ||
| # to_cwd: bool - whether to copy to cwd (default) or a target dir | ||
| # expected: list - path of copied example(s) and/or pack(s) | ||
| # 1a) user wants to copy one example to cwd | ||
| # 1b) user wants to copy one example to a target dir | ||
| (), | ||
| # 2a) user wants to copy multiple examples to cwd | ||
| # 2b) user wants to copy multiple examples to a target dir | ||
| (), | ||
| # 3a) user wants to copy all examples from a pack to cwd | ||
| # 3b) user wants to copy all examples from a pack to a target dir | ||
| (), | ||
| # 4a) user wants to copy all examples from multiple packs to cwd | ||
| # 4b) user wants to copy all examples from multiple packs to target dir | ||
| (), | ||
| # 5a) user wants to copy a combination of packs and examples to cwd | ||
| # 5b) user wants to copy a combination of packs and examples to target | ||
| (), | ||
| # 6a) user wants to copy all examples from all packs to cwd | ||
| # 6b) user wants to copy all examples from all packs to a target dir | ||
| (), | ||
| ], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sbillinge ready for review here^^^^. Some use cases I jotted down. Let me know what you think. |
||
| ) | ||
| def test_copy_examples(input, to_cwd, expected, example_cases, target_dir): | ||
| tmp_ex_dir = example_cases / input | ||
| copytree(tmp_ex_dir, target_dir) | ||
| # pkmg = PacksManager() | ||
| # actual = cli.copy_examples(str(target_dir)) | ||
| assert False | ||
|
|
||
|
|
||
| def test_print_info(temp_path, capsys): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. heads up, I am, not reviewing these till we have the dict building done. |
||
| pkmg = PacksManager() | ||
| actual = pkmg.available_examples(temp_path) | ||
| # pretty print the actual dict | ||
| pkmg.print_info(actual) | ||
| captured = capsys.readouterr() | ||
| output = captured.out.strip() | ||
| # check that output contains expected headers | ||
| assert "Available packs" in output or "Installed packs" in output | ||
|
|
||
|
|
||
| # NOTE: double check and remove these test after new above tests are made | ||
| def test_map_pack_to_examples_structure(): | ||
| """Test that map_pack_to_examples returns the right shape of | ||
| data.""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import pytest | ||
|
|
||
| from diffpy.cmi.packsmanager import PacksManager | ||
|
|
||
| example_params = [ | ||
| # 1) pack with no examples. Expect {'empty_pack': []} | ||
| # 2) pack with multiple examples. | ||
| # Expect {'full_pack': [('example1`, path_to_1'), 'example2', path_to_2)] | ||
| # 3) multiple packs. Expect dict with multiple pack:tuple pairs | ||
| # 4) no pack found. Expect {} | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we leave these here, we need to add case 5 here also. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| # case 1: pack with no examples. Expect {'empty_pack': []} | ||
| # 5) multiple packs with the same example names | ||
| # Expect dict with multiple pack:tuple pairs | ||
| ( | ||
| "case1", | ||
| {"empty_pack": []}, | ||
| ), | ||
| # case 2: pack with multiple examples. | ||
| # Expect {'full_pack': [('example1', path_to_1), | ||
| # ('example2', path_to_2)]} | ||
| ( | ||
| "case2", | ||
| { | ||
| "full_pack": [ | ||
| ("ex1", "case2/docs/examples/full_pack/ex1"), | ||
| ("ex2", "case2/docs/examples/full_pack/ex2"), | ||
| ] | ||
| }, | ||
| ), | ||
| # case 3: multiple packs. Expect dict with multiple pack:tuple pairs | ||
| ( | ||
| "case3", | ||
| { | ||
| "packA": [ | ||
| ("ex1", "case3/docs/examples/packA/ex1"), | ||
| ("ex2", "case3/docs/examples/packA/ex2"), | ||
| ], | ||
| "packB": [("ex3", "case3/docs/examples/packB/ex3")], | ||
| }, | ||
| ), | ||
| ( # case 4: no pack found. Expect {} | ||
| "case4", | ||
| {}, | ||
| ), | ||
| ( # case 5: multiple packs with duplicate example names | ||
| # Expect dict with multiple pack:tuple pairs | ||
| "case5", | ||
| { | ||
| "packA": [ | ||
| ("ex1", "case5/docs/examples/packA/ex1"), | ||
| ], | ||
| "packB": [ | ||
| ("ex1", "case5/docs/examples/packB/ex1"), | ||
| ], | ||
| }, | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| def paths_and_names_match(expected, actual, root): | ||
| """Compare two (example_name, path) lists ignoring temp dir | ||
| differences.""" | ||
| if len(expected) != len(actual): | ||
| return False | ||
| for (exp_name, exp_path), (act_name, act_path) in zip(expected, actual): | ||
| if exp_name != act_name: | ||
| return False | ||
| actual_rel_path = str(act_path.relative_to(root)) | ||
| if actual_rel_path != exp_path: | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("input,expected", example_params) | ||
| def test_available_examples(input, expected, example_cases): | ||
| case_dir = example_cases / input | ||
| pkmg = PacksManager(case_dir) | ||
| actual = pkmg.available_examples() | ||
| assert actual.keys() == expected.keys() | ||
| for pack in expected: | ||
| assert paths_and_names_match( | ||
| expected[pack], actual[pack], example_cases | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("input,expected", example_params) | ||
| def test_tmp_file_structure(input, expected, example_cases): | ||
| example_path = example_cases / input | ||
| for path in example_path.rglob("*"): | ||
| if path.suffix: | ||
| assert path.is_file() | ||
| else: | ||
| assert path.is_dir() | ||
Uh oh!
There was an error while loading. Please reload this page.