Skip to content

Commit c30ea2f

Browse files
committed
Create blob for GCS test, DRY up asserts.
1 parent 1ace6fd commit c30ea2f

File tree

1 file changed

+75
-19
lines changed

1 file changed

+75
-19
lines changed

system_tests/speech.py

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,56 @@
1414

1515
import unittest
1616

17+
from google.cloud import exceptions
18+
from google.cloud import speech
19+
from google.cloud import storage
20+
21+
from system_test_utils import unique_resource_id
22+
from retry import RetryErrors
23+
24+
25+
class Config(object):
26+
"""Run-time configuration to be modified at set-up.
27+
This is a mutable stand-in to allow test set-up to modify
28+
global state.
29+
"""
30+
CLIENT = None
31+
TEST_BUCKET = None
32+
33+
34+
def setUpModule():
35+
Config.CLIENT = speech.Client()
36+
# Now create a bucket for GCS stored content.
37+
storage_client = storage.Client()
38+
bucket_name = 'new' + unique_resource_id()
39+
Config.TEST_BUCKET = storage_client.bucket(bucket_name)
40+
# 429 Too Many Requests in case API requests rate-limited.
41+
retry_429 = RetryErrors(exceptions.TooManyRequests)
42+
retry_429(Config.TEST_BUCKET.create)()
43+
44+
45+
def tearDownModule():
46+
# 409 Conflict if the bucket is full.
47+
# 429 Too Many Requests in case API requests rate-limited.
48+
bucket_retry = RetryErrors(
49+
(exceptions.TooManyRequests, exceptions.Conflict))
50+
bucket_retry(Config.TEST_BUCKET.delete)(force=True)
51+
1752

1853
class TestSpeechClient(unittest.TestCase):
54+
def setUp(self):
55+
self.to_delete_by_case = []
56+
57+
def tearDown(self):
58+
for value in self.to_delete_by_case:
59+
value.delete()
1960

2061
def _make_sync_request(self, content=None, source_uri=None,
2162
max_alternatives=None):
22-
from google.cloud.speech.encoding import Encoding
23-
from google.cloud import speech
24-
25-
client = speech.Client()
63+
client = Config.CLIENT
2664
sample = client.sample(content=content,
2765
source_uri=source_uri,
28-
encoding=Encoding.LINEAR16,
66+
encoding=speech.Encoding.LINEAR16,
2967
sample_rate=16000)
3068
result = client.sync_recognize(sample,
3169
language_code='en-US',
@@ -35,27 +73,45 @@ def _make_sync_request(self, content=None, source_uri=None,
3573
'cloud'])
3674
return result
3775

76+
def _check_best_results(self, results):
77+
from google.cloud.speech.transcript import Transcript
78+
79+
top_result = results[0]
80+
self.assertIsInstance(top_result, Transcript)
81+
self.assertEqual(top_result.transcript,
82+
'hello thank you for using Google Cloud platform')
83+
self.assertGreater(top_result.confidence, 0.90)
84+
3885
def test_sync_recognize_local_file(self):
39-
file_name = 'system_tests/data/hello.wav'
86+
import os
87+
from google.cloud.speech.transcript import Transcript
4088

41-
with open(file_name, 'rb') as file_obj:
42-
result = self._make_sync_request(content=file_obj.read(),
43-
max_alternatives=2)
89+
file_name = os.path.join('system_tests', 'data', 'hello.wav')
4490

45-
self.assertEqual(result[0]['transcript'],
46-
'hello thank you for using Google Cloud platform')
47-
self.assertGreater(result[0]['confidence'], .90)
48-
self.assertEqual(result[1]['transcript'],
91+
with open(file_name, 'rb') as file_obj:
92+
results = self._make_sync_request(content=file_obj.read(),
93+
max_alternatives=2)
94+
second_alternative = results[1]
95+
self.assertEqual(len(results), 2)
96+
self._check_best_results(results)
97+
self.assertIsInstance(second_alternative, Transcript)
98+
self.assertEqual(second_alternative.transcript,
4999
'thank you for using Google Cloud platform')
50-
self.assertEqual(len(result), 2)
100+
self.assertEqual(second_alternative.confidence, 0.0)
51101

52102
def test_sync_recognize_gcs_file(self):
53103
import os
54104

55-
source_uri = os.getenv('SPEECH_GCS_URI')
105+
file_name = os.path.join('system_tests', 'data', 'hello.wav')
106+
bucket_name = Config.TEST_BUCKET.name
107+
blob_name = 'document.txt'
108+
blob = Config.TEST_BUCKET.blob(blob_name)
109+
self.to_delete_by_case.append(blob) # Clean-up.
110+
with open(file_name, 'rb') as file_obj:
111+
blob.upload_from_file(file_obj)
112+
113+
# source_uri = os.getenv('SPEECH_GCS_URI')
114+
source_uri = 'gs://%s/%s' % (bucket_name, blob_name)
56115
result = self._make_sync_request(source_uri=source_uri,
57116
max_alternatives=1)
58-
self.assertEqual(result[0]['transcript'],
59-
'hello thank you for using Google Cloud platform')
60-
self.assertGreater(result[0]['confidence'], .90)
61-
self.assertEqual(len(result), 1)
117+
self._check_best_results(result)

0 commit comments

Comments
 (0)