def test_reduce_reductiondirectory_any_raise(self, _: Mock):
        """
        Test: ReductionDirectory raising any exception
        """
        with tempfile.NamedTemporaryFile() as tmpfile:
            self.message.data = tmpfile.name

            runner = ReductionRunner(self.message, self.run_name)
            runner.reduce()

        assert runner.message.message == "Error encountered when trying to read the reduction directory"
        assert "Exception:" in runner.message.reduction_log
        assert "error message" in runner.message.reduction_log
 def test_reduce_bad_datafile(self, _get_mantid_version: Mock,
                              mock_logger_info: Mock):
     """
     Test: Bad datafile is provided
     """
     self.bad_message.description = "testdescription"
     runner = ReductionRunner(self.bad_message, self.run_name)
     runner.reduce()
     mock_logger_info.assert_called_once()
     assert mock_logger_info.call_args[0][1] == "testdescription"
     assert runner.message.message, (
         'Error encountered when trying to access the datafile'
         ' /isis/NDXTESTINSTRUMENT/Instrument/data/cycle_21_1/data.nxs')
    def test_flat_output_respected(self, reduce: Mock,
                                   _get_mantid_version: Mock):
        """
        Test: The flat_output state is respected
        """
        self.message.flat_output = True

        with tempfile.NamedTemporaryFile() as tmpfile:
            self.message.data = tmpfile.name

            runner = ReductionRunner(self.message, self.run_name)
            runner.reduce()

        reduce.assert_called_once()
        assert str(reduce.call_args[0][2][0].path) == tmpfile.name
        assert runner.message.flat_output is True
    def test_reduce_throws_any_exception(self, reduce: Mock,
                                         _get_mantid_version: Mock):
        """
        Test: Reduce throwing any exception
        """
        reduce.side_effect = Exception
        with tempfile.NamedTemporaryFile() as tmpfile:
            self.message.data = tmpfile.name

            runner = ReductionRunner(self.message, self.run_name)
            runner.reduce()

        reduce.assert_called_once()
        assert str(reduce.call_args[0][2][0].path) == tmpfile.name
        assert runner.message.reduction_data is None
        assert runner.message.software == {
            "name": "Mantid",
            "version": "latest",
        }
        assert "REDUCTION Error:" in runner.message.message
    def test_reduce_throws_reductionscripterror(self, reduce: Mock,
                                                _get_mantid_version: Mock):
        """
        Test: reduce throwing an ReductionScriptError
        """
        reduce.side_effect = ReductionScriptError
        with tempfile.NamedTemporaryFile() as tmpfile:
            self.message.data = tmpfile.name

            runner = ReductionRunner(self.message, self.run_name)
            runner.reduce()

        reduce.assert_called_once()
        assert str(reduce.call_args[0][2][0].path) == tmpfile.name
        assert runner.message.reduction_data is None
        assert runner.message.software == {
            "name": "Mantid",
            "version": "latest",
        }
        assert "Error encountered when running the reduction script" in runner.message.message
    def test_reduce_ok(self, datafile_type: str, reduce: Mock,
                       _get_mantid_version: Mock):
        """
        Test: An OK reduction
        """
        with tempfile.NamedTemporaryFile() as tmpfile:
            if datafile_type == "str":
                self.message.data = tmpfile.name
            else:
                self.message.data = [tmpfile.name, tmpfile.name]

            runner = ReductionRunner(self.message, self.run_name)
            runner.reduce()

        reduce.assert_called_once()
        assert str(reduce.call_args[0][2][0].path) == tmpfile.name
        assert runner.message.reduction_data is not None
        assert runner.message.reduction_log is not None
        assert runner.message.message is None
        assert runner.message.software == {
            "name": "Mantid",
            "version": "latest",
        }