Exemple #1
0
 def test_get_icat_instrument_prefix_log_invalid_instrument(
         self, _mock_execute_query, mock_logger_warning, _):
     """
     Test: If invalid instrument name in utils.settings.VALID_INSTRUMENTS is logged as not found
     When: Testing if get_icat_instrument_prefix picks up invalid instruments
     """
     get_icat_instrument_prefix()
     mock_logger_warning.assert_called_once()
Exemple #2
0
 def test_get_icat_instrument_prefix_executes_icat_query(
         self, mock_execute_query, _):
     """
     Test: If instruments are fetched through a query
     When: Testing if get_icat_instrument_prefix executes an ICAT query
     """
     get_icat_instrument_prefix()
     mock_execute_query.assert_called_once()
def get_location_and_rb_from_icat(icat_client, instrument, run_number,
                                  file_ext):
    """
    Retrieves a run's data-file location and rb_number from ICAT.
    Attempts first with the default file name, then with prepended zeroes.
    :param icat_client: Client to access the ICAT service
    :param instrument: The name of instrument
    :param run_number: The run number to be processed
    :param file_ext: The expected file extension
    :return: The data file location and rb_number
    :raises SystemExit: If the given run information cannot return a location and rb_number
    """
    if icat_client is None:
        print("ICAT not connected")  # pragma: no cover
        sys.exit(1)  # pragma: no cover

    icat_instrument_prefix = get_icat_instrument_prefix()[instrument]
    file_name = f"{icat_instrument_prefix}{str(run_number).zfill(5)}.{file_ext}"
    datafile = icat_client.execute_query(
        "SELECT df FROM Datafile df WHERE df.name = '" + file_name +
        "' INCLUDE df.dataset AS ds, ds.investigation")

    if not datafile:
        print("Cannot find datafile '" + file_name +
              "' in ICAT. Will try with zeros in front of run number.")
        file_name = f"{icat_instrument_prefix}{str(run_number).zfill(8)}.{file_ext}"
        datafile = icat_client.execute_query(
            "SELECT df FROM Datafile df WHERE df.name = '" + file_name +
            "' INCLUDE df.dataset AS ds, ds.investigation")

    if not datafile:
        print("Cannot find datafile '" + file_name +
              "' in ICAT. Exiting...")  # pragma: no cover
        sys.exit(1)  # pragma: no cover
    return datafile[0].location, datafile[0].dataset.investigation.name
Exemple #4
0
 def test_icat_prefix_mapping_length(self, _mock_instruments,
                                     _mock_execute_query):
     """
     Test: get_icat_instrument_prefix produces the same number of results as stored in
           utils.settings.VALID_INSTRUMENTS
     When: Called when testing to see if the correct number of instruments in prefix mapping
     """
     actual = get_icat_instrument_prefix()
     self.assertEqual(2, len(actual.keys()))
Exemple #5
0
    def test_get_icat_instrument_prefix_with_argument(self, mock_execute_query,
                                                      _):
        """
        Test: If get_icat_instrument_prefix properly maps instrument names map to ICAT
              instrument prefixes using a passed in list of instruments to check
        When: Called when testing correct mapping using passed in list
        """
        expected = ("ENG", "ENGINX")
        mock_execute_query.return_value = [
            MockInstrumentQueryResult(*expected)
        ]

        actual = get_icat_instrument_prefix([expected[1]])
        self.assertEqual(expected[0], actual[expected[1]])
Exemple #6
0
    def test_get_icat_instrument_prefix_without_argument(
            self, mock_execute_query, _):
        """
        Test: If get_icat_instrument_prefix properly maps instrument names map to ICAT
              instrument prefixes using utils.settings.VALID_INSTRUMENTS
        When: Called when testing correct mapping
        """
        expected = ("ENG", "ENGINX")
        mock_execute_query.return_value = [
            MockInstrumentQueryResult(*expected)
        ]

        actual = get_icat_instrument_prefix()
        self.assertEqual(expected[0], actual[expected[1]])