/
test_endpoint_service.py
771 lines (603 loc) · 29.8 KB
/
test_endpoint_service.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
# -*- 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
#
# 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.
#
from unittest import mock
import grpc
import math
import pytest
from google import auth
from google.api_core import client_options
from google.api_core import future
from google.api_core import operations_v1
from google.auth import credentials
from google.cloud.aiplatform_v1beta1.services.endpoint_service import (
EndpointServiceClient,
)
from google.cloud.aiplatform_v1beta1.services.endpoint_service import pagers
from google.cloud.aiplatform_v1beta1.services.endpoint_service import transports
from google.cloud.aiplatform_v1beta1.types import accelerator_type
from google.cloud.aiplatform_v1beta1.types import endpoint
from google.cloud.aiplatform_v1beta1.types import endpoint as gca_endpoint
from google.cloud.aiplatform_v1beta1.types import endpoint_service
from google.cloud.aiplatform_v1beta1.types import explanation
from google.cloud.aiplatform_v1beta1.types import explanation_metadata
from google.cloud.aiplatform_v1beta1.types import machine_resources
from google.cloud.aiplatform_v1beta1.types import operation as gca_operation
from google.longrunning import operations_pb2
from google.oauth2 import service_account
from google.protobuf import field_mask_pb2 as field_mask # type: ignore
from google.protobuf import struct_pb2 as struct # type: ignore
from google.protobuf import timestamp_pb2 as timestamp # type: ignore
def test_endpoint_service_client_from_service_account_file():
creds = credentials.AnonymousCredentials()
with mock.patch.object(
service_account.Credentials, "from_service_account_file"
) as factory:
factory.return_value = creds
client = EndpointServiceClient.from_service_account_file("dummy/file/path.json")
assert client._transport._credentials == creds
client = EndpointServiceClient.from_service_account_json("dummy/file/path.json")
assert client._transport._credentials == creds
assert client._transport._host == "aiplatform.googleapis.com:443"
def test_endpoint_service_client_client_options():
# Check the default options have their expected values.
assert (
EndpointServiceClient.DEFAULT_OPTIONS.api_endpoint
== "aiplatform.googleapis.com"
)
# Check that options can be customized.
options = client_options.ClientOptions(api_endpoint="squid.clam.whelk")
with mock.patch(
"google.cloud.aiplatform_v1beta1.services.endpoint_service.EndpointServiceClient.get_transport_class"
) as gtc:
transport = gtc.return_value = mock.MagicMock()
client = EndpointServiceClient(client_options=options)
transport.assert_called_once_with(credentials=None, host="squid.clam.whelk")
def test_endpoint_service_client_client_options_from_dict():
with mock.patch(
"google.cloud.aiplatform_v1beta1.services.endpoint_service.EndpointServiceClient.get_transport_class"
) as gtc:
transport = gtc.return_value = mock.MagicMock()
client = EndpointServiceClient(
client_options={"api_endpoint": "squid.clam.whelk"}
)
transport.assert_called_once_with(credentials=None, host="squid.clam.whelk")
def test_create_endpoint(transport: str = "grpc"):
client = EndpointServiceClient(
credentials=credentials.AnonymousCredentials(), transport=transport,
)
# Everything is optional in proto3 as far as the runtime is concerned,
# and we are mocking out the actual API, so just send an empty request.
request = endpoint_service.CreateEndpointRequest()
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.create_endpoint), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = operations_pb2.Operation(name="operations/spam")
response = client.create_endpoint(request)
# Establish that the underlying gRPC stub method was called.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0] == request
# Establish that the response is the type that we expect.
assert isinstance(response, future.Future)
def test_create_endpoint_flattened():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.create_endpoint), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = operations_pb2.Operation(name="operations/op")
# Call the method with a truthy value for each flattened field,
# using the keyword arguments to the method.
response = client.create_endpoint(
parent="parent_value", endpoint=gca_endpoint.Endpoint(name="name_value"),
)
# Establish that the underlying call was made with the expected
# request object values.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0].parent == "parent_value"
assert args[0].endpoint == gca_endpoint.Endpoint(name="name_value")
def test_create_endpoint_flattened_error():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Attempting to call a method with both a request object and flattened
# fields is an error.
with pytest.raises(ValueError):
client.create_endpoint(
endpoint_service.CreateEndpointRequest(),
parent="parent_value",
endpoint=gca_endpoint.Endpoint(name="name_value"),
)
def test_get_endpoint(transport: str = "grpc"):
client = EndpointServiceClient(
credentials=credentials.AnonymousCredentials(), transport=transport,
)
# Everything is optional in proto3 as far as the runtime is concerned,
# and we are mocking out the actual API, so just send an empty request.
request = endpoint_service.GetEndpointRequest()
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.get_endpoint), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = endpoint.Endpoint(
name="name_value",
display_name="display_name_value",
description="description_value",
etag="etag_value",
)
response = client.get_endpoint(request)
# Establish that the underlying gRPC stub method was called.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0] == request
# Establish that the response is the type that we expect.
assert isinstance(response, endpoint.Endpoint)
assert response.name == "name_value"
assert response.display_name == "display_name_value"
assert response.description == "description_value"
assert response.etag == "etag_value"
def test_get_endpoint_field_headers():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Any value that is part of the HTTP/1.1 URI should be sent as
# a field header. Set these to a non-empty value.
request = endpoint_service.GetEndpointRequest(name="name/value",)
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.get_endpoint), "__call__") as call:
call.return_value = endpoint.Endpoint()
client.get_endpoint(request)
# Establish that the underlying gRPC stub method was called.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0] == request
# Establish that the field header was sent.
_, _, kw = call.mock_calls[0]
assert ("x-goog-request-params", "name=name/value",) in kw["metadata"]
def test_get_endpoint_flattened():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.get_endpoint), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = endpoint.Endpoint()
# Call the method with a truthy value for each flattened field,
# using the keyword arguments to the method.
response = client.get_endpoint(name="name_value",)
# Establish that the underlying call was made with the expected
# request object values.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0].name == "name_value"
def test_get_endpoint_flattened_error():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Attempting to call a method with both a request object and flattened
# fields is an error.
with pytest.raises(ValueError):
client.get_endpoint(
endpoint_service.GetEndpointRequest(), name="name_value",
)
def test_list_endpoints(transport: str = "grpc"):
client = EndpointServiceClient(
credentials=credentials.AnonymousCredentials(), transport=transport,
)
# Everything is optional in proto3 as far as the runtime is concerned,
# and we are mocking out the actual API, so just send an empty request.
request = endpoint_service.ListEndpointsRequest()
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.list_endpoints), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = endpoint_service.ListEndpointsResponse(
next_page_token="next_page_token_value",
)
response = client.list_endpoints(request)
# Establish that the underlying gRPC stub method was called.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0] == request
# Establish that the response is the type that we expect.
assert isinstance(response, pagers.ListEndpointsPager)
assert response.next_page_token == "next_page_token_value"
def test_list_endpoints_field_headers():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Any value that is part of the HTTP/1.1 URI should be sent as
# a field header. Set these to a non-empty value.
request = endpoint_service.ListEndpointsRequest(parent="parent/value",)
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.list_endpoints), "__call__") as call:
call.return_value = endpoint_service.ListEndpointsResponse()
client.list_endpoints(request)
# Establish that the underlying gRPC stub method was called.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0] == request
# Establish that the field header was sent.
_, _, kw = call.mock_calls[0]
assert ("x-goog-request-params", "parent=parent/value",) in kw["metadata"]
def test_list_endpoints_flattened():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.list_endpoints), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = endpoint_service.ListEndpointsResponse()
# Call the method with a truthy value for each flattened field,
# using the keyword arguments to the method.
response = client.list_endpoints(parent="parent_value",)
# Establish that the underlying call was made with the expected
# request object values.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0].parent == "parent_value"
def test_list_endpoints_flattened_error():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Attempting to call a method with both a request object and flattened
# fields is an error.
with pytest.raises(ValueError):
client.list_endpoints(
endpoint_service.ListEndpointsRequest(), parent="parent_value",
)
def test_list_endpoints_pager():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials,)
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.list_endpoints), "__call__") as call:
# Set the response to a series of pages.
call.side_effect = (
endpoint_service.ListEndpointsResponse(
endpoints=[
endpoint.Endpoint(),
endpoint.Endpoint(),
endpoint.Endpoint(),
],
next_page_token="abc",
),
endpoint_service.ListEndpointsResponse(
endpoints=[], next_page_token="def",
),
endpoint_service.ListEndpointsResponse(
endpoints=[endpoint.Endpoint(),], next_page_token="ghi",
),
endpoint_service.ListEndpointsResponse(
endpoints=[endpoint.Endpoint(), endpoint.Endpoint(),],
),
RuntimeError,
)
results = [i for i in client.list_endpoints(request={},)]
assert len(results) == 6
assert all(isinstance(i, endpoint.Endpoint) for i in results)
def test_list_endpoints_pages():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials,)
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.list_endpoints), "__call__") as call:
# Set the response to a series of pages.
call.side_effect = (
endpoint_service.ListEndpointsResponse(
endpoints=[
endpoint.Endpoint(),
endpoint.Endpoint(),
endpoint.Endpoint(),
],
next_page_token="abc",
),
endpoint_service.ListEndpointsResponse(
endpoints=[], next_page_token="def",
),
endpoint_service.ListEndpointsResponse(
endpoints=[endpoint.Endpoint(),], next_page_token="ghi",
),
endpoint_service.ListEndpointsResponse(
endpoints=[endpoint.Endpoint(), endpoint.Endpoint(),],
),
RuntimeError,
)
pages = list(client.list_endpoints(request={}).pages)
for page, token in zip(pages, ["abc", "def", "ghi", ""]):
assert page.raw_page.next_page_token == token
def test_update_endpoint(transport: str = "grpc"):
client = EndpointServiceClient(
credentials=credentials.AnonymousCredentials(), transport=transport,
)
# Everything is optional in proto3 as far as the runtime is concerned,
# and we are mocking out the actual API, so just send an empty request.
request = endpoint_service.UpdateEndpointRequest()
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.update_endpoint), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = gca_endpoint.Endpoint(
name="name_value",
display_name="display_name_value",
description="description_value",
etag="etag_value",
)
response = client.update_endpoint(request)
# Establish that the underlying gRPC stub method was called.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0] == request
# Establish that the response is the type that we expect.
assert isinstance(response, gca_endpoint.Endpoint)
assert response.name == "name_value"
assert response.display_name == "display_name_value"
assert response.description == "description_value"
assert response.etag == "etag_value"
def test_update_endpoint_flattened():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.update_endpoint), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = gca_endpoint.Endpoint()
# Call the method with a truthy value for each flattened field,
# using the keyword arguments to the method.
response = client.update_endpoint(
endpoint=gca_endpoint.Endpoint(name="name_value"),
update_mask=field_mask.FieldMask(paths=["paths_value"]),
)
# Establish that the underlying call was made with the expected
# request object values.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0].endpoint == gca_endpoint.Endpoint(name="name_value")
assert args[0].update_mask == field_mask.FieldMask(paths=["paths_value"])
def test_update_endpoint_flattened_error():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Attempting to call a method with both a request object and flattened
# fields is an error.
with pytest.raises(ValueError):
client.update_endpoint(
endpoint_service.UpdateEndpointRequest(),
endpoint=gca_endpoint.Endpoint(name="name_value"),
update_mask=field_mask.FieldMask(paths=["paths_value"]),
)
def test_delete_endpoint(transport: str = "grpc"):
client = EndpointServiceClient(
credentials=credentials.AnonymousCredentials(), transport=transport,
)
# Everything is optional in proto3 as far as the runtime is concerned,
# and we are mocking out the actual API, so just send an empty request.
request = endpoint_service.DeleteEndpointRequest()
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.delete_endpoint), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = operations_pb2.Operation(name="operations/spam")
response = client.delete_endpoint(request)
# Establish that the underlying gRPC stub method was called.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0] == request
# Establish that the response is the type that we expect.
assert isinstance(response, future.Future)
def test_delete_endpoint_flattened():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.delete_endpoint), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = operations_pb2.Operation(name="operations/op")
# Call the method with a truthy value for each flattened field,
# using the keyword arguments to the method.
response = client.delete_endpoint(name="name_value",)
# Establish that the underlying call was made with the expected
# request object values.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0].name == "name_value"
def test_delete_endpoint_flattened_error():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Attempting to call a method with both a request object and flattened
# fields is an error.
with pytest.raises(ValueError):
client.delete_endpoint(
endpoint_service.DeleteEndpointRequest(), name="name_value",
)
def test_deploy_model(transport: str = "grpc"):
client = EndpointServiceClient(
credentials=credentials.AnonymousCredentials(), transport=transport,
)
# Everything is optional in proto3 as far as the runtime is concerned,
# and we are mocking out the actual API, so just send an empty request.
request = endpoint_service.DeployModelRequest()
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.deploy_model), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = operations_pb2.Operation(name="operations/spam")
response = client.deploy_model(request)
# Establish that the underlying gRPC stub method was called.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0] == request
# Establish that the response is the type that we expect.
assert isinstance(response, future.Future)
def test_deploy_model_flattened():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.deploy_model), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = operations_pb2.Operation(name="operations/op")
# Call the method with a truthy value for each flattened field,
# using the keyword arguments to the method.
response = client.deploy_model(
endpoint="endpoint_value",
deployed_model=gca_endpoint.DeployedModel(
dedicated_resources=machine_resources.DedicatedResources(
machine_spec=machine_resources.MachineSpec(
machine_type="machine_type_value"
)
)
),
traffic_split={"key_value": 541},
)
# Establish that the underlying call was made with the expected
# request object values.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0].endpoint == "endpoint_value"
assert args[0].deployed_model == gca_endpoint.DeployedModel(
dedicated_resources=machine_resources.DedicatedResources(
machine_spec=machine_resources.MachineSpec(
machine_type="machine_type_value"
)
)
)
assert args[0].traffic_split == {"key_value": 541}
def test_deploy_model_flattened_error():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Attempting to call a method with both a request object and flattened
# fields is an error.
with pytest.raises(ValueError):
client.deploy_model(
endpoint_service.DeployModelRequest(),
endpoint="endpoint_value",
deployed_model=gca_endpoint.DeployedModel(
dedicated_resources=machine_resources.DedicatedResources(
machine_spec=machine_resources.MachineSpec(
machine_type="machine_type_value"
)
)
),
traffic_split={"key_value": 541},
)
def test_undeploy_model(transport: str = "grpc"):
client = EndpointServiceClient(
credentials=credentials.AnonymousCredentials(), transport=transport,
)
# Everything is optional in proto3 as far as the runtime is concerned,
# and we are mocking out the actual API, so just send an empty request.
request = endpoint_service.UndeployModelRequest()
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.undeploy_model), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = operations_pb2.Operation(name="operations/spam")
response = client.undeploy_model(request)
# Establish that the underlying gRPC stub method was called.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0] == request
# Establish that the response is the type that we expect.
assert isinstance(response, future.Future)
def test_undeploy_model_flattened():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client._transport.undeploy_model), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = operations_pb2.Operation(name="operations/op")
# Call the method with a truthy value for each flattened field,
# using the keyword arguments to the method.
response = client.undeploy_model(
endpoint="endpoint_value",
deployed_model_id="deployed_model_id_value",
traffic_split={"key_value": 541},
)
# Establish that the underlying call was made with the expected
# request object values.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
assert args[0].endpoint == "endpoint_value"
assert args[0].deployed_model_id == "deployed_model_id_value"
assert args[0].traffic_split == {"key_value": 541}
def test_undeploy_model_flattened_error():
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
# Attempting to call a method with both a request object and flattened
# fields is an error.
with pytest.raises(ValueError):
client.undeploy_model(
endpoint_service.UndeployModelRequest(),
endpoint="endpoint_value",
deployed_model_id="deployed_model_id_value",
traffic_split={"key_value": 541},
)
def test_credentials_transport_error():
# It is an error to provide credentials and a transport instance.
transport = transports.EndpointServiceGrpcTransport(
credentials=credentials.AnonymousCredentials(),
)
with pytest.raises(ValueError):
client = EndpointServiceClient(
credentials=credentials.AnonymousCredentials(), transport=transport,
)
def test_transport_instance():
# A client may be instantiated with a custom transport instance.
transport = transports.EndpointServiceGrpcTransport(
credentials=credentials.AnonymousCredentials(),
)
client = EndpointServiceClient(transport=transport)
assert client._transport is transport
def test_transport_grpc_default():
# A client should use the gRPC transport by default.
client = EndpointServiceClient(credentials=credentials.AnonymousCredentials(),)
assert isinstance(client._transport, transports.EndpointServiceGrpcTransport,)
def test_endpoint_service_base_transport():
# Instantiate the base transport.
transport = transports.EndpointServiceTransport(
credentials=credentials.AnonymousCredentials(),
)
# Every method on the transport should just blindly
# raise NotImplementedError.
methods = (
"create_endpoint",
"get_endpoint",
"list_endpoints",
"update_endpoint",
"delete_endpoint",
"deploy_model",
"undeploy_model",
)
for method in methods:
with pytest.raises(NotImplementedError):
getattr(transport, method)(request=object())
# Additionally, the LRO client (a property) should
# also raise NotImplementedError
with pytest.raises(NotImplementedError):
transport.operations_client
def test_endpoint_service_auth_adc():
# If no credentials are provided, we should use ADC credentials.
with mock.patch.object(auth, "default") as adc:
adc.return_value = (credentials.AnonymousCredentials(), None)
EndpointServiceClient()
adc.assert_called_once_with(
scopes=("https://www.googleapis.com/auth/cloud-platform",)
)
def test_endpoint_service_host_no_port():
client = EndpointServiceClient(
credentials=credentials.AnonymousCredentials(),
client_options=client_options.ClientOptions(
api_endpoint="aiplatform.googleapis.com"
),
transport="grpc",
)
assert client._transport._host == "aiplatform.googleapis.com:443"
def test_endpoint_service_host_with_port():
client = EndpointServiceClient(
credentials=credentials.AnonymousCredentials(),
client_options=client_options.ClientOptions(
api_endpoint="aiplatform.googleapis.com:8000"
),
transport="grpc",
)
assert client._transport._host == "aiplatform.googleapis.com:8000"
def test_endpoint_service_grpc_transport_channel():
channel = grpc.insecure_channel("http://localhost/")
transport = transports.EndpointServiceGrpcTransport(channel=channel,)
assert transport.grpc_channel is channel
def test_endpoint_service_grpc_lro_client():
client = EndpointServiceClient(
credentials=credentials.AnonymousCredentials(), transport="grpc",
)
transport = client._transport
# Ensure that we have a api-core operations client.
assert isinstance(transport.operations_client, operations_v1.OperationsClient,)
# Ensure that subsequent calls to the property send the exact same object.
assert transport.operations_client is transport.operations_client
def test_endpoint_path():
project = "squid"
location = "clam"
endpoint = "whelk"
expected = "projects/{project}/locations/{location}/endpoints/{endpoint}".format(
project=project, location=location, endpoint=endpoint,
)
actual = EndpointServiceClient.endpoint_path(project, location, endpoint)
assert expected == actual