|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | +from click.testing import CliRunner |
| 5 | + |
| 6 | +from paths_cli.commands.append import * |
| 7 | + |
| 8 | +import openpathsampling as paths |
| 9 | + |
| 10 | +def make_input_file(tps_network_and_traj): |
| 11 | + input_file = paths.Storage("setup.py", mode='w') |
| 12 | + for obj in tps_network_and_traj: |
| 13 | + input_file.save(obj) |
| 14 | + |
| 15 | + input_file.tags['template'] = input_file.snapshots[0] |
| 16 | + input_file.close() |
| 17 | + return "setup.py" |
| 18 | + |
| 19 | +def test_append(tps_network_and_traj): |
| 20 | + runner = CliRunner() |
| 21 | + with runner.isolated_filesystem(): |
| 22 | + in_file = make_input_file(tps_network_and_traj) |
| 23 | + result = runner.invoke(append, [in_file, '-a', 'output.nc', |
| 24 | + '--volume', 'A', '--volume', 'B']) |
| 25 | + assert result.exit_code == 0 |
| 26 | + assert result.exception is None |
| 27 | + storage = paths.Storage('output.nc', mode='r') |
| 28 | + assert len(storage.volumes) == 2 |
| 29 | + assert len(storage.snapshots) == 0 |
| 30 | + storage.volumes['A'] # smoke tests that we can load |
| 31 | + storage.volumes['B'] |
| 32 | + storage.close() |
| 33 | + |
| 34 | + result = runner.invoke(append, [in_file, '-a', 'output.nc', |
| 35 | + '--tag', 'template']) |
| 36 | + storage = paths.Storage('output.nc', mode='r') |
| 37 | + assert len(storage.volumes) == 2 |
| 38 | + assert len(storage.snapshots) == 2 # one snapshot + reverse |
| 39 | + |
| 40 | +@pytest.mark.parametrize('n_objects', [0, 2]) |
| 41 | +def test_append_tag_error(tps_network_and_traj, n_objects): |
| 42 | + objs = {2: ['--volume', "A", '--volume', "B"], 0: []}[n_objects] |
| 43 | + runner = CliRunner() |
| 44 | + with runner.isolated_filesystem(): |
| 45 | + in_file = make_input_file(tps_network_and_traj) |
| 46 | + result = runner.invoke(append, |
| 47 | + [in_file, '-a', "output.nc"] + objs |
| 48 | + + ["--save-tag", "foo"]) |
| 49 | + assert isinstance(result.exception, RuntimeError) |
| 50 | + assert "Can't identify the object to tag" in str(result.exception) |
| 51 | + |
| 52 | +def test_append_tag(tps_network_and_traj): |
| 53 | + runner = CliRunner() |
| 54 | + with runner.isolated_filesystem(): |
| 55 | + in_file = make_input_file(tps_network_and_traj) |
| 56 | + result = runner.invoke(append, |
| 57 | + [in_file, '-a', "output.nc", |
| 58 | + '--tag', 'template', '--save-tag', 'foo']) |
| 59 | + assert result.exit_code == 0 |
| 60 | + assert result.exception is None |
| 61 | + |
| 62 | + storage = paths.Storage("output.nc", mode='r') |
| 63 | + assert len(storage.snapshots) == 2 |
| 64 | + assert len(storage.tags) == 1 |
| 65 | + assert storage.tags['foo'] is not None |
| 66 | + storage.close() |
| 67 | + |
| 68 | +def test_append_same_tag(tps_network_and_traj): |
| 69 | + runner = CliRunner() |
| 70 | + with runner.isolated_filesystem(): |
| 71 | + in_file = make_input_file(tps_network_and_traj) |
| 72 | + result = runner.invoke(append, |
| 73 | + [in_file, '-a', "output.nc", |
| 74 | + '--tag', 'template']) |
| 75 | + assert result.exit_code == 0 |
| 76 | + assert result.exception is None |
| 77 | + |
| 78 | + storage = paths.Storage("output.nc", mode='r') |
| 79 | + assert len(storage.snapshots) == 2 |
| 80 | + assert len(storage.tags) == 1 |
| 81 | + assert storage.tags['template'] is not None |
| 82 | + storage.close() |
| 83 | + |
| 84 | +def test_append_remove_tag(tps_network_and_traj): |
| 85 | + runner = CliRunner() |
| 86 | + with runner.isolated_filesystem(): |
| 87 | + in_file = make_input_file(tps_network_and_traj) |
| 88 | + result = runner.invoke(append, |
| 89 | + [in_file, '-a', "output.nc", |
| 90 | + "--tag", 'template', '--save-tag', '']) |
| 91 | + print(result.output) |
| 92 | + assert result.exception is None |
| 93 | + assert result.exit_code == 0 |
| 94 | + |
| 95 | + storage = paths.Storage("output.nc", mode='r') |
| 96 | + assert len(storage.snapshots) == 2 |
| 97 | + assert len(storage.tags) == 0 |
| 98 | + storage.close() |
| 99 | + |
0 commit comments