|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import os |
15 | 16 | import unittest |
16 | 17 |
|
17 | 18 | from google.cloud import exceptions |
18 | 19 | from google.cloud import speech |
19 | 20 | from google.cloud import storage |
| 21 | +from google.cloud.speech.transcript import Transcript |
20 | 22 |
|
21 | 23 | from system_test_utils import unique_resource_id |
22 | 24 | from retry import RetryErrors |
23 | 25 |
|
24 | 26 |
|
25 | 27 | class Config(object): |
26 | 28 | """Run-time configuration to be modified at set-up. |
| 29 | +
|
27 | 30 | This is a mutable stand-in to allow test set-up to modify |
28 | 31 | global state. |
29 | 32 | """ |
30 | 33 | CLIENT = None |
31 | 34 | TEST_BUCKET = None |
| 35 | + AUDIO_FILE = os.path.join(os.path.dirname(__file__), 'data', 'hello.wav') |
| 36 | + ASSERT_TEXT = 'thank you for using Google Cloud platform' |
32 | 37 |
|
33 | 38 |
|
34 | 39 | def setUpModule(): |
@@ -69,48 +74,35 @@ def _make_sync_request(self, content=None, source_uri=None, |
69 | 74 | language_code='en-US', |
70 | 75 | max_alternatives=max_alternatives, |
71 | 76 | profanity_filter=True, |
72 | | - speech_context=['Google', |
73 | | - 'cloud']) |
| 77 | + speech_context=['Google', 'cloud']) |
74 | 78 | return result |
75 | 79 |
|
76 | 80 | def _check_best_results(self, results): |
77 | | - from google.cloud.speech.transcript import Transcript |
78 | | - |
79 | 81 | top_result = results[0] |
80 | 82 | self.assertIsInstance(top_result, Transcript) |
81 | 83 | self.assertEqual(top_result.transcript, |
82 | | - 'hello thank you for using Google Cloud platform') |
| 84 | + 'hello ' + Config.ASSERT_TEXT) |
83 | 85 | self.assertGreater(top_result.confidence, 0.90) |
84 | 86 |
|
85 | 87 | def test_sync_recognize_local_file(self): |
86 | | - import os |
87 | | - from google.cloud.speech.transcript import Transcript |
88 | | - |
89 | | - file_name = os.path.join('system_tests', 'data', 'hello.wav') |
90 | | - |
91 | | - with open(file_name, 'rb') as file_obj: |
| 88 | + with open(Config.AUDIO_FILE, 'rb') as file_obj: |
92 | 89 | results = self._make_sync_request(content=file_obj.read(), |
93 | 90 | max_alternatives=2) |
94 | 91 | second_alternative = results[1] |
95 | 92 | self.assertEqual(len(results), 2) |
96 | 93 | self._check_best_results(results) |
97 | 94 | self.assertIsInstance(second_alternative, Transcript) |
98 | | - self.assertEqual(second_alternative.transcript, |
99 | | - 'thank you for using Google Cloud platform') |
| 95 | + self.assertEqual(second_alternative.transcript, Config.ASSERT_TEXT) |
100 | 96 | self.assertEqual(second_alternative.confidence, 0.0) |
101 | 97 |
|
102 | 98 | def test_sync_recognize_gcs_file(self): |
103 | | - import os |
104 | | - |
105 | | - file_name = os.path.join('system_tests', 'data', 'hello.wav') |
106 | 99 | bucket_name = Config.TEST_BUCKET.name |
107 | | - blob_name = 'document.txt' |
| 100 | + blob_name = 'hello.wav' |
108 | 101 | blob = Config.TEST_BUCKET.blob(blob_name) |
109 | 102 | self.to_delete_by_case.append(blob) # Clean-up. |
110 | | - with open(file_name, 'rb') as file_obj: |
| 103 | + with open(Config.AUDIO_FILE, 'rb') as file_obj: |
111 | 104 | blob.upload_from_file(file_obj) |
112 | 105 |
|
113 | | - # source_uri = os.getenv('SPEECH_GCS_URI') |
114 | 106 | source_uri = 'gs://%s/%s' % (bucket_name, blob_name) |
115 | 107 | result = self._make_sync_request(source_uri=source_uri, |
116 | 108 | max_alternatives=1) |
|
0 commit comments