def test_intercept_unary_unary_failed_request(self): """_logger.warning and _logger.info should be called. LoggingInterceptor should call _logger.warning and _logger.info with a specific str parameter when a request fails. """ mock_client_call_details = self._get_mock_client_call_details() mock_continuation_fn = self._get_mock_continuation_fn(fail=True) mock_request = self._get_mock_request() with mock.patch('logging.config.dictConfig'), \ mock.patch('google.ads.google_ads.client._logger') as mock_logger: interceptor = self._create_test_interceptor() mock_response = interceptor.intercept_unary_unary( mock_continuation_fn, mock_client_call_details, mock_request) mock_trailing_metadata = mock_response.trailing_metadata() mock_logger.warning.assert_called_once_with( interceptor._SUMMARY_LOG_LINE.format( self._MOCK_CUSTOMER_ID, self._MOCK_ENDPOINT, mock_client_call_details.method, self._MOCK_REQUEST_ID, True, self._MOCK_ERROR_MESSAGE)) initial_metadata = Client._parse_metadata_to_json( mock_client_call_details.metadata) trailing_metadata = Client._parse_metadata_to_json( mock_trailing_metadata) mock_logger.info.assert_called_once_with( interceptor._FULL_FAULT_LOG_LINE.format( self._MOCK_METHOD, self._MOCK_ENDPOINT, initial_metadata, mock_request, trailing_metadata, mock_response.exception().failure))
def test_intercept_unary_unary_successful_request(self): """_logger.info and _logger.debug should be called. LoggingInterceptor should call _logger.info and _logger.debug with a specific str parameter when a request succeeds. """ mock_client_call_details = self._get_mock_client_call_details() mock_continuation_fn = self._get_mock_continuation_fn() mock_request = self._get_mock_request() mock_response = mock_continuation_fn(mock_client_call_details, mock_request) mock_trailing_metadata = mock_response.trailing_metadata() with mock.patch('logging.config.dictConfig'), \ mock.patch('google.ads.google_ads.client._logger') as mock_logger: interceptor = self._create_test_interceptor() interceptor.intercept_unary_unary(mock_continuation_fn, mock_client_call_details, mock_request) mock_logger.info.assert_called_once_with( interceptor._SUMMARY_LOG_LINE.format( self._MOCK_CUSTOMER_ID, self._MOCK_ENDPOINT, mock_client_call_details.method, self._MOCK_REQUEST_ID, False, None)) initial_metadata = Client._parse_metadata_to_json( mock_client_call_details.metadata) trailing_metadata = Client._parse_metadata_to_json( mock_trailing_metadata) mock_logger.debug.assert_called_once_with( interceptor._FULL_REQUEST_LOG_LINE.format( self._MOCK_METHOD, self._MOCK_ENDPOINT, initial_metadata, mock_request, trailing_metadata, mock_response.result()))
def test_parse_metadata_to_json(self): mock_metadata = [('x-goog-api-client', 'gl-python/123 grpc/123 gax/123'), ('developer-token', '0000000000'), ('login-customer-id', '9999999999')] result = (Client._parse_metadata_to_json(mock_metadata)) self.assertEqual( result, '{\n' ' "developer-token": "REDACTED",\n' ' "login-customer-id": "9999999999",\n' ' "x-goog-api-client": "gl-python/123 ' 'grpc/123 gax/123"\n' '}')
def test_parse_metadata_to_json_with_none(self): mock_metadata = None result = (Client._parse_metadata_to_json(mock_metadata)) self.assertEqual(result, '{}')