Skip to content

Commit 9d6c3dd

Browse files
committed
Add tests for shooting, load_from_ops
1 parent 13f7108 commit 9d6c3dd

3 files changed

Lines changed: 168 additions & 1 deletion

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import pytest
2+
import mock
3+
4+
import openpathsampling as paths
5+
6+
from paths_cli.tests.wizard.mock_wizard import mock_wizard
7+
8+
from paths_cli.wizard.load_from_ops import (
9+
named_objs_helper, _get_ops_storage, _get_ops_object, load_from_ops
10+
)
11+
12+
# for some reason I couldn't get these to work with MagicMock
13+
class NamedObj:
14+
def __init__(self, name):
15+
self.name = name
16+
self.is_named = True
17+
18+
class FakeStore:
19+
def __init__(self, objects):
20+
self._objects = objects
21+
self._named_objects = {obj.name: obj for obj in objects}
22+
23+
def __getitem__(self, key):
24+
if isinstance(key, int):
25+
return self._objects[key]
26+
elif isinstance(key, str):
27+
return self._named_objects[key]
28+
else:
29+
raise TypeError("Huh?")
30+
31+
def __iter__(self):
32+
return iter(self._objects)
33+
34+
class FakeStorage:
35+
def __init__(self, foo):
36+
self.foo = foo
37+
38+
@pytest.fixture
39+
def ops_file_fixture():
40+
# store name 'foo', objects named 'bar', 'baz'
41+
bar = NamedObj('bar')
42+
baz = NamedObj('baz')
43+
foo = FakeStore([bar, baz])
44+
storage = FakeStorage(foo)
45+
return storage
46+
47+
def test_named_objs_helper(ops_file_fixture):
48+
wizard = mock_wizard([])
49+
helper_func = named_objs_helper(ops_file_fixture, 'foo')
50+
helper_func(wizard, 'any')
51+
assert "what I found" in wizard.console.log_text
52+
assert "bar" in wizard.console.log_text
53+
assert "baz" in wizard.console.log_text
54+
55+
@pytest.mark.parametrize('with_failure', [False, True])
56+
def test_get_ops_storage(tmpdir, with_failure):
57+
fake_file = tmpdir / 'foo.db'
58+
fake_file.write_text('bar', 'utf-8')
59+
60+
failure_text = ['baz.db'] if with_failure else []
61+
wizard = mock_wizard(failure_text + [str(fake_file)])
62+
63+
with mock.patch('paths_cli.wizard.load_from_ops.INPUT_FILE',
64+
new=mock.Mock(get=open)):
65+
storage = _get_ops_storage(wizard)
66+
assert storage.read() == 'bar'
67+
if with_failure:
68+
assert 'something went wrong' in wizard.console.log_text
69+
else:
70+
assert 'something went wrong' not in wizard.console.log_text
71+
72+
@pytest.mark.parametrize('with_failure', [False, True])
73+
def test_get_ops_object(ops_file_fixture, with_failure):
74+
failure_text = ['qux'] if with_failure else []
75+
wizard = mock_wizard(failure_text + ['bar'])
76+
obj = _get_ops_object(wizard, ops_file_fixture,
77+
store_name='foo',
78+
obj_name='FOOMAGIC')
79+
assert isinstance(obj, NamedObj)
80+
assert obj.name == 'bar'
81+
log = wizard.console.log_text
82+
assert 'name of the FOOMAGIC' in log
83+
fail_msg = 'Something went wrong'
84+
if with_failure:
85+
assert fail_msg in log
86+
else:
87+
assert fail_msg not in log
88+
89+
def test_load_from_ops(ops_file_fixture):
90+
wizard = mock_wizard(['anyfile.db', 'bar'])
91+
with mock.patch('paths_cli.wizard.load_from_ops.INPUT_FILE.get',
92+
mock.Mock(return_value=ops_file_fixture)):
93+
obj = load_from_ops(wizard, 'foo', 'FOOMAGIC')
94+
95+
assert isinstance(obj, NamedObj)
96+
assert obj.name == 'bar'
97+
98+
pass
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import pytest
2+
import mock
3+
from functools import partial
4+
5+
from paths_cli.tests.wizard.mock_wizard import mock_wizard
6+
7+
from paths_cli.wizard.shooting import (
8+
# selectors
9+
uniform_selector, gaussian_selector, _get_selector, SHOOTING_SELECTORS,
10+
# shooting algorithms
11+
one_way_shooting, spring_shooting,
12+
# main func
13+
shooting, SHOOTING_TYPES
14+
)
15+
16+
import openpathsampling as paths
17+
from openpathsampling.experimental.storage.collective_variables import \
18+
CoordinateFunctionCV
19+
20+
def test_uniform_selector():
21+
wizard = mock_wizard([])
22+
sel = uniform_selector(wizard)
23+
assert isinstance(sel, paths.UniformSelector)
24+
assert wizard.console.input_call_count == 0
25+
26+
def test_gaussian_selector():
27+
wizard = mock_wizard(['x', '1.0', '0.5'])
28+
cv = CoordinateFunctionCV(lambda s: s.xyz[0][0]).named('x')
29+
wizard.cvs[cv.name] = cv
30+
sel = gaussian_selector(wizard)
31+
assert isinstance(sel, paths.GaussianBiasSelector)
32+
assert sel.alpha == 2.0
33+
assert sel.l_0 == 1.0
34+
35+
def test_get_selector():
36+
wizard = mock_wizard(['Uniform random'])
37+
sel = _get_selector(wizard)
38+
assert isinstance(sel, paths.UniformSelector)
39+
40+
def test_one_way_shooting(toy_engine):
41+
wizard = mock_wizard(['Uniform random'])
42+
wizard.engines[toy_engine.name] = toy_engine
43+
strategy = one_way_shooting(wizard)
44+
assert isinstance(strategy, paths.strategies.OneWayShootingStrategy)
45+
assert isinstance(strategy.selector, paths.UniformSelector)
46+
assert strategy.engine == toy_engine
47+
48+
def test_spring_shooting(toy_engine):
49+
wizard = mock_wizard(['10', '0.25'])
50+
wizard.engines[toy_engine.name] = toy_engine
51+
strategy = spring_shooting(wizard)
52+
assert isinstance(strategy, partial)
53+
assert strategy.func == paths.SpringShootingMoveScheme
54+
assert strategy.keywords['delta_max'] == 10
55+
assert strategy.keywords['k_spring'] == 0.25
56+
assert strategy.keywords['engine'] == toy_engine
57+
58+
@pytest.mark.parametrize('shooting_types', [{}, None])
59+
def test_shooting(toy_engine, shooting_types):
60+
key = 'One-way (stochastic) shooting'
61+
wizard = mock_wizard([key])
62+
wizard.engines[toy_engine.name] = toy_engine
63+
foo = mock.Mock(return_value='foo')
64+
if shooting_types == {}:
65+
shooting_types = {key: foo}
66+
with mock.patch.dict(SHOOTING_TYPES, {key: foo}):
67+
assert shooting(wizard, shooting_types=shooting_types) == 'foo'

‎paths_cli/wizard/load_from_ops.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
LABEL = "Load existing from OPS file"
21
from paths_cli.parameters import INPUT_FILE
32
from paths_cli.wizard.core import get_object
43

4+
from paths_cli.wizard.errors import FILE_LOADING_ERROR_MSG
5+
LABEL = "Load existing from OPS file"
6+
57
def named_objs_helper(storage, store_name):
68
def list_items(wizard, user_input):
79
store = getattr(storage, store_name)

0 commit comments

Comments
 (0)