Skip to content

Commit cc505bd

Browse files
authored
Merge pull request #2547 from daspecster/add-speech-sync-system-tests
Add Speech sync recognize system test.
2 parents 2572967 + 9ce2626 commit cc505bd

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

system_tests/attempt_system_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
MODULES = ( # ordered from most to least stable
6060
'datastore',
6161
'storage',
62+
'speech',
6263
'bigquery',
6364
'pubsub',
6465
'language',

system_tests/data/hello.wav

119 KB
Binary file not shown.

system_tests/run_system_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import logging_
2424
import monitoring
2525
import pubsub
26+
import speech
2627
import storage
2728
import system_test_utils
2829
import translate
2930

3031

3132
TEST_MODULES = {
3233
'datastore': datastore,
34+
'speech': speech,
3335
'storage': storage,
3436
'pubsub': pubsub,
3537
'bigquery': bigquery,

system_tests/speech.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import unittest
17+
18+
from google.cloud import exceptions
19+
from google.cloud import speech
20+
from google.cloud import storage
21+
from google.cloud.speech.transcript import Transcript
22+
23+
from system_test_utils import unique_resource_id
24+
from retry import RetryErrors
25+
26+
AUDIO_FILE = os.path.join(os.path.dirname(__file__), 'data', 'hello.wav')
27+
28+
29+
class Config(object):
30+
"""Run-time configuration to be modified at set-up.
31+
32+
This is a mutable stand-in to allow test set-up to modify
33+
global state.
34+
"""
35+
CLIENT = None
36+
TEST_BUCKET = None
37+
38+
39+
def setUpModule():
40+
Config.CLIENT = speech.Client()
41+
# Now create a bucket for GCS stored content.
42+
storage_client = storage.Client()
43+
bucket_name = 'new' + unique_resource_id()
44+
Config.TEST_BUCKET = storage_client.bucket(bucket_name)
45+
# 429 Too Many Requests in case API requests rate-limited.
46+
retry_429 = RetryErrors(exceptions.TooManyRequests)
47+
retry_429(Config.TEST_BUCKET.create)()
48+
49+
50+
def tearDownModule():
51+
# 409 Conflict if the bucket is full.
52+
# 429 Too Many Requests in case API requests rate-limited.
53+
bucket_retry = RetryErrors(
54+
(exceptions.TooManyRequests, exceptions.Conflict))
55+
bucket_retry(Config.TEST_BUCKET.delete)(force=True)
56+
57+
58+
class TestSpeechClient(unittest.TestCase):
59+
ASSERT_TEXT = 'thank you for using Google Cloud platform'
60+
61+
def setUp(self):
62+
self.to_delete_by_case = []
63+
64+
def tearDown(self):
65+
for value in self.to_delete_by_case:
66+
value.delete()
67+
68+
def _make_sync_request(self, content=None, source_uri=None,
69+
max_alternatives=None):
70+
client = Config.CLIENT
71+
sample = client.sample(content=content,
72+
source_uri=source_uri,
73+
encoding=speech.Encoding.LINEAR16,
74+
sample_rate=16000)
75+
result = client.sync_recognize(sample,
76+
language_code='en-US',
77+
max_alternatives=max_alternatives,
78+
profanity_filter=True,
79+
speech_context=['Google', 'cloud'])
80+
return result
81+
82+
def _check_best_results(self, results):
83+
top_result = results[0]
84+
self.assertIsInstance(top_result, Transcript)
85+
self.assertEqual(top_result.transcript,
86+
'hello ' + self.ASSERT_TEXT)
87+
self.assertGreater(top_result.confidence, 0.90)
88+
89+
def test_sync_recognize_local_file(self):
90+
with open(Config.AUDIO_FILE, 'rb') as file_obj:
91+
results = self._make_sync_request(content=file_obj.read(),
92+
max_alternatives=2)
93+
second_alternative = results[1]
94+
self.assertEqual(len(results), 2)
95+
self._check_best_results(results)
96+
self.assertIsInstance(second_alternative, Transcript)
97+
self.assertEqual(second_alternative.transcript, self.ASSERT_TEXT)
98+
self.assertEqual(second_alternative.confidence, 0.0)
99+
100+
def test_sync_recognize_gcs_file(self):
101+
bucket_name = Config.TEST_BUCKET.name
102+
blob_name = 'hello.wav'
103+
blob = Config.TEST_BUCKET.blob(blob_name)
104+
self.to_delete_by_case.append(blob) # Clean-up.
105+
with open(Config.AUDIO_FILE, 'rb') as file_obj:
106+
blob.upload_from_file(file_obj)
107+
108+
source_uri = 'gs://%s/%s' % (bucket_name, blob_name)
109+
result = self._make_sync_request(source_uri=source_uri,
110+
max_alternatives=1)
111+
self._check_best_results(result)

0 commit comments

Comments
 (0)