@@ -94,10 +94,8 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
9494
9595 from google .cloud ._helpers import _bytes_to_unicode
9696 from google .cloud ._helpers import _to_bytes
97- from google .cloud ._testing import _Monkey
9897
9998 from google .cloud import speech
100- from google .cloud .speech import client as MUT
10199 from google .cloud .speech .alternative import Alternative
102100 from google .cloud .speech .sample import Sample
103101 from unit_tests ._fixtures import SYNC_RECOGNIZE_RESPONSE
@@ -130,12 +128,12 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
130128
131129 sample = Sample (content = self .AUDIO_CONTENT , encoding = encoding ,
132130 sample_rate = self .SAMPLE_RATE )
133- with _Monkey ( MUT , _USE_GAX = False ):
134- response = client .sync_recognize (sample ,
135- language_code = 'EN' ,
136- max_alternatives = 2 ,
137- profanity_filter = True ,
138- speech_context = self .HINTS )
131+
132+ response = client .sync_recognize (sample ,
133+ language_code = 'EN' ,
134+ max_alternatives = 2 ,
135+ profanity_filter = True ,
136+ speech_context = self .HINTS )
139137
140138 self .assertEqual (len (client .connection ._requested ), 1 )
141139 req = client .connection ._requested [0 ]
@@ -152,10 +150,7 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
152150 self .assertEqual (response [0 ].confidence , expected .confidence )
153151
154152 def test_sync_recognize_source_uri_without_optional_params_no_gax (self ):
155- from google .cloud ._testing import _Monkey
156-
157153 from google .cloud import speech
158- from google .cloud .speech import client as MUT
159154 from google .cloud .speech .alternative import Alternative
160155 from google .cloud .speech .sample import Sample
161156 from unit_tests ._fixtures import SYNC_RECOGNIZE_RESPONSE
@@ -178,8 +173,8 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):
178173
179174 sample = Sample (source_uri = self .AUDIO_SOURCE_URI , encoding = encoding ,
180175 sample_rate = self .SAMPLE_RATE )
181- with _Monkey ( MUT , _USE_GAX = False ):
182- response = client .sync_recognize (sample )
176+
177+ response = client .sync_recognize (sample )
183178
184179 self .assertEqual (len (client .connection ._requested ), 1 )
185180 req = client .connection ._requested [0 ]
@@ -196,23 +191,20 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):
196191 self .assertEqual (response [0 ].confidence , expected .confidence )
197192
198193 def test_sync_recognize_with_empty_results_no_gax (self ):
199- from google .cloud ._testing import _Monkey
200-
201194 from google .cloud import speech
202- from google .cloud .speech import client as MUT
203195 from google .cloud .speech .sample import Sample
204196 from unit_tests ._fixtures import SYNC_RECOGNIZE_EMPTY_RESPONSE
205197
206198 credentials = _Credentials ()
207199 client = self ._makeOne (credentials = credentials , use_gax = False )
208200 client .connection = _Connection (SYNC_RECOGNIZE_EMPTY_RESPONSE )
209201
202+ sample = Sample (source_uri = self .AUDIO_SOURCE_URI ,
203+ encoding = speech .Encoding .FLAC ,
204+ sample_rate = self .SAMPLE_RATE )
205+
210206 with self .assertRaises (ValueError ):
211- with _Monkey (MUT , _USE_GAX = False ):
212- sample = Sample (source_uri = self .AUDIO_SOURCE_URI ,
213- encoding = speech .Encoding .FLAC ,
214- sample_rate = self .SAMPLE_RATE )
215- client .sync_recognize (sample )
207+ client .sync_recognize (sample )
216208
217209 def test_sync_recognize_with_empty_results_gax (self ):
218210 from google .cloud ._testing import _Monkey
@@ -334,7 +326,11 @@ def test_async_recognize_with_gax(self):
334326 sample = client .sample (source_uri = self .AUDIO_SOURCE_URI ,
335327 encoding = speech .Encoding .LINEAR16 ,
336328 sample_rate = self .SAMPLE_RATE )
337- with _Monkey (_gax , SpeechApi = _MockGAPICSpeechAPI ):
329+
330+ def speech_api ():
331+ return _MockGAPICSpeechAPI ()
332+
333+ with _Monkey (_gax , SpeechApi = speech_api ):
338334 operation = client .async_recognize (sample )
339335
340336 self .assertIsInstance (operation , Operation )
@@ -349,10 +345,18 @@ def test_speech_api_with_gax(self):
349345
350346 creds = _Credentials ()
351347 client = self ._makeOne (credentials = creds , use_gax = True )
348+ client .connection = _Connection ()
349+ client .connection .credentials = creds
350+
351+ def speech_api ():
352+ return _MockGAPICSpeechAPI ()
353+
354+ self .assertIsNone (client ._speech_api )
355+
356+ with _Monkey (_gax , SpeechApi = speech_api ):
357+ client ._speech_api = _gax .GAPICSpeechAPI (client )
352358
353- with _Monkey (_gax , SpeechApi = _MockGAPICSpeechAPI ):
354- self .assertIsNone (client ._speech_api )
355- self .assertIsInstance (client .speech_api , GAPICSpeechAPI )
359+ self .assertIsInstance (client .speech_api , GAPICSpeechAPI )
356360
357361 def test_speech_api_without_gax (self ):
358362 from google .cloud .connection import Connection
0 commit comments