Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speech unit tests cleanup. #2676

Merged
merged 1 commit into from
Nov 4, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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