Skip to content

Commit

Permalink
Speech unit tests cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
daspecster committed Nov 4, 2016
1 parent 3de5bc0 commit 3186072
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,8 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):

from google.cloud._helpers import _bytes_to_unicode
from google.cloud._helpers import _to_bytes
from google.cloud._testing import _Monkey

from google.cloud import speech
from google.cloud.speech import client as MUT
from google.cloud.speech.alternative import Alternative
from google.cloud.speech.sample import Sample
from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE
Expand Down Expand Up @@ -130,12 +128,12 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):

sample = Sample(content=self.AUDIO_CONTENT, encoding=encoding,
sample_rate=self.SAMPLE_RATE)
with _Monkey(MUT, _USE_GAX=False):
response = client.sync_recognize(sample,
language_code='EN',
max_alternatives=2,
profanity_filter=True,
speech_context=self.HINTS)

response = client.sync_recognize(sample,
language_code='EN',
max_alternatives=2,
profanity_filter=True,
speech_context=self.HINTS)

self.assertEqual(len(client.connection._requested), 1)
req = client.connection._requested[0]
Expand All @@ -152,10 +150,7 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
self.assertEqual(response[0].confidence, expected.confidence)

def test_sync_recognize_source_uri_without_optional_params_no_gax(self):
from google.cloud._testing import _Monkey

from google.cloud import speech
from google.cloud.speech import client as MUT
from google.cloud.speech.alternative import Alternative
from google.cloud.speech.sample import Sample
from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE
Expand All @@ -178,8 +173,8 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):

sample = Sample(source_uri=self.AUDIO_SOURCE_URI, encoding=encoding,
sample_rate=self.SAMPLE_RATE)
with _Monkey(MUT, _USE_GAX=False):
response = client.sync_recognize(sample)

response = client.sync_recognize(sample)

self.assertEqual(len(client.connection._requested), 1)
req = client.connection._requested[0]
Expand All @@ -196,23 +191,20 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):
self.assertEqual(response[0].confidence, expected.confidence)

def test_sync_recognize_with_empty_results_no_gax(self):
from google.cloud._testing import _Monkey

from google.cloud import speech
from google.cloud.speech import client as MUT
from google.cloud.speech.sample import Sample
from unit_tests._fixtures import SYNC_RECOGNIZE_EMPTY_RESPONSE

credentials = _Credentials()
client = self._makeOne(credentials=credentials, use_gax=False)
client.connection = _Connection(SYNC_RECOGNIZE_EMPTY_RESPONSE)

sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)

with self.assertRaises(ValueError):
with _Monkey(MUT, _USE_GAX=False):
sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
client.sync_recognize(sample)
client.sync_recognize(sample)

def test_sync_recognize_with_empty_results_gax(self):
from google.cloud._testing import _Monkey
Expand Down Expand Up @@ -334,7 +326,11 @@ def test_async_recognize_with_gax(self):
sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=speech.Encoding.LINEAR16,
sample_rate=self.SAMPLE_RATE)
with _Monkey(_gax, SpeechApi=_MockGAPICSpeechAPI):

def speech_api():
return _MockGAPICSpeechAPI()

with _Monkey(_gax, SpeechApi=speech_api):
operation = client.async_recognize(sample)

self.assertIsInstance(operation, Operation)
Expand All @@ -349,10 +345,18 @@ def test_speech_api_with_gax(self):

creds = _Credentials()
client = self._makeOne(credentials=creds, use_gax=True)
client.connection = _Connection()
client.connection.credentials = creds

def speech_api():
return _MockGAPICSpeechAPI()

self.assertIsNone(client._speech_api)

with _Monkey(_gax, SpeechApi=speech_api):
client._speech_api = _gax.GAPICSpeechAPI(client)

with _Monkey(_gax, SpeechApi=_MockGAPICSpeechAPI):
self.assertIsNone(client._speech_api)
self.assertIsInstance(client.speech_api, GAPICSpeechAPI)
self.assertIsInstance(client.speech_api, GAPICSpeechAPI)

def test_speech_api_without_gax(self):
from google.cloud.connection import Connection
Expand Down

0 comments on commit 3186072

Please sign in to comment.