class TestApplyMagFieldTask(object): def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = ApplyMagFieldTask(name='Test', parallel={'activated': False}) self.root.add_child_task(0, self.task) self.root.run_time[DRIVERS] = { 'Test': (InstrHelper, InstrHelperStarter()) } self.root.run_time[PROFILES] =\ {'Test1': {'connections': {'C': {'owner': [], 'output_fluctuations': 1e-6, 'heater_state': []}}, 'settings': {'S': {'sweep_to_field': [DummyJob(), DummyJob(), DummyJob()], 'sweep_to_persistent_field': [DummyJob()], 'read_persistent_field': [1], 'check_connection': [True]}} } } # This is set simply to make sure the test of InstrTask pass. self.task.selected_instrument = ('Test1', 'Test', 'C', 'S') def test_check1(self): """Simply test that everything is ok if field can be evaluated. """ self.task.field = '3.0' test, traceback = self.task.check(test_instr=True) assert test assert not traceback assert self.task.get_from_database('Test_field') == 3.0 def test_check2(self): """Check handling a wrong field. """ self.task.field = '*1.0*' test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 assert 'root/Test-field' in traceback assert self.task.get_from_database('Test_field') == 0.01 def test_perform1(self): """Simple test when everything is right. """ self.task.field = '2.0' self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_field') == 2.0
class TestSleepTask(object): """Test SleepTask. """ def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = SleepTask(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_check1(self): """ Test handling a correct string in the 'time' field """ self.task.time = '2.0' test, traceback = self.task.check() assert test assert not traceback assert self.task.get_from_database('Test_time') == 2 def test_check2(self): """Test handling a wrong string in the 'time' field. """ self.task.time = 'a1.0' test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test-time' in traceback def test_check3(self): """Test handling a negative value 'time' field. """ self.task.time = '-1.0' test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test' in traceback def test_perform1(self): """Test performing when 'time' is correctly formatted, and checking that the time value gets written to the database """ self.task.time = '1.0+5.0' self.root.prepare() self.task.perform() assert self.task.get_from_database('Test_time') == 6.0
class TestSetDCVoltageTask(object): def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = MeasDCVoltageTask(name='Test') self.root.add_child_task(0, self.task) self.root.run_time[DRIVERS] = {'Test': (InstrHelper, InstrHelperStarter())} self.root.run_time[PROFILES] =\ {'Test1': {'connections': {'C': {'owner': []}}, 'settings': {'S': {'check_connection': [True]}} } } # This is set simply to make sure the test of InstrTask pass. self.task.selected_instrument = ('Test1', 'Test', 'C', 'S') def test_perform(self): self.task.wait_time = 1.0 p = self.root.run_time[PROFILES]['Test1'] p['settings']['S']['read_voltage_dc'] = [2.0] self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_voltage') == 2.0
class TestPNASetRFPowerTask(object): """Test of the PNA set power interface. """ def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = SetRFPowerTask(name='Test') self.root.add_child_task(0, self.task) self.root.run_time[DRIVERS] = { 'Test': (InstrHelper, InstrHelperStarter()) } self.root.run_time[PROFILES] =\ {'Test1': {'connections': {'C': {'owner': []}}, 'settings': {'S': {'check_connection': [True]}} } } # This is set simply to make sure the test of InstrTask pass. self.task.selected_instrument = ('Test1', 'Test', 'C', 'S') def test_check_pna_interface1(self): """Simply test that everything is ok if power can be evaluated. """ self.task.interface = PNASetRFPowerInterface(task=self.task, channel=1) self.task.power = '1.0' c = self.root.run_time[PROFILES]['Test1']['connections'] c['C'] = {'defined_channels': [[1]]} test, traceback = self.task.check(test_instr=True) assert test assert not traceback def test_check_pna_interface2(self): """Check handling a wrong channel. """ self.task.interface = PNASetRFPowerInterface(task=self.task, channel=2) self.task.power = '1.0' c = self.root.run_time[PROFILES]['Test1']['connections'] c['C'] = {'defined_channels': [[1]]} test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 def test_perform_pna_interface(self): self.task.interface = PNASetRFPowerInterface(task=self.task, channel=1) self.task.power = '1.0' c = self.root.run_time[PROFILES]['Test1']['connections'] c['C'] = {'power': [0.0], 'port': [1], 'owner': [None]} s = self.root.run_time[PROFILES]['Test1']['settings'] s['S'] = {'get_channel': lambda x, i: x} self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_power') == 1.0
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
class TestSetRFFrequencyTask(object): def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = SetRFFrequencyTask(name='Test') self.task.unit = 'GHz' self.root.add_child_task(0, self.task) self.root.run_time[DRIVERS] = { 'Test': (InstrHelper, InstrHelperStarter()) } self.root.run_time[PROFILES] =\ {'Test1': {'connections': {'C': {'owner': []}}, 'settings': {'S': {'check_connection': [True]}} } } # This is set simply to make sure the test of InstrTask pass. self.task.selected_instrument = ('Test1', 'Test', 'C', 'S') def test_check_base_interface1(self): """Simply test that everything is ok if voltage can be evaluated. """ self.task.frequency = '1.0' test, traceback = self.task.check(test_instr=True) assert test assert not traceback def test_check_base_interface2(self): # Check handling a wrong frequency. self.task.frequency = '*1.0*' test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 def test_perform_base_interface(self): self.task.frequency = '1.0' c = self.root.run_time[PROFILES]['Test1']['connections'] c['C'] = { 'frequency': [0.0], 'frequency_unit': ['GHz'], 'owner': [None] } self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_frequency') == 1.0
class TestArrayExtremaTask(object): def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = ArrayExtremaTask(name='Test') self.root.add_child_task(0, self.task) array = np.zeros((5, ), dtype={ 'names': ['var1', 'var2'], 'formats': ['f8', 'f8'] }) array['var1'][1] = -1 array['var1'][3] = 1 self.root.write_in_database('array', array) def test_mode_observation(self): """Check that the database is correctly updated when the mode change. """ self.task.mode = 'Min' assert self.task.get_from_database('Test_min_ind') == 0 assert self.task.get_from_database('Test_min_value') == 1.0 aux = self.task.list_accessible_database_entries() assert 'Test_max_ind' not in aux assert 'Test_max_value' not in aux self.task.mode = 'Max' assert self.task.get_from_database('Test_max_ind') == 0 assert self.task.get_from_database('Test_max_value') == 2.0 aux = self.task.list_accessible_database_entries() assert 'Test_min_ind' not in aux assert 'Test_min_value' not in aux self.task.mode = 'Max & min' assert self.task.get_from_database('Test_min_ind') == 0 assert self.task.get_from_database('Test_min_value') == 1.0 assert self.task.get_from_database('Test_max_ind') == 0 assert self.task.get_from_database('Test_max_value') == 2.0 def test_check1(self): """Simply test that everything is ok if the array exists in the database. """ self.root.write_in_database('array', np.zeros((5, ))) self.task.target_array = '{array}' test, traceback = self.task.check() assert test assert not traceback def test_check2(self): """Simply test that everything is ok if the array exists in the database and the column name is ok. """ self.task.target_array = '{array}' self.task.column_name = 'var1' test, traceback = self.task.check() assert test assert not traceback def test_check3(self): """Test handling a wrong array name. """ self.task.target_array = '*{array}' self.task.column_name = 'var3' test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test-target_array' in traceback def test_check4(self): """Test handling an array without names when a name is given. """ self.root.write_in_database('array', np.zeros((5, ))) self.task.target_array = '{array}' self.task.column_name = 'var1' test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test' in traceback def test_check5(self): """Test handling an array with names when no name is given. """ self.task.target_array = '{array}' test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test' in traceback def test_check6(self): """Test handling a wrong column name. """ self.task.target_array = '{array}' self.task.column_name = 'var3' test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test' in traceback def test_check7(self): """Test handling a 2d array without names. """ self.task.target_array = '{array}' array = np.zeros((5, 5)) self.root.write_in_database('array', array) test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test' in traceback def test_perform1(self): """Test performing when mode is 'Max'. """ self.task.mode = 'Max' self.task.target_array = '{array}' self.task.column_name = 'var1' self.root.prepare() self.task.perform() assert self.task.get_from_database('Test_max_ind') == 3 assert self.task.get_from_database('Test_max_value') == 1.0 def test_perform2(self): """Test performing when mode is 'Min'. """ self.task.mode = 'Min' self.task.target_array = '{array}' self.task.column_name = 'var1' self.root.prepare() self.task.perform() assert self.task.get_from_database('Test_min_ind') == 1 assert self.task.get_from_database('Test_min_value') == -1.0 def test_perform3(self): """Test performing when mode is 'Max & min'. """ self.task.mode = 'Max & min' self.task.target_array = '{array}' self.task.column_name = 'var1' self.root.prepare() self.task.perform() assert self.task.get_from_database('Test_max_ind') == 3 assert self.task.get_from_database('Test_max_value') == 1.0 assert self.task.get_from_database('Test_min_ind') == 1 assert self.task.get_from_database('Test_min_value') == -1.0 def test_perform4(self): """Test performing when no column name is given. """ self.root.write_in_database('array', np.zeros((5, ))) self.task.mode = 'Max' self.task.target_array = '{array}' self.root.prepare() self.task.perform() assert self.task.get_from_database('Test_max_ind') == 0 assert self.task.get_from_database('Test_max_value') == 0.0
class TestArrayFindValueTask(object): def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = ArrayFindValueTask(name='Test') self.root.add_child_task(0, self.task) array = np.zeros((5, ), dtype={ 'names': ['var1', 'var2'], 'formats': ['f8', 'f8'] }) array['var1'][1] = -1.5 array['var1'][3] = 1.6359 array['var1'][4] = 1.6359 self.root.write_in_database('array', array) def test_check1(self): """Simply test that everything is ok if the array exists in the database and value can be evaluated. """ self.root.write_in_database('array', np.zeros((5, ))) self.task.target_array = '{array}' self.task.value = '1.6359' test, traceback = self.task.check() assert test assert not traceback def test_check2(self): """Simply test that everything is ok if the array exists in the database the column name is ok, and value can be evaluated. """ self.task.target_array = '{array}' self.task.column_name = 'var1' self.task.value = '1.6359' test, traceback = self.task.check() assert test assert not traceback def test_check3(self): """Test handling a wrong array name. """ self.task.target_array = '*{array}' self.task.column_name = 'var3' self.task.value = '1.6359' test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test-target_array' in traceback def test_check4(self): """Test handling an array without names when a name is given. """ self.root.write_in_database('array', np.zeros((5, ))) self.task.target_array = '{array}' self.task.column_name = 'var1' self.task.value = '1.6359' test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test' in traceback def test_check5(self): """Test handling an array with names when no name is given. """ self.task.target_array = '{array}' self.task.value = '1.6359' test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test' in traceback def test_check6(self): """Test handling a wrong column name. """ self.task.target_array = '{array}' self.task.column_name = 'var3' self.task.value = '1.6359' test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test' in traceback def test_check7(self): """Test handling a wrong value. """ self.task.target_array = '{array}' self.task.column_name = 'var1' self.task.value = '*1.6359' test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test-value' in traceback def test_check8(self): """Test handling a 2d array value. """ self.task.target_array = '{array}' self.task.value = '1.6359' array = np.zeros((5, 5)) self.root.write_in_database('array', array) test, traceback = self.task.check() assert not test assert len(traceback) == 1 assert 'root/Test' in traceback def test_perform1(self): """Test performing. """ self.task.value = '1.6359' self.task.target_array = '{array}' self.task.column_name = 'var1' self.root.prepare() self.task.perform() assert self.task.get_from_database('Test_index') == 3
class TestSetDCVoltageTask(object): def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = SetDCVoltageTask(name='Test') self.task.back_step = 0.1 self.task.delay = 0.1 self.root.add_child_task(0, self.task) self.root.run_time[DRIVERS] = {'Test': (InstrHelper, InstrHelperStarter())} self.root.run_time[PROFILES] =\ {'Test1': {'connections': {'C': {'owner': []}}, 'settings': {'S': {'check_connection': [True]}} } } # This is set simply to make sure the test of InstrTask pass. self.task.selected_instrument = ('Test1', 'Test', 'C', 'S') def test_check_base_interface1(self): """Simply test that everything is ok if voltage can be evaluated. """ self.task.target_value = '1.0' test, traceback = self.task.check(test_instr=True) assert test assert not traceback def test_check_base_interface2(self): """Check handling a wrong voltage. """ self.task.target_value = '*1.0*' test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 def test_check_multichannel_interface1(self): """Check the multichannel specific tests, passing. """ interface = MultiChannelVoltageSourceInterface(task=self.task) interface.channel = (1, 1) self.task.interface = interface self.task.target_value = '1.0' c = self.root.run_time[PROFILES]['Test1']['connections'] c['C'] = {'defined_channels': [[(1, 1)]]} test, traceback = self.task.check(test_instr=True) assert test assert not traceback def test_check_multichannel_interface2(self): """Check the multichannel specific tests, failing = driver. """ interface = MultiChannelVoltageSourceInterface(task=self.task) interface.channel = (1, 1) self.task.interface = interface self.task.target_value = '1.0' self.root.run_time[DRIVERS] = {} c = self.root.run_time[PROFILES]['Test1']['connections'] c['C'] = {'defined_channels': [[(1, 1)]]} test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 def test_check_multichannel_interface3(self): """Check the multichannel specific tests, failing = profile. """ interface = MultiChannelVoltageSourceInterface(task=self.task) interface.channel = (1, 1) self.task.interface = interface self.task.target_value = '1.0' self.task.selected_instrument = () test, traceback = self.task.check() assert not test assert len(traceback) == 1 def test_check_multichannel_interface4(self): """Check the multichannel specific tests, failing = channel. """ interface = MultiChannelVoltageSourceInterface(task=self.task) interface.channel = (2, 1) self.task.interface = interface self.task.target_value = '1.0' c = self.root.run_time[PROFILES]['Test1']['connections'] c['C'] = {'defined_channels': [[(1, 1)]]} test, traceback = self.task.check(test_instr=True) assert not test assert len(traceback) == 1 def test_volt_function(self): """Make sure that the driver compares function with 'VOLT' """ c = self.root.run_time[PROFILES]['Test1']['connections'] c['C'] = {'voltage': [0.0], 'function': 'CURR', 'owner': [None]} self.task.target_value = '0.05' self.root.prepare() with pytest.raises(ValueError) as e: self.task.perform() assert "not configured" in str(e.value) def test_smooth_set_stopping(self): """Test stopping in the middle of a smooth stepping. """ c = self.root.run_time[PROFILES]['Test1']['connections'] c['C'] = {'voltage': [0.0], 'function': 'VOLT', 'owner': [None]} self.root.prepare() self.root.should_stop.set() setter = lambda value: setattr(self.driver, 'voltage', value) self.task.smooth_set(1.0, setter, 0.0) assert self.root.get_from_database('Test_voltage') == 0.0 def test_perform_base_interface(self): """Test also that a target which is not a multiple of the back step is correctly handled. """ self.task.target_value = '0.05' c = self.root.run_time[PROFILES]['Test1']['connections'] c['C'] = {'voltage': [0.0], 'function': 'VOLT', 'owner': [None]} self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_voltage') == 0.05 self.task.target_value = '1.06' self.task.perform() assert self.root.get_from_database('Test_voltage') == 1.06 def test_perform_multichannel_interface(self): """Test using the interface for the setting. """ interface = MultiChannelVoltageSourceInterface(task=self.task) interface.channel = (1, 1) self.task.interface = interface self.task.target_value = '1.0' c = self.root.run_time[PROFILES]['Test1']['connections'] c['C'] = {'voltage': [0.0], 'function': 'VOLT', 'owner': [None]} s = self.root.run_time[PROFILES]['Test1']['settings'] s['S'] = {'get_channel': lambda x, i: x} self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_voltage') == 1.0
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
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)
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)
class TestLockInMeasureTask(object): def setup(self): self.root = RootTask(should_stop=Event(), should_pause=Event()) self.task = LockInMeasureTask(name='Test') self.root.add_child_task(0, self.task) self.root.run_time[DRIVERS] = {'Test': (InstrHelper, InstrHelperStarter())} self.root.run_time[PROFILES] =\ {'Test1': {'connections': {'C': {'owner': []}}, 'settings': {'S': {'check_connection': [True]}} } } # This is set simply to make sure the test of InstrTask pass. self.task.selected_instrument = ('Test1', 'Test', 'C', 'S') def test_mode_observation(self): """Check database is correctly updated when the mode change. """ self.task.mode = 'X' assert self.task.get_from_database('Test_x') == 1.0 aux = self.task.list_accessible_database_entries() assert 'Test_y' not in aux assert 'Test_amplitude' not in aux assert 'Test_phase' not in aux self.task.mode = 'Y' assert self.task.get_from_database('Test_y') == 1.0 aux = self.task.list_accessible_database_entries() assert 'Test_x' not in aux assert 'Test_amplitude' not in aux assert 'Test_phase' not in aux self.task.mode = 'X&Y' assert self.task.get_from_database('Test_x') == 1.0 assert self.task.get_from_database('Test_y') == 1.0 aux = self.task.list_accessible_database_entries() assert 'Test_amplitude' not in aux assert 'Test_phase' not in aux self.task.mode = 'Amp' assert self.task.get_from_database('Test_amplitude') == 1.0 aux = self.task.list_accessible_database_entries() assert 'Test_x' not in aux assert 'Test_y' not in aux assert 'Test_phase' not in aux self.task.mode = 'Phase' assert self.task.get_from_database('Test_phase') == 1.0 aux = self.task.list_accessible_database_entries() assert 'Test_x' not in aux assert 'Test_y' not in aux assert 'Test_amplitude' not in aux self.task.mode = 'Amp&Phase' assert self.task.get_from_database('Test_amplitude') == 1.0 assert self.task.get_from_database('Test_phase') == 1.0 aux = self.task.list_accessible_database_entries() assert 'Test_x' not in aux assert 'Test_y' not in aux def test_perform1(self): self.task.mode = 'X' p = self.root.run_time[PROFILES]['Test1'] p['settings']['S']['read_x'] = [2.0] self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_x') == 2.0 def test_perform2(self): self.task.mode = 'Y' p = self.root.run_time[PROFILES]['Test1'] p['settings']['S']['read_y'] = [2.0] self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_y') == 2.0 def test_perform3(self): self.task.mode = 'X&Y' p = self.root.run_time[PROFILES]['Test1'] p['settings']['S']['read_xy'] = [(2.0, 3.0)] self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_x') == 2.0 assert self.root.get_from_database('Test_y') == 3.0 def test_perform4(self): self.task.mode = 'Amp' p = self.root.run_time[PROFILES]['Test1'] p['settings']['S']['read_amplitude'] = [2.0] self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_amplitude') == 2.0 def test_perform5(self): self.task.mode = 'Phase' p = self.root.run_time[PROFILES]['Test1'] p['settings']['S']['read_phase'] = [2.0] self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_phase') == 2.0 def test_perform6(self): self.task.mode = 'Amp&Phase' p = self.root.run_time[PROFILES]['Test1'] p['settings']['S']['read_amp_and_phase'] = [(2.0, 3.0)] self.root.prepare() self.task.perform() assert self.root.get_from_database('Test_amplitude') == 2.0 assert self.root.get_from_database('Test_phase') == 3.0
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 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_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_perform3(self): """Test performing when condition is False. """ self.task.condition = 'False' self.root.prepare() self.task.perform() assert not self.check.perform_called
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 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_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
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 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_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_perform3(self): """Test performing when condition is False. """ self.task.condition = 'False' self.root.prepare() self.task.perform() assert not self.check.perform_called