-
Notifications
You must be signed in to change notification settings - Fork 535
/
speech_client_test.rb
306 lines (258 loc) · 11 KB
/
speech_client_test.rb
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Copyright 2020 Google LLC
#
# 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
#
# https://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.
require "minitest/autorun"
require "minitest/spec"
require "google/gax"
require "google/cloud/speech"
require "google/cloud/speech/v1/speech_client"
require "google/cloud/speech/v1/cloud_speech_services_pb"
require "google/longrunning/operations_pb"
class CustomTestError_v1 < StandardError; end
# Mock for the GRPC::ClientStub class.
class MockGrpcClientStub_v1
# @param expected_symbol [Symbol] the symbol of the grpc method to be mocked.
# @param mock_method [Proc] The method that is being mocked.
def initialize(expected_symbol, mock_method)
@expected_symbol = expected_symbol
@mock_method = mock_method
end
# This overrides the Object#method method to return the mocked method when the mocked method
# is being requested. For methods that aren't being tested, this method returns a proc that
# will raise an error when called. This is to assure that only the mocked grpc method is being
# called.
#
# @param symbol [Symbol] The symbol of the method being requested.
# @return [Proc] The proc of the requested method. If the requested method is not being mocked
# the proc returned will raise when called.
def method(symbol)
return @mock_method if symbol == @expected_symbol
# The requested method is not being tested, raise if it called.
proc do
raise "The method #{symbol} was unexpectedly called during the " \
"test for #{@expected_symbol}."
end
end
end
class MockSpeechCredentials_v1 < Google::Cloud::Speech::V1::Credentials
def initialize(method_name)
@method_name = method_name
end
def updater_proc
proc do
raise "The method `#{@method_name}` was trying to make a grpc request. This should not " \
"happen since the grpc layer is being mocked."
end
end
end
describe Google::Cloud::Speech::V1::SpeechClient do
describe 'recognize' do
custom_error = CustomTestError_v1.new "Custom test error for Google::Cloud::Speech::V1::SpeechClient#recognize."
it 'invokes recognize without error' do
# Create request parameters
encoding = :FLAC
sample_rate_hertz = 44100
language_code = "en-US"
config = {
encoding: encoding,
sample_rate_hertz: sample_rate_hertz,
language_code: language_code
}
uri = "gs://bucket_name/file_name.flac"
audio = { uri: uri }
# Create expected grpc response
expected_response = {}
expected_response = Google::Gax::to_proto(expected_response, Google::Cloud::Speech::V1::RecognizeResponse)
# Mock Grpc layer
mock_method = proc do |request|
assert_instance_of(Google::Cloud::Speech::V1::RecognizeRequest, request)
assert_equal(Google::Gax::to_proto(config, Google::Cloud::Speech::V1::RecognitionConfig), request.config)
assert_equal(Google::Gax::to_proto(audio, Google::Cloud::Speech::V1::RecognitionAudio), request.audio)
OpenStruct.new(execute: expected_response)
end
mock_stub = MockGrpcClientStub_v1.new(:recognize, mock_method)
# Mock auth layer
mock_credentials = MockSpeechCredentials_v1.new("recognize")
Google::Cloud::Speech::V1::Speech::Stub.stub(:new, mock_stub) do
Google::Cloud::Speech::V1::Credentials.stub(:default, mock_credentials) do
client = Google::Cloud::Speech.new(version: :v1)
# Call method
response = client.recognize(config, audio)
# Verify the response
assert_equal(expected_response, response)
# Call method with block
client.recognize(config, audio) do |response, operation|
# Verify the response
assert_equal(expected_response, response)
refute_nil(operation)
end
end
end
end
it 'invokes recognize with error' do
# Create request parameters
encoding = :FLAC
sample_rate_hertz = 44100
language_code = "en-US"
config = {
encoding: encoding,
sample_rate_hertz: sample_rate_hertz,
language_code: language_code
}
uri = "gs://bucket_name/file_name.flac"
audio = { uri: uri }
# Mock Grpc layer
mock_method = proc do |request|
assert_instance_of(Google::Cloud::Speech::V1::RecognizeRequest, request)
assert_equal(Google::Gax::to_proto(config, Google::Cloud::Speech::V1::RecognitionConfig), request.config)
assert_equal(Google::Gax::to_proto(audio, Google::Cloud::Speech::V1::RecognitionAudio), request.audio)
raise custom_error
end
mock_stub = MockGrpcClientStub_v1.new(:recognize, mock_method)
# Mock auth layer
mock_credentials = MockSpeechCredentials_v1.new("recognize")
Google::Cloud::Speech::V1::Speech::Stub.stub(:new, mock_stub) do
Google::Cloud::Speech::V1::Credentials.stub(:default, mock_credentials) do
client = Google::Cloud::Speech.new(version: :v1)
# Call method
err = assert_raises Google::Gax::GaxError, CustomTestError_v1 do
client.recognize(config, audio)
end
# Verify the GaxError wrapped the custom error that was raised.
assert_match(custom_error.message, err.message)
end
end
end
end
describe 'long_running_recognize' do
custom_error = CustomTestError_v1.new "Custom test error for Google::Cloud::Speech::V1::SpeechClient#long_running_recognize."
it 'invokes long_running_recognize without error' do
# Create request parameters
encoding = :FLAC
sample_rate_hertz = 44100
language_code = "en-US"
config = {
encoding: encoding,
sample_rate_hertz: sample_rate_hertz,
language_code: language_code
}
uri = "gs://bucket_name/file_name.flac"
audio = { uri: uri }
# Create expected grpc response
expected_response = {}
expected_response = Google::Gax::to_proto(expected_response, Google::Cloud::Speech::V1::LongRunningRecognizeResponse)
result = Google::Protobuf::Any.new
result.pack(expected_response)
operation = Google::Longrunning::Operation.new(
name: 'operations/long_running_recognize_test',
done: true,
response: result
)
# Mock Grpc layer
mock_method = proc do |request|
assert_instance_of(Google::Cloud::Speech::V1::LongRunningRecognizeRequest, request)
assert_equal(Google::Gax::to_proto(config, Google::Cloud::Speech::V1::RecognitionConfig), request.config)
assert_equal(Google::Gax::to_proto(audio, Google::Cloud::Speech::V1::RecognitionAudio), request.audio)
OpenStruct.new(execute: operation)
end
mock_stub = MockGrpcClientStub_v1.new(:long_running_recognize, mock_method)
# Mock auth layer
mock_credentials = MockSpeechCredentials_v1.new("long_running_recognize")
Google::Cloud::Speech::V1::Speech::Stub.stub(:new, mock_stub) do
Google::Cloud::Speech::V1::Credentials.stub(:default, mock_credentials) do
client = Google::Cloud::Speech.new(version: :v1)
# Call method
response = client.long_running_recognize(config, audio)
# Verify the response
assert_equal(expected_response, response.response)
end
end
end
it 'invokes long_running_recognize and returns an operation error.' do
# Create request parameters
encoding = :FLAC
sample_rate_hertz = 44100
language_code = "en-US"
config = {
encoding: encoding,
sample_rate_hertz: sample_rate_hertz,
language_code: language_code
}
uri = "gs://bucket_name/file_name.flac"
audio = { uri: uri }
# Create expected grpc response
operation_error = Google::Rpc::Status.new(
message: 'Operation error for Google::Cloud::Speech::V1::SpeechClient#long_running_recognize.'
)
operation = Google::Longrunning::Operation.new(
name: 'operations/long_running_recognize_test',
done: true,
error: operation_error
)
# Mock Grpc layer
mock_method = proc do |request|
assert_instance_of(Google::Cloud::Speech::V1::LongRunningRecognizeRequest, request)
assert_equal(Google::Gax::to_proto(config, Google::Cloud::Speech::V1::RecognitionConfig), request.config)
assert_equal(Google::Gax::to_proto(audio, Google::Cloud::Speech::V1::RecognitionAudio), request.audio)
OpenStruct.new(execute: operation)
end
mock_stub = MockGrpcClientStub_v1.new(:long_running_recognize, mock_method)
# Mock auth layer
mock_credentials = MockSpeechCredentials_v1.new("long_running_recognize")
Google::Cloud::Speech::V1::Speech::Stub.stub(:new, mock_stub) do
Google::Cloud::Speech::V1::Credentials.stub(:default, mock_credentials) do
client = Google::Cloud::Speech.new(version: :v1)
# Call method
response = client.long_running_recognize(config, audio)
# Verify the response
assert(response.error?)
assert_equal(operation_error, response.error)
end
end
end
it 'invokes long_running_recognize with error' do
# Create request parameters
encoding = :FLAC
sample_rate_hertz = 44100
language_code = "en-US"
config = {
encoding: encoding,
sample_rate_hertz: sample_rate_hertz,
language_code: language_code
}
uri = "gs://bucket_name/file_name.flac"
audio = { uri: uri }
# Mock Grpc layer
mock_method = proc do |request|
assert_instance_of(Google::Cloud::Speech::V1::LongRunningRecognizeRequest, request)
assert_equal(Google::Gax::to_proto(config, Google::Cloud::Speech::V1::RecognitionConfig), request.config)
assert_equal(Google::Gax::to_proto(audio, Google::Cloud::Speech::V1::RecognitionAudio), request.audio)
raise custom_error
end
mock_stub = MockGrpcClientStub_v1.new(:long_running_recognize, mock_method)
# Mock auth layer
mock_credentials = MockSpeechCredentials_v1.new("long_running_recognize")
Google::Cloud::Speech::V1::Speech::Stub.stub(:new, mock_stub) do
Google::Cloud::Speech::V1::Credentials.stub(:default, mock_credentials) do
client = Google::Cloud::Speech.new(version: :v1)
# Call method
err = assert_raises Google::Gax::GaxError, CustomTestError_v1 do
client.long_running_recognize(config, audio)
end
# Verify the GaxError wrapped the custom error that was raised.
assert_match(custom_error.message, err.message)
end
end
end
end
end