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_init(self):
     """
     Test: init parameters are as expected
     When: called with expected arguments
     """
     runner = ReductionRunner(self.message, self.run_name)
     self.assertEqual(runner.message, self.message)
     self.assertIsNotNone(runner.admin_log_stream)
     self.assertEqual(
         runner.data_file,
         '/isis/NDXTESTINSTRUMENT/Instrument/data/cycle_21_1/data.nxs',
     )
     self.assertEqual(runner.facility, 'ISIS')
     self.assertEqual(runner.instrument, 'TESTINSTRUMENT')
     self.assertEqual(runner.proposal, '1234')
     self.assertEqual(runner.run_number, '4321')
     self.assertEqual(
         runner.reduction_arguments, {
             "standard_vars": {
                 "arg1": "differentvalue",
                 "arg2": 321
             },
             "advanced_vars": {
                 "adv_arg1": "advancedvalue2",
                 "adv_arg2": ""
             }
         })
    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_get_mantid_version():
     """
     Test: Getting the mantid version
     """
     pytest.importorskip(modname="mantid",
                         minversion="6.2.0",
                         reason="Mantid not installed")
     assert ReductionRunner._get_mantid_version() is not None
    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_main_write_reduction_message(self):
     """
     Test: write_reduction_message is called
     When: called with expected arguments
     """
     # Patch write_reduction_message
     with patch('builtins.open', mock_open()) as m_open:
         runner = ReductionRunner(self.message, self.run_name)
         write_reduction_message(runner)
     m_open.assert_called_with(
         '/home/isisautoreduce/.autoreduce/output.txt',
         mode='w+',
         encoding='utf-8')
    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",
        }