Skip to content

Commit 68066b1

Browse files
committed
tests for most of the Wizard object
1 parent 0b60338 commit 68066b1

4 files changed

Lines changed: 274 additions & 23 deletions

File tree

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
import pytest
2+
import mock
3+
from paths_cli.tests.wizard.mock_wizard import (
4+
make_mock_wizard, make_mock_retry_wizard
5+
)
6+
import pathlib
7+
8+
from openpathsampling.experimental.storage.collective_variables import \
9+
CoordinateFunctionCV
10+
11+
from paths_cli.wizard.wizard import *
12+
13+
class MockConsole:
14+
def __init__(self, inputs=None):
15+
if isinstance(inputs, str):
16+
inputs = [inputs]
17+
elif inputs is None:
18+
inputs = []
19+
self.inputs = inputs
20+
self._input_iter = iter(inputs)
21+
self.log = []
22+
self.width = 80
23+
self.input_call_count = 0
24+
25+
def print(self, content=""):
26+
self.log.append(content)
27+
28+
def input(self, content):
29+
self.input_call_count += 1
30+
user_input = next(self._input_iter)
31+
self.log.append(content + " " + user_input)
32+
return user_input
33+
34+
@property
35+
def log_text(self):
36+
return "\n".join(self.log)
37+
38+
39+
class TestWizard:
40+
def setup(self):
41+
self.wizard = Wizard([])
42+
43+
def test_initialization(self):
44+
wizard = Wizard([SINGLE_ENGINE_STEP])
45+
assert wizard.requirements == {'engine': ('engines', 1, 1)}
46+
47+
def test_ask(self):
48+
console = MockConsole('foo')
49+
self.wizard.console = console
50+
result = self.wizard.ask('bar')
51+
assert result == 'foo'
52+
assert 'bar' in console.log_text
53+
54+
def test_ask_help(self):
55+
pass
56+
57+
def _generic_speak_test(self, func_name):
58+
console = MockConsole()
59+
self.wizard.console = console
60+
func = getattr(self.wizard, func_name)
61+
func('foo')
62+
assert 'foo' in console.log_text
63+
64+
def test_say(self):
65+
self._generic_speak_test('say')
66+
67+
def test_start(self):
68+
self._generic_speak_test('start')
69+
70+
def test_bad_input(self):
71+
self._generic_speak_test('bad_input')
72+
73+
@pytest.mark.parametrize('bad_choice', [False, True])
74+
def test_ask_enumerate(self, bad_choice):
75+
inputs = {False: '1', True: ['10', '1']}[bad_choice]
76+
console = MockConsole(inputs)
77+
self.wizard.console = console
78+
selected = self.wizard.ask_enumerate('foo', options=['bar', 'baz'])
79+
assert selected == 'bar'
80+
assert 'foo' in console.log_text
81+
assert '1. bar' in console.log_text
82+
assert '2. baz' in console.log_text
83+
assert console.input_call_count == len(inputs)
84+
if bad_choice:
85+
assert "'10'" in console.log_text
86+
assert 'not a valid option' in console.log_text
87+
else:
88+
assert "'10'" not in console.log_text
89+
assert 'not a valid option' not in console.log_text
90+
91+
@pytest.mark.parametrize('inputs, type_, expected', [
92+
("2+2", int, 4), ("0.1 * 0.1", float, 0.1*0.1), ("2.4", int, 2)
93+
])
94+
def test_ask_custom_eval(self, inputs, type_, expected):
95+
console = MockConsole(inputs)
96+
self.wizard.console = console
97+
result = self.wizard.ask_custom_eval('foo', type_=type_)
98+
assert result == expected
99+
assert console.input_call_count == 1
100+
assert 'foo' in console.log_text
101+
102+
def test_ask_custom_eval_bad_type(self):
103+
console = MockConsole(['"bar"', '2+2'])
104+
self.wizard.console = console
105+
result = self.wizard.ask_custom_eval('foo')
106+
assert result == 4
107+
assert console.input_call_count == 2
108+
assert "I couldn't understand" in console.log_text
109+
assert "ValueError" in console.log_text
110+
111+
def test_ask_custom_eval_bad_input(self):
112+
console = MockConsole(['bar', '2+2'])
113+
self.wizard.console = console
114+
result = self.wizard.ask_custom_eval('foo')
115+
assert result == 4
116+
assert console.input_call_count == 2
117+
assert "I couldn't understand" in console.log_text
118+
assert "NameError" in console.log_text
119+
120+
@pytest.mark.parametrize('inputs,expected', [('1', 'bar'),
121+
('2', 'new')])
122+
def test_obj_selector(self, inputs, expected):
123+
create_func = lambda wiz: 'new'
124+
console = MockConsole(inputs)
125+
self.wizard.console = console
126+
self.wizard.cvs = {'foo': 'bar'}
127+
def mock_register(obj, name, store):
128+
console.print("registered")
129+
return obj
130+
self.wizard.register = mock_register
131+
sel = self.wizard.obj_selector('cvs', "CV", create_func)
132+
assert sel == expected
133+
assert "CV" in console.log_text
134+
if inputs == '2':
135+
assert "registered" in console.log_text
136+
137+
def test_exception(self):
138+
console = MockConsole()
139+
self.wizard.console = console
140+
err = RuntimeError("baz")
141+
self.wizard.exception('foo', err)
142+
assert 'foo' in console.log_text
143+
assert "RuntimeError: baz" in console.log_text
144+
145+
def test_name(self):
146+
# why does that exist? cf core.name
147+
# actually, it looks like we never use core.name; remove that and
148+
# move its test here?
149+
pass
150+
151+
@pytest.mark.parametrize('named', [True, False])
152+
def test_register(self, named):
153+
cv = CoordinateFunctionCV(lambda s: s.xyz[0][0])
154+
if named:
155+
cv = cv.named('foo')
156+
do_naming = lambda obj, obj_type, store_name: cv.named('foo')
157+
assert cv.is_named == named
158+
self.wizard.name = mock.Mock(side_effect=do_naming)
159+
assert len(self.wizard.cvs) == 0
160+
self.wizard.register(cv, 'CV', 'cvs')
161+
assert len(self.wizard.cvs) == 1
162+
assert cv.name == 'foo'
163+
164+
def _get_storage(self, inputs, expected):
165+
console = MockConsole(inputs)
166+
self.wizard.console = console
167+
storage = self.wizard.get_storage()
168+
assert storage.backend.filename == expected
169+
assert console.input_call_count == len(inputs)
170+
171+
@pytest.mark.parametrize('fnames', [(['setup.db']),
172+
(['setup.nc', 'setup.db'])])
173+
def test_get_storage(self, tmpdir, fnames):
174+
inputs = [os.path.join(tmpdir, inp) for inp in fnames]
175+
self._get_storage(inputs, inputs[-1])
176+
177+
@pytest.mark.parametrize('overwrite', [True, False])
178+
def test_get_storage_exists(self, overwrite, tmpdir):
179+
inputs = [os.path.join(tmpdir, 'setup.db'),
180+
{True: 'y', False: 'n'}[overwrite]]
181+
if not overwrite:
182+
inputs.append(os.path.join(tmpdir, 'setup2.db'))
183+
184+
pathlib.Path(inputs[0]).touch()
185+
expected = 'setup.db' if overwrite else 'setup2.db'
186+
self._get_storage(inputs, os.path.join(tmpdir, expected))
187+
188+
def test_get_storage_file_error(self, tmpdir):
189+
inputs = ['/oogabooga/foo/bar.db', os.path.join(tmpdir, 'setup.db')]
190+
self._get_storage(inputs, inputs[-1])
191+
192+
@pytest.mark.parametrize('count', [0, 1, 2])
193+
def test_storage_description_line(self, count):
194+
from openpathsampling.experimental.storage.collective_variables \
195+
import CoordinateFunctionCV
196+
expected = {0: "",
197+
1: "* 1 cv: ['foo']",
198+
2: "* 2 cvs: ['foo', 'bar']"}[count]
199+
200+
self.wizard.cvs = {
201+
name: CoordinateFunctionCV(lambda s: s.xyz[0][0]).named(name)
202+
for name in ['foo', 'bar'][:count]
203+
}
204+
line = self.wizard._storage_description_line('cvs')
205+
assert line == expected
206+
207+
def test_save_to_file(self):
208+
pass
209+
210+
@pytest.mark.parametrize('req,count,expected', [
211+
(('cvs', 1, 1), 0, (True, True)),
212+
(('cvs', 1, 1), 1, (False, False)),
213+
(('cvs', 1, 2), 1, (False, True)),
214+
((None, 0, 0), 0, (True, False))
215+
])
216+
def test_req_do_another(self, req, count, expected):
217+
self.wizard.cvs = {str(i): 'foo' for i in range(count)}
218+
assert self.wizard._req_do_another(req) == expected
219+
220+
@pytest.mark.parametrize('inputs,expected', [
221+
(['y'], True), (['z', 'y'], True), (['z', 'n'], False),
222+
(['n'], False)
223+
])
224+
def test_ask_do_another(self, inputs, expected):
225+
console = MockConsole(inputs)
226+
self.wizard.console = console
227+
result = self.wizard._ask_do_another("CV")
228+
assert result == expected
229+
assert "another CV" in console.log_text
230+
if len(inputs) > 1:
231+
assert "Sorry" in console.log_text
232+
233+
def test_run_wizard(self):
234+
pass

‎paths_cli/wizard/errors.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ def __init__(self, msg=None):
44
msg = "Something went really wrong. You should never see this."
55
super().__init__(msg)
66

7+
class RestartObjectException(BaseException):
8+
pass
9+
710
def not_installed(package, obj_type):
811
retry = wizard.ask("Hey, it looks like you don't have {package} "
912
"installed. Do you want to try a different "

‎paths_cli/wizard/openmm.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
HAS_OPENMM = True
99

1010

11-
def _openmm_serialization_helper(wizard, user_input):
11+
def _openmm_serialization_helper(wizard, user_input): # no-cov
1212
wizard.say("You can write OpenMM objects like systems and integrators "
1313
"to XML files using the XMLSerializer class. Learn more "
1414
"here: \n"

0 commit comments

Comments
 (0)