This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
/
test_translation_service_client_v3beta1.py
470 lines (384 loc) · 17 KB
/
test_translation_service_client_v3beta1.py
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# -*- coding: utf-8 -*-
#
# 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.
"""Unit tests."""
import mock
import pytest
from google.rpc import status_pb2
from google.cloud import translate_v3beta1
from google.cloud.translate_v3beta1.proto import translation_service_pb2
from google.longrunning import operations_pb2
class MultiCallableStub(object):
"""Stub for the grpc.UnaryUnaryMultiCallable interface."""
def __init__(self, method, channel_stub):
self.method = method
self.channel_stub = channel_stub
def __call__(self, request, timeout=None, metadata=None, credentials=None):
self.channel_stub.requests.append((self.method, request))
response = None
if self.channel_stub.responses:
response = self.channel_stub.responses.pop()
if isinstance(response, Exception):
raise response
if response:
return response
class ChannelStub(object):
"""Stub for the grpc.Channel interface."""
def __init__(self, responses=[]):
self.responses = responses
self.requests = []
def unary_unary(self, method, request_serializer=None, response_deserializer=None):
return MultiCallableStub(method, self)
class CustomException(Exception):
pass
class TestTranslationServiceClient(object):
def test_translate_text(self):
# Setup Expected Response
expected_response = {}
expected_response = translation_service_pb2.TranslateTextResponse(
**expected_response
)
# Mock the API response
channel = ChannelStub(responses=[expected_response])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup Request
contents = []
target_language_code = "targetLanguageCode1323228230"
parent = client.location_path("[PROJECT]", "[LOCATION]")
response = client.translate_text(contents, target_language_code, parent)
assert expected_response == response
assert len(channel.requests) == 1
expected_request = translation_service_pb2.TranslateTextRequest(
contents=contents, target_language_code=target_language_code, parent=parent
)
actual_request = channel.requests[0][1]
assert expected_request == actual_request
def test_translate_text_exception(self):
# Mock the API response
channel = ChannelStub(responses=[CustomException()])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup request
contents = []
target_language_code = "targetLanguageCode1323228230"
parent = client.location_path("[PROJECT]", "[LOCATION]")
with pytest.raises(CustomException):
client.translate_text(contents, target_language_code, parent)
def test_detect_language(self):
# Setup Expected Response
expected_response = {}
expected_response = translation_service_pb2.DetectLanguageResponse(
**expected_response
)
# Mock the API response
channel = ChannelStub(responses=[expected_response])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup Request
parent = client.location_path("[PROJECT]", "[LOCATION]")
response = client.detect_language(parent)
assert expected_response == response
assert len(channel.requests) == 1
expected_request = translation_service_pb2.DetectLanguageRequest(parent=parent)
actual_request = channel.requests[0][1]
assert expected_request == actual_request
def test_detect_language_exception(self):
# Mock the API response
channel = ChannelStub(responses=[CustomException()])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup request
parent = client.location_path("[PROJECT]", "[LOCATION]")
with pytest.raises(CustomException):
client.detect_language(parent)
def test_get_supported_languages(self):
# Setup Expected Response
expected_response = {}
expected_response = translation_service_pb2.SupportedLanguages(
**expected_response
)
# Mock the API response
channel = ChannelStub(responses=[expected_response])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup Request
parent = client.location_path("[PROJECT]", "[LOCATION]")
response = client.get_supported_languages(parent)
assert expected_response == response
assert len(channel.requests) == 1
expected_request = translation_service_pb2.GetSupportedLanguagesRequest(
parent=parent
)
actual_request = channel.requests[0][1]
assert expected_request == actual_request
def test_get_supported_languages_exception(self):
# Mock the API response
channel = ChannelStub(responses=[CustomException()])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup request
parent = client.location_path("[PROJECT]", "[LOCATION]")
with pytest.raises(CustomException):
client.get_supported_languages(parent)
def test_batch_translate_text(self):
# Setup Expected Response
total_characters = 1368640955
translated_characters = 1337326221
failed_characters = 1723028396
expected_response = {
"total_characters": total_characters,
"translated_characters": translated_characters,
"failed_characters": failed_characters,
}
expected_response = translation_service_pb2.BatchTranslateResponse(
**expected_response
)
operation = operations_pb2.Operation(
name="operations/test_batch_translate_text", done=True
)
operation.response.Pack(expected_response)
# Mock the API response
channel = ChannelStub(responses=[operation])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup Request
parent = client.location_path("[PROJECT]", "[LOCATION]")
source_language_code = "sourceLanguageCode1687263568"
target_language_codes = []
input_configs = []
output_config = {}
response = client.batch_translate_text(
parent,
source_language_code,
target_language_codes,
input_configs,
output_config,
)
result = response.result()
assert expected_response == result
assert len(channel.requests) == 1
expected_request = translation_service_pb2.BatchTranslateTextRequest(
parent=parent,
source_language_code=source_language_code,
target_language_codes=target_language_codes,
input_configs=input_configs,
output_config=output_config,
)
actual_request = channel.requests[0][1]
assert expected_request == actual_request
def test_batch_translate_text_exception(self):
# Setup Response
error = status_pb2.Status()
operation = operations_pb2.Operation(
name="operations/test_batch_translate_text_exception", done=True
)
operation.error.CopyFrom(error)
# Mock the API response
channel = ChannelStub(responses=[operation])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup Request
parent = client.location_path("[PROJECT]", "[LOCATION]")
source_language_code = "sourceLanguageCode1687263568"
target_language_codes = []
input_configs = []
output_config = {}
response = client.batch_translate_text(
parent,
source_language_code,
target_language_codes,
input_configs,
output_config,
)
exception = response.exception()
assert exception.errors[0] == error
def test_create_glossary(self):
# Setup Expected Response
name = "name3373707"
entry_count = 811131134
expected_response = {"name": name, "entry_count": entry_count}
expected_response = translation_service_pb2.Glossary(**expected_response)
operation = operations_pb2.Operation(
name="operations/test_create_glossary", done=True
)
operation.response.Pack(expected_response)
# Mock the API response
channel = ChannelStub(responses=[operation])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup Request
parent = client.location_path("[PROJECT]", "[LOCATION]")
glossary = {}
response = client.create_glossary(parent, glossary)
result = response.result()
assert expected_response == result
assert len(channel.requests) == 1
expected_request = translation_service_pb2.CreateGlossaryRequest(
parent=parent, glossary=glossary
)
actual_request = channel.requests[0][1]
assert expected_request == actual_request
def test_create_glossary_exception(self):
# Setup Response
error = status_pb2.Status()
operation = operations_pb2.Operation(
name="operations/test_create_glossary_exception", done=True
)
operation.error.CopyFrom(error)
# Mock the API response
channel = ChannelStub(responses=[operation])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup Request
parent = client.location_path("[PROJECT]", "[LOCATION]")
glossary = {}
response = client.create_glossary(parent, glossary)
exception = response.exception()
assert exception.errors[0] == error
def test_list_glossaries(self):
# Setup Expected Response
next_page_token = ""
glossaries_element = {}
glossaries = [glossaries_element]
expected_response = {
"next_page_token": next_page_token,
"glossaries": glossaries,
}
expected_response = translation_service_pb2.ListGlossariesResponse(
**expected_response
)
# Mock the API response
channel = ChannelStub(responses=[expected_response])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup Request
parent = client.location_path("[PROJECT]", "[LOCATION]")
paged_list_response = client.list_glossaries(parent)
resources = list(paged_list_response)
assert len(resources) == 1
assert expected_response.glossaries[0] == resources[0]
assert len(channel.requests) == 1
expected_request = translation_service_pb2.ListGlossariesRequest(parent=parent)
actual_request = channel.requests[0][1]
assert expected_request == actual_request
def test_list_glossaries_exception(self):
channel = ChannelStub(responses=[CustomException()])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup request
parent = client.location_path("[PROJECT]", "[LOCATION]")
paged_list_response = client.list_glossaries(parent)
with pytest.raises(CustomException):
list(paged_list_response)
def test_get_glossary(self):
# Setup Expected Response
name_2 = "name2-1052831874"
entry_count = 811131134
expected_response = {"name": name_2, "entry_count": entry_count}
expected_response = translation_service_pb2.Glossary(**expected_response)
# Mock the API response
channel = ChannelStub(responses=[expected_response])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup Request
name = client.glossary_path("[PROJECT]", "[LOCATION]", "[GLOSSARY]")
response = client.get_glossary(name)
assert expected_response == response
assert len(channel.requests) == 1
expected_request = translation_service_pb2.GetGlossaryRequest(name=name)
actual_request = channel.requests[0][1]
assert expected_request == actual_request
def test_get_glossary_exception(self):
# Mock the API response
channel = ChannelStub(responses=[CustomException()])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup request
name = client.glossary_path("[PROJECT]", "[LOCATION]", "[GLOSSARY]")
with pytest.raises(CustomException):
client.get_glossary(name)
def test_delete_glossary(self):
# Setup Expected Response
name_2 = "name2-1052831874"
expected_response = {"name": name_2}
expected_response = translation_service_pb2.DeleteGlossaryResponse(
**expected_response
)
operation = operations_pb2.Operation(
name="operations/test_delete_glossary", done=True
)
operation.response.Pack(expected_response)
# Mock the API response
channel = ChannelStub(responses=[operation])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup Request
name = client.glossary_path("[PROJECT]", "[LOCATION]", "[GLOSSARY]")
response = client.delete_glossary(name)
result = response.result()
assert expected_response == result
assert len(channel.requests) == 1
expected_request = translation_service_pb2.DeleteGlossaryRequest(name=name)
actual_request = channel.requests[0][1]
assert expected_request == actual_request
def test_delete_glossary_exception(self):
# Setup Response
error = status_pb2.Status()
operation = operations_pb2.Operation(
name="operations/test_delete_glossary_exception", done=True
)
operation.error.CopyFrom(error)
# Mock the API response
channel = ChannelStub(responses=[operation])
patch = mock.patch("google.api_core.grpc_helpers.create_channel")
with patch as create_channel:
create_channel.return_value = channel
client = translate_v3beta1.TranslationServiceClient()
# Setup Request
name = client.glossary_path("[PROJECT]", "[LOCATION]", "[GLOSSARY]")
response = client.delete_glossary(name)
exception = response.exception()
assert exception.errors[0] == error