Skip to content

Commit 77b6d16

Browse files
committed
Remove incorrect use of MUT, test multiple alternatives.
1 parent 2e9f486 commit 77b6d16

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

speech/unit_tests/test_client.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ class TestClient(unittest.TestCase):
2222
AUDIO_CONTENT = '/9j/4QNURXhpZgAASUkq'
2323

2424
@staticmethod
25-
def _make_result(transcript, confidence):
25+
def _make_result(alternatives):
2626
from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2
27+
2728
return cloud_speech_pb2.SpeechRecognitionResult(
2829
alternatives=[
2930
cloud_speech_pb2.SpeechRecognitionAlternative(
30-
transcript=transcript,
31-
confidence=confidence,
32-
),
31+
transcript=alternative['transcript'],
32+
confidence=alternative['confidence'],
33+
) for alternative in alternatives
3334
],
3435
)
3536

3637
def _make_sync_response(self, *results):
3738
from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2
39+
3840
response = cloud_speech_pb2.SyncRecognizeResponse(
3941
results=results,
4042
)
@@ -216,18 +218,19 @@ def test_sync_recognize_with_empty_results_gax(self):
216218
from google.cloud._testing import _Monkey
217219

218220
from google.cloud import speech
219-
from google.cloud.speech import _gax as MUT
221+
from google.cloud.speech import _gax
220222
from google.cloud.speech.sample import Sample
221223

222224
credentials = _Credentials()
223225
client = self._makeOne(credentials=credentials, use_gax=True)
224226
client.connection = _Connection()
225227
client.connection.credentials = credentials
226228

227-
_MockGAPICSpeechAPI._response = self._make_sync_response()
229+
def speech_api():
230+
return _MockGAPICSpeechAPI(response=self._make_sync_response())
228231

229232
with self.assertRaises(ValueError):
230-
with _Monkey(MUT, SpeechApi=_MockGAPICSpeechAPI):
233+
with _Monkey(_gax, SpeechApi=speech_api):
231234
sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
232235
encoding=speech.Encoding.FLAC,
233236
sample_rate=self.SAMPLE_RATE)
@@ -237,25 +240,39 @@ def test_sync_recognize_with_gax(self):
237240
from google.cloud._testing import _Monkey
238241

239242
from google.cloud import speech
240-
from google.cloud.speech import _gax as MUT
243+
from google.cloud.speech import _gax
241244

242245
creds = _Credentials()
243246
client = self._makeOne(credentials=creds, use_gax=True)
244247
client.connection = _Connection()
245248
client.connection.credentials = creds
246249
client._speech_api = None
247-
transcript = 'testing 1 2 3'
248-
confidence = 0.9224355
249-
result = self._make_result(transcript, confidence)
250-
_MockGAPICSpeechAPI._response = self._make_sync_response(result)
251-
252-
with _Monkey(MUT, SpeechApi=_MockGAPICSpeechAPI):
250+
alternatives = [{
251+
"transcript": 'testing 1 2 3',
252+
"confidence": 0.9224355
253+
}, {
254+
"transcript": 'testing 4 5 6',
255+
"confidence": 0.0123456
256+
}]
257+
result = self._make_result(alternatives)
258+
259+
def speech_api():
260+
return _MockGAPICSpeechAPI(
261+
response=self._make_sync_response(result))
262+
263+
with _Monkey(_gax, SpeechApi=speech_api):
253264
sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
254265
encoding=speech.Encoding.FLAC,
255266
sample_rate=self.SAMPLE_RATE)
256267
results = client.sync_recognize(sample)
257-
self.assertEqual(results[0].transcript, transcript)
258-
self.assertEqual(results[0].confidence, confidence)
268+
self.assertEqual(results[0].transcript,
269+
alternatives[0]['transcript'])
270+
self.assertEqual(results[0].confidence,
271+
alternatives[0]['confidence'])
272+
self.assertEqual(results[1].transcript,
273+
alternatives[1]['transcript'])
274+
self.assertEqual(results[1].confidence,
275+
alternatives[1]['confidence'])
259276

260277
def test_async_supported_encodings(self):
261278
from google.cloud import speech
@@ -298,7 +315,7 @@ def test_async_recognize_with_gax(self):
298315
from google.cloud._testing import _Monkey
299316

300317
from google.cloud import speech
301-
from google.cloud.speech import _gax as MUT
318+
from google.cloud.speech import _gax
302319
from google.cloud.speech.operation import Operation
303320

304321
credentials = _Credentials()
@@ -309,7 +326,7 @@ def test_async_recognize_with_gax(self):
309326
sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
310327
encoding=speech.Encoding.LINEAR16,
311328
sample_rate=self.SAMPLE_RATE)
312-
with _Monkey(MUT, SpeechApi=_MockGAPICSpeechAPI):
329+
with _Monkey(_gax, SpeechApi=_MockGAPICSpeechAPI):
313330
operation = client.async_recognize(sample)
314331

315332
self.assertIsInstance(operation, Operation)
@@ -319,13 +336,13 @@ def test_async_recognize_with_gax(self):
319336
def test_speech_api_with_gax(self):
320337
from google.cloud._testing import _Monkey
321338

322-
from google.cloud.speech import _gax as MUT
339+
from google.cloud.speech import _gax
323340
from google.cloud.speech.client import GAPICSpeechAPI
324341

325342
creds = _Credentials()
326343
client = self._makeOne(credentials=creds, use_gax=True)
327344

328-
with _Monkey(MUT, SpeechApi=_MockGAPICSpeechAPI):
345+
with _Monkey(_gax, SpeechApi=_MockGAPICSpeechAPI):
329346
self.assertIsNone(client._speech_api)
330347
self.assertIsInstance(client.speech_api, GAPICSpeechAPI)
331348

@@ -353,8 +370,12 @@ class _MockGAPICSpeechAPI(object):
353370
_response = None
354371
_results = None
355372

373+
def __init__(self, response=None):
374+
self._response = response
375+
356376
def async_recognize(self, config, audio):
357377
from google.longrunning.operations_pb2 import Operation
378+
358379
self.config = config
359380
self.audio = audio
360381
operation = Operation()
@@ -363,7 +384,6 @@ def async_recognize(self, config, audio):
363384
def sync_recognize(self, config, audio):
364385
self.config = config
365386
self.audio = audio
366-
# self._response.results = self._results
367387
return self._response
368388

369389

0 commit comments

Comments
 (0)