Skip to content

Commit 4f13b1b

Browse files
committed
Remove unneeded mocks, and PR feedback.
1 parent bf805e3 commit 4f13b1b

File tree

2 files changed

+65
-50
lines changed

2 files changed

+65
-50
lines changed

speech/google/cloud/speech/_gax.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414

1515
"""GAX/GAPIC module for managing Speech API requests."""
1616

17-
from google.longrunning import operations_grpc
17+
18+
from google.cloud._helpers import make_secure_stub
19+
from google.cloud.connection import DEFAULT_USER_AGENT
20+
21+
from google.cloud.speech.alternative import Alternative
22+
from google.cloud.speech.operation import Operation
1823

1924
from google.cloud.gapic.speech.v1beta1.speech_api import SpeechApi
20-
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
21-
AsyncRecognizeMetadata)
22-
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
23-
AsyncRecognizeResponse)
2425
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import SpeechContext
2526
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionConfig
2627
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionAudio
@@ -29,24 +30,21 @@
2930
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
3031
StreamingRecognizeRequest)
3132

32-
from google.cloud.speech.alternative import Alternative
33-
from google.cloud._helpers import make_secure_stub
34-
from google.cloud.connection import DEFAULT_USER_AGENT
35-
from google.cloud.speech.operation import Operation
36-
from google.cloud.operation import register_type
37-
33+
from google.longrunning import operations_grpc
3834

3935
OPERATIONS_API_HOST = 'speech.googleapis.com'
4036

41-
register_type(AsyncRecognizeMetadata)
42-
register_type(AsyncRecognizeResponse)
43-
4437

4538
class GAPICSpeechAPI(object):
4639
"""Manage calls through GAPIC wrappers to the Speech API."""
4740
def __init__(self, client=None):
4841
self._client = client
4942
self._gapic_api = SpeechApi()
43+
self._operations_stub = make_secure_stub(
44+
self._client.connection.credentials,
45+
DEFAULT_USER_AGENT,
46+
operations_grpc.OperationsStub,
47+
OPERATIONS_API_HOST)
5048

5149
def async_recognize(self, sample, language_code=None,
5250
max_alternatives=None, profanity_filter=None,
@@ -88,7 +86,7 @@ def async_recognize(self, sample, language_code=None,
8886
and phrases. This can also be used to add new
8987
words to the vocabulary of the recognizer.
9088
91-
:rtype: :class:`~google.cloud.operation.Opeartion`
89+
:rtype: :class:`~google.cloud.speech.operation.Operation`
9290
:returns: Instance of ``Operation`` to poll for results.
9391
"""
9492
config = RecognitionConfig(
@@ -102,12 +100,7 @@ def async_recognize(self, sample, language_code=None,
102100
api = self._gapic_api
103101
response = api.async_recognize(config=config, audio=audio)
104102

105-
self._client._operations_stub = make_secure_stub(
106-
self._client.connection.credentials,
107-
DEFAULT_USER_AGENT,
108-
operations_grpc.OperationsStub,
109-
OPERATIONS_API_HOST)
110-
return Operation.from_pb(response, self._client)
103+
return Operation.from_pb(response, self)
111104

112105
def sync_recognize(self, sample, language_code=None, max_alternatives=None,
113106
profanity_filter=None, speech_context=None):

speech/unit_tests/test_client.py

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ class TestClient(unittest.TestCase):
2121
AUDIO_SOURCE_URI = 'gs://sample-bucket/sample-recording.flac'
2222
AUDIO_CONTENT = '/9j/4QNURXhpZgAASUkq'
2323

24+
@staticmethod
25+
def _make_result(transcript, confidence):
26+
from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2
27+
return cloud_speech_pb2.SpeechRecognitionResult(
28+
alternatives=[
29+
cloud_speech_pb2.SpeechRecognitionAlternative(
30+
transcript=transcript,
31+
confidence=confidence,
32+
),
33+
],
34+
)
35+
36+
def _make_sync_response(self, *results):
37+
from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2
38+
response = cloud_speech_pb2.SyncRecognizeResponse(
39+
results=results,
40+
)
41+
42+
return response
43+
2444
def _getTargetClass(self):
2545
from google.cloud.speech.client import Client
2646

@@ -199,7 +219,9 @@ def test_sync_recognize_with_empty_results_gax(self):
199219
credentials = _Credentials()
200220
client = self._makeOne(credentials=credentials, use_gax=True)
201221
client.connection = _Connection()
202-
_MockGAPICSpeechAPI._results = []
222+
client.connection.credentials = credentials
223+
224+
_MockGAPICSpeechAPI._response = self._make_sync_response()
203225

204226
with self.assertRaises(ValueError):
205227
with _Monkey(MUT, SpeechApi=_MockGAPICSpeechAPI):
@@ -216,18 +238,20 @@ def test_sync_recognize_with_gax(self):
216238
creds = _Credentials()
217239
client = self._makeOne(credentials=creds, use_gax=True)
218240
client.connection = _Connection()
241+
client.connection.credentials = creds
219242
client._speech_api = None
220-
_MockGAPICSpeechAPI._results = [_MockGAPICSyncResult]
243+
transcript = 'testing 1 2 3'
244+
confidence = 0.9224355
245+
result = self._make_result(transcript, confidence)
246+
_MockGAPICSpeechAPI._response = self._make_sync_response(result)
221247

222248
with _Monkey(MUT, SpeechApi=_MockGAPICSpeechAPI):
223249
sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
224250
encoding=speech.Encoding.FLAC,
225251
sample_rate=self.SAMPLE_RATE)
226252
results = client.sync_recognize(sample)
227-
self.assertEqual(results[0].transcript,
228-
_MockGAPICAlternative.transcript)
229-
self.assertEqual(results[0].confidence,
230-
_MockGAPICAlternative.confidence)
253+
self.assertEqual(results[0].transcript, transcript)
254+
self.assertEqual(results[0].confidence, confidence)
231255

232256
def test_async_supported_encodings(self):
233257
from google.cloud import speech
@@ -270,6 +294,7 @@ def test_async_recognize_with_gax(self):
270294
from google.cloud.speech import _gax as MUT
271295
from google.cloud._testing import _Monkey
272296
from google.cloud import speech
297+
from google.cloud.speech.operation import Operation
273298

274299
credentials = _Credentials()
275300
client = self._makeOne(credentials=credentials)
@@ -282,6 +307,7 @@ def test_async_recognize_with_gax(self):
282307
with _Monkey(MUT, SpeechApi=_MockGAPICSpeechAPI):
283308
operation = client.async_recognize(sample)
284309

310+
self.assertIsInstance(operation, Operation)
285311
self.assertFalse(operation.complete)
286312
self.assertIsNone(operation.response)
287313

@@ -316,32 +342,28 @@ def test_speech_api_preset(self):
316342
self.assertIs(client.speech_api, fake_api)
317343

318344

319-
class _MockGAPICAlternative(object):
320-
transcript = 'testing 1 2 3'
321-
confidence = 0.95234356
322-
323-
324-
class _MockGAPICMetadata(object):
325-
type_url = None
326-
327-
328-
class _MockGAPICSyncResult(object):
329-
alternatives = [_MockGAPICAlternative()]
330-
331-
332-
class _MockGAPICSpeechResponse(object):
333-
error = None
334-
endpointer_type = None
335-
name = None
336-
metadata = _MockGAPICMetadata()
337-
results = []
338-
result_index = 0
345+
# class _MockGAPICAlternative(object):
346+
# transcript = 'testing 1 2 3'
347+
# confidence = 0.95234356
348+
#
349+
#
350+
# class _MockGAPICSyncResult(object):
351+
# alternatives = [_MockGAPICAlternative()]
352+
#
353+
#
354+
# class _MockGAPICSpeechResponse(object):
355+
# error = None
356+
# endpointer_type = None
357+
# name = None
358+
# metadata = _MockGAPICMetadata()
359+
# results = []
360+
# result_index = 0
339361

340362

341363
class _MockGAPICSpeechAPI(object):
342364
_requests = None
343-
_response = _MockGAPICSpeechResponse
344-
_results = [_MockGAPICSyncResult()]
365+
_response = None
366+
_results = None
345367

346368
def async_recognize(self, config, audio):
347369
from google.longrunning.operations_pb2 import Operation
@@ -353,7 +375,7 @@ def async_recognize(self, config, audio):
353375
def sync_recognize(self, config, audio):
354376
self.config = config
355377
self.audio = audio
356-
self._response.results = self._results
378+
# self._response.results = self._results
357379
return self._response
358380

359381

0 commit comments

Comments
 (0)