forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest__gax.py
More file actions
132 lines (114 loc) · 5.97 KB
/
test__gax.py
File metadata and controls
132 lines (114 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
class TestSpeechGAXMakeRequests(unittest.TestCase):
SAMPLE_RATE = 16000
HINTS = ['hi']
AUDIO_CONTENT = b'/9j/4QNURXhpZgAASUkq'
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,
max_alternatives=max_alternatives,
profanity_filter=profanity_filter,
speech_context=speech_context,
single_utterance=single_utterance,
interim_results=interim_results)
def test_ctor(self):
from google.cloud import speech
from google.cloud.speech.sample import Sample
from google.cloud.proto.speech.v1beta1.cloud_speech_pb2 import (
RecognitionConfig, SpeechContext, StreamingRecognitionConfig,
StreamingRecognizeRequest)
sample = Sample(content=self.AUDIO_CONTENT,
encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
language_code = 'US-en'
max_alternatives = 2
profanity_filter = True
speech_context = SpeechContext(phrases=self.HINTS)
single_utterance = True
interim_results = False
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().
# The first request can only have `streaming_config` set.
# The following requests can only have `audio_content` set.
self.assertEqual(streaming_request.audio_content, b'')
self.assertIsInstance(streaming_request.streaming_config,
StreamingRecognitionConfig)
streaming_config = streaming_request.streaming_config
self.assertTrue(streaming_config.single_utterance)
self.assertFalse(streaming_config.interim_results)
config = streaming_config.config
self.assertIsInstance(config, RecognitionConfig)
self.assertEqual(config.encoding, 2) # speech.Encoding.FLAC maps to 2.
self.assertEqual(config.sample_rate, self.SAMPLE_RATE)
self.assertEqual(config.language_code, language_code)
self.assertEqual(config.max_alternatives, max_alternatives)
self.assertTrue(config.profanity_filter)
self.assertEqual(config.speech_context.phrases, self.HINTS)
class TestSpeechGAXMakeRequestsStream(unittest.TestCase):
SAMPLE_RATE = 16000
HINTS = ['hi']
AUDIO_CONTENT = b'/9j/4QNURXhpZgAASUkq'
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,
max_alternatives=max_alternatives,
profanity_filter=profanity_filter,
speech_context=speech_context,
single_utterance=single_utterance,
interim_results=interim_results)
def test_stream_requests(self):
from io import BytesIO
from google.cloud import speech
from google.cloud.speech.sample import Sample
from google.cloud.proto.speech.v1beta1.cloud_speech_pb2 import (
StreamingRecognitionConfig, StreamingRecognizeRequest)
sample = Sample(stream=BytesIO(self.AUDIO_CONTENT),
encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
language_code = 'US-en'
max_alternatives = 2
profanity_filter = True
speech_context = self.HINTS
single_utterance = True
interim_results = False
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)
all_requests.append(streaming_request)
self.assertEqual(len(all_requests), 2)
config_request = all_requests[0]
streaming_request = all_requests[1]
# This isn't set by _make_streaming_request().
# The first request can only have `streaming_config` set.
# The following requests can only have `audio_content` set.
self.assertEqual(config_request.audio_content, b'')
self.assertEqual(streaming_request.audio_content, self.AUDIO_CONTENT)
self.assertIsInstance(config_request.streaming_config,
StreamingRecognitionConfig)