Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test_result.py for speech, update GAPICSpeechAPI docstring and test formatting. #3084

Merged
merged 2 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion speech/google/cloud/speech/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@


class GAPICSpeechAPI(object):
"""Manage calls through GAPIC wrappers to the Speech API."""
"""Manage calls through GAPIC wrappers to the Speech API.

:type client: `~google.cloud.core.client.Client`
:param client: Instance of ``Client`.
"""
def __init__(self, client=None):
self._client = client
credentials = self._client._credentials
Expand Down
6 changes: 3 additions & 3 deletions speech/google/cloud/speech/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, alternatives):
def from_pb(cls, result):
"""Factory: construct instance of ``Result``.

:type result: :class:`~google.cloud.grpc.speech.v1beta1\
:type result: :class:`~google.cloud.proto.speech.v1beta1\
.cloud_speech_pb2.SpeechRecognitionResult`
:param result: Instance of ``SpeechRecognitionResult`` protobuf.

Expand All @@ -50,7 +50,7 @@ def from_api_repr(cls, result):
"""Factory: construct instance of ``Result``.

:type result: dict
:param result: Dictionary of a :class:`~google.cloud.grpc.speech.\
:param result: Dictionary of a :class:`~google.cloud.proto.speech.\
v1beta1.cloud_speech_pb2.SpeechRecognitionResult`

:rtype: :class:`~google.cloud.speech.result.Result`
Expand Down Expand Up @@ -101,7 +101,7 @@ def __init__(self, alternatives, is_final=False, stability=0.0):
def from_pb(cls, response):
"""Factory: construct instance of ``StreamingSpeechResult``.

:type response: :class:`~google.cloud.grpc.speech.v1beta1\
:type response: :class:`~google.cloud.proto.speech.v1beta1\
.cloud_speech_pb2.StreamingRecognizeResult`
:param response: Instance of ``StreamingRecognizeResult`` protobuf.

Expand Down
4 changes: 2 additions & 2 deletions speech/unit_tests/_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}

ASYNC_RECOGNIZE_RESPONSE = {
'name': '123456789'
'name': '123456789',
}

OPERATION_COMPLETE_RESPONSE = {
Expand All @@ -51,7 +51,7 @@
'alternatives': [
{
'transcript': 'how old is the Brooklyn Bridge',
'confidence': 0.98267895
'confidence': 0.98267895,
},
],
},
Expand Down
93 changes: 36 additions & 57 deletions speech/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,39 +50,24 @@ def test_constructor(self, mocked_stub, mocked_cls, mocked_channel):
from google.cloud.speech._gax import OPERATIONS_API_HOST

credentials = _make_credentials()
mock_cnxn = mock.Mock(
credentials=credentials,
spec=['credentials'],
)
mock_cnxn = mock.Mock(credentials=credentials, spec=['credentials'])
mock_client = mock.Mock(
_connection=mock_cnxn,
_credentials=credentials,
spec=['_connection', '_credentials'],
)
_connection=mock_cnxn, _credentials=credentials,
spec=['_connection', '_credentials'])

speech_api = self._make_one(mock_client)
self.assertIs(speech_api._client, mock_client)
self.assertIs(
speech_api._gapic_api,
mocked_cls.return_value,
)
self.assertIs(speech_api._gapic_api, mocked_cls.return_value)

mocked_stub.assert_called_once_with(
mock_cnxn.credentials,
DEFAULT_USER_AGENT,
operations_grpc.OperationsStub,
OPERATIONS_API_HOST,
)
mock_cnxn.credentials, DEFAULT_USER_AGENT,
operations_grpc.OperationsStub, OPERATIONS_API_HOST)

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

mocked_cls.assert_called_once_with(
channel=mock.sentinel.channel,
lib_name='gccl',
lib_version=__version__,
)
channel=mock.sentinel.channel, lib_name='gccl',
lib_version=__version__)
mocked_channel.assert_called_once_with(
mock_cnxn.credentials,
DEFAULT_USER_AGENT,
mocked_cls.SERVICE_ADDRESS,
)
mock_cnxn.credentials, DEFAULT_USER_AGENT,
mocked_cls.SERVICE_ADDRESS)


class TestSpeechGAXMakeRequests(unittest.TestCase):
Expand All @@ -95,13 +80,11 @@ def _call_fut(self, sample, language_code, max_alternatives,
interim_results):
from google.cloud.speech._gax import _make_streaming_request

return _make_streaming_request(sample=sample,
language_code=language_code,
max_alternatives=max_alternatives,
profanity_filter=profanity_filter,
speech_context=speech_context,
single_utterance=single_utterance,
interim_results=interim_results)
return _make_streaming_request(
sample=sample, language_code=language_code,
max_alternatives=max_alternatives,
profanity_filter=profanity_filter, speech_context=speech_context,
single_utterance=single_utterance, interim_results=interim_results)

def test_ctor(self):
from google.cloud import speech
Expand All @@ -110,29 +93,28 @@ def test_ctor(self):
RecognitionConfig, SpeechContext, StreamingRecognitionConfig,
StreamingRecognizeRequest)

sample = Sample(content=self.AUDIO_CONTENT,
encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
sample = Sample(
content=self.AUDIO_CONTENT, encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
language_code = 'US-en'
max_alternatives = 2
profanity_filter = True
speech_context = SpeechContext(phrases=self.HINTS)
single_utterance = True
interim_results = False

streaming_request = self._call_fut(sample, language_code,
max_alternatives, profanity_filter,
speech_context, single_utterance,
interim_results)
streaming_request = self._call_fut(
sample, language_code, max_alternatives, profanity_filter,
speech_context, single_utterance, interim_results)
self.assertIsInstance(streaming_request, StreamingRecognizeRequest)

# This isn't set by _make_streaming_request().
# The first request can only have `streaming_config` set.
# The following requests can only have `audio_content` set.
self.assertEqual(streaming_request.audio_content, b'')

self.assertIsInstance(streaming_request.streaming_config,
StreamingRecognitionConfig)
self.assertIsInstance(
streaming_request.streaming_config, StreamingRecognitionConfig)
streaming_config = streaming_request.streaming_config
self.assertTrue(streaming_config.single_utterance)
self.assertFalse(streaming_config.interim_results)
Expand All @@ -156,13 +138,11 @@ def _call_fut(self, sample, language_code, max_alternatives,
interim_results):
from google.cloud.speech._gax import _stream_requests

return _stream_requests(sample=sample,
language_code=language_code,
max_alternatives=max_alternatives,
profanity_filter=profanity_filter,
speech_context=speech_context,
single_utterance=single_utterance,
interim_results=interim_results)
return _stream_requests(
sample=sample, language_code=language_code,
max_alternatives=max_alternatives,
profanity_filter=profanity_filter, speech_context=speech_context,
single_utterance=single_utterance, interim_results=interim_results)

def test_stream_requests(self):
from io import BytesIO
Expand All @@ -171,19 +151,18 @@ def test_stream_requests(self):
from google.cloud.proto.speech.v1beta1.cloud_speech_pb2 import (
StreamingRecognitionConfig, StreamingRecognizeRequest)

sample = Sample(stream=BytesIO(self.AUDIO_CONTENT),
encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
sample = Sample(
stream=BytesIO(self.AUDIO_CONTENT), encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
language_code = 'US-en'
max_alternatives = 2
profanity_filter = True
speech_context = self.HINTS
single_utterance = True
interim_results = False
streaming_requests = self._call_fut(sample, language_code,
max_alternatives, profanity_filter,
speech_context, single_utterance,
interim_results)
streaming_requests = self._call_fut(
sample, language_code, max_alternatives, profanity_filter,
speech_context, single_utterance, interim_results)
all_requests = []
for streaming_request in streaming_requests:
self.assertIsInstance(streaming_request, StreamingRecognizeRequest)
Expand All @@ -198,5 +177,5 @@ def test_stream_requests(self):
# The following requests can only have `audio_content` set.
self.assertEqual(config_request.audio_content, b'')
self.assertEqual(streaming_request.audio_content, self.AUDIO_CONTENT)
self.assertIsInstance(config_request.streaming_config,
StreamingRecognitionConfig)
self.assertIsInstance(
config_request.streaming_config, StreamingRecognitionConfig)
7 changes: 2 additions & 5 deletions speech/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,5 @@ def test_extra_headers(self):
}
expected_uri = conn.build_api_url('/rainbow')
http.request.assert_called_once_with(
body=req_data,
headers=expected_headers,
method='GET',
uri=expected_uri,
)
body=req_data, headers=expected_headers, method='GET',
uri=expected_uri)
Loading