def test_database_update_with_exception(): """Test that replacing the database_entries members refreshes the database. """ root = RootTask() task1 = ComplexTask(name='task1', database_entries={'val1': 2.0}) task2 = SimpleTask(name='task2', database_entries={'val2': 1}, access_exs={'val2': 1}) task3 = ComplexTask(name='task3') task1.add_child_task(0, task2) root.add_child_task(0, task1) root.add_child_task(1, task3) assert task3.get_from_database('task2_val2') entries = task2.database_entries.copy() del entries['val2'] task2.database_entries = entries with pytest.raises(KeyError): task1.get_from_database('task2_val2') with pytest.raises(KeyError): task3.get_from_database('task2_val2')
def test_deleting_child(): """Test deleting a child. """ root = RootTask() task1 = ComplexTask(name='task1', database_entries={'val1': 2.0}) task2 = SimpleTask(name='task2', database_entries={'val2': 1}, access_exs={'val2': 2}) task3 = ComplexTask(name='task3') task4 = ComplexTask(name='task4') task1.add_child_task(0, task2) task1.add_child_task(1, task4) root.add_child_task(0, task1) root.add_child_task(1, task3) listener = SignalListener() task1.observe('children_changed', listener.listen) task1.remove_child_task(0) assert listener.counter == 1 assert listener.signals[0].removed assert task1.preferences['children_0']['name'] == 'task4' assert 'task2_val2' not in task3.list_accessible_database_entries() root.remove_child_task(0) assert len(root.children) == 1 with pytest.raises(KeyError): root.get_from_database('task1_val1')
def setup(self): r = RootTask() r.run_time = { d_id: { 'd': (object, FalseStarter()) }, p_id: { 'p': { 'connections': { 'c': {}, 'c2': {} }, 'settings': { 's': {} } } } } class InTask(InstrumentTask): feval = Unicode('1').tag(feval=Feval()) self.task = InTask(name='Dummy', selected_instrument=('p', 'd', 'c', 's')) r.add_child_task(0, self.task) self.err_path = 'root/Dummy-instrument'
def test_task_renaming(): """Test renaming simple and complex task. """ root = RootTask() task1 = ComplexTask(name='task1', database_entries={'val1': 2.0}) task2 = ComplexTask(name='task2') task3 = SimpleTask(name='task3', database_entries={'val2': 1}, access_exs={'val2': 2}) task2.add_child_task(0, task3) task1.add_child_task(0, task2) root.add_child_task(0, task1) task3.name = 'worker3' with pytest.raises(KeyError): root.get_from_database('task3_val2') assert root.get_from_database('worker3_val2') == 1 task1.name = 'worker1' with pytest.raises(KeyError): root.get_from_database('task1_val1') assert root.get_from_database('worker1_val1') == 2.0 assert root.get_from_database('worker3_val2') == 1
def setup(self): self.root = RootTask() database = self.root.database database.set_value('root', 'val1', 1) database.create_node('root', 'node1') database.set_value('root/node1', 'val2', 10.0) database.add_access_exception('root', 'root/node1', 'val2')
def test_moving_child(): """Test moving a child. """ root = RootTask() task1 = ComplexTask(name='task1', database_entries={'val1': 2.0}) task2 = SimpleTask(name='task2', database_entries={'val2': 1}, access_exs={'val2': 2}) task3 = ComplexTask(name='task3') task4 = ComplexTask(name='task4') task1.add_child_task(0, task2) task1.add_child_task(1, task4) root.add_child_task(0, task1) root.add_child_task(1, task3) listener = SignalListener() task1.observe('children_changed', listener.listen) assert task1.preferences['children_0']['name'] == 'task2' assert task1.preferences['children_1']['name'] == 'task4' task1.move_child_task(0, 1) assert listener.counter == 1 assert listener.signals[0].moved assert task1.preferences['children_0']['name'] == 'task4' assert task1.preferences['children_1']['name'] == 'task2' assert task3.get_from_database('task2_val2') == 1
def test_moving_through_remove_add_child(): """Test moving a child between different tasks through remove/add. """ root = RootTask() task1 = ComplexTask(name='task1', database_entries={'val1': 2.0}) task2 = ComplexTask(name='task2', database_entries={'val2': 2.0}) task3 = SimpleTask(name='task3', database_entries={'val3': 1}, access_exs={'val3': 2}) task2.add_child_task(0, task3) task1.add_child_task(1, task2) root.add_child_task(0, task1) task1.remove_child_task(0) assert task2.parent is None assert task2.root is None assert task2.database is None assert task3.parent is not None assert task3.root is None assert task3.database is None root.add_child_task(0, task2) assert len(root.children) == 2
def test_create_task2(exopy_qtbot, task_workbench, dialog_sleep, task_config): """Test handling user cancellation. """ core = task_workbench.get_plugin('enaml.workbench.core') def answer_dialog(exopy_qtbot, dial): selector = dial.selector qlist = selector.widgets()[-1] qlist.selected_item = qlist.items[-1] def assert_dial_config(): assert dial.config exopy_qtbot.wait_until(assert_dial_config) exopy_qtbot.wait(dialog_sleep) dial._choose_config('_dummy_') assert not dial.config exopy_qtbot.wait(dialog_sleep) root = RootTask() with handle_dialog(exopy_qtbot, 'reject', answer_dialog): res = core.invoke_command('exopy.tasks.create_task', dict(future_parent=root)) assert res is None
def test_database_operation(): """Test setting, getting, deleting a value from the database. """ root = RootTask() root.write_in_database('test', 1) assert root.get_from_database('test') == 1 root.remove_from_database('test') with pytest.raises(KeyError): root.get_from_database('test')
def setup(self): root = RootTask() root.should_pause = Event() root.should_stop = Event() root.paused = Event() root.resumed = Event() root.default_path = 'toto' root.write_in_database('meas_name', 'M') root.write_in_database('meas_id', '001') self.root = root
def test_build_from_config1(self): """Test building a interfaceable task with no interface from a config. """ aux = RootTask() aux.add_child_task(0, IMixin()) bis = RootTask.build_from_config(aux.preferences, {'exopy.task': {'tasks.IMixin': IMixin, 'exopy.RootTask': RootTask}}) assert type(bis.children[0]).__name__ == 'IMixin'
def test_database_update(): """Test that replacing the database_entries members refreshes the database. """ root = RootTask() entries = root.database_entries.copy() del entries['default_path'] entries['name'] = 'Test' root.database_entries = entries assert root.get_from_database('name') == 'Test' with pytest.raises(KeyError): root.get_from_database('default_path')
def test_build_from_config1(self): """Test building a interfaceable interface with no interface from a config. """ aux = RootTask() mixin = Mixin() mixin.interface = InterfaceTest3() aux.add_child_task(0, mixin) deps = {'exopy.task': {'tasks.Mixin': Mixin, 'exopy.RootTask': RootTask}, 'exopy.tasks.interface': {'tasks.Mixin:tasks.InterfaceTest3': InterfaceTest3}} bis = RootTask.build_from_config(aux.preferences, deps) assert type(bis.children[0].interface).__name__ == 'InterfaceTest3'
def test_template_task_config(exopy_qtbot, task_workbench): """Test the template task configurer. """ plugin = task_workbench.get_plugin('exopy.tasks') path = os.path.join(os.path.dirname(__file__), 'test_template.task.ini') root = RootTask() config = TemplateTaskConfig(manager=plugin, template_path=path, future_parent=root) assert config.template_doc task = config.build_task() assert len(task.children) == 1 show_and_close_widget(exopy_qtbot, TemplateConfigView(config=config))
def test_root_registering(): """Check that the root task does write its default entries in the database when instantiated. """ root = RootTask() assert root.get_from_database('default_path') == '' root.children = [ SimpleTask(name='task2', database_entries={'val2': 1}, root=root, parent=root, database=root.database) ] root.register_in_database() assert root.get_from_database('task2_val2') == 1
def test_swapping(exopy_qtbot, task_workbench, dialog_sleep): """Test moving a view between containers. """ task = RootTask() view = RootTaskView(task=task, core=task_workbench.get_plugin('enaml.workbench.core')) subtask = ComplexTask(name='Test') subview = view.view_for(subtask) task.add_child_task(0, subtask) cont = Container() show_widget(exopy_qtbot, cont) view.set_parent(cont) view.refresh() def assert_children(): assert cont.children == [view] exopy_qtbot.wait_until(assert_children) exopy_qtbot.wait(dialog_sleep) view.set_parent(None) subview.set_parent(cont) subview.refresh() def assert_children(): assert cont.children == [subview] exopy_qtbot.wait_until(assert_children) exopy_qtbot.wait(dialog_sleep) subview.set_parent(None) view.set_parent(cont) view.refresh() def assert_children(): assert cont.children == [view] exopy_qtbot.wait_until(assert_children) assert subview.visible exopy_qtbot.wait(dialog_sleep)
def test_py_task_config(exopy_qtbot, task_workbench): """Test the basic python task configurer. """ plugin = task_workbench.get_plugin('exopy.tasks') root = RootTask() config = PyTaskConfig(manager=plugin, task_class=plugin.get_task('exopy.ComplexTask'), future_parent=root) assert config.task_name assert config.ready assert config.task_doc config.task_name = '' assert not config.ready config.task_name = 'Test' assert config.ready task = config.build_task() assert task.name == 'Test' root.add_child_task(0, task) config2 = PyTaskConfig(manager=plugin, task_class=plugin.get_task('exopy.ComplexTask'), future_parent=root) config2.task_name = 'Test' assert not config2.ready config2.task_name = 'ADifferentName' assert config2.ready plugin.auto_task_names = [] config = PyTaskConfig(manager=plugin, task_class=plugin.get_task('exopy.ComplexTask'), future_parent=root) assert not config.task_name assert not config.ready show_and_close_widget(exopy_qtbot, PyConfigView(config=config)) show_and_close_widget(exopy_qtbot, PyConfigView(config=config, loop=True))
def test_loop_config_with_subtask(task_workbench, exopy_qtbot, dialog_sleep, monkeypatch): """Test the loop config. """ plugin = task_workbench.get_plugin('exopy.tasks') config = LoopTaskConfig(manager=plugin, task_class=plugin.get_task('exopy.LoopTask'), future_parent=RootTask(), task_name='Test') show_widget(exopy_qtbot, LoopConfigView(config=config)) assert config.ready exopy_qtbot.wait(dialog_sleep) config.use_subtask = True assert not config.ready exopy_qtbot.wait(dialog_sleep + 100) config.subtask = 'exopy.BreakTask' assert config.ready exopy_qtbot.wait(dialog_sleep + 100) def dummy(self): self.ready = False monkeypatch.setattr(type(config.subconfig), 'check_parameters', dummy) config.task_name = 'Bis' assert config.subconfig.task_name == 'Bis' # Check sync assert not config.ready # Result from the monkeypatch exopy_qtbot.wait(dialog_sleep + 100) config.use_subtask = False assert config.ready exopy_qtbot.wait(dialog_sleep + 100) config.use_subtask = True config.subtask = 'exopy.ContinueTask' task = config.build_task() assert task.name == 'Bis' assert type(task.task).__name__ == 'ContinueTask'
def test_update_preferences_from_members(): """Test updating the preferences. Only operation on the children cause re-registering to ensure the children ordering. """ root = RootTask() task1 = SimpleTask(name='task1') root.add_child_task(0, task1) assert root.preferences['children_0']['name'] == 'task1' task1.name = 'worker1' assert root.preferences['children_0']['name'] == 'task1' root.update_preferences_from_members() assert root.preferences['children_0']['name'] == 'worker1'
def test_traverse(): """Test traversing a task hierarchy to collect infos. """ root = RootTask() task1 = ComplexTask(name='task1', database_entries={'val1': 2.0}) task2 = SimpleTask(name='task2', database_entries={'val2': 1}, access_exs={'val2': 2}) task3 = ComplexTask(name='task3') task1.add_child_task(0, task2) root.add_child_task(0, task1) root.add_child_task(1, task3) flat = list(root.traverse()) assert flat == [root, task1, task2, task3] flat = list(root.traverse(0)) assert flat == [root, task1, task3]
def test_loop_config(exopy_qtbot, task_workbench): """Test the loop config. """ plugin = task_workbench.get_plugin('exopy.tasks') root = RootTask() config = LoopTaskConfig(manager=plugin, task_class=plugin.get_task('exopy.LoopTask'), future_parent=root) assert config.task_name assert config.ready assert config.task_doc config.task_name = '' assert not config.ready config.task_name = 'Test' assert config.ready task = config.build_task() assert task.name == 'Test' root.add_child_task(0, task) config2 = LoopTaskConfig(manager=plugin, task_class=plugin.get_task('exopy.LoopTask'), future_parent=root) config2.task_name = 'Test' assert not config2.ready config2.task_name = 'ADifferentName' assert config2.ready plugin.auto_task_names = [] config = LoopTaskConfig(manager=plugin, task_class=plugin.get_task('exopy.LoopTask')) assert not config.task_name assert not config.ready show_and_close_widget(exopy_qtbot, LoopConfigView(config=config))
def test_access_exceptions(): """Test adding, modifying and removing an access exception after creation. """ root = RootTask() listener = SignalListener() root.observe('children_changed', listener.listen) task1 = ComplexTask(name='task1', database_entries={'val1': 2.0}) task2 = ComplexTask(name='task2') task3 = SimpleTask( name='task3', database_entries={ 'val2': 1, 'val3': 2 }, ) task2.add_child_task(0, task3) task1.add_child_task(0, task2) root.add_child_task(0, task1) with pytest.raises(KeyError): task2.get_from_database('task3_val2') task3.add_access_exception('val2', 1) task3.add_access_exception('val3', 1) assert task2.get_from_database('task3_val2') == 1 assert task2.get_from_database('task3_val2') == 1 with pytest.raises(KeyError): task1.get_from_database('task3_val2') task3.modify_access_exception('val2', 2) task3.modify_access_exception('val3', -1) assert task1.get_from_database('task3_val2') == 1 with pytest.raises(KeyError): task2.get_from_database('task3_val3') task3.remove_access_exception('val2') with pytest.raises(KeyError): task2.get_from_database('task3_val2')
def test_adding_child(): """Test adding children. This test adding a child with and without access_exs to a task which is not root and then to the root. This makes sure that giving the root afterwards does trigger the right updates. """ root = RootTask() listener = SignalListener() root.observe('children_changed', listener.listen) task1 = ComplexTask(name='task1', database_entries={'val1': 2.0}) task2 = SimpleTask(name='task2', database_entries={'val2': 1}, access_exs={'val2': 2}) task3 = ComplexTask(name='task3') task1.add_child_task(0, task2) root.add_child_task(0, task1) root.add_child_task(1, task3) assert task1.depth == 1 assert task1.path == 'root' assert task1.database is root.database assert task1.root is root assert task1.parent is root assert task2.depth == 2 assert task2.path == 'root/task1' assert task2.database is root.database assert task2.root is root assert task2.parent is task1 assert task1.get_from_database('task1_val1') == 2.0 assert root.get_from_database('task1_val1') == 2.0 assert task3.get_from_database('task2_val2') == 1 assert listener.counter == 2 assert all([bool(c.added) for c in listener.signals])
def test_root_path_edition(exopy_qtbot, task_workbench, dialog_sleep, monkeypatch): """Test the behavior of the root task view. """ task = RootTask() view = RootTaskView(task=task, core=task_workbench.get_plugin('enaml.workbench.core')) butt = view.widgets()[2] @classmethod def choose_path(cls, **kwargs): return 'test/path' with enaml.imports(): from exopy.tasks.tasks.base_views import FileDialogEx monkeypatch.setattr(FileDialogEx, 'get_existing_directory', choose_path) butt.clicked = True assert task.default_path == 'test/path' @classmethod def choose_path(cls, **kwargs): return '' monkeypatch.setattr(FileDialogEx, 'get_existing_directory', choose_path) butt.clicked = True assert task.default_path == 'test/path' @classmethod def choose_path(cls, **kwargs): return '' monkeypatch.setattr(FileDialogEx, 'get_existing_directory', choose_path)
def test_root_view(exopy_qtbot, task_workbench, dialog_sleep): """Test the behavior of the root task view. """ task = RootTask() view = RootTaskView(task=task, core=task_workbench.get_plugin('enaml.workbench.core')) editor = view.children[-1] win = show_widget(exopy_qtbot, view) exopy_qtbot.wait(dialog_sleep) assert editor.task is task assert editor.root is view TASK_NAME = 'Foo' def answer_dialog(bot, dial): selector = dial.selector selector.selected_task = 'exopy.ComplexTask' dial.config.task_name = TASK_NAME with handle_dialog(exopy_qtbot, 'accept', answer_dialog, cls=BuilderView): editor._empty_button.clicked = True def assert_task_children(): assert task.children exopy_qtbot.wait_until(assert_task_children) assert type(task.children[0]) is ComplexTask assert len(editor._children_buttons) == 1 exopy_qtbot.wait(dialog_sleep) TASK_NAME = 'Bar' with handle_dialog(exopy_qtbot, 'accept', answer_dialog, cls=BuilderView): editor.operations['add'](0, 'after') exopy_qtbot.wait(10) exopy_qtbot.wait(dialog_sleep) task.children[0].add_child_task(0, ComplexTask(name='Test')) get_window(exopy_qtbot).maximize() exopy_qtbot.wait(10) exopy_qtbot.wait(dialog_sleep) editor.operations['move'](0, 1) exopy_qtbot.wait(10) exopy_qtbot.wait(dialog_sleep) task.remove_child_task(1) exopy_qtbot.wait(10) exopy_qtbot.wait(dialog_sleep) assert len(view._cache) == 2 # Test removing the last child and removing a view for an already removed # task child_task = task.children[0] editor.operations['remove'](0) exopy_qtbot.wait(10) exopy_qtbot.wait(dialog_sleep) assert len(view._cache) == 1 view.discard_view(child_task) win.close()
def setup(self): self.root = RootTask() self.mixin = InterfaceTest3() self.root.add_child_task(0, Mixin(name='Simple', interface=self.mixin))
def setup(self): self.root = RootTask() self.mixin = Mixin(name='Simple') self.root.add_child_task(0, self.mixin)
def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = DefinitionTask(name='Test') self.root.add_child_task(0, self.task)