Skip to content

Commit 128301d

Browse files
committed
Add tests for load_trajectory
1 parent b61f831 commit 128301d

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from click.testing import CliRunner
2+
from contextlib import contextmanager
3+
import pytest
4+
from importlib import resources
5+
from openpathsampling.tests.test_helpers import data_filename
6+
import openpathsampling as paths
7+
8+
from paths_cli.commands.load_trajectory import *
9+
10+
11+
@contextmanager
12+
def run_load_trajectory(args):
13+
runner = CliRunner()
14+
with runner.isolated_filesystem():
15+
storage = paths.Storage("setup.nc", 'w')
16+
storage.close()
17+
results = runner.invoke(
18+
load_trajectory,
19+
args
20+
)
21+
assert results.exit_code == 0
22+
st = paths.Storage("setup.nc", mode='r')
23+
assert len(st.trajectories) == 1
24+
yield st
25+
26+
27+
@pytest.mark.parametrize("with_top", [True, False])
28+
@pytest.mark.parametrize("with_tag", [True, False])
29+
def test_load_trajectory_pdb(with_top, with_tag):
30+
# test that we can load a PDB file with or without topology; also tests
31+
# that the taging works correctly
32+
pdb_path = data_filename("ala_small_traj.pdb")
33+
out_file = "setup.nc"
34+
args = [
35+
pdb_path,
36+
'--append-file', out_file,
37+
]
38+
if with_top:
39+
args.extend(['--top', pdb_path])
40+
41+
if with_tag:
42+
args.extend(['--tag', 'init_snap'])
43+
44+
with run_load_trajectory(args) as st:
45+
traj = st.trajectories[0]
46+
assert len(traj) == 10
47+
if with_tag:
48+
tagged = st.tags['init_snap']
49+
assert tagged == traj
50+
51+
def test_load_trajectory_trr():
52+
trr = data_filename("gromacs_engine/project_trr/0000000.trr")
53+
gro = data_filename("gromacs_engine/conf.gro")
54+
out_file = "setup.nc"
55+
args = [
56+
trr,
57+
'--append-file', out_file,
58+
'--top', gro,
59+
]
60+
with run_load_trajectory(args) as st:
61+
traj = st.trajectories[0]
62+
assert len(traj) == 4
63+
64+
def test_load_trajectory_bad_topology():
65+
trr = data_filename("gromacs_engine/project_trr/0000000.trr")
66+
pdb = data_filename("tip4p_water.pdb")
67+
out_file = "setup.nc"
68+
args = [
69+
trr,
70+
'--append-file', out_file,
71+
'--top', pdb,
72+
]
73+
runner = CliRunner()
74+
with runner.isolated_filesystem():
75+
storage = paths.Storage("setup.nc", 'w')
76+
storage.close()
77+
result = runner.invoke(
78+
load_trajectory,
79+
args
80+
)
81+
assert result.exit_code == 1
82+
assert "topology" in str(result.exception)
83+
assert "same atoms" in str(result.exception)

0 commit comments

Comments
 (0)