Skip to content

Commit 8b072ed

Browse files
authored
Merge pull request googleapis#3084 from daspecster/speech-tests-update
Add test_result.py for speech, update GAPICSpeechAPI docstring and test formatting.
2 parents 770a821 + 346d154 commit 8b072ed

File tree

9 files changed

+279
-225
lines changed

9 files changed

+279
-225
lines changed

speech/google/cloud/speech/_gax.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939

4040

4141
class GAPICSpeechAPI(object):
42-
"""Manage calls through GAPIC wrappers to the Speech API."""
42+
"""Manage calls through GAPIC wrappers to the Speech API.
43+
44+
:type client: `~google.cloud.core.client.Client`
45+
:param client: Instance of ``Client`.
46+
"""
4347
def __init__(self, client=None):
4448
self._client = client
4549
credentials = self._client._credentials

speech/google/cloud/speech/result.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, alternatives):
3434
def from_pb(cls, result):
3535
"""Factory: construct instance of ``Result``.
3636
37-
:type result: :class:`~google.cloud.grpc.speech.v1beta1\
37+
:type result: :class:`~google.cloud.proto.speech.v1beta1\
3838
.cloud_speech_pb2.SpeechRecognitionResult`
3939
:param result: Instance of ``SpeechRecognitionResult`` protobuf.
4040
@@ -50,7 +50,7 @@ def from_api_repr(cls, result):
5050
"""Factory: construct instance of ``Result``.
5151
5252
:type result: dict
53-
:param result: Dictionary of a :class:`~google.cloud.grpc.speech.\
53+
:param result: Dictionary of a :class:`~google.cloud.proto.speech.\
5454
v1beta1.cloud_speech_pb2.SpeechRecognitionResult`
5555
5656
:rtype: :class:`~google.cloud.speech.result.Result`
@@ -101,7 +101,7 @@ def __init__(self, alternatives, is_final=False, stability=0.0):
101101
def from_pb(cls, response):
102102
"""Factory: construct instance of ``StreamingSpeechResult``.
103103
104-
:type response: :class:`~google.cloud.grpc.speech.v1beta1\
104+
:type response: :class:`~google.cloud.proto.speech.v1beta1\
105105
.cloud_speech_pb2.StreamingRecognizeResult`
106106
:param response: Instance of ``StreamingRecognizeResult`` protobuf.
107107

speech/unit_tests/_fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
}
3131

3232
ASYNC_RECOGNIZE_RESPONSE = {
33-
'name': '123456789'
33+
'name': '123456789',
3434
}
3535

3636
OPERATION_COMPLETE_RESPONSE = {
@@ -51,7 +51,7 @@
5151
'alternatives': [
5252
{
5353
'transcript': 'how old is the Brooklyn Bridge',
54-
'confidence': 0.98267895
54+
'confidence': 0.98267895,
5555
},
5656
],
5757
},

speech/unit_tests/test__gax.py

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,24 @@ def test_constructor(self, mocked_stub, mocked_cls, mocked_channel):
5050
from google.cloud.speech._gax import OPERATIONS_API_HOST
5151

5252
credentials = _make_credentials()
53-
mock_cnxn = mock.Mock(
54-
credentials=credentials,
55-
spec=['credentials'],
56-
)
53+
mock_cnxn = mock.Mock(credentials=credentials, spec=['credentials'])
5754
mock_client = mock.Mock(
58-
_connection=mock_cnxn,
59-
_credentials=credentials,
60-
spec=['_connection', '_credentials'],
61-
)
55+
_connection=mock_cnxn, _credentials=credentials,
56+
spec=['_connection', '_credentials'])
6257

6358
speech_api = self._make_one(mock_client)
6459
self.assertIs(speech_api._client, mock_client)
65-
self.assertIs(
66-
speech_api._gapic_api,
67-
mocked_cls.return_value,
68-
)
60+
self.assertIs(speech_api._gapic_api, mocked_cls.return_value)
6961

7062
mocked_stub.assert_called_once_with(
71-
mock_cnxn.credentials,
72-
DEFAULT_USER_AGENT,
73-
operations_grpc.OperationsStub,
74-
OPERATIONS_API_HOST,
75-
)
63+
mock_cnxn.credentials, DEFAULT_USER_AGENT,
64+
operations_grpc.OperationsStub, OPERATIONS_API_HOST)
7665
mocked_cls.assert_called_once_with(
77-
channel=mock.sentinel.channel,
78-
lib_name='gccl',
79-
lib_version=__version__,
80-
)
66+
channel=mock.sentinel.channel, lib_name='gccl',
67+
lib_version=__version__)
8168
mocked_channel.assert_called_once_with(
82-
mock_cnxn.credentials,
83-
DEFAULT_USER_AGENT,
84-
mocked_cls.SERVICE_ADDRESS,
85-
)
69+
mock_cnxn.credentials, DEFAULT_USER_AGENT,
70+
mocked_cls.SERVICE_ADDRESS)
8671

8772

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

98-
return _make_streaming_request(sample=sample,
99-
language_code=language_code,
100-
max_alternatives=max_alternatives,
101-
profanity_filter=profanity_filter,
102-
speech_context=speech_context,
103-
single_utterance=single_utterance,
104-
interim_results=interim_results)
83+
return _make_streaming_request(
84+
sample=sample, language_code=language_code,
85+
max_alternatives=max_alternatives,
86+
profanity_filter=profanity_filter, speech_context=speech_context,
87+
single_utterance=single_utterance, interim_results=interim_results)
10588

10689
def test_ctor(self):
10790
from google.cloud import speech
@@ -110,29 +93,28 @@ def test_ctor(self):
11093
RecognitionConfig, SpeechContext, StreamingRecognitionConfig,
11194
StreamingRecognizeRequest)
11295

113-
sample = Sample(content=self.AUDIO_CONTENT,
114-
encoding=speech.Encoding.FLAC,
115-
sample_rate=self.SAMPLE_RATE)
96+
sample = Sample(
97+
content=self.AUDIO_CONTENT, encoding=speech.Encoding.FLAC,
98+
sample_rate=self.SAMPLE_RATE)
11699
language_code = 'US-en'
117100
max_alternatives = 2
118101
profanity_filter = True
119102
speech_context = SpeechContext(phrases=self.HINTS)
120103
single_utterance = True
121104
interim_results = False
122105

123-
streaming_request = self._call_fut(sample, language_code,
124-
max_alternatives, profanity_filter,
125-
speech_context, single_utterance,
126-
interim_results)
106+
streaming_request = self._call_fut(
107+
sample, language_code, max_alternatives, profanity_filter,
108+
speech_context, single_utterance, interim_results)
127109
self.assertIsInstance(streaming_request, StreamingRecognizeRequest)
128110

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

134-
self.assertIsInstance(streaming_request.streaming_config,
135-
StreamingRecognitionConfig)
116+
self.assertIsInstance(
117+
streaming_request.streaming_config, StreamingRecognitionConfig)
136118
streaming_config = streaming_request.streaming_config
137119
self.assertTrue(streaming_config.single_utterance)
138120
self.assertFalse(streaming_config.interim_results)
@@ -156,13 +138,11 @@ def _call_fut(self, sample, language_code, max_alternatives,
156138
interim_results):
157139
from google.cloud.speech._gax import _stream_requests
158140

159-
return _stream_requests(sample=sample,
160-
language_code=language_code,
161-
max_alternatives=max_alternatives,
162-
profanity_filter=profanity_filter,
163-
speech_context=speech_context,
164-
single_utterance=single_utterance,
165-
interim_results=interim_results)
141+
return _stream_requests(
142+
sample=sample, language_code=language_code,
143+
max_alternatives=max_alternatives,
144+
profanity_filter=profanity_filter, speech_context=speech_context,
145+
single_utterance=single_utterance, interim_results=interim_results)
166146

167147
def test_stream_requests(self):
168148
from io import BytesIO
@@ -171,19 +151,18 @@ def test_stream_requests(self):
171151
from google.cloud.proto.speech.v1beta1.cloud_speech_pb2 import (
172152
StreamingRecognitionConfig, StreamingRecognizeRequest)
173153

174-
sample = Sample(stream=BytesIO(self.AUDIO_CONTENT),
175-
encoding=speech.Encoding.FLAC,
176-
sample_rate=self.SAMPLE_RATE)
154+
sample = Sample(
155+
stream=BytesIO(self.AUDIO_CONTENT), encoding=speech.Encoding.FLAC,
156+
sample_rate=self.SAMPLE_RATE)
177157
language_code = 'US-en'
178158
max_alternatives = 2
179159
profanity_filter = True
180160
speech_context = self.HINTS
181161
single_utterance = True
182162
interim_results = False
183-
streaming_requests = self._call_fut(sample, language_code,
184-
max_alternatives, profanity_filter,
185-
speech_context, single_utterance,
186-
interim_results)
163+
streaming_requests = self._call_fut(
164+
sample, language_code, max_alternatives, profanity_filter,
165+
speech_context, single_utterance, interim_results)
187166
all_requests = []
188167
for streaming_request in streaming_requests:
189168
self.assertIsInstance(streaming_request, StreamingRecognizeRequest)
@@ -198,5 +177,5 @@ def test_stream_requests(self):
198177
# The following requests can only have `audio_content` set.
199178
self.assertEqual(config_request.audio_content, b'')
200179
self.assertEqual(streaming_request.audio_content, self.AUDIO_CONTENT)
201-
self.assertIsInstance(config_request.streaming_config,
202-
StreamingRecognitionConfig)
180+
self.assertIsInstance(
181+
config_request.streaming_config, StreamingRecognitionConfig)

speech/unit_tests/test__http.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,5 @@ def test_extra_headers(self):
6363
}
6464
expected_uri = conn.build_api_url('/rainbow')
6565
http.request.assert_called_once_with(
66-
body=req_data,
67-
headers=expected_headers,
68-
method='GET',
69-
uri=expected_uri,
70-
)
66+
body=req_data, headers=expected_headers, method='GET',
67+
uri=expected_uri)

0 commit comments

Comments
 (0)