Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
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('Test_val')
        assert self.task.preferences['task']['name'] == 'Test'

        subtask2 = CheckTask(name='rep', database_entries={'new': 1})
        self.task.task = subtask2

        assert not subtask1.root
        assert not subtask1.parent
        with pytest.raises(KeyError):
            subtask1.get_from_database('Test_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('Test_new')
        assert self.task.preferences['task']['name'] == 'Test'

        self.task.name += '2'
        assert subtask2.name == self.task.name

        self.task.task = None

        assert not subtask2.root
        assert not subtask2.parent
        with pytest.raises(KeyError):
            subtask2.get_from_database('Test2_new')
        assert 'value' in self.task.database_entries

        # check that we handle properly the case of no subtask
        self.task.name += '2'

    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 = {
            'exopy.task': {
                'exopy.RootTask': RootTask,
                'exopy.LoopTask': LoopTask,
                'exopy.CheckTask': CheckTask
            }
        }
        new = RootTask.build_from_config(self.root.preferences, deps)

        assert new.children[0].task.name == 'Test'

        self.task.interface = iterable_interface
        self.root.update_preferences_from_members()
        prefs = self.root.preferences
        assert prefs['children_0'].sections[1] == 'task'
        del prefs['children_0']['task']
        deps = {
            'exopy.task': {
                'exopy.RootTask': RootTask,
                'exopy.LoopTask': LoopTask,
                'exopy.CheckTask': CheckTask
            },
            'exopy.tasks.interface': {
                'exopy.LoopTask:exopy.IterableLoopInterface':
                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
        assert self.task.get_from_database('Test_value') == 1.0

    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 exopy.tasks.tasks.logic.loop_linspace_interface as li
        monkeypatch.setattr(li.np, 'arange', lambda x: x)

        test, traceback = self.task.check(test_instr=True)
        assert not test
        assert len(traceback) == 1
        assert 'root/Test-arange' 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-iterable' 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

    def test_view(self, exopy_qtbot, task_workbench):
        """Test the LoopTask view.

        """
        core = task_workbench.get_plugin('enaml.workbench.core')
        root = RootTaskView(core=core)
        show_and_close_widget(exopy_qtbot, LoopView(task=self.task, root=root))

    def test_view_interface_not_inline(self, exopy_qtbot, task_workbench,
                                       linspace_interface):
        """Test the LoopTask view.

        """
        core = task_workbench.get_plugin('enaml.workbench.core')
        root = RootTaskView(core=core)
        self.task.interface = linspace_interface
        show_and_close_widget(exopy_qtbot, LoopView(task=self.task, root=root))

    def test_view_with_subtask(self, exopy_qtbot, 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(exopy_qtbot, LoopView(task=self.task, root=root))

    def test_view_changing_interface(self, exopy_qtbot, task_workbench):
        """Test the LoopTask view.

        """
        core = task_workbench.get_plugin('enaml.workbench.core')
        root = RootTaskView(core=core)
        view = LoopView(task=self.task, root=root)
        show_widget(exopy_qtbot, view)
        selector = view.widgets()[2]
        current_interface = view.task.interface
        selector.selected = selector.items[1]

        def assert_interface_changed():
            assert view.task.interface is not current_interface

        exopy_qtbot.wait_until(assert_interface_changed)

        current_interface = view.task.interface
        selector.selected = selector.items[0]

        def assert_interface_changed():
            assert view.task.interface is not current_interface

        exopy_qtbot.wait_until(assert_interface_changed)
Ejemplo n.º 4
0
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 teardown(self):
        del self.root.should_pause
        del self.root.should_stop
        # Ensure we collect the file descriptor of the events. Otherwise we can
        # get funny errors on MacOS.
        gc.collect()

    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('Test_val')
        assert self.task.preferences['task']['name'] == 'Test'

        subtask2 = CheckTask(name='rep', database_entries={'new': 1})
        self.task.task = subtask2

        assert not subtask1.root
        assert not subtask1.parent
        with pytest.raises(KeyError):
            subtask1.get_from_database('Test_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('Test_new')
        assert self.task.preferences['task']['name'] == 'Test'

        self.task.name += '2'
        assert subtask2.name == self.task.name

        self.task.task = None

        assert not subtask2.root
        assert not subtask2.parent
        with pytest.raises(KeyError):
            subtask2.get_from_database('Test2_new')
        assert 'value' in self.task.database_entries

        # check that we handle properly the case of no subtask
        self.task.name += '2'

    def test_renaming(self):
        """Test renaming the task.

        """
        subtask1 = CheckTask(name='check', database_entries={'val': 1})
        self.task.task = subtask1
        assert subtask1.get_from_database('Test_val') == 1

        self.task.name = 'Test2'
        assert subtask1.get_from_database('Test2_val') == 1
        assert self.task.get_from_database('Test2_point_number') == 11

    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 = {'exopy.task': {'exopy.RootTask': RootTask,
                               'exopy.LoopTask': LoopTask,
                               'exopy.CheckTask': CheckTask}
                }
        new = RootTask.build_from_config(self.root.preferences, deps)

        assert new.children[0].task.name == 'Test'

        self.task.interface = iterable_interface
        self.root.update_preferences_from_members()
        prefs = self.root.preferences
        assert prefs['children_0'].sections[1] == 'task'
        del prefs['children_0']['task']
        deps = {'exopy.task': {'exopy.RootTask': RootTask,
                               'exopy.LoopTask': LoopTask,
                               'exopy.CheckTask': CheckTask},
                'exopy.tasks.interface':
                    {'exopy.LoopTask:exopy.IterableLoopInterface':
                        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
        assert self.task.get_from_database('Test_value') == 1.0

    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 exopy.tasks.tasks.logic.loop_linspace_interface as li
        monkeypatch.setattr(li.np, 'arange', lambda x: x)

        test, traceback = self.task.check(test_instr=True)
        assert not test
        assert len(traceback) == 1
        assert 'root/Test-arange' 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-iterable' 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

    def test_view(self, exopy_qtbot, task_workbench):
        """Test the LoopTask view.

        """
        core = task_workbench.get_plugin('enaml.workbench.core')
        root = RootTaskView(core=core)
        show_and_close_widget(exopy_qtbot, LoopView(task=self.task, root=root))

    def test_view_interface_not_inline(self, exopy_qtbot, task_workbench,
                                       linspace_interface):
        """Test the LoopTask view.

        """
        core = task_workbench.get_plugin('enaml.workbench.core')
        root = RootTaskView(core=core)
        self.task.interface = linspace_interface
        show_and_close_widget(exopy_qtbot, LoopView(task=self.task, root=root))

    def test_view_with_subtask(self, exopy_qtbot, 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(exopy_qtbot, LoopView(task=self.task, root=root))

    def test_view_changing_interface(self, exopy_qtbot, task_workbench):
        """Test the LoopTask view.

        """
        core = task_workbench.get_plugin('enaml.workbench.core')
        root = RootTaskView(core=core)
        view = LoopView(task=self.task, root=root)
        show_widget(exopy_qtbot, view)
        selector = view.widgets()[2]
        current_interface = view.task.interface
        selector.selected = selector.items[1]

        def assert_interface_changed():
            assert view.task.interface is not current_interface
        exopy_qtbot.wait_until(assert_interface_changed)

        current_interface = view.task.interface
        selector.selected = selector.items[0]

        def assert_interface_changed():
            assert view.task.interface is not current_interface
        exopy_qtbot.wait_until(assert_interface_changed)