def test_openmm(ad_openmm): inputs = ['ad.pdb', 'integrator.xml', 'system.xml', '10', '10000'] wizard = mock_wizard(inputs) with ad_openmm.as_cwd(): engine = OPENMM_PLUGIN(wizard) assert engine.n_frames_max == 10000 assert engine.n_steps_per_frame == 10
def test_from_proxy_call_existing(self): # using a proxy parameter that seeks an existing object should work # and give the expected result proxy = ProxyParameter(name='bar', ask="ask bar?") cat = standard_categories.Category(name='bar', singular='bar', plural='bars', storage='bars') cat_loc = 'paths_cli.wizard.standard_categories.CATEGORIES' get_cat_wiz_loc = ('paths_cli.wizard.plugin_registration' '.get_category_wizard') get_cat_wiz_mock = mock.Mock() with mock.patch(cat_loc, {'bar': cat}) as p1, \ mock.patch(get_cat_wiz_loc, get_cat_wiz_mock) as p2: parameter = WizardParameter.from_proxy(proxy, self.compiler_plugin) wizard = mock_wizard(['bar', 'baz']) wizard.bars = {'baz': 'qux'} result = parameter(wizard, context={}) assert result == 'qux' assert "1. baz" in wizard.console.log_text assert "2. Create a new bar" in wizard.console.log_text assert "'bar' is not a valid option" in wizard.console.log_text
def test_get_atom_indices(ad_engine, inputs): wizard = mock_wizard(inputs) arr = _get_atom_indices(wizard, ad_engine.topology, 2, "use") np.testing.assert_array_equal(arr, np.array([[1, 2]])) assert wizard.console.input_call_count == len(inputs) if len(inputs) > 1: assert "I didn't understand" in wizard.console.log_text
def test_one_way_shooting(toy_engine): wizard = mock_wizard(['Uniform random']) wizard.engines[toy_engine.name] = toy_engine strategy = one_way_shooting(wizard) assert isinstance(strategy, paths.strategies.OneWayShootingStrategy) assert isinstance(strategy.selector, paths.UniformSelector) assert strategy.engine == toy_engine
def test_get_category_wizard_error(): # if a category plugin of the given name does not exist, an error is # raised func = get_category_wizard("foo") wiz = mock_wizard([]) with pytest.raises(CategoryWizardPluginRegistrationError, match="No wizard"): func(wiz, {})
def test_call(self): # check that calling the plugin creates the correct object and # outputs intro/summary to the wizard wizard = mock_wizard([]) res = self.plugin(wizard) assert "foo intro" in wizard.console.log_text assert "foo summary" in wizard.console.log_text assert res == "foo_obj"
def test_not_installed(inputs): expected = {'r': RestartObjectException, 'q': SystemExit}[inputs] wizard = mock_wizard([inputs]) with pytest.raises(expected): not_installed(wizard, 'foo', 'widget') log = wizard.console.log_text assert 'foo installed' in log assert 'different widget' in log
def test_gaussian_selector(): wizard = mock_wizard(['x', '1.0', '0.5']) cv = CoordinateFunctionCV(lambda s: s.xyz[0][0]).named('x') wizard.cvs[cv.name] = cv sel = gaussian_selector(wizard) assert isinstance(sel, paths.GaussianBiasSelector) assert sel.alpha == 2.0 assert sel.l_0 == 1.0
def test_from_proxy_call_standard(self): # using a proxy for a normal parameter (not an existing object) # should work and give the expected result proxy = ProxyParameter(name='foo', ask="ask foo?") wizard = mock_wizard(['baz']) real_param = WizardParameter.from_proxy(proxy, self.compiler_plugin) result = real_param(wizard, context={}) assert result == "baz" assert "ask foo?" in wizard.console.log_text
def test_two_state_tps(tps_network): wizard = mock_wizard([]) state_A = tps_network.initial_states[0] state_B = tps_network.final_states[0] with mock.patch('paths_cli.wizard.two_state_tps.volumes', new=mock.Mock(side_effect=[state_A, state_B])): scheme = two_state_tps(wizard) assert scheme == "this would be a scheme" assert "network.TPSNetwork" in wizard.console.log_text
def test_coordinate(inputs): wizard = mock_wizard(inputs) cv = COORDINATE_CV(wizard) traj = make_1d_traj([5.0]) assert cv(traj[0]) == 5.0 if 'foo' in inputs: assert "I can't make an atom index" in wizard.console.log_text if 'q' in inputs: assert "Please select one of" in wizard.console.log_text
def test_default_summarize(self): # ensure that we get the correct output from default_summarize wizard = mock_wizard([]) context = {} result = "foo" summ = self.plugin.default_summarize(wizard, context, result) assert len(summ) == 1 assert "Here's what we'll make" in summ[0] assert "foo" in summ[0]
def test_shooting(toy_engine, shooting_types): key = 'One-way (stochastic) shooting' wizard = mock_wizard([key]) wizard.engines[toy_engine.name] = toy_engine foo = mock.Mock(return_value='foo') if shooting_types == {}: shooting_types = {key: foo} with mock.patch.dict(SHOOTING_TYPES, {key: foo}): assert shooting(wizard, shooting_types=shooting_types) == 'foo'
def test_spring_shooting(toy_engine): wizard = mock_wizard(['10', '0.25']) wizard.engines[toy_engine.name] = toy_engine strategy = spring_shooting(wizard) assert isinstance(strategy, partial) assert strategy.func == paths.SpringShootingMoveScheme assert strategy.keywords['delta_max'] == 10 assert strategy.keywords['k_spring'] == 0.25 assert strategy.keywords['engine'] == toy_engine
def _run_pause_test(func): test_style = PAUSE_STYLES['testing'] expected = getattr(test_style, func.__name__) wiz = mock_wizard([]) with pause_style(test_style): start = time.time() func(wiz) duration = time.time() - start assert expected <= duration < 1.1 * duration return wiz
def test_volume_ask(depth): context = {0: {'depth': 0}, 1: {'depth': 1}, None: {}}[depth] wiz = mock_wizard([]) result = volume_ask(wiz, context) if depth == 1: assert result == "What describes this volume?" else: assert result == "What describes this state?"
def test_get_existing(self): # the get_existing method should load objects currently in the # storage, after passing them through the load_func. This should be # done without any statements to the user. wiz = mock_wizard([]) wiz.store = {'bar': self.Wrapper("11"), 'baz': self.Wrapper("22")} obj = self.prereq._get_existing(wiz) assert obj == [11, 22] assert wiz.console.log_text == ""
def test_get_category_wizard(): # get_category_wizard returns a function that will run a given wizard # (even if the specific wizard is only registered at a later point in # time) func = get_category_wizard('foo') wiz = mock_wizard([]) with mock.patch.dict(CAT_PLUGINS_LOC, {'foo': MockPlugin('foo')}): result = func(wiz, {}) assert result == 10 assert "bar" in wiz.console.log_text
def test_create_new(self): # the create_new method should return the (integer) value from the # load_func, and the Wrapper object should be registered with the # wizard as a side-effect. The say_create should appear in logs. wiz = mock_wizard(["1", "name1"]) wiz.store = {} obj = self.prereq._create_new(wiz) assert obj == 11 assert len(wiz.store) == 1 assert wiz.store['name1'].val == "11" assert "create one" in wiz.console.log_text
def test_select_existing(self): # select_existing should return the selected result based on # existing objects in storage and logs should include question from # wizard.obj_selector wiz = mock_wizard(['1']) wiz.store = {'bar': self.Wrapper("11"), 'baz': self.Wrapper("22"), 'qux': self.Wrapper("33")} obj = self.prereq._select_single_existing(wiz) assert obj == 11 assert "Which obj_name would you" in wiz.console.log_text
def _binary_volume_test(volume_setup, func): vol1, vol2 = volume_setup wizard = mock_wizard([]) mock_volumes = mock.Mock(side_effect=[vol1, vol2]) patch_loc = 'paths_cli.wizard.volumes.VOLUMES_PLUGIN' with mock.patch(patch_loc, new=mock_volumes): vol = func(wizard) assert "first constituent volume" in wizard.console.log_text assert "second constituent volume" in wizard.console.log_text assert "Here's what we'll make" in wizard.console.log_text return wizard, vol
def setup(self): self.parameters = [ WizardParameter(name="foo", ask="Gimme a foo!", loader=int), WizardParameter(name="bar", ask="Tell me bar!", loader=str) ] self.plugin = WizardParameterObjectPlugin( name="baz", category="baz_cat", parameters=self.parameters, builder=self.MyClass, ) self.wizard = mock_wizard(['11', '22'])
def test_call(self): # the created object should call the load_from_ops method with # appropriate parameters (note that the test of load_from_ops is # done separately) loader = LoadFromOPS("cat_name", obj_name='foo', store_name='foos') wiz = mock_wizard([]) mock_load = mock.Mock(return_value="some_object") patch_loc = "paths_cli.wizard.plugin_classes.load_from_ops" with mock.patch(patch_loc, mock_load): result = loader(wiz) assert mock_load.called_once_with(wiz, "foos", "foo") assert result == "some_object"
def test_negated_volume(volume_setup): init_vol, _ = volume_setup traj = make_1d_traj([0.5, 1.5]) assert init_vol(traj[0]) assert not init_vol(traj[1]) wizard = mock_wizard([]) mock_vol = mock.Mock(return_value=init_vol) patch_loc = 'paths_cli.wizard.volumes.VOLUMES_PLUGIN' with mock.patch(patch_loc, new=mock_vol): vol = NEGATED_VOLUME_PLUGIN(wizard) assert "not in" in wizard.console.log_text assert not vol(traj[0]) assert vol(traj[1])
def test_get_ask(self, input_type): # wrapper.get_ask should create the appropriate input string whether # it is a plain string, or a string that is formatted by the context # dict, or a method that takes wizard and context to return a string ask = { 'method': lambda wizard, context: f"Bar is {context['bar']}", 'format': "Bar is {bar}", 'string': "Bar is 10" }[input_type] context = {'bar': 10} wrapper = WrapCategory("foo", ask) wizard = mock_wizard([]) ask_string = wrapper._get_ask(wizard, context) assert ask_string == "Bar is 10"
def test_call_with_prereq(self): # ensure that prerequisites get run if they are provided def prereq(wizard, context): wizard.say("Running prereq") return {'prereq': ['results']} plugin = WizardObjectPlugin( name="foo", category="foo_category", builder=lambda wizard, context: "foo_obj", prerequisite=prereq, ) wizard = mock_wizard([]) result = plugin(wizard, context={}) assert result == "foo_obj" assert "Running prereq" in wizard.console.log_text
def test_TOPOLOGY_CV_PREREQ(ad_engine, n_engines): inputs = {0: [], 1: [], 2: ['1']}[n_engines] wizard = mock_wizard(inputs) engines = [ad_engine, paths.engines.NoEngine(None).named('foo')] wizard.engines = {eng.name: eng for eng in engines[:n_engines]} def mock_register(obj, obj_type, store_name): wizard.engines[obj.name] = obj wizard.register = mock_register mock_engines = mock.Mock(return_value=ad_engine) patch_loc = 'paths_cli.wizard.cvs.TOPOLOGY_CV_PREREQ.create_func' with mock.patch(patch_loc, new=mock_engines): topology_result = TOPOLOGY_CV_PREREQ(wizard) topology = topology_result['topology'][0] assert isinstance(topology, paths.engines.MDTrajTopology)
def setup(self): self.parameter = WizardParameter( name='foo', ask="How should I {do_what}?", loader=self._reverse, summarize=lambda string: f"Should be unused. Input: {string}", ) self.wizard = mock_wizard(["bar"]) self.compiler_plugin = compiling.InstanceCompilerPlugin( builder=lambda foo: {'foo': foo, 'bar': bar}, parameters=[ compiling.Parameter(name='foo', loader=lambda x: x), compiling.Parameter(name='bar', loader=_CategoryCompilerProxy('bar')) ] )
def test_tps_scheme(tps_network, toy_engine, as_scheme): wizard = mock_wizard([]) wizard.networks = {tps_network.name: tps_network} if as_scheme: strategy = partial(paths.SpringShootingMoveScheme, k_spring=0.01, delta_max=10, engine=toy_engine) else: strategy = strategies.OneWayShootingStrategy( selector=paths.UniformSelector(), engine=toy_engine) with mock.patch('paths_cli.wizard.tps.shooting', new=mock.Mock(return_value=strategy)): scheme = tps_scheme(wizard) assert isinstance(scheme, paths.MoveScheme) if as_scheme: assert isinstance(scheme, paths.SpringShootingMoveScheme)
def test_call(self, objs): n_objs = {'fewer': 1, 'exact': 2, 'more': 3}[objs] inputs = {'fewer': ['2', 'name2'], 'exact': [], 'more': ['1', '2']}[objs] wrappers = {"name" + i: self.Wrapper(i*2) for i in ['1', '2', '3']} items = list(wrappers.items())[:n_objs] wiz = mock_wizard(inputs) wiz.store = dict(items) results = self.prereq(wiz) assert len(results) == 1 assert len(results['foo']) == 2 assert results['foo'] == [11, 22] if objs != 'exact': assert "now we're done" in wiz.console.log_text