@@ -65,8 +65,11 @@ def test_sync_recognize_content_with_optional_parameters(self):
6565 from google .cloud ._helpers import _to_bytes
6666 from google .cloud ._helpers import _bytes_to_unicode
6767
68+ from google .cloud ._testing import _Monkey
69+ from google .cloud .speech import client as MUT
6870 from google .cloud .speech .encoding import Encoding
6971 from google .cloud .speech .sample import Sample
72+ from google .cloud .speech .transcript import Transcript
7073 from unit_tests ._fixtures import SYNC_RECOGNIZE_RESPONSE
7174 _AUDIO_CONTENT = _to_bytes (self .AUDIO_CONTENT )
7275 _B64_AUDIO_CONTENT = _bytes_to_unicode (b64encode (_AUDIO_CONTENT ))
@@ -96,25 +99,30 @@ def test_sync_recognize_content_with_optional_parameters(self):
9699
97100 sample = Sample (content = self .AUDIO_CONTENT , encoding = encoding ,
98101 sample_rate = self .SAMPLE_RATE )
99- response = client .sync_recognize (sample ,
100- language_code = 'EN' ,
101- max_alternatives = 2 ,
102- profanity_filter = True ,
103- speech_context = self .HINTS )
102+ with _Monkey (MUT , _USE_GAX = False ):
103+ response = client .sync_recognize (sample ,
104+ language_code = 'EN' ,
105+ max_alternatives = 2 ,
106+ profanity_filter = True ,
107+ speech_context = self .HINTS )
104108
105109 self .assertEqual (len (client .connection ._requested ), 1 )
106110 req = client .connection ._requested [0 ]
107111 self .assertEqual (len (req ), 3 )
108112 self .assertEqual (req ['data' ], REQUEST )
109113 self .assertEqual (req ['method' ], 'POST' )
110114 self .assertEqual (req ['path' ], 'speech:syncrecognize' )
111-
112- expected = SYNC_RECOGNIZE_RESPONSE ['results' ][0 ]['alternatives' ]
113- self .assertEqual (response , expected )
115+ alternative = SYNC_RECOGNIZE_RESPONSE ['results' ][0 ]['alternatives' ][0 ]
116+ expected = [Transcript .from_api_repr (alternative )]
117+ self .assertEqual (response [0 ].transcript , expected [0 ].transcript )
118+ self .assertEqual (response [0 ].confidence , expected [0 ].confidence )
114119
115120 def test_sync_recognize_source_uri_without_optional_parameters (self ):
121+ from google .cloud ._testing import _Monkey
122+ from google .cloud .speech import client as MUT
116123 from google .cloud .speech .encoding import Encoding
117124 from google .cloud .speech .sample import Sample
125+ from google .cloud .speech .transcript import Transcript
118126 from unit_tests ._fixtures import SYNC_RECOGNIZE_RESPONSE
119127
120128 RETURNED = SYNC_RECOGNIZE_RESPONSE
@@ -135,7 +143,8 @@ def test_sync_recognize_source_uri_without_optional_parameters(self):
135143
136144 sample = Sample (source_uri = self .AUDIO_SOURCE_URI , encoding = encoding ,
137145 sample_rate = self .SAMPLE_RATE )
138- response = client .sync_recognize (sample )
146+ with _Monkey (MUT , _USE_GAX = False ):
147+ response = client .sync_recognize (sample )
139148
140149 self .assertEqual (len (client .connection ._requested ), 1 )
141150 req = client .connection ._requested [0 ]
@@ -144,10 +153,14 @@ def test_sync_recognize_source_uri_without_optional_parameters(self):
144153 self .assertEqual (req ['method' ], 'POST' )
145154 self .assertEqual (req ['path' ], 'speech:syncrecognize' )
146155
147- expected = SYNC_RECOGNIZE_RESPONSE ['results' ][0 ]['alternatives' ]
148- self .assertEqual (response , expected )
156+ expected = [Transcript .from_api_repr (
157+ SYNC_RECOGNIZE_RESPONSE ['results' ][0 ]['alternatives' ][0 ])]
158+ self .assertEqual (response [0 ].transcript , expected [0 ].transcript )
159+ self .assertEqual (response [0 ].confidence , expected [0 ].confidence )
149160
150161 def test_sync_recognize_with_empty_results (self ):
162+ from google .cloud ._testing import _Monkey
163+ from google .cloud .speech import client as MUT
151164 from google .cloud .speech .encoding import Encoding
152165 from google .cloud .speech .sample import Sample
153166 from unit_tests ._fixtures import SYNC_RECOGNIZE_EMPTY_RESPONSE
@@ -156,11 +169,32 @@ def test_sync_recognize_with_empty_results(self):
156169 client = self ._makeOne (credentials = credentials )
157170 client .connection = _Connection (SYNC_RECOGNIZE_EMPTY_RESPONSE )
158171
159- with self .assertRaises (ValueError ):
160- sample = Sample (source_uri = self .AUDIO_SOURCE_URI ,
161- encoding = Encoding .FLAC ,
162- sample_rate = self .SAMPLE_RATE )
163- client .sync_recognize (sample )
172+ with self .assertRaises (IndexError ):
173+ with _Monkey (MUT , _USE_GAX = False ):
174+ sample = Sample (source_uri = self .AUDIO_SOURCE_URI ,
175+ encoding = Encoding .FLAC ,
176+ sample_rate = self .SAMPLE_RATE )
177+ client .sync_recognize (sample )
178+
179+ def test_sync_recognize_with_gapic (self ):
180+ from google .cloud .speech import client as MUT
181+ from google .cloud .speech import Encoding
182+ from google .cloud ._testing import _Monkey
183+ creds = _Credentials ()
184+ client = self ._makeOne (credentials = creds )
185+ client .connection = _Connection ()
186+
187+ client ._speech_api = _MockGAPICSpeechAPI ()
188+ client ._speech_api ._responses = []
189+
190+ with _Monkey (MUT , _USE_GAX = True , RecognitionConfig = _RecognitionConfig ,
191+ RecognitionAudio = _RecognitionAudio ):
192+ sample = client .sample (source_uri = self .AUDIO_SOURCE_URI ,
193+ encoding = Encoding .FLAC ,
194+ sample_rate = self .SAMPLE_RATE )
195+ results = client .sync_recognize (sample )
196+ self .assertEqual (results [0 ].transcript , 'testing 1 2 3' )
197+ self .assertEqual (results [0 ].confidence , 0.95234356 )
164198
165199 def test_async_supported_encodings (self ):
166200 from google .cloud .speech .encoding import Encoding
@@ -260,6 +294,31 @@ def test_stream_recognize(self):
260294 self .assertEqual (len (requests ), 2 )
261295
262296
297+ class _RecognitionConfig (object ):
298+ def __init__ (self , * args , ** kwargs ):
299+ self .args = args
300+ self .kwargs = kwargs
301+
302+
303+ class _RecognitionAudio (object ):
304+ def __init__ (self , content , uri ):
305+ self .content = content
306+ self .uri = uri
307+
308+
309+ class _MockGAPICAlternative (object ):
310+ transcript = 'testing 1 2 3'
311+ confidence = 0.95234356
312+
313+
314+ class _MockGAPICSyncResult (object ):
315+ alternatives = [_MockGAPICAlternative ()]
316+
317+
318+ class _MockGAPICSyncSpeechResponse (object ):
319+ results = [_MockGAPICSyncResult ()]
320+
321+
263322class _MockGAPICSpeechResponse (object ):
264323 error = None
265324 endpointer_type = None
@@ -275,6 +334,11 @@ def streaming_recognize(self, requests):
275334 self ._requests = requests
276335 return self ._responses
277336
337+ def sync_recognize (self , config , audio ):
338+ self .config = config
339+ self .audio = audio
340+ return _MockGAPICSyncSpeechResponse ()
341+
278342
279343class _Credentials (object ):
280344
0 commit comments