Skip to content

Commit 16469c4

Browse files
authored
chore: restore 100% coverage for generated clients (googleapis#1173)
1 parent 4f4ad39 commit 16469c4

File tree

8 files changed

+456
-269
lines changed

8 files changed

+456
-269
lines changed

gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -395,42 +395,6 @@ def test_{{ service.client_name|snake_case }}_client_options_credentials_file(cl
395395
client_info=transports.base.DEFAULT_CLIENT_INFO,
396396
always_use_jwt_access=True,
397397
)
398-
399-
{% if 'grpc' in opts.transport %}
400-
if "grpc" in transport_name:
401-
# test that the credentials from file are saved and used as the credentials.
402-
with mock.patch.object(
403-
google.auth, "load_credentials_from_file", autospec=True
404-
) as load_creds, mock.patch.object(
405-
google.auth, "default", autospec=True
406-
) as adc, mock.patch.object(
407-
grpc_helpers, "create_channel"
408-
) as create_channel:
409-
creds = ga_credentials.AnonymousCredentials()
410-
file_creds = ga_credentials.AnonymousCredentials()
411-
load_creds.return_value = (file_creds, None)
412-
adc.return_value = (creds, None)
413-
client = client_class(client_options=options, transport=transport_name)
414-
{% with host = (service.host|default('localhost', true)) %}
415-
create_channel.assert_called_with(
416-
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
417-
credentials=file_creds,
418-
credentials_file=None,
419-
quota_project_id=None,
420-
default_scopes=(
421-
{% for scope in service.oauth_scopes %}
422-
'{{ scope }}',
423-
{% endfor %}),
424-
scopes=None,
425-
default_host="{{ host }}",
426-
ssl_credentials=None,
427-
options=[
428-
("grpc.max_send_message_length", -1),
429-
("grpc.max_receive_message_length", -1),
430-
],
431-
)
432-
{% endwith %}
433-
{% endif %}
434398
{% if 'grpc' in opts.transport %}
435399
{# TODO(dovs): genericize this function#}
436400

@@ -450,6 +414,63 @@ def test_{{ service.client_name|snake_case }}_client_options_from_dict():
450414
client_info=transports.base.DEFAULT_CLIENT_INFO,
451415
always_use_jwt_access=True,
452416
)
417+
418+
419+
@pytest.mark.parametrize("client_class,transport_class,transport_name", [
420+
({{ service.client_name }}, transports.{{ service.grpc_transport_name }}, "grpc"),
421+
])
422+
def test_{{ service.client_name|snake_case }}_create_channel_credentials_file(client_class, transport_class, transport_name):
423+
# Check the case credentials file is provided.
424+
options = client_options.ClientOptions(
425+
credentials_file="credentials.json"
426+
)
427+
428+
with mock.patch.object(transport_class, '__init__') as patched:
429+
patched.return_value = None
430+
client = client_class(client_options=options, transport=transport_name)
431+
patched.assert_called_once_with(
432+
credentials=None,
433+
credentials_file="credentials.json",
434+
host=client.DEFAULT_ENDPOINT,
435+
scopes=None,
436+
client_cert_source_for_mtls=None,
437+
quota_project_id=None,
438+
client_info=transports.base.DEFAULT_CLIENT_INFO,
439+
always_use_jwt_access=True,
440+
)
441+
442+
# test that the credentials from file are saved and used as the credentials.
443+
with mock.patch.object(
444+
google.auth, "load_credentials_from_file", autospec=True
445+
) as load_creds, mock.patch.object(
446+
google.auth, "default", autospec=True
447+
) as adc, mock.patch.object(
448+
grpc_helpers, "create_channel"
449+
) as create_channel:
450+
creds = ga_credentials.AnonymousCredentials()
451+
file_creds = ga_credentials.AnonymousCredentials()
452+
load_creds.return_value = (file_creds, None)
453+
adc.return_value = (creds, None)
454+
client = client_class(client_options=options, transport=transport_name)
455+
{% with host = (service.host|default('localhost', true)) %}
456+
create_channel.assert_called_with(
457+
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
458+
credentials=file_creds,
459+
credentials_file=None,
460+
quota_project_id=None,
461+
default_scopes=(
462+
{% for scope in service.oauth_scopes %}
463+
'{{ scope }}',
464+
{% endfor %}),
465+
scopes=None,
466+
default_host="{{ host }}",
467+
ssl_credentials=None,
468+
options=[
469+
("grpc.max_send_message_length", -1),
470+
("grpc.max_receive_message_length", -1),
471+
],
472+
)
473+
{% endwith %}
453474
{% endif %}
454475

455476

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -476,42 +476,6 @@ def test_{{ service.client_name|snake_case }}_client_options_credentials_file(cl
476476
client_info=transports.base.DEFAULT_CLIENT_INFO,
477477
always_use_jwt_access=True,
478478
)
479-
480-
{% if 'grpc' in opts.transport %}
481-
if "grpc" in transport_name:
482-
# test that the credentials from file are saved and used as the credentials.
483-
with mock.patch.object(
484-
google.auth, "load_credentials_from_file", autospec=True
485-
) as load_creds, mock.patch.object(
486-
google.auth, "default", autospec=True
487-
) as adc, mock.patch.object(
488-
grpc_helpers, "create_channel"
489-
) as create_channel:
490-
creds = ga_credentials.AnonymousCredentials()
491-
file_creds = ga_credentials.AnonymousCredentials()
492-
load_creds.return_value = (file_creds, None)
493-
adc.return_value = (creds, None)
494-
client = client_class(client_options=options, transport=transport_name)
495-
{% with host = (service.host|default('localhost', true)) %}
496-
create_channel.assert_called_with(
497-
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
498-
credentials=file_creds,
499-
credentials_file=None,
500-
quota_project_id=None,
501-
default_scopes=(
502-
{% for scope in service.oauth_scopes %}
503-
'{{ scope }}',
504-
{% endfor %}),
505-
scopes=None,
506-
default_host="{{ host }}",
507-
ssl_credentials=None,
508-
options=[
509-
("grpc.max_send_message_length", -1),
510-
("grpc.max_receive_message_length", -1),
511-
],
512-
)
513-
{% endwith %}
514-
{% endif %}
515479
{% if 'grpc' in opts.transport %}
516480
{# TODO(dovs): genericize this function#}
517481

@@ -531,6 +495,64 @@ def test_{{ service.client_name|snake_case }}_client_options_from_dict():
531495
client_info=transports.base.DEFAULT_CLIENT_INFO,
532496
always_use_jwt_access=True,
533497
)
498+
499+
500+
@pytest.mark.parametrize("client_class,transport_class,transport_name,grpc_helpers", [
501+
({{ service.client_name }}, transports.{{ service.grpc_transport_name }}, "grpc", grpc_helpers),
502+
({{ service.async_client_name }}, transports.{{ service.grpc_asyncio_transport_name }}, "grpc_asyncio", grpc_helpers_async),
503+
])
504+
def test_{{ service.client_name|snake_case }}_create_channel_credentials_file(client_class, transport_class, transport_name, grpc_helpers):
505+
# Check the case credentials file is provided.
506+
options = client_options.ClientOptions(
507+
credentials_file="credentials.json"
508+
)
509+
510+
with mock.patch.object(transport_class, '__init__') as patched:
511+
patched.return_value = None
512+
client = client_class(client_options=options, transport=transport_name)
513+
patched.assert_called_once_with(
514+
credentials=None,
515+
credentials_file="credentials.json",
516+
host=client.DEFAULT_ENDPOINT,
517+
scopes=None,
518+
client_cert_source_for_mtls=None,
519+
quota_project_id=None,
520+
client_info=transports.base.DEFAULT_CLIENT_INFO,
521+
always_use_jwt_access=True,
522+
)
523+
524+
# test that the credentials from file are saved and used as the credentials.
525+
with mock.patch.object(
526+
google.auth, "load_credentials_from_file", autospec=True
527+
) as load_creds, mock.patch.object(
528+
google.auth, "default", autospec=True
529+
) as adc, mock.patch.object(
530+
grpc_helpers, "create_channel"
531+
) as create_channel:
532+
creds = ga_credentials.AnonymousCredentials()
533+
file_creds = ga_credentials.AnonymousCredentials()
534+
load_creds.return_value = (file_creds, None)
535+
adc.return_value = (creds, None)
536+
client = client_class(client_options=options, transport=transport_name)
537+
{% with host = (service.host|default('localhost', true)) %}
538+
create_channel.assert_called_with(
539+
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
540+
credentials=file_creds,
541+
credentials_file=None,
542+
quota_project_id=None,
543+
default_scopes=(
544+
{% for scope in service.oauth_scopes %}
545+
'{{ scope }}',
546+
{% endfor %}),
547+
scopes=None,
548+
default_host="{{ host }}",
549+
ssl_credentials=None,
550+
options=[
551+
("grpc.max_send_message_length", -1),
552+
("grpc.max_receive_message_length", -1),
553+
],
554+
)
555+
{% endwith %}
534556
{% endif %}
535557

536558

tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -418,37 +418,6 @@ def test_asset_service_client_client_options_credentials_file(client_class, tran
418418
always_use_jwt_access=True,
419419
)
420420

421-
if "grpc" in transport_name:
422-
# test that the credentials from file are saved and used as the credentials.
423-
with mock.patch.object(
424-
google.auth, "load_credentials_from_file", autospec=True
425-
) as load_creds, mock.patch.object(
426-
google.auth, "default", autospec=True
427-
) as adc, mock.patch.object(
428-
grpc_helpers, "create_channel"
429-
) as create_channel:
430-
creds = ga_credentials.AnonymousCredentials()
431-
file_creds = ga_credentials.AnonymousCredentials()
432-
load_creds.return_value = (file_creds, None)
433-
adc.return_value = (creds, None)
434-
client = client_class(client_options=options, transport=transport_name)
435-
create_channel.assert_called_with(
436-
"cloudasset.googleapis.com:443",
437-
credentials=file_creds,
438-
credentials_file=None,
439-
quota_project_id=None,
440-
default_scopes=(
441-
'https://www.googleapis.com/auth/cloud-platform',
442-
),
443-
scopes=None,
444-
default_host="cloudasset.googleapis.com",
445-
ssl_credentials=None,
446-
options=[
447-
("grpc.max_send_message_length", -1),
448-
("grpc.max_receive_message_length", -1),
449-
],
450-
)
451-
452421
def test_asset_service_client_client_options_from_dict():
453422
with mock.patch('google.cloud.asset_v1.services.asset_service.transports.AssetServiceGrpcTransport.__init__') as grpc_transport:
454423
grpc_transport.return_value = None
@@ -467,6 +436,61 @@ def test_asset_service_client_client_options_from_dict():
467436
)
468437

469438

439+
@pytest.mark.parametrize("client_class,transport_class,transport_name,grpc_helpers", [
440+
(AssetServiceClient, transports.AssetServiceGrpcTransport, "grpc", grpc_helpers),
441+
(AssetServiceAsyncClient, transports.AssetServiceGrpcAsyncIOTransport, "grpc_asyncio", grpc_helpers_async),
442+
])
443+
def test_asset_service_client_create_channel_credentials_file(client_class, transport_class, transport_name, grpc_helpers):
444+
# Check the case credentials file is provided.
445+
options = client_options.ClientOptions(
446+
credentials_file="credentials.json"
447+
)
448+
449+
with mock.patch.object(transport_class, '__init__') as patched:
450+
patched.return_value = None
451+
client = client_class(client_options=options, transport=transport_name)
452+
patched.assert_called_once_with(
453+
credentials=None,
454+
credentials_file="credentials.json",
455+
host=client.DEFAULT_ENDPOINT,
456+
scopes=None,
457+
client_cert_source_for_mtls=None,
458+
quota_project_id=None,
459+
client_info=transports.base.DEFAULT_CLIENT_INFO,
460+
always_use_jwt_access=True,
461+
)
462+
463+
# test that the credentials from file are saved and used as the credentials.
464+
with mock.patch.object(
465+
google.auth, "load_credentials_from_file", autospec=True
466+
) as load_creds, mock.patch.object(
467+
google.auth, "default", autospec=True
468+
) as adc, mock.patch.object(
469+
grpc_helpers, "create_channel"
470+
) as create_channel:
471+
creds = ga_credentials.AnonymousCredentials()
472+
file_creds = ga_credentials.AnonymousCredentials()
473+
load_creds.return_value = (file_creds, None)
474+
adc.return_value = (creds, None)
475+
client = client_class(client_options=options, transport=transport_name)
476+
create_channel.assert_called_with(
477+
"cloudasset.googleapis.com:443",
478+
credentials=file_creds,
479+
credentials_file=None,
480+
quota_project_id=None,
481+
default_scopes=(
482+
'https://www.googleapis.com/auth/cloud-platform',
483+
),
484+
scopes=None,
485+
default_host="cloudasset.googleapis.com",
486+
ssl_credentials=None,
487+
options=[
488+
("grpc.max_send_message_length", -1),
489+
("grpc.max_receive_message_length", -1),
490+
],
491+
)
492+
493+
470494
@pytest.mark.parametrize("request_type", [
471495
asset_service.ExportAssetsRequest,
472496
dict,

0 commit comments

Comments
 (0)