Ejemplo n.º 1
0
 def setUp(self):
     self._qapp = QApplication.instance()
     patcher = mock.patch('Muon.GUI.Common.thread_model.warning')
     self.addCleanup(patcher.stop)
     self.warning_box_patcher = patcher.start()
     self.model = testModel()
     self.thread = ThreadModel(self.model)
Ejemplo n.º 2
0
    def test_that_AttributeError_raised_if_trying_to_load_data_into_model_without_loadData_method(
            self):
        model = testModelWithoutLoadData()

        thread = ThreadModel(model)
        with self.assertRaises(AttributeError):
            thread.loadData(None)
Ejemplo n.º 3
0
    def test_that_message_box_called_when_execute_throws_even_without_setup_and_teardown_methods(self, mock_box):
        # Need to instantiate a new thread for patch to work
        self.thread = ThreadModel(self.model)

        def raise_error():
            raise ValueError()

        self.model.execute = mock.Mock(side_effect=raise_error)

        self.Runner(self.thread)

        self.assertEqual(mock_box.call_count, 1)
 def create_mock_signal_handler(self):
     self.thread_model = ThreadModel(self.model)
     self.thread_model_worker = ThreadModelWorker(self.thread_model)
     self.thread_model_worker.signals.error = mock.Mock()
     self.signal_handler = MockSignalHandler()
     self.thread_model_worker.signals.error.connect(
         self.signal_handler.signalReceived())
Ejemplo n.º 5
0
 def _create_fitting_thread(self, callback) -> ThreadModel:
     """Create a thread for fitting."""
     self.fitting_calculation_model = ThreadModelWrapperWithOutput(callback)
     return ThreadModel(self.fitting_calculation_model)
Ejemplo n.º 6
0
 def setUp(self):
     self.model = testModel()
     self.thread = ThreadModel(self.model)
Ejemplo n.º 7
0
class LoadFileWidgetViewTest(unittest.TestCase):
    class Runner:
        """This runner class creates a main event loop for threaded code to run within (otherwise the threaded
        code will not connect signals/slots properly).
        The finished signal of a QThread is connected to the finished method below"""
        QT_APP = mock_widget.mockQapp()

        def __init__(self, thread):
            if thread:
                self._thread = thread
                self._thread.finished.connect(self.finished)
                self._thread.start()
                if self._thread.isRunning():
                    self.QT_APP.exec_()

        def finished(self):
            self.QT_APP.processEvents()
            self.QT_APP.exit(0)

    def setUp(self):
        self.model = testModel()
        self.thread = ThreadModel(self.model)

    def mock_model(self):
        model = mock.Mock()
        model.loadData = mock.Mock(side_effect=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
        model.execute = mock.Mock()
        model.output = mock.Mock()
        return model

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS
    # ------------------------------------------------------------------------------------------------------------------

    def test_that_loadData_called_in_model_with_correct_inputs(self):
        self.model.loadData = mock.Mock()

        self.thread.loadData([1, 2, 3, 4, 5])

        self.assertEqual(self.model.loadData.call_count, 1)
        self.assertEqual(self.model.loadData.call_args_list[0][0][0], [1, 2, 3, 4, 5])

    def test_that_execute_is_called_in_model_when_thread_is_started(self):
        self.model.execute = mock.Mock()

        self.Runner(self.thread)

        self.assertEqual(self.model.execute.call_count, 1)

    def test_that_output_is_called_if_thread_executes_successfully(self):
        self.model.execute = mock.Mock()
        self.model.output = mock.Mock()

        self.Runner(self.thread)

        self.assertEqual(self.model.output.call_count, 1)

    def test_that_starting_and_finishing_callbacks_are_called_when_thread_starts_and_finishes(self):
        start_slot = mock.Mock()
        end_slot = mock.Mock()

        self.thread.threadWrapperSetUp(start_slot, end_slot)

        self.Runner(self.thread)

        self.assertEqual(start_slot.call_count, 1)
        self.assertEqual(end_slot.call_count, 1)

    def test_that_AttributeError_raised_if_trying_to_load_data_into_model_without_loadData_method(self):
        model = testModelWithoutLoadData()

        thread = ThreadModel(model)
        with self.assertRaises(AttributeError):
            thread.loadData(None)

    def test_that_attribute_error_raised_if_model_does_not_contain_execute_method(self):
        model = testModelWithoutExecute()

        with self.assertRaises(AttributeError):
            ThreadModel(model)

    def test_that_attribute_error_raised_if_model_does_not_contain_output_method(self):
        model = testModelWithoutOutput

        with self.assertRaises(AttributeError):
            ThreadModel(model)

    def test_that_tearDown_function_called_automatically(self):
        start_slot = mock.Mock()
        end_slot = mock.Mock()

        self.thread.threadWrapperSetUp(start_slot, end_slot)

        self.Runner(self.thread)
        self.Runner(self.thread)

        self.assertEqual(start_slot.call_count, 1)
        self.assertEqual(end_slot.call_count, 1)

    @mock.patch("Muon.GUI.Common.message_box.warning")
    def test_that_message_box_called_when_execute_throws_even_without_setup_and_teardown_methods(self, mock_box):
        # Need to instantiate a new thread for patch to work
        self.thread = ThreadModel(self.model)

        def raise_error():
            raise ValueError()

        self.model.execute = mock.Mock(side_effect=raise_error)

        self.Runner(self.thread)

        self.assertEqual(mock_box.call_count, 1)

    def test_that_passing_non_callables_to_setUp_throws_AssertionError(self):

        with self.assertRaises(AssertionError):
            self.thread.threadWrapperSetUp(1, 2)
Ejemplo n.º 8
0
    def test_that_attribute_error_raised_if_model_does_not_contain_output_method(self):
        model = testModelWithoutOutput

        with self.assertRaises(AttributeError):
            ThreadModel(model)
Ejemplo n.º 9
0
 def setUp(self):
     patcher = mock.patch('Muon.GUI.Common.thread_model.warning')
     self.addCleanup(patcher.stop)
     self.warning_box_patcher = patcher.start()
     self.model = testModel()
     self.thread = ThreadModel(self.model)
Ejemplo n.º 10
0
 def create_calculation_thread(self, callback, *args) -> ThreadModel:
     """Create a thread for calculations."""
     self.thread_model_wrapper = ThreadModelWrapperWithOutput(
         callback, *args)
     return ThreadModel(self.thread_model_wrapper)
 def _create_parameter_combinations_thread(self, callback) -> ThreadModel:
     """Create a thread for fitting."""
     self.parameter_combinations_creator = ThreadModelWrapperWithOutput(
         callback)
     return ThreadModel(self.parameter_combinations_creator)
Ejemplo n.º 12
0
 def setUp(self):
     patcher = mock.patch('Muon.GUI.Common.thread_model.warning')
     self.addCleanup(patcher.stop)
     self.warning_box_patcher = patcher.start()
     self.model = testModel()
     self.thread = ThreadModel(self.model)
Ejemplo n.º 13
0
class LoadFileWidgetViewTest(unittest.TestCase):
    class Runner:
        """This runner class creates a main event loop for threaded code to run within (otherwise the threaded
        code will not connect signals/slots properly).
        The finished signal of a QThread is connected to the finished method below"""
        QT_APP = mock_widget.mockQapp()

        def __init__(self, thread_model):
            if thread_model:
                thread_model.start()

    def setUp(self):
        patcher = mock.patch('Muon.GUI.Common.thread_model.warning')
        self.addCleanup(patcher.stop)
        self.warning_box_patcher = patcher.start()
        self.model = testModel()
        self.thread = ThreadModel(self.model)

    def mock_model(self):
        model = mock.Mock()
        model.loadData = mock.Mock(side_effect=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
        model.execute = mock.Mock()
        model.output = mock.Mock()
        return model

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS
    # ------------------------------------------------------------------------------------------------------------------

    def test_that_loadData_called_in_model_with_correct_inputs(self):
        self.model.loadData = mock.Mock()

        self.thread.loadData([1, 2, 3, 4, 5])

        self.assertEqual(self.model.loadData.call_count, 1)
        self.assertEqual(self.model.loadData.call_args_list[0][0][0], [1, 2, 3, 4, 5])

    def test_that_execute_is_called_in_model_when_thread_is_started(self):
        self.model.execute = mock.Mock()

        self.Runner(self.thread)
        self.thread._thread.wait()
        self.Runner.QT_APP.processEvents()

        self.assertEqual(self.model.execute.call_count, 1)

    def test_that_output_is_called_if_thread_executes_successfully(self):
        self.model.execute = mock.Mock()
        self.model.output = mock.Mock()

        self.Runner(self.thread)
        self.thread._thread.wait()
        self.Runner.QT_APP.processEvents()

        self.assertEqual(self.model.output.call_count, 1)

    def test_that_starting_and_finishing_callbacks_are_called_when_thread_starts_and_finishes(self):
        start_slot = mock.Mock()
        end_slot = mock.Mock()

        self.thread.threadWrapperSetUp(start_slot, end_slot)

        self.Runner(self.thread)
        self.thread._thread.wait()
        self.Runner.QT_APP.processEvents()

        self.assertEqual(start_slot.call_count, 1)
        self.assertEqual(end_slot.call_count, 1)

    def test_that_AttributeError_raised_if_trying_to_load_data_into_model_without_loadData_method(self):
        model = testModelWithoutLoadData()

        thread = ThreadModel(model)
        with self.assertRaises(AttributeError):
            thread.loadData(None)

    def test_that_attribute_error_raised_if_model_does_not_contain_execute_method(self):
        model = testModelWithoutExecute()

        with self.assertRaises(AttributeError):
            ThreadModel(model)

    def test_that_attribute_error_raised_if_model_does_not_contain_output_method(self):
        model = testModelWithoutOutput

        with self.assertRaises(AttributeError):
            ThreadModel(model)

    def test_that_tearDown_function_called_automatically(self):
        start_slot = mock.Mock()
        end_slot = mock.Mock()

        self.thread.threadWrapperSetUp(start_slot, end_slot)

        self.Runner(self.thread)
        self.thread._thread.wait()
        self.Runner.QT_APP.processEvents()

        self.assertEqual(start_slot.call_count, 1)
        self.assertEqual(end_slot.call_count, 1)

    def test_that_message_box_called_when_execute_throws_even_without_setup_and_teardown_methods(self):
        def raise_error():
            raise ValueError()

        self.model.execute = mock.Mock(side_effect=raise_error)

        self.Runner(self.thread)
        self.thread._thread.wait()
        self.Runner.QT_APP.processEvents()

        self.assertEqual(self.warning_box_patcher.call_count, 1)

    def test_that_passing_non_callables_to_setUp_throws_AssertionError(self):

        with self.assertRaises(AssertionError):
            self.thread.threadWrapperSetUp(1, 2)
Ejemplo n.º 14
0
    def test_that_AttributeError_raised_if_trying_to_load_data_into_model_without_loadData_method(self):
        model = testModelWithoutLoadData()

        thread = ThreadModel(model)
        with self.assertRaises(AttributeError):
            thread.loadData(None)