Skip to content

Commit 0b60338

Browse files
committed
Start to add tests for wizard
1 parent 8910ba2 commit 0b60338

9 files changed

Lines changed: 100 additions & 27 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from paths_cli.wizard.wizard import Wizard
2+
import mock
3+
4+
def make_mock_wizard(inputs):
5+
wizard = Wizard([])
6+
wizard.console.input = mock.Mock(return_value=inputs)
7+
return wizard
8+
9+
def make_mock_retry_wizard(inputs):
10+
wizard = Wizard([])
11+
wizard.console.input = mock.Mock(side_effect=inputs)
12+
return wizard
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pytest
2+
import mock
3+
4+
from openpathsampling.experimental.storage.collective_variables import \
5+
CoordinateFunctionCV
6+
7+
from paths_cli.wizard.core import *
8+
9+
from paths_cli.tests.wizard.mock_wizard import (
10+
make_mock_wizard, make_mock_retry_wizard
11+
)
12+
13+
def test_name():
14+
wizard = make_mock_wizard('foo')
15+
cv = CoordinateFunctionCV(lambda s: s.xyz[0][0])
16+
assert not cv.is_named
17+
result = name(wizard, cv, obj_type="CV", store_name="cvs")
18+
assert result is cv
19+
assert result.is_named
20+
assert result.name == 'foo'
21+
22+
def test_name_exists():
23+
wizard = make_mock_retry_wizard(['foo', 'bar'])
24+
wizard.cvs['foo'] = 'placeholder'
25+
cv = CoordinateFunctionCV(lambda s: s.xyz[0][0])
26+
assert not cv.is_named
27+
result = name(wizard, cv, obj_type="CV", store_name="cvs")
28+
assert result is cv
29+
assert result.is_named
30+
assert result.name == 'bar'
31+
assert wizard.console.input.call_count == 2
32+
33+
@pytest.mark.parametrize('req,expected', [
34+
(('foo', 2, 2), '2'), (('foo', 2, float('inf')), 'at least 2'),
35+
(('foo', 0, 2), 'at most 2'),
36+
(('foo', 1, 3), 'at least 1 and at most 3')
37+
])
38+
def test_interpret_req(req, expected):
39+
assert interpret_req(req) == expected
40+
41+
@pytest.mark.parametrize('length,expected', [
42+
(0, 'foo'), (1, 'baz'), (2, 'quux'),
43+
])
44+
def test_get_missing_object(length, expected):
45+
dct = dict([('bar', 'baz'), ('qux', 'quux')][:length])
46+
fallback = lambda x: 'foo'
47+
wizard = make_mock_wizard('2')
48+
result = get_missing_object(wizard, dct, display_name='string',
49+
fallback_func=fallback)
50+
assert result == expected
51+
pass
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
import mock
3+
4+
from paths_cli.wizard.tools import *
5+
6+
@pytest.mark.parametrize('word,expected', [
7+
('foo', 'a'), ('egg', 'an')
8+
])
9+
def test_a_an(word, expected):
10+
assert a_an(word) == expected
11+
12+
13+
@pytest.mark.parametrize('user_inp,expected', [
14+
('y', True), ('n', False),
15+
# ('Y', True), ('N', False), ('yes', True), ('no', False)
16+
])
17+
def test_yes_no(user_inp, expected):
18+
assert yes_no(user_inp) == expected

‎paths_cli/wizard/__init__.py‎

Whitespace-only changes.

‎paths_cli/wizard/core.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import random
2-
from joke import name_joke
3-
from tools import a_an
2+
from paths_cli.wizard.joke import name_joke
3+
from paths_cli.wizard.tools import a_an
44

55
def name(wizard, obj, obj_type, store_name, default=None):
66
wizard.say(f"Now let's name your {obj_type}.")
@@ -9,7 +9,8 @@ def name(wizard, obj, obj_type, store_name, default=None):
99
name = wizard.ask("What do you want to call it?")
1010
if name in getattr(wizard, store_name):
1111
wizard.bad_input(f"Sorry, you already have {a_an(obj_type)} "
12-
f"named {name}. Please try another name.")
12+
f"{obj_type} named {name}. Please try another "
13+
"name.")
1314
name = None
1415

1516
obj = obj.named(name)
@@ -19,7 +20,7 @@ def name(wizard, obj, obj_type, store_name, default=None):
1920
return obj
2021

2122
def abort_retry_quit(wizard, obj_type):
22-
a_an = 'an' if obj_type[0] in 'aeiou' else 'a'
23+
a_an = a_an(obj_type)
2324
retry = wizard.ask(f"Do you want to try again to make {a_an} "
2425
f"{obj_type}, do you want to continue without, or "
2526
f"do you want to quit?",
@@ -60,6 +61,6 @@ def get_missing_object(wizard, obj_dict, display_name, fallback_func):
6061
objs = list(obj_dict.keys())
6162
sel = wizard.ask_enumerate(f"Which {display_name} would you like "
6263
"to use?", options=objs)
63-
obj = objs[sel]
64+
obj = obj_dict[sel]
6465
return obj
6566

‎paths_cli/wizard/engines.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import paths_cli.wizard.openmm as openmm
2-
from load_from_ops import load_from_ops, LABEL as load_label
2+
from paths_cli.wizard.load_from_ops import (
3+
load_from_ops, LABEL as load_label
4+
)
35
from functools import partial
46

57
SUPPORTED_ENGINES = {}

‎paths_cli/wizard/openmm.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from paths_cli.wizard.errors import FILE_LOADING_ERROR_MSG, not_installed
2-
from paths_cli.wizard.tools import get_int_value
32
try:
43
from simtk import openmm as mm
54
import mdtraj as md
@@ -69,10 +68,12 @@ def openmm(wizard):
6968

7069
n_frames_max = n_steps_per_frame = None
7170

72-
n_steps_per_frame = get_int_value(wizard,
73-
"How many MD steps per saved frame?")
74-
n_frames_max = get_int_value(wizard, ("How many frames before aborting "
75-
"a trajectory?"))
71+
n_steps_per_frame = wizard.ask_custom_eval(
72+
"How many MD steps per saved frame?", type_=int
73+
)
74+
n_frames_max = wizard.ask_custom_eval(
75+
"How many frames before aborting a trajectory?", type_=int
76+
)
7677
# TODO: assemble the OpenMM simulation
7778
engine = paths.engines.openmm.Engine(
7879
topology=topology,

‎paths_cli/wizard/tools.py‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
21
def a_an(obj):
3-
return "an" if obj[0] in "aeiou" else "a"
2+
return "an" if obj[0].lower() in "aeiou" else "a"
43

54
def yes_no(char):
65
return {'y': True, 'n': False}[char]
7-
8-
def get_int_value(wizard, question, default=None, helper=None):
9-
as_int = None
10-
while as_int is None:
11-
evald = wizard.ask_custom_eval(question, default=default,
12-
helper=helper)
13-
try:
14-
as_int = int(evald)
15-
except Exception as e:
16-
wizard.exception("Sorry, I didn't understand that.", e)
17-
return as_int

‎paths_cli/wizard/wizard.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import shutil
1818

19-
class Console:
19+
class Console: # no-cov
2020
# TODO: add logging so we can output the session
2121
def print(self, *content):
2222
print(*content)
@@ -45,15 +45,15 @@ def __init__(self, steps):
4545
self.default = {}
4646
self._patched = False # if we've done the monkey-patching
4747

48-
def _patch(self):
48+
def _patch(self): # no-cov
4949
import openpathsampling as paths
5050
from openpathsampling.experimental.storage import monkey_patch_all
5151
if not self._patched:
5252
paths = monkey_patch_all(paths)
5353
paths.InterfaceSet.simstore = True
5454
self._patched = True
5555

56-
def debug(content):
56+
def debug(content): # no-cov
5757
self.console.print(content)
5858

5959
def _speak(self, content, preface):

0 commit comments

Comments
 (0)