Example #1
0
    def test_long_running_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.LongRunningRecognizeResponse()
        operation = operations_pb2.Operation(
            'operations/test_long_running_recognize', True)
        operation.response.Pack(expected_response)
        grpc_stub.LongRunningRecognize.return_value = operation

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

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

        self.assertEqual(config, request.config)
        self.assertEqual(audio, request.audio)
Example #2
0
    def _make_operation_pb(self, *results):
        from google.cloud.proto.speech.v1 import cloud_speech_pb2
        from google.longrunning import operations_pb2
        from google.protobuf.any_pb2 import Any

        any_pb = None
        if results:
            result_pb = cloud_speech_pb2.LongRunningRecognizeResponse(
                results=results, )
            type_url = 'type.googleapis.com/%s' % (
                result_pb.DESCRIPTOR.full_name, )
            any_pb = Any(type_url=type_url,
                         value=result_pb.SerializeToString())

        return operations_pb2.Operation(name=self.OPERATION_NAME,
                                        response=any_pb)
    def test_long_running_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.LongRunningRecognizeResponse()
        operation = operations_pb2.Operation(
            name='operations/test_long_running_recognize', done=True)
        operation.response.Pack(expected_response)
        grpc_stub.LongRunningRecognize.return_value = operation

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

        grpc_stub.LongRunningRecognize.assert_called_once()
        args, kwargs = grpc_stub.LongRunningRecognize.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.LongRunningRecognizeRequest(
            config=config, audio=audio)
        self.assertEqual(expected_request, actual_request)
Example #4
0
    def test__update_state_with_empty_response(self):
        from google.cloud.proto.speech.v1 import cloud_speech_pb2
        from google.longrunning import operations_pb2
        from google.protobuf.any_pb2 import Any

        # Simulate an empty response (rather than no response yet, which
        # is distinct).
        response = cloud_speech_pb2.LongRunningRecognizeResponse(results=[])
        type_url = 'type.googleapis.com/%s' % response.DESCRIPTOR.full_name
        any_pb = Any(
            type_url=type_url,
            value=response.SerializeToString(),
        )
        operation_pb = operations_pb2.Operation(
            name=self.OPERATION_NAME,
            response=any_pb,
        )

        # Establish that we raise ValueError at state update time.
        client = object()
        operation = self._make_one(self.OPERATION_NAME, client)
        with self.assertRaises(ValueError):
            operation._update_state(operation_pb)