def test_recognize(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = speech_client.SpeechClient()

        # Mock request
        encoding = enums.RecognitionConfig.AudioEncoding.FLAC
        sample_rate_hertz = 44100
        language_code = 'en-US'
        config = cloud_speech_pb2.RecognitionConfig(
            encoding=encoding,
            sample_rate_hertz=sample_rate_hertz,
            language_code=language_code)
        uri = 'gs://bucket_name/file_name.flac'
        audio = cloud_speech_pb2.RecognitionAudio(uri=uri)

        # Mock response
        expected_response = cloud_speech_pb2.RecognizeResponse()
        grpc_stub.Recognize.return_value = expected_response

        response = client.recognize(config, audio)
        self.assertEqual(expected_response, response)

        grpc_stub.Recognize.assert_called_once()
        args, kwargs = grpc_stub.Recognize.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = cloud_speech_pb2.RecognizeRequest(config=config,
                                                             audio=audio)
        self.assertEqual(expected_request, actual_request)
Example #2
0
def _make_sync_response(*results):
    from google.cloud.proto.speech.v1 import cloud_speech_pb2

    response = cloud_speech_pb2.RecognizeResponse(
        results=results,
    )
    return response
Example #3
0
    def test_recognize(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=cloud_speech_pb2.SpeechStub)
        mock_create_stub.return_value = grpc_stub

        client = speech_client.SpeechClient()

        # Mock request
        encoding = enums.RecognitionConfig.AudioEncoding.FLAC
        sample_rate_hertz = 44100
        language_code = 'en-US'
        config = cloud_speech_pb2.RecognitionConfig(encoding,
                                                    sample_rate_hertz,
                                                    language_code)
        uri = 'gs://bucket_name/file_name.flac'
        audio = cloud_speech_pb2.RecognitionAudio(uri)

        # Mock response
        expected_response = cloud_speech_pb2.RecognizeResponse()
        grpc_stub.Recognize.return_value = expected_response

        response = client.recognize(config, audio)
        self.assertEqual(expected_response, response)

        grpc_stub.Recognize.assert_called_once()
        request = grpc_stub.Recognize.call_args[0]

        self.assertEqual(config, request.config)
        self.assertEqual(audio, request.audio)