Skip to content

Commit

Permalink
Merge pull request #2710 from dhermes/pep8-names-in-tests
Browse files Browse the repository at this point in the history
Use PEP8 names in test helper methods
  • Loading branch information
dhermes committed Nov 10, 2016
2 parents 66ec62a + 8c01f5c commit ec34d90
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 85 deletions.
37 changes: 19 additions & 18 deletions packages/google-cloud-python-speech/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
class TestGAPICSpeechAPI(unittest.TestCase):
SAMPLE_RATE = 16000

def _getTargetClass(self):
@staticmethod
def _get_target_class():
from google.cloud.speech._gax import GAPICSpeechAPI

return GAPICSpeechAPI

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_use_bytes_instead_of_file_like_object(self):
from google.cloud import speech
Expand All @@ -38,7 +39,7 @@ def test_use_bytes_instead_of_file_like_object(self):
sample = Sample(content=b'', encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)

api = self._makeOne(client)
api = self._make_one(client)
with self.assertRaises(ValueError):
api.streaming_recognize(sample)
self.assertEqual(client.connection._requested, [])
Expand All @@ -49,9 +50,9 @@ class TestSpeechGAXMakeRequests(unittest.TestCase):
HINTS = ['hi']
AUDIO_CONTENT = b'/9j/4QNURXhpZgAASUkq'

def _callFUT(self, sample, language_code, max_alternatives,
profanity_filter, speech_context, single_utterance,
interim_results):
def _call_fut(self, sample, language_code, max_alternatives,
profanity_filter, speech_context, single_utterance,
interim_results):
from google.cloud.speech._gax import _make_streaming_request
return _make_streaming_request(sample=sample,
language_code=language_code,
Expand Down Expand Up @@ -83,10 +84,10 @@ def test_ctor(self):
single_utterance = True
interim_results = False

streaming_request = self._callFUT(sample, language_code,
max_alternatives, profanity_filter,
speech_context, single_utterance,
interim_results)
streaming_request = self._call_fut(sample, language_code,
max_alternatives, profanity_filter,
speech_context, single_utterance,
interim_results)
self.assertIsInstance(streaming_request, StreamingRecognizeRequest)

# This isn't set by _make_streaming_request().
Expand Down Expand Up @@ -114,9 +115,9 @@ class TestSpeechGAXMakeRequestsStream(unittest.TestCase):
HINTS = ['hi']
AUDIO_CONTENT = b'/9j/4QNURXhpZgAASUkq'

def _callFUT(self, sample, language_code, max_alternatives,
profanity_filter, speech_context, single_utterance,
interim_results):
def _call_fut(self, sample, language_code, max_alternatives,
profanity_filter, speech_context, single_utterance,
interim_results):
from google.cloud.speech._gax import _stream_requests
return _stream_requests(sample=sample,
language_code=language_code,
Expand Down Expand Up @@ -144,10 +145,10 @@ def test_stream_requests(self):
speech_context = self.HINTS
single_utterance = True
interim_results = False
streaming_requests = self._callFUT(sample, language_code,
max_alternatives, profanity_filter,
speech_context, single_utterance,
interim_results)
streaming_requests = self._call_fut(sample, language_code,
max_alternatives, profanity_filter,
speech_context, single_utterance,
interim_results)
all_requests = []
for streaming_request in streaming_requests:
self.assertIsInstance(streaming_request, StreamingRecognizeRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,37 @@

class TestAlternative(unittest.TestCase):

def _getTargetClass(self):
@staticmethod
def _get_target_class():
from google.cloud.speech.alternative import Alternative
return Alternative

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)
def _make_one(self, *args, **kwargs):
return self._get_target_class()(*args, **kwargs)

def test_constructor(self):
text = 'hello goodbye upstairs'
confidence = 0.5546875
alternative = self._makeOne(text, confidence)
alternative = self._make_one(text, confidence)
self.assertEqual(alternative._transcript, text)
self.assertEqual(alternative._confidence, confidence)

def test_transcript_property(self):
text = 'is this thing on?'
alternative = self._makeOne(text, None)
alternative = self._make_one(text, None)
self.assertEqual(alternative.transcript, text)

def test_confidence_property(self):
confidence = 0.412109375
alternative = self._makeOne(None, confidence)
alternative = self._make_one(None, confidence)
self.assertEqual(alternative.confidence, confidence)

def test_from_api_repr_with_no_confidence(self):
data = {
'transcript': 'testing 1 2 3',
}

klass = self._getTargetClass()
klass = self._get_target_class()
alternative = klass.from_api_repr(data)
self.assertEqual(alternative.transcript, data['transcript'])
self.assertIsNone(alternative.confidence)
Expand All @@ -59,7 +60,7 @@ def test_from_pb_with_no_confidence(self):
transcript=text)
self.assertEqual(pb_value.confidence, 0.0)

klass = self._getTargetClass()
klass = self._get_target_class()
alternative = klass.from_pb(pb_value)
self.assertEqual(alternative.transcript, text)
self.assertIsNone(alternative.confidence)
47 changes: 24 additions & 23 deletions packages/google-cloud-python-speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,37 @@ class TestClient(unittest.TestCase):
AUDIO_SOURCE_URI = 'gs://sample-bucket/sample-recording.flac'
AUDIO_CONTENT = '/9j/4QNURXhpZgAASUkq'

def _getTargetClass(self):
@staticmethod
def _get_target_class():
from google.cloud.speech.client import Client

return Client

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_ctor(self):
from google.cloud.speech.connection import Connection

creds = _Credentials()
http = object()
client = self._makeOne(credentials=creds, http=http)
client = self._make_one(credentials=creds, http=http)
self.assertIsInstance(client.connection, Connection)
self.assertTrue(client.connection.credentials is creds)
self.assertTrue(client.connection.http is http)

def test_ctor_use_gax_preset(self):
creds = _Credentials()
http = object()
client = self._makeOne(credentials=creds, http=http, use_gax=True)
client = self._make_one(credentials=creds, http=http, use_gax=True)
self.assertTrue(client._use_gax)

def test_create_sample_from_client(self):
from google.cloud import speech
from google.cloud.speech.sample import Sample

credentials = _Credentials()
client = self._makeOne(credentials=credentials)
client = self._make_one(credentials=credentials)

sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=speech.Encoding.FLAC,
Expand Down Expand Up @@ -144,7 +145,7 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
}
}
credentials = _Credentials()
client = self._makeOne(credentials=credentials, use_gax=False)
client = self._make_one(credentials=credentials, use_gax=False)
client.connection = _Connection(RETURNED)

encoding = speech.Encoding.FLAC
Expand Down Expand Up @@ -189,7 +190,7 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):
}
}
credentials = _Credentials()
client = self._makeOne(credentials=credentials, use_gax=False)
client = self._make_one(credentials=credentials, use_gax=False)
client.connection = _Connection(RETURNED)

encoding = speech.Encoding.FLAC
Expand Down Expand Up @@ -219,7 +220,7 @@ def test_sync_recognize_with_empty_results_no_gax(self):
from unit_tests._fixtures import SYNC_RECOGNIZE_EMPTY_RESPONSE

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

sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
Expand All @@ -237,7 +238,7 @@ def test_sync_recognize_with_empty_results_gax(self):
from google.cloud.speech.sample import Sample

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

Expand Down Expand Up @@ -280,7 +281,7 @@ def test_sync_recognize_with_gax(self):
from google.cloud.speech import _gax

creds = _Credentials()
client = self._makeOne(credentials=creds, use_gax=True)
client = self._make_one(credentials=creds, use_gax=True)
client.connection = _Connection()
client.connection.credentials = creds
client._speech_api = None
Expand Down Expand Up @@ -341,7 +342,7 @@ def test_async_supported_encodings(self):
from google.cloud.speech.sample import Sample

credentials = _Credentials()
client = self._makeOne(credentials=credentials)
client = self._make_one(credentials=credentials)
client.connection = _Connection({})

sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
Expand All @@ -359,7 +360,7 @@ def test_async_recognize_no_gax(self):
RETURNED = ASYNC_RECOGNIZE_RESPONSE

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

sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
Expand All @@ -381,8 +382,8 @@ def test_async_recognize_with_gax(self):
from google.cloud.speech.operation import Operation

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

Expand Down Expand Up @@ -423,7 +424,7 @@ def test_streaming_depends_on_gax(self):
from google.cloud._testing import _Monkey

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

with self.assertRaises(EnvironmentError):
Expand All @@ -439,7 +440,7 @@ def test_streaming_closed_stream(self):

stream = BytesIO(b'Some audio data...')
credentials = _Credentials()
client = self._makeOne(credentials=credentials)
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials

Expand Down Expand Up @@ -479,7 +480,7 @@ def test_stream_recognize_interim_results(self):

stream = BytesIO(b'Some audio data...')
credentials = _Credentials()
client = self._makeOne(credentials=credentials)
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials

Expand Down Expand Up @@ -541,7 +542,7 @@ def test_stream_recognize(self):

stream = BytesIO(b'Some audio data...')
credentials = _Credentials()
client = self._makeOne(credentials=credentials)
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials

Expand Down Expand Up @@ -597,7 +598,7 @@ def test_stream_recognize_no_results(self):

stream = BytesIO(b'Some audio data...')
credentials = _Credentials()
client = self._makeOne(credentials=credentials)
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials

Expand Down Expand Up @@ -633,7 +634,7 @@ def test_speech_api_with_gax(self):
from google.cloud.speech import _gax

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

Expand Down Expand Up @@ -666,14 +667,14 @@ def test_speech_api_without_gax(self):
from google.cloud.speech.client import _JSONSpeechAPI

creds = _Credentials()
client = self._makeOne(credentials=creds, use_gax=False)
client = self._make_one(credentials=creds, use_gax=False)
self.assertIsNone(client._speech_api)
self.assertIsInstance(client.speech_api, _JSONSpeechAPI)
self.assertIsInstance(client.speech_api.connection, Connection)

def test_speech_api_preset(self):
creds = _Credentials()
client = self._makeOne(credentials=creds)
client = self._make_one(credentials=creds)
fake_api = object()
client._speech_api = fake_api

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

class TestConnection(unittest.TestCase):

def _getTargetClass(self):
@staticmethod
def _get_target_class():
from google.cloud.speech.connection import Connection
return Connection

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_build_api_url(self):
conn = self._makeOne()
conn = self._make_one()
method = 'speech:syncrecognize'
uri = '/'.join([
conn.API_BASE_URL,
Expand Down
15 changes: 8 additions & 7 deletions packages/google-cloud-python-speech/unit_tests/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ class TestOperation(unittest.TestCase):

OPERATION_NAME = '123456789'

def _getTargetClass(self):
@staticmethod
def _get_target_class():
from google.cloud.speech.operation import Operation
return Operation

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)
def _make_one(self, *args, **kwargs):
return self._get_target_class()(*args, **kwargs)

def test_constructor(self):
client = object()
operation = self._makeOne(
operation = self._make_one(
self.OPERATION_NAME, client)
self.assertEqual(operation.name, self.OPERATION_NAME)
self.assertIs(operation.client, client)
Expand Down Expand Up @@ -74,7 +75,7 @@ def _make_operation_pb(self, *results):

def test__update_state_no_response(self):
client = object()
operation = self._makeOne(
operation = self._make_one(
self.OPERATION_NAME, client)

operation_pb = self._make_operation_pb()
Expand All @@ -86,7 +87,7 @@ def test__update_state_with_response(self):
from google.cloud.speech.alternative import Alternative

client = object()
operation = self._makeOne(
operation = self._make_one(
self.OPERATION_NAME, client)

text = 'hi mom'
Expand All @@ -104,7 +105,7 @@ def test__update_state_with_response(self):

def test__update_state_bad_response(self):
client = object()
operation = self._makeOne(
operation = self._make_one(
self.OPERATION_NAME, client)

result1 = self._make_result('is this ok?', 0.625)
Expand Down
Loading

0 comments on commit ec34d90

Please sign in to comment.