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_saving_building_from_config(self, iterable_interface): """Done here as the LoopTask is a viable case of a member tagged with child. """ subtask1 = CheckTask(name='check', database_entries={'val': 1}) self.task.task = subtask1 self.root.update_preferences_from_members() deps = {'ecpy.task': {'ecpy.RootTask': RootTask, 'ecpy.LoopTask': LoopTask, 'ecpy.CheckTask': CheckTask} } new = RootTask.build_from_config(self.root.preferences, deps) assert new.children[0].task.name == 'check' self.task.interface = iterable_interface self.root.update_preferences_from_members() prefs = self.root.preferences del prefs['children_0']['task'] deps = {'ecpy.task': {'ecpy.RootTask': RootTask, 'ecpy.LoopTask': LoopTask, 'ecpy.CheckTask': CheckTask}, 'ecpy.tasks.interface': {('IterableLoopInterface', ('ecpy.LoopTask',)): IterableLoopInterface} } new = RootTask.build_from_config(prefs, deps) assert not new.children[0].task
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}, ) 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) assert task2.get_from_database('task3_val2') == 1 with pytest.raises(KeyError): task1.get_from_database('task3_val2') task3.modify_access_exception('val2', 2) assert task1.get_from_database('task3_val2') == 1 task3.remove_access_exception('val2') with pytest.raises(KeyError): task2.get_from_database('task3_val2')
def test_saving_building_from_config(self): """Done here as the LoopTask is a viable case of a member tagged with child. """ subtask1 = CheckTask(name='check', database_entries={'val': 1}) self.task.task = subtask1 self.root.update_preferences_from_members() new = RootTask.build_from_config(self.root.preferences, {'ecpy.task': {'RootTask': RootTask, 'LoopTask': LoopTask, 'CheckTask': CheckTask} }) assert new.children[0].task.name == 'check' self.root.update_preferences_from_members() prefs = self.root.preferences del prefs['children_0']['task'] new = RootTask.build_from_config(prefs, {'ecpy.task': {'RootTask': RootTask, 'LoopTask': LoopTask, 'CheckTask': CheckTask} }) assert not new.children[0].task
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 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 setup(self): root = RootTask() root.should_pause = Event() root.should_stop = Event() root.paused = Event() root.default_path = 'toto' self.root = root
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 test_build_from_config1(self): """Test building a interfaceable task with no interface from a config. """ aux = RootTask() aux.add_child_task(0, IMixin()) print(aux.preferences) bis = RootTask.build_from_config(aux.preferences, {'ecpy.task': {'IMixin': IMixin, '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['meas_name'] entries['name'] = 'Test' root.database_entries = entries assert root.get_from_database('name') == 'Test' with pytest.raises(KeyError): root.get_from_database('meas_name')
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 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_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 = {'ecpy.task': {'tests.Mixin': Mixin, 'ecpy.RootTask': RootTask}, 'ecpy.tasks.interface': {('InterfaceTest3', ('tests.Mixin',)): InterfaceTest3}} bis = RootTask.build_from_config(aux.preferences, deps) assert type(bis.children[0].interface).__name__ == 'InterfaceTest3'
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_build_from_config2(self): """Test building a interfaceable interface with an interface from a config. """ self.mixin.interface = IIinterfaceTest1(answer=True) self.root.update_preferences_from_members() deps = { 'ecpy.task': { 'tests.Mixin': Mixin, 'ecpy.RootTask': RootTask }, 'ecpy.tasks.interface': { ('InterfaceTest3', ('tests.Mixin', )): InterfaceTest3, ('IIinterfaceTest1', ('tests.Mixin', 'InterfaceTest3')): IIinterfaceTest1 } } bis = RootTask.build_from_config(self.root.preferences, deps) interface = bis.children[0].interface.interface assert type(interface).__name__ == 'IIinterfaceTest1' assert self.root.children[0].database_entries ==\ {'test': 2.0, 'itest': 1.0}
def setup(self): root = RootTask() root.should_pause = Event() root.should_stop = Event() root.paused = Event() root.resumed = Event() root.default_path = 'toto' self.root = root
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_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') == '' assert root.get_from_database('meas_name') == '' assert root.get_from_database('meas_id') == '' assert root.get_from_database('meas_date') == '' 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_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 = { 'ecpy.task': { 'tests.Mixin': Mixin, 'ecpy.RootTask': RootTask }, 'ecpy.tasks.interface': { ('InterfaceTest3', ('tests.Mixin', )): InterfaceTest3 } } bis = RootTask.build_from_config(aux.preferences, deps) assert type(bis.children[0].interface).__name__ == 'InterfaceTest3'
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_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_saving_building_from_config(self, iterable_interface): """Done here as the LoopTask is a viable case of a member tagged with child. """ subtask1 = CheckTask(name='check', database_entries={'val': 1}) self.task.task = subtask1 self.root.update_preferences_from_members() deps = { 'ecpy.task': { 'ecpy.RootTask': RootTask, 'ecpy.LoopTask': LoopTask, 'ecpy.CheckTask': CheckTask } } new = RootTask.build_from_config(self.root.preferences, deps) assert new.children[0].task.name == 'check' self.task.interface = iterable_interface self.root.update_preferences_from_members() prefs = self.root.preferences del prefs['children_0']['task'] deps = { 'ecpy.task': { 'ecpy.RootTask': RootTask, 'ecpy.LoopTask': LoopTask, 'ecpy.CheckTask': CheckTask }, 'ecpy.tasks.interface': { ('IterableLoopInterface', ('ecpy.LoopTask', )): IterableLoopInterface } } new = RootTask.build_from_config(prefs, deps) assert not new.children[0].task
def test_build_root_from_config(): """Test building a RootTask from config. """ config = {'name': 'test', 'children_0': {'name': 'test_child', 'task_class': 'SimpleTask'}} task = RootTask.build_from_config(config, {'ecpy.task': {'SimpleTask': SimpleTask}}) assert task.name == 'Root' assert len(task.children) == 1 assert task.children[0].name == 'test_child' assert isinstance(task.children[0], SimpleTask)
def test_swapping(windows, task_workbench): """Test moving a view between containers. """ from ..conftest import DIALOG_SLEEP 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(cont) view.set_parent(cont) view.refresh() process_app_events() assert cont.children == [view] sleep(DIALOG_SLEEP) view.set_parent(None) subview.set_parent(cont) subview.refresh() process_app_events() assert cont.children == [subview] sleep(DIALOG_SLEEP) subview.set_parent(None) view.set_parent(cont) view.refresh() process_app_events() assert cont.children == [view] assert subview.visible sleep(DIALOG_SLEEP)
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_build_root_from_config(): """Test building a RootTask from config. """ config = {'name': 'test', 'children_0': {'name': 'test_child', 'task_id': 'SimpleTask'}} task = RootTask.build_from_config(config, {'ecpy.task': {'SimpleTask': SimpleTask}}) assert task.name == 'Root' assert len(task.children) == 1 assert task.children[0].name == 'test_child' assert task.children[0].root assert isinstance(task.children[0], SimpleTask)
class TestConditionTask(object): """Test ConditionalTask. """ def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = ConditionalTask(name='Test') self.root.add_child_task(0, self.task) self.check = CheckTask(name='check') self.task.add_child_task(0, self.check) def test_check1(self): """Test that everything is ok if condition is evaluable. """ self.task.condition = 'True' test, traceback = self.task.check() assert test assert not traceback assert self.check.check_called def test_check2(self): """Test handling a wrong condition. """ self.task.condition = '*True' test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-condition' in traceback def test_perform1(self): """Test performing when condition is True. """ self.task.condition = 'True' self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.check.perform_called def test_perform2(self): """Test performing when condition is False. """ self.task.condition = '1 < 0' self.root.database.prepare_for_running() self.root.check() self.task.perform() assert not self.check.perform_called
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'
class TestConditionTask(object): """Test ConditionalTask. """ def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = ConditionalTask(name='Test') self.root.add_child_task(0, self.task) self.check = CheckTask(name='check') self.task.add_child_task(0, self.check) def test_check1(self): """Test that everything is ok if condition is evaluable. """ self.task.condition = 'True' test, traceback = self.task.check() assert test assert not traceback assert self.check.check_called def test_check2(self): """Test handling a wrong condition. """ self.task.condition = '*True' test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-condition' in traceback def test_perform1(self): """Test performing when condition is True. """ self.task.condition = 'True' self.root.prepare() self.task.perform() assert self.check.perform_called def test_perform2(self): """Test performing when condition is False. """ self.task.condition = '1 < 0' self.root.prepare() self.task.perform() assert not self.check.perform_called
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_config2(self): """Test building a interfaceable interface with an interface from a config. """ self.mixin.interface = IIinterfaceTest1(answer=True) self.root.update_preferences_from_members() deps = {'ecpy.task': {'tests.Mixin': Mixin, 'ecpy.RootTask': RootTask}, 'ecpy.tasks.interface': {('InterfaceTest3', ('tests.Mixin',)): InterfaceTest3, ('IIinterfaceTest1', ('tests.Mixin', 'InterfaceTest3')): IIinterfaceTest1 } } bis = RootTask.build_from_config(self.root.preferences, deps) interface = bis.children[0].interface.interface assert type(interface).__name__ == 'IIinterfaceTest1' assert self.root.children[0].database_entries ==\ {'test': 2.0, 'itest': 1.0}
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_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')
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]
def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = LoopTask(name='Test') self.root.add_child_task(0, self.task)
class TestLoopTask(object): """Test Loop task with and without included child. """ def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = LoopTask(name='Test') self.root.add_child_task(0, self.task) def test_subtask_handling(self): """Test adding, changing, removing the subtask. """ subtask1 = CheckTask(name='check', database_entries={'val': 1}) self.task.task = subtask1 assert subtask1.root is self.root assert subtask1.database is self.root.database assert subtask1.parent is self.task assert subtask1.path and subtask1.depth assert 'value' not in self.task.database_entries assert subtask1.get_from_database('check_val') assert self.task.preferences['task']['name'] == 'check' subtask2 = CheckTask(name='rep', database_entries={'new': 1}) self.task.task = subtask2 assert not subtask1.root assert not subtask1.parent with pytest.raises(KeyError): assert subtask1.get_from_database('check_val') assert subtask2.root is self.root assert subtask2.database is self.root.database assert subtask2.parent is self.task assert subtask2.path and subtask1.depth assert 'value' not in self.task.database_entries assert subtask2.get_from_database('rep_new') assert self.task.preferences['task']['name'] == 'rep' self.task.task = None assert not subtask2.root assert not subtask2.parent with pytest.raises(KeyError): assert subtask2.get_from_database('rep_new') assert 'value' in self.task.database_entries def test_traverse(self, linspace_interface): """Test traversing a with interfaces ComplexTask. """ self.task.interface = linspace_interface self.task.add_child_task(0, CheckTask(name='check')) assert len(list(self.task.traverse())) == 3 def test_saving_building_from_config(self, iterable_interface): """Done here as the LoopTask is a viable case of a member tagged with child. """ subtask1 = CheckTask(name='check', database_entries={'val': 1}) self.task.task = subtask1 self.root.update_preferences_from_members() deps = { 'ecpy.task': { 'ecpy.RootTask': RootTask, 'ecpy.LoopTask': LoopTask, 'ecpy.CheckTask': CheckTask } } new = RootTask.build_from_config(self.root.preferences, deps) assert new.children[0].task.name == 'check' self.task.interface = iterable_interface self.root.update_preferences_from_members() prefs = self.root.preferences del prefs['children_0']['task'] deps = { 'ecpy.task': { 'ecpy.RootTask': RootTask, 'ecpy.LoopTask': LoopTask, 'ecpy.CheckTask': CheckTask }, 'ecpy.tasks.interface': { ('IterableLoopInterface', ('ecpy.LoopTask', )): IterableLoopInterface } } new = RootTask.build_from_config(prefs, deps) assert not new.children[0].task def test_timing_handling(self): """Test enabling/disabling the timing. """ assert 'elapsed_time' not in self.task.database_entries self.task.timing = True assert 'elapsed_time' in self.task.database_entries self.task.timing = False assert 'elapsed_time' not in self.task.database_entries def test_check_missing(self): """Test handling a missing interface (check overridden so necessary). """ res, tb = self.task.check() assert not res assert len(tb) == 1 assert 'root/Test-interface' in tb def test_check_linspace_interface1(self, linspace_interface): """Test that everything is ok when all formulas are true. """ self.task.interface = linspace_interface test, traceback = self.task.check() assert test assert not traceback assert self.task.get_from_database('Test_point_number') == 11 def test_check_linspace_interface2(self, linspace_interface): """Test handling a wrong start. """ linspace_interface.start = '1.0*' self.task.interface = linspace_interface test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-start' in traceback def test_check_linspace_interface3(self, linspace_interface): """Test handling a wrong stop. """ linspace_interface.stop = '2.0*' self.task.interface = linspace_interface test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-stop' in traceback def test_check_linspace_interface4(self, linspace_interface): """Test handling a wrong step. """ linspace_interface.step = '0.1*' self.task.interface = linspace_interface test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-step' in traceback def test_check_linspace_interface5(self, linspace_interface): """Test handling a wrong number of point. """ linspace_interface.step = '0.0' self.task.interface = linspace_interface test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-points' in traceback def test_check_linspace_interface6(self, monkeypatch, linspace_interface): """Test handling an issue in linspace. """ self.task.interface = linspace_interface import ecpy.tasks.tasks.logic.loop_linspace_interface as li monkeypatch.setattr(li, 'linspace', lambda x: x) test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-linspace' in traceback def test_check_iterable_interface1(self, iterable_interface): """Test that everything is ok when all formulas are true. """ self.task.interface = iterable_interface test, traceback = self.task.check() assert test assert not traceback assert self.task.get_from_database('Test_point_number') == 11 iterable_interface.iterable = 'dict(a=1)' test, traceback = self.task.check() assert test assert not traceback assert self.task.get_from_database('Test_point_number') == 1 assert self.task.get_from_database('Test_value') == 'a' def test_check_iterable_interface2(self, iterable_interface): """Test handling a wrong iterable formula. """ iterable_interface.iterable = '*range(11)' self.task.interface = iterable_interface test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test-iterable' in traceback def test_check_iterable_interface3(self, iterable_interface): """Test handling a wrong iterable type. """ iterable_interface.iterable = '1.0' self.task.interface = iterable_interface test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test' in traceback def test_check_execution_order(self, iterable_interface): """Test that the interface checks are run before the children checks. """ iterable_interface.iterable = '[(1, 0)]' self.task.interface = iterable_interface subiter = IterableLoopInterface(iterable='{Test_value}') self.task.add_child_task(0, LoopTask(interface=subiter)) test, traceback = self.task.check() print(traceback) assert test def test_perform1(self, iterable_interface): """Test performing a simple loop no timing. Iterable interface. """ self.task.interface = iterable_interface self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_value') == 10 def test_perform2(self, linspace_interface): """Test performing a simple loop no timing. Linspace interface. """ self.task.interface = linspace_interface self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_value') == 2.0 def test_perform3(self, iterable_interface): """Test performing a simple loop no timing. Break. """ self.task.interface = iterable_interface self.task.add_child_task( 0, BreakTask(name='break', condition='{Test_value} == 5')) self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_value') == 5 def test_perform4(self, iterable_interface): """Test performing a simple loop no timing. Continue """ self.task.interface = iterable_interface for i, t in enumerate([ ContinueTask(name='break', condition='True'), CheckTask(name='check') ]): self.task.add_child_task(i, t) self.root.prepare() self.task.perform() assert not self.task.children[1].perform_called def test_perform_task1(self, iterable_interface): """Test performing a loop with an embedded task no timing. """ self.task.interface = iterable_interface self.task.task = CheckTask(name='check') self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_index') == 11 assert self.task.task.perform_called assert self.task.task.perform_value == 10 def test_perform_task2(self): """Test performing a loop with an embedded task no timing. Break. """ interface = IterableLoopInterface() interface.iterable = 'range(11)' self.task.interface = interface self.task.task = CheckTask(name='check') self.task.add_child_task( 0, BreakTask(name='Break', condition='{Test_index} == 6')) self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_index') == 6 assert self.task.task.perform_called assert self.task.task.perform_value == 5 def test_perform_task3(self, iterable_interface): """Test performing a loop with an embedded task no timing. Continue. """ self.task.interface = iterable_interface self.task.task = CheckTask(name='check') self.task.add_child_task( 0, ContinueTask(name='Continue', condition='True')) self.task.children.append(CheckTask(name='check')) self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_index') == 11 assert self.task.task.perform_called assert self.task.task.perform_value == 10 assert not self.task.children[1].perform_called def test_perform_timing1(self, iterable_interface): """Test performing a simple loop timing. """ self.task.interface = iterable_interface self.task.timing = True self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_value') == 10 assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_perform_timing2(self, iterable_interface): """Test performing a simple loop timing. Break. """ self.task.interface = iterable_interface self.task.timing = True self.task.add_child_task( 0, BreakTask(name='break', condition='{Test_value} == 0')) self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_value') == 0 assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_perform_timing3(self, iterable_interface): """Test performing a simple loop timing. Continue. """ self.task.interface = iterable_interface self.task.timing = True self.task.add_child_task( 0, ContinueTask(name='Continue', condition='True')) self.task.add_child_task(1, CheckTask(name='check')) self.root.prepare() self.task.perform() assert not self.task.children[1].perform_called assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_perform_timing_task1(self, iterable_interface): """Test performing a loop with an embedded task no timing. """ self.task.interface = iterable_interface self.task.timing = True self.task.task = CheckTask(name='check') self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_index') == 11 assert self.task.task.perform_called assert self.task.task.perform_value == 10 assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_perform_timing_task2(self, iterable_interface): """Test performing a loop with an embedded task no timing. Break. """ self.task.interface = iterable_interface self.task.timing = True self.task.task = CheckTask(name='check') self.task.add_child_task( 0, BreakTask(name='break', condition='{Test_index} == 1')) self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_index') == 1 assert self.task.task.perform_called assert self.task.task.perform_value == 0 assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_perform_timing_task3(self, iterable_interface): """Test performing a loop with an embedded task no timing. Continue. """ self.task.interface = iterable_interface self.task.timing = True self.task.task = CheckTask(name='check') self.task.add_child_task(0, ContinueTask(name='break', condition='True')) self.task.add_child_task(1, CheckTask(name='check')) self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_index') == 11 assert self.task.task.perform_called assert self.task.task.perform_value == 10 assert not self.task.children[1].perform_called assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_performing_stop1(self, iterable_interface): """Test handling stop in the middle of an iteration. no child, no timing. """ self.task.interface = iterable_interface stop = lambda t, v: t.root.should_stop.set() self.task.add_child_task( 0, CheckTask(name='Stop', custom=stop, stoppable=False)) self.task.prepare() self.task.perform() assert self.task.children[0].perform_called == 1 def test_performing_stop2(self, iterable_interface): """Test handling stop in the middle of an iteration. No child, timing. """ self.task.timing = True self.task.interface = iterable_interface stop = lambda t, v: t.root.should_stop.set() self.task.add_child_task( 0, CheckTask(name='Stop', custom=stop, stoppable=False)) self.task.prepare() self.task.perform() assert self.task.children[0].perform_called == 1 def test_performing_stop3(self, iterable_interface): """Test handling stop in the middle of an iteration. Child, no timing """ self.task.interface = iterable_interface self.task.task = CheckTask(name='check') stop = lambda t, v: t.root.should_stop.set() self.task.add_child_task( 0, CheckTask(name='Stop', custom=stop, stoppable=False)) self.task.prepare() self.task.perform() assert self.task.children[0].perform_called == 1 def test_performing_stop4(self, iterable_interface): """Test handling stop in the middle of an iteration. Child, timing """ self.task.timing = True self.task.interface = iterable_interface self.task.task = CheckTask(name='check') stop = lambda t, v: t.root.should_stop.set() self.task.add_child_task( 0, CheckTask(name='Stop', custom=stop, stoppable=False)) self.task.prepare() self.task.perform() assert self.task.children[0].perform_called == 1 @pytest.mark.ui def test_view(self, windows, task_workbench): """Test the LoopTask view. """ core = task_workbench.get_plugin('enaml.workbench.core') root = RootTaskView(core=core) show_and_close_widget(LoopView(task=self.task, root=root)) @pytest.mark.ui def test_view_with_subtask(self, windows, task_workbench): """Test the LoopTask view. """ core = task_workbench.get_plugin('enaml.workbench.core') root = RootTaskView(core=core) self.task.task = BreakTask(name='Aux') show_and_close_widget(LoopView(task=self.task, root=root))
class TestInterfaceableInterfaceMixin(object): """Test the capabilities of task interfaces. """ def setup(self): self.root = RootTask() self.mixin = InterfaceTest3() self.root.add_child_task(0, Mixin(name='Simple', interface=self.mixin)) def test_interface_observer(self): """Test changing the interface. """ i1 = IIinterfaceTest1() i2 = IIinterfaceTest2() self.mixin.interface = i1 assert i1.parent is self.mixin assert i1.task is self.mixin.task assert i1.interface_id[1] ==\ (self.mixin.interface_id[1] + (self.mixin.interface_id[0],)) assert self.mixin.task.database_entries == {'test': 2.0, 'itest': 1.0} self.mixin.interface = i2 assert i2.task is self.mixin.task assert i1.parent is None with pytest.raises(AttributeError): i1.task assert self.mixin.task.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 = IIinterfaceTest1(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. """ interface = InterfaceTest4() self.root.children[0].interface = interface res, traceback = interface.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/InterfaceTest3-interface' in traceback def test_check4(self): """Test handling a non-passing test from the interface. """ self.mixin.interface = IIinterfaceTest1() 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 = IIinterfaceTest2(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 = IIinterfaceTest2(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 = IIinterfaceTest1() self.root.database.prepare_to_run() self.mixin.perform() assert self.root.get_from_database('Simple_itest') == 2.0 def test_perform2(self): """Test perform use i_perform when no interface exists. """ self.mixin = InterfaceTest4() self.root.children[0].interface = 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 interface with no interface from a config. """ aux = RootTask() mixin = Mixin() mixin.interface = InterfaceTest3() aux.add_child_task(0, mixin) deps = { 'ecpy.task': { 'tests.Mixin': Mixin, 'ecpy.RootTask': RootTask }, 'ecpy.tasks.interface': { ('InterfaceTest3', ('tests.Mixin', )): InterfaceTest3 } } bis = RootTask.build_from_config(aux.preferences, deps) assert type(bis.children[0].interface).__name__ == 'InterfaceTest3' def test_build_from_config2(self): """Test building a interfaceable interface with an interface from a config. """ self.mixin.interface = IIinterfaceTest1(answer=True) self.root.update_preferences_from_members() deps = { 'ecpy.task': { 'tests.Mixin': Mixin, 'ecpy.RootTask': RootTask }, 'ecpy.tasks.interface': { ('InterfaceTest3', ('tests.Mixin', )): InterfaceTest3, ('IIinterfaceTest1', ('tests.Mixin', 'InterfaceTest3')): IIinterfaceTest1 } } bis = RootTask.build_from_config(self.root.preferences, deps) interface = bis.children[0].interface.interface assert type(interface).__name__ == 'IIinterfaceTest1' assert self.root.children[0].database_entries ==\ {'test': 2.0, 'itest': 1.0} def test_traverse(self): """Test traversing a task with an interfaceable interface. """ class Test(InterfaceableInterfaceMixin, IIinterfaceTest2): pass iaux = IIinterfaceTest1() self.mixin.interface = Test() self.mixin.interface.interface = iaux task = self.root.children[0] w = list(task.traverse()) assert w == [task, self.mixin, self.mixin.interface, iaux] w = list(task.traverse(1)) assert w == [task, self.mixin, self.mixin.interface]
def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event())
class TestInterfaceableInterfaceMixin(object): """Test the capabilities of task interfaces. """ def setup(self): self.root = RootTask() self.mixin = InterfaceTest3() self.root.add_child_task(0, Mixin(name='Simple', interface=self.mixin)) def test_interface_observer(self): """Test changing the interface. """ i1 = IIinterfaceTest1() i2 = IIinterfaceTest2() self.mixin.interface = i1 assert i1.parent is self.mixin assert i1.task is self.mixin.task assert i1.interface_id[1] ==\ (self.mixin.interface_id[1] + (self.mixin.interface_id[0],)) assert self.mixin.task.database_entries == {'test': 2.0, 'itest': 1.0} self.mixin.interface = i2 assert i2.task is self.mixin.task assert i1.parent is None with pytest.raises(AttributeError): i1.task assert self.mixin.task.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 = IIinterfaceTest1(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. """ interface = InterfaceTest4() self.root.children[0].interface = interface res, traceback = interface.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/InterfaceTest3-interface' in traceback def test_check4(self): """Test handling a non-passing test from the interface. """ self.mixin.interface = IIinterfaceTest1() 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 = IIinterfaceTest2(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 = IIinterfaceTest2(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 = IIinterfaceTest1() self.root.database.prepare_to_run() self.mixin.perform() assert self.root.get_from_database('Simple_itest') == 2.0 def test_perform2(self): """Test perform use i_perform when no interface exists. """ self.mixin = InterfaceTest4() self.root.children[0].interface = 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 interface with no interface from a config. """ aux = RootTask() mixin = Mixin() mixin.interface = InterfaceTest3() aux.add_child_task(0, mixin) deps = {'ecpy.task': {'tests.Mixin': Mixin, 'ecpy.RootTask': RootTask}, 'ecpy.tasks.interface': {('InterfaceTest3', ('tests.Mixin',)): InterfaceTest3}} bis = RootTask.build_from_config(aux.preferences, deps) assert type(bis.children[0].interface).__name__ == 'InterfaceTest3' def test_build_from_config2(self): """Test building a interfaceable interface with an interface from a config. """ self.mixin.interface = IIinterfaceTest1(answer=True) self.root.update_preferences_from_members() deps = {'ecpy.task': {'tests.Mixin': Mixin, 'ecpy.RootTask': RootTask}, 'ecpy.tasks.interface': {('InterfaceTest3', ('tests.Mixin',)): InterfaceTest3, ('IIinterfaceTest1', ('tests.Mixin', 'InterfaceTest3')): IIinterfaceTest1 } } bis = RootTask.build_from_config(self.root.preferences, deps) interface = bis.children[0].interface.interface assert type(interface).__name__ == 'IIinterfaceTest1' assert self.root.children[0].database_entries ==\ {'test': 2.0, 'itest': 1.0} def test_traverse(self): """Test traversing a task with an interfaceable interface. """ class Test(InterfaceableInterfaceMixin, IIinterfaceTest2): pass iaux = IIinterfaceTest1() self.mixin.interface = Test() self.mixin.interface.interface = iaux task = self.root.children[0] w = list(task.traverse()) assert w == [task, self.mixin, self.mixin.interface, iaux] w = list(task.traverse(1)) assert w == [task, self.mixin, self.mixin.interface]
class TestEvaluation(object): """Test evaluating strings and caching in running mode. """ 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_eval_pure_string_editing_mode(self): """Test evaluating a word. """ test = 'test' formatted = self.root.format_and_eval_string(test) assert formatted == 'test' def test_eval_editing_mode1(self): """Test eval expression with only standard operators. """ test = '{val1}/{val2}' formatted = self.root.format_and_eval_string(test) assert formatted == 0.1 assert not self.root._eval_cache def test_eval_editing_mode2(self): """Test eval expression containing a math function. """ test = 'cos({val1}/{val2})' formatted = self.root.format_and_eval_string(test) assert formatted == cos(0.1) assert not self.root._eval_cache def test_eval_editing_mode3(self): """Test eval expression containing a cmath function. """ self.root.database.set_value('root', 'val1', 10.0) test = 'cm.sqrt({val1}/{val2})' formatted = self.root.format_and_eval_string(test) assert formatted == 1 + 0j assert not self.root._eval_cache def test_eval_editing_mode4(self): """Test eval expression containing a numpy function. """ self.root.database.set_value('root', 'val1', [1.0, -1.0]) test = 'np.abs({val1})' formatted = self.root.format_and_eval_string(test) assert_array_equal(formatted, numpy.array((1.0, 1.0))) assert not self.root._eval_cache def test_eval_pure_string_running_mode(self): """Test evaluating a word. """ self.root.database.prepare_to_run() test = 'test' formatted = self.root.format_and_eval_string(test) assert formatted == 'test' def test_eval_running_mode1(self): """Test eval expression with only standard operators. """ self.root.database.prepare_to_run() test = '{val1}/{val2}' formatted = self.root.format_and_eval_string(test) assert formatted == 0.1 assert self.root._eval_cache assert test in self.root._eval_cache self.root.database.set_value('root', 'val1', 2) formatted = self.root.format_and_eval_string(test) assert formatted == 0.2 def test_eval_running_mode2(self): """Test eval expression containing a math function. """ self.root.database.prepare_to_run() test = 'cos({val1}/{val2})' formatted = self.root.format_and_eval_string(test) assert formatted == cos(0.1) assert self.root._eval_cache assert test in self.root._eval_cache self.root.database.set_value('root', 'val1', 2) formatted = self.root.format_and_eval_string(test) assert formatted == cos(0.2) def test_eval_running_mode3(self): """Test eval expression containing a cmath function. """ self.root.database.prepare_to_run() self.root.database.set_value('root', 'val1', 10.0) test = 'cm.sqrt({val1}/{val2})' formatted = self.root.format_and_eval_string(test) assert formatted == (1 + 0j) assert self.root._eval_cache assert test in self.root._eval_cache self.root.database.set_value('root', 'val1', 40.0) formatted = self.root.format_and_eval_string(test) assert formatted == (2 + 0j) def test_eval_running_mode4(self): """Test eval expression containing a numpy function. """ self.root.database.prepare_to_run() self.root.database.set_value('root', 'val1', [1.0, -1.0]) test = 'np.abs({val1})' formatted = self.root.format_and_eval_string(test) assert_array_equal(formatted, numpy.array((1.0, 1.0))) assert self.root._eval_cache assert test in self.root._eval_cache self.root.database.set_value('root', 'val1', [2.0, -1.0]) self.root.database.set_value('root', 'val2', 0) test = 'np.abs({val1})[{val2}]' formatted = self.root.format_and_eval_string(test) assert formatted == 2.0
def setup(self): self.root = RootTask() self.mixin = Mixin(name='Simple') self.root.add_child_task(0, self.mixin)
class TestWhileTask(object): """The Whiletask behaviour. """ def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = WhileTask(name='Test') self.root.add_child_task(0, self.task) self.check = CheckTask(name='check') self.task.add_child_task(0, self.check) def test_check1(self): """Simply test that everything is ok if condition is evaluable. """ self.task.condition = 'True' test, traceback = self.task.check() assert test assert not traceback assert self.check.check_called def test_check2(self): """Test handling a wrong condition. """ self.task.condition = '*True' test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-condition' in traceback def test_perform1(self): """Test performing when condition is True. """ self.task.condition = '{Test_index} < 5' self.root.prepare() self.task.perform() assert self.check.perform_called == 4 def test_perform2(self): """Test performing when condition is False. """ self.task.condition = '1 < 0' self.root.prepare() self.task.perform() assert not self.check.perform_called def test_perform3(self): """Test handling of BreakTask and ContinueTask. """ self.task.condition = 'True' self.task.add_child_task(0, BreakTask(name='Break', condition='True')) self.task.add_child_task(0, ContinueTask(name='Continue', condition='{Test_index} < 5')) self.root.prepare() self.task.perform() assert not self.check.perform_called assert self.task.get_from_database('Test_index') == 5 @pytest.mark.timeout(1) def test_perform4(self): """Test handling stopping while iterating. """ self.task.condition = 'True' stop = lambda t, v: t.root.should_stop.set() self.task.add_child_task(0, CheckTask(name='Stop', custom=stop, stoppable=False)) self.root.prepare() self.task.perform() assert self.task.children[0].perform_called == 1
def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = ConditionalTask(name='Test') self.root.add_child_task(0, self.task) self.check = CheckTask(name='check') self.task.add_child_task(0, self.check)
class TestFormatting(object): """Test formatting strings and caching in running mode. """ 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_formatting_editing_mode1(self): """Test formatting values with text on both sides of expression. """ test = 'progress is {val1}/{val2}, it is good.' formatted = self.root.format_string(test) assert formatted == 'progress is 1/10.0, it is good.' assert not self.root._format_cache def test_formatting_editing_mode2(self): """Test formatting values with text only on the left of expression. """ test = 'progress is {val1}/{val2}' formatted = self.root.format_string(test) assert formatted == 'progress is 1/10.0' assert not self.root._format_cache def test_formatting_editing_mode3(self): """Test formatting values with text only on the right of expression. """ test = '{val1}/{val2}, it is good.' formatted = self.root.format_string(test) assert formatted == '1/10.0, it is good.' assert not self.root._format_cache def test_formatting_editing_mode4(self): """Test formatting values with no other text. """ test = '{val1}/{val2}' formatted = self.root.format_string(test) assert formatted == '1/10.0' assert not self.root._format_cache def test_formatting_editing_mode5(self): """Test formatting when there is only text. """ test = 'test' formatted = self.root.format_string(test) assert formatted == 'test' assert not self.root._format_cache def test_formatting_running_mode1(self): """Test formatting values with text on both sides of expression. """ self.root.database.prepare_to_run() test = 'progress is {val1}/{val2}, it is good.' formatted = self.root.format_string(test) assert formatted == 'progress is 1/10.0, it is good.' assert self.root._format_cache assert test in self.root._format_cache self.root.database.set_value('root', 'val1', 2) formatted = self.root.format_string(test) assert formatted == 'progress is 2/10.0, it is good.' def test_formatting_running_mode2(self): """Test formatting values with text only on the left of expression. """ self.root.database.prepare_to_run() test = 'progress is {val1}/{val2}' formatted = self.root.format_string(test) assert formatted == 'progress is 1/10.0' assert self.root._format_cache assert test in self.root._format_cache self.root.database.set_value('root', 'val1', 2) formatted = self.root.format_string(test) assert formatted == 'progress is 2/10.0' def test_formatting_running_mode3(self): """Test formatting values with text only on the right of expression. """ self.root.database.prepare_to_run() test = '{val1}/{val2}, it is good.' formatted = self.root.format_string(test) assert formatted == '1/10.0, it is good.' assert self.root._format_cache assert test in self.root._format_cache self.root.database.set_value('root', 'val1', 2) formatted = self.root.format_string(test) assert formatted == '2/10.0, it is good.' def test_formatting_running_mode4(self): """Test formatting values with no other text. """ self.root.database.prepare_to_run() test = '{val1}/{val2}' formatted = self.root.format_string(test) assert formatted == '1/10.0' assert self.root._format_cache assert test in self.root._format_cache self.root.database.set_value('root', 'val1', 2) formatted = self.root.format_string(test) assert formatted == '2/10.0' def test_formatting_running_mode5(self): """Test formatting when there is only text. """ self.root.database.prepare_to_run() test = 'test' formatted = self.root.format_string(test) assert formatted == 'test' assert self.root._format_cache assert test in self.root._format_cache
class TestLoopTask(object): """Test Loop task with and without included child. """ def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = LoopTask(name='Test') self.root.add_child_task(0, self.task) def test_subtask_handling(self): """Test adding, changing, removing the subtask. """ subtask1 = CheckTask(name='check', database_entries={'val': 1}) self.task.task = subtask1 assert subtask1.root is self.root assert subtask1.database is self.root.database assert subtask1.parent is self.task assert subtask1.path and subtask1.depth assert 'value' not in self.task.database_entries assert subtask1.get_from_database('check_val') assert self.task.preferences['task']['name'] == 'check' subtask2 = CheckTask(name='rep', database_entries={'new': 1}) self.task.task = subtask2 assert not subtask1.root assert not subtask1.parent with pytest.raises(KeyError): assert subtask1.get_from_database('check_val') assert subtask2.root is self.root assert subtask2.database is self.root.database assert subtask2.parent is self.task assert subtask2.path and subtask1.depth assert 'value' not in self.task.database_entries assert subtask2.get_from_database('rep_new') assert self.task.preferences['task']['name'] == 'rep' self.task.task = None assert not subtask2.root assert not subtask2.parent with pytest.raises(KeyError): assert subtask2.get_from_database('rep_new') assert 'value' in self.task.database_entries def test_traverse(self, linspace_interface): """Test traversing a with interfaces ComplexTask. """ self.task.interface = linspace_interface self.task.add_child_task(0, CheckTask(name='check')) assert len(list(self.task.traverse())) == 3 def test_saving_building_from_config(self): """Done here as the LoopTask is a viable case of a member tagged with child. """ subtask1 = CheckTask(name='check', database_entries={'val': 1}) self.task.task = subtask1 self.root.update_preferences_from_members() new = RootTask.build_from_config(self.root.preferences, {'ecpy.task': {'RootTask': RootTask, 'LoopTask': LoopTask, 'CheckTask': CheckTask} }) assert new.children[0].task.name == 'check' self.root.update_preferences_from_members() prefs = self.root.preferences del prefs['children_0']['task'] new = RootTask.build_from_config(prefs, {'ecpy.task': {'RootTask': RootTask, 'LoopTask': LoopTask, 'CheckTask': CheckTask} }) assert not new.children[0].task def test_timing_handling(self): """Test enabling/disabling the timing. """ assert 'elapsed_time' not in self.task.database_entries self.task.timing = True assert 'elapsed_time' in self.task.database_entries self.task.timing = False assert 'elapsed_time' not in self.task.database_entries def test_check_missing(self): """Test handling a missing interface (check overridden so necessary). """ res, tb = self.task.check() assert not res assert len(tb) == 1 assert 'root/Test-interface' in tb def test_check_linspace_interface1(self, linspace_interface): """Test that everything is ok when all formulas are true. """ self.task.interface = linspace_interface test, traceback = self.task.check() assert test assert not traceback assert self.task.get_from_database('Test_point_number') == 11 def test_check_linspace_interface2(self, linspace_interface): """Test handling a wrong start. """ linspace_interface.start = '1.0*' self.task.interface = linspace_interface test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-start' in traceback def test_check_linspace_interface3(self, linspace_interface): """Test handling a wrong stop. """ linspace_interface.stop = '2.0*' self.task.interface = linspace_interface test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-stop' in traceback def test_check_linspace_interface4(self, linspace_interface): """Test handling a wrong step. """ linspace_interface.step = '0.1*' self.task.interface = linspace_interface test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-step' in traceback def test_check_linspace_interface5(self, linspace_interface): """Test handling a wrong number of point. """ linspace_interface.step = '0.0' self.task.interface = linspace_interface test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-points' in traceback def test_check_linspace_interface6(self, monkeypatch, linspace_interface): """Test handling an issue in linspace. """ self.task.interface = linspace_interface import ecpy.tasks.tasks.logic.loop_linspace_interface as li monkeypatch.setattr(li, 'linspace', lambda x: x) test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-linspace' in traceback def test_check_iterable_interface1(self, iterable_interface): """Test that everything is ok when all formulas are true. """ self.task.interface = iterable_interface test, traceback = self.task.check() assert test assert not traceback assert self.task.get_from_database('Test_point_number') == 11 iterable_interface.iterable = 'dict(a=1)' test, traceback = self.task.check() assert test assert not traceback assert self.task.get_from_database('Test_point_number') == 1 assert self.task.get_from_database('Test_value') == 'a' def test_check_iterable_interface2(self, iterable_interface): """Test handling a wrong iterable formula. """ iterable_interface.iterable = '*range(11)' self.task.interface = iterable_interface test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test-iterable' in traceback def test_check_iterable_interface3(self, iterable_interface): """Test handling a wrong iterable type. """ iterable_interface.iterable = '1.0' self.task.interface = iterable_interface test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test' in traceback def test_check_execution_order(self, iterable_interface): """Test that the interface checks are run before the children checks. """ iterable_interface.iterable = '[(1, 0)]' self.task.interface = iterable_interface subiter = IterableLoopInterface(iterable='{Test_value}') self.task.add_child_task(0, LoopTask(interface=subiter)) test, traceback = self.task.check() print(traceback) assert test def test_perform1(self, iterable_interface): """Test performing a simple loop no timing. Iterable interface. """ self.task.interface = iterable_interface self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.root.get_from_database('Test_value') == 10 def test_perform2(self, linspace_interface): """Test performing a simple loop no timing. Linspace interface. """ self.task.interface = linspace_interface self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.root.get_from_database('Test_value') == 2.0 def test_perform3(self, iterable_interface): """Test performing a simple loop no timing. Break. """ self.task.interface = iterable_interface self.task.add_child_task(0, BreakTask(name='break', condition='{Test_value} == 5') ) self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.root.get_from_database('Test_value') == 5 def test_perform4(self, iterable_interface): """Test performing a simple loop no timing. Continue """ self.task.interface = iterable_interface for i, t in enumerate([ContinueTask(name='break', condition='True'), CheckTask(name='check')]): self.task.add_child_task(i, t) self.root.database.prepare_for_running() self.root.check() self.task.perform() assert not self.task.children[1].perform_called def test_perform_task1(self, iterable_interface): """Test performing a loop with an embedded task no timing. """ self.task.interface = iterable_interface self.task.task = CheckTask(name='check') self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.root.get_from_database('Test_index') == 11 assert self.task.task.perform_called assert self.task.task.perform_value == 10 def test_perform_task2(self): """Test performing a loop with an embedded task no timing. Break. """ interface = IterableLoopInterface() interface.iterable = 'range(11)' self.task.interface = interface self.task.task = CheckTask(name='check') self.task.add_child_task(0, BreakTask(name='Break', condition='{Test_index} == 6') ) self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.root.get_from_database('Test_index') == 6 assert self.task.task.perform_called assert self.task.task.perform_value == 5 def test_perform_task3(self, iterable_interface): """Test performing a loop with an embedded task no timing. Continue. """ self.task.interface = iterable_interface self.task.task = CheckTask(name='check') self.task.add_child_task(0, ContinueTask(name='Continue', condition='True') ) self.task.children.append(CheckTask(name='check')) self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.root.get_from_database('Test_index') == 11 assert self.task.task.perform_called assert self.task.task.perform_value == 10 assert not self.task.children[1].perform_called def test_perform_timing1(self, iterable_interface): """Test performing a simple loop timing. """ self.task.interface = iterable_interface self.task.timing = True self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.root.get_from_database('Test_value') == 10 assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_perform_timing2(self, iterable_interface): """Test performing a simple loop timing. Break. """ self.task.interface = iterable_interface self.task.timing = True self.task.add_child_task(0, BreakTask(name='break', condition='{Test_value} == 0') ) self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.root.get_from_database('Test_value') == 0 assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_perform_timing3(self, iterable_interface): """Test performing a simple loop timing. Continue. """ self.task.interface = iterable_interface self.task.timing = True self.task.add_child_task(0, ContinueTask(name='Continue', condition='True') ) self.task.add_child_task(1, CheckTask(name='check')) self.root.database.prepare_for_running() self.root.check() self.task.perform() assert not self.task.children[1].perform_called assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_perform_timing_task1(self, iterable_interface): """Test performing a loop with an embedded task no timing. """ self.task.interface = iterable_interface self.task.timing = True self.task.task = CheckTask(name='check') self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.root.get_from_database('Test_index') == 11 assert self.task.task.perform_called assert self.task.task.perform_value == 10 assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_perform_timing_task2(self, iterable_interface): """Test performing a loop with an embedded task no timing. Break. """ self.task.interface = iterable_interface self.task.timing = True self.task.task = CheckTask(name='check') self.task.add_child_task(0, BreakTask(name='break', condition='{Test_index} == 1') ) self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.root.get_from_database('Test_index') == 1 assert self.task.task.perform_called assert self.task.task.perform_value == 0 assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_perform_timing_task3(self, iterable_interface): """Test performing a loop with an embedded task no timing. Continue. """ self.task.interface = iterable_interface self.task.timing = True self.task.task = CheckTask(name='check') self.task.add_child_task(0, ContinueTask(name='break', condition='True') ) self.task.add_child_task(1, CheckTask(name='check')) self.root.database.prepare_for_running() self.root.check() self.task.perform() assert self.root.get_from_database('Test_index') == 11 assert self.task.task.perform_called assert self.task.task.perform_value == 10 assert not self.task.children[1].perform_called assert self.root.get_from_database('Test_elapsed_time') != 1.0 def test_performing_stop1(self, iterable_interface): """Test handling stop in the middle of an iteration. no child, no timing. """ self.task.interface = iterable_interface stop = lambda t, v: t.root.should_stop.set() self.task.add_child_task(0, CheckTask(name='Stop', custom=stop, stoppable=False)) self.root.check() self.task.perform() assert self.task.children[0].perform_called == 1 def test_performing_stop2(self, iterable_interface): """Test handling stop in the middle of an iteration. No child, timing. """ self.task.timing = True self.task.interface = iterable_interface stop = lambda t, v: t.root.should_stop.set() self.task.add_child_task(0, CheckTask(name='Stop', custom=stop, stoppable=False)) self.root.check() self.task.perform() assert self.task.children[0].perform_called == 1 def test_performing_stop3(self, iterable_interface): """Test handling stop in the middle of an iteration. Child, no timing """ self.task.interface = iterable_interface self.task.task = CheckTask(name='check') stop = lambda t, v: t.root.should_stop.set() self.task.add_child_task(0, CheckTask(name='Stop', custom=stop, stoppable=False)) self.root.check() self.task.perform() assert self.task.children[0].perform_called == 1 def test_performing_stop4(self, iterable_interface): """Test handling stop in the middle of an iteration. Child, timing """ self.task.timing = True self.task.interface = iterable_interface self.task.task = CheckTask(name='check') stop = lambda t, v: t.root.should_stop.set() self.task.add_child_task(0, CheckTask(name='Stop', custom=stop, stoppable=False)) self.root.check() self.task.perform() assert self.task.children[0].perform_called == 1
def setup(self): self.root = RootTask() self.mixin = InterfaceTest3() self.root.add_child_task(0, Mixin(name='Simple', interface=self.mixin))
class TestExceptionTasks(object): """Test Break and Continue tasks checks, perform will be tested in each looping task """ def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) def test_check1(self, exception_task): """Test that everything is ok condition is evaluable and parent is a Loop. """ loop = LoopTask() loop.add_child_task(0, exception_task) self.root.add_child_task(0, loop) exception_task.condition = 'True' test, traceback = exception_task.check() assert test assert not traceback def test_check2(self, exception_task): """Simply test that everything is ok condition is evaluable and parent is a While. """ whil = WhileTask() whil.add_child_task(0, exception_task) exception_task.condition = 'True' self.root.add_child_task(0, whil) test, traceback = exception_task.check() assert test assert not traceback def test_check3(self, exception_task): """Test handling a wrong condition. """ loop = LoopTask(name='Parent') loop.add_child_task(0, exception_task) exception_task.condition = '*True' self.root.add_child_task(0, loop) test, traceback = exception_task.check() assert not test assert len(traceback) == 1 assert 'root/Parent/Test-condition' in traceback def test_check4(self, exception_task): """Test handling a wrong parent type. """ self.root.add_child_task(0, exception_task) exception_task.condition = 'True' test, traceback = exception_task.check() assert not test assert len(traceback) == 1 assert 'root/Test-parent' in traceback
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]