def test_refresh(self, mock_autorefresh, _): """ Test: refresh is called on the stored client When: refresh is called """ client = ICATClient() client.refresh() mock_autorefresh.assert_called_once()
def test_disconnect(self, mock_logout, _): """ Test: logout is called on the stored client When: disconnect is called """ client = ICATClient() client.disconnect() mock_logout.assert_called_once()
def test_query_icat(self, mock_refresh, mock_search, _): """ Test: A query is executed When: execute_query is called with a connection having been established """ client = ICATClient() client.execute_query('icat query') mock_refresh.assert_called_once() mock_search.assert_called_once_with('icat query')
def test_valid_test_connection(self, mock_refresh, _): """ Test: refresh is called on the stored client When: _test_connection is called """ client = ICATClient() # pylint:disable=protected-access self.assertTrue(client._test_connection()) mock_refresh.assert_called_once()
def login_icat(): """ Log into the ICATClient :return: The client connected, or None if failed """ print("Logging into ICAT") icat_client = ICATClient() try: icat_client.connect() except ConnectionException: print("Couldn't connect to ICAT. Continuing without ICAT connection.") icat_client = None return icat_client
def test_valid_connection(self, mock_icat_login, mock_icat): """ Test: login is called on the stored client When: connect is called while valid credentials are held """ client = ICATClient() mock_icat.assert_called_once() client.connect() mock_icat_login.assert_called_once_with(auth='simple', credentials={ 'username': '******', 'password': '******' })
def test_query_without_conn(self, mock_search, mock_refresh, mock_connect, mock_set_attr, _): """ Test: A query is executed When: execute_query is called without a connection having been established """ # Add side effect to raise exception with icat.refresh mock_refresh.side_effect = raise_icat_session_error client = ICATClient() client.execute_query('icat query') mock_refresh.assert_called_once() mock_set_attr.assert_called_once() mock_connect.assert_called_once() mock_search.assert_called_once_with('icat query')
def test_failing_test_connection(self, mock_refresh, _): """ Test: A ConnectionException is raised When: _test_connection is called while an invalid connection is held """ mock_refresh.side_effect = raise_icat_session_error client = ICATClient() # pylint:disable=protected-access self.assertRaisesRegex(ConnectionException, 'ICAT', client._test_connection)
def get_icat_instrument_prefix(autoreduction_instruments=None): """ Queries ICAT for shorter names for all Autoreduction instruments or only selection if passed in :param autoreduction_instruments: Optionally input custom list of autoreduction instrument names :return: A map of Autoreduction to ICAT instrument prefixes """ if not autoreduction_instruments: autoreduction_instruments = AUTOREDUCTION_INSTRUMENT_NAMES client = ICATClient() try: icat_instruments = client.execute_query("SELECT i FROM Instrument i") except Exception: # pylint:disable=broad-except warning_message = "ICAT instrument query failed" print(warning_message) logger.warning(warning_message) return None instrument_fullname_to_short_name_map = {} for instrument_fullname in autoreduction_instruments: icat_instrument = next( (x for x in icat_instruments if x.fullName == instrument_fullname), None) if not icat_instrument: warning_message = f"No instrument in ICAT with fullName {instrument_fullname}" print(warning_message) logger.warning(warning_message) # Missing an instrument should also be picked up in the tests continue instrument_fullname_to_short_name_map[ instrument_fullname] = icat_instrument.name return instrument_fullname_to_short_name_map
def test_default_init( self, mock_icat, ): """ Test: Class variables are created and set When: ICATClient is initialised with default credentials """ client = ICATClient() self.assertEqual(client.credentials.username, 'YOUR-ICAT-USERNAME') self.assertEqual(client.credentials.password, 'YOUR-PASSWORD') self.assertEqual(client.credentials.host, 'YOUR-ICAT-WSDL-URL') self.assertEqual(client.credentials.port, '') self.assertEqual(client.credentials.auth, 'simple') mock_icat.assert_called_once_with('YOUR-ICAT-WSDL-URL')
def get_icat_instrument_prefix(instrument_fullname: str) -> str: """ Queries ICAT for shorter names for all Autoreduction instruments or only selection if passed in :return: instrument prefix """ client = ICATClient() try: icat_instruments = client.execute_query("SELECT i FROM Instrument i") except Exception as exc: warning_message = "ICAT instrument query failed" print(warning_message) logger.warning(warning_message) raise RuntimeError(warning_message) from exc icat_instrument = next((x for x in icat_instruments if x.fullName == instrument_fullname), None) if not icat_instrument: warning_message = f"No instrument in ICAT with fullName {instrument_fullname}" print(warning_message) logger.warning(warning_message) raise RuntimeError(f"Instrument with fullname {instrument_fullname} not found in ICAT.") return icat_instrument.name