Skip to content

Commit 9c687f9

Browse files
committed
tests for append
1 parent a4ad5c9 commit 9c687f9

File tree

3 files changed

+108
-12
lines changed

3 files changed

+108
-12
lines changed

paths_cli/cli.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ def format_commands(self, ctx, formatter):
118118
openpathsampling strip-snapshots --help
119119
"""
120120

121+
OPS_CLI = OpenPathSamplingCLI(
122+
name="openpathsampling",
123+
help=_MAIN_HELP,
124+
context_settings=CONTEXT_SETTINGS
125+
)
121126

122127
def main(): # no-cov
123-
cli = OpenPathSamplingCLI(
124-
name="openpathsampling",
125-
help=_MAIN_HELP,
126-
context_settings=CONTEXT_SETTINGS
127-
)
128-
cli()
128+
OPS_CLI()
129129

130130

131131
if __name__ == '__main__': # no-cov

paths_cli/commands/append.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
help=("save object to a tag; requires that only one "
2121
+ "object be specfied. Can also be used to rename "
2222
+ "tagged objects. To append a tagged object without "
23-
+ "a tag, use --add-tag \"\""))
23+
+ "a tag, use --save-tag \"\""))
2424
def append(input_file, append_file, engine, cv, volume, network, scheme,
2525
tag, save_tag):
2626
"""Append objects from INPUT_FILE to another file.
@@ -42,17 +42,14 @@ def append(input_file, append_file, engine, cv, volume, network, scheme,
4242
for obj in to_save:
4343
output_storage.save(obj)
4444

45-
if tag and save_tag is None:
46-
save_tag = tag
45+
if tag and len(tag) == 1 and save_tag is None:
46+
save_tag = tag[0]
4747

4848
if save_tag:
4949
output_storage.tags[save_tag] = to_save[0]
5050

5151
# TO TEST
52-
# 1. append a tag in a new file: its tag comes with it
53-
# 2. rename a tagged object
5452
# 3. "untag" an object by not associating a tag in the new storage
55-
# 4. tag a previously untagged object
5653

5754
output_storage.close()
5855

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)