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 test_root_view(windows, 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(view) sleep(dialog_sleep) assert editor.task is task assert editor.root is view TASK_NAME = 'Foo' def answer_dialog(dial): selector = dial.selector selector.selected_task = 'ecpy.ComplexTask' dial.config.task_name = TASK_NAME process_app_events() with handle_dialog('accept', answer_dialog, cls=BuilderView): editor._empty_button.clicked = True process_app_events() assert task.children assert type(task.children[0]) is ComplexTask assert len(editor._children_buttons) == 1 sleep(dialog_sleep) TASK_NAME = 'Bar' with handle_dialog('accept', answer_dialog, cls=BuilderView): editor.operations['add'](0, 'after') process_app_events() sleep(dialog_sleep) task.children[0].add_child_task(0, ComplexTask(name='Test')) get_window().maximize() process_app_events() sleep(dialog_sleep) editor.operations['move'](0, 1) process_app_events() sleep(dialog_sleep) task.remove_child_task(1) process_app_events() sleep(dialog_sleep) assert len(view._cache) == 2 editor.operations['remove'](0) process_app_events() sleep(dialog_sleep) assert len(view._cache) == 1 win.close()
class TestInterfaceableTaskMixin(object): """Test the capabilities of task interfaces. """ def setup(self): self.root = RootTask() self.mixin = Mixin(name='Simple') self.root.add_child_task(0, self.mixin) def test_interface_observer(self): """Test changing the interface. """ i1 = InterfaceTest() i2 = InterfaceTest2() self.mixin.interface = i1 assert i1.task is self.mixin assert self.mixin.database_entries == {'test': 2.0, 'itest': 1.0} assert i1.interface_id == (self.mixin.task_id + ':tests.' + i1.__class__.__name__) self.mixin.interface = i2 assert i2.task is self.mixin assert i1.task is None assert self.mixin.database_entries == { 'test': 2.0, 'itest': 2.0, 'fmt': '', 'feval': 0 } def test_check1(self): """Test running checks when the interface is present. """ self.mixin.interface = InterfaceTest(answer=True) res, traceback = self.mixin.check() assert res assert not traceback assert self.mixin.interface.called def test_check2(self): """Test running checks when no interface exist but i_perform is implemented. """ res, traceback = IMixin().check() assert res assert not traceback def test_check3(self): """Test handling missing interface. """ res, traceback = self.mixin.check() assert not res assert traceback assert len(traceback) == 1 assert 'root/Simple-interface' in traceback def test_check4(self): """Test handling a non-passing test from the interface. """ self.mixin.interface = InterfaceTest() res, traceback = self.mixin.check() assert not res assert len(traceback) == 1 assert self.mixin.interface.called def test_check5(self): """Check that auto-check of fmt and feval tagged members works. """ self.mixin.interface = InterfaceTest2(fmt='{Simple_test}', feval='2*{Simple_test}') res, traceback = self.mixin.check() assert res assert not traceback assert self.root.get_from_database('Simple_fmt') == '2.0' assert self.root.get_from_database('Simple_feval') == 4.0 self.mixin.interface = InterfaceTest2bis(fmt='{Simple_test}', feval='2*{Simple_test}') res, traceback = self.mixin.check() assert not res assert 'root/Simple-feval' in traceback def test_check6(self): """Check that auto-check of fmt and feavl handle errors. """ self.mixin.interface = InterfaceTest2(fmt='{Simple_test*}', feval='2*{Simple_test}*') res, traceback = self.mixin.check() assert not res assert self.root.get_from_database('Simple_fmt') == '' assert self.root.get_from_database('Simple_feval') == 0 assert len(traceback) == 2 assert 'root/Simple-fmt' in traceback assert 'root/Simple-feval' in traceback def test_prepare(self): """Test that the prepare method does prepare the interface. """ self.mixin.interface = InterfaceTest() self.mixin.prepare() assert self.mixin.interface.called def test_perform1(self): """Test perform does call interface if present. """ self.mixin.interface = InterfaceTest() self.root.database.prepare_to_run() self.mixin.perform() assert self.mixin.get_from_database('Simple_itest') == 2.0 def test_perform2(self): """Test perform use i_perform when no interface exists. """ self.root.remove_child_task(0) self.mixin = IMixin(name='Simple') self.root.add_child_task(0, self.mixin) self.root.database.prepare_to_run() self.mixin.perform() assert self.root.get_from_database('Simple_test') == 3.0 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, {'ecpy.task': { 'tests.IMixin': IMixin, 'ecpy.RootTask': RootTask }}) assert type(bis.children[0]).__name__ == 'IMixin' def test_build_from_config2(self): """Test building a interfaceable task with an interface from a config. """ self.mixin.interface = InterfaceTest(answer=True) self.root.update_preferences_from_members() deps = { 'ecpy.task': { 'tests.Mixin': Mixin, 'ecpy.RootTask': RootTask }, 'ecpy.tasks.interface': { 'tests.Mixin:tests.InterfaceTest': InterfaceTest } } bis = RootTask.build_from_config(self.root.preferences, deps) assert type(bis.children[0].interface).__name__ == 'InterfaceTest' def test_traverse(self): """Test traversing a task with interface. """ self.mixin.interface = InterfaceTest2() w = list(self.mixin.traverse()) assert w == [self.mixin, self.mixin.interface]
class TestInterfaceableTaskMixin(object): """Test the capabilities of task interfaces. """ def setup(self): self.root = RootTask() self.mixin = Mixin(name='Simple') self.root.add_child_task(0, self.mixin) def test_interface_observer(self): """Test changing the interface. """ i1 = InterfaceTest() i2 = InterfaceTest2() self.mixin.interface = i1 assert i1.task is self.mixin assert self.mixin.database_entries == {'test': 2.0, 'itest': 1.0} assert i1.interface_id[1] == (self.mixin.task_id,) self.mixin.interface = i2 assert i2.task is self.mixin assert i1.task is None assert self.mixin.database_entries == {'test': 2.0, 'itest': 2.0, 'fmt': '', 'feval': 0} def test_check1(self): """Test running checks when the interface is present. """ self.mixin.interface = InterfaceTest(answer=True) res, traceback = self.mixin.check() assert res assert not traceback assert self.mixin.interface.called def test_check2(self): """Test running checks when no interface exist but i_perform is implemented. """ res, traceback = IMixin().check() assert res assert not traceback def test_check3(self): """Test handling missing interface. """ res, traceback = self.mixin.check() assert not res assert traceback assert len(traceback) == 1 assert 'root/Simple-interface' in traceback def test_check4(self): """Test handling a non-passing test from the interface. """ self.mixin.interface = InterfaceTest() res, traceback = self.mixin.check() assert not res assert len(traceback) == 1 assert self.mixin.interface.called def test_check5(self): """Check that auto-check of fmt and feavl tagged members works. """ self.mixin.interface = InterfaceTest2(fmt='{Simple_test}', feval='2*{Simple_test}') res, traceback = self.mixin.check() assert res assert not traceback assert self.root.get_from_database('Simple_fmt') == '2.0' assert self.root.get_from_database('Simple_feval') == 4.0 def test_check6(self): """Check that auto-check of fmt and feavl handle errors. """ self.mixin.interface = InterfaceTest2(fmt='{Simple_test*}', feval='2*{Simple_test}*') res, traceback = self.mixin.check() assert not res assert self.root.get_from_database('Simple_fmt') == '' assert self.root.get_from_database('Simple_feval') == 0 assert len(traceback) == 2 assert 'root/Simple-fmt' in traceback assert 'root/Simple-feval' in traceback def test_perform1(self): """Test perform does call interface if present. """ self.mixin.interface = InterfaceTest() self.root.database.prepare_to_run() self.mixin.perform() assert self.mixin.get_from_database('Simple_itest') == 2.0 def test_perform2(self): """Test perform use i_perform when no interface exists. """ self.root.remove_child_task(0) self.mixin = IMixin(name='Simple') self.root.add_child_task(0, self.mixin) self.root.database.prepare_to_run() self.mixin.perform() assert self.root.get_from_database('Simple_test') == 3.0 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, {'ecpy.task': {'tests.IMixin': IMixin, 'ecpy.RootTask': RootTask}}) assert type(bis.children[0]).__name__ == 'IMixin' def test_build_from_config2(self): """Test building a interfaceable task with an interface from a config. """ self.mixin.interface = InterfaceTest(answer=True) self.root.update_preferences_from_members() deps = {'ecpy.task': {'tests.Mixin': Mixin, 'ecpy.RootTask': RootTask}, 'ecpy.tasks.interface': {('InterfaceTest', ('tests.Mixin',)): InterfaceTest}} bis = RootTask.build_from_config(self.root.preferences, deps) assert type(bis.children[0].interface).__name__ == 'InterfaceTest' def test_traverse(self): """Test traversing a task with interface. """ self.mixin.interface = InterfaceTest2() w = list(self.mixin.traverse()) assert w == [self.mixin, self.mixin.interface]