Skip to content

Commit

Permalink
chore: restore 100% coverage for generated clients (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea authored Jan 31, 2022
1 parent 4f4ad39 commit 16469c4
Show file tree
Hide file tree
Showing 8 changed files with 456 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,42 +395,6 @@ def test_{{ service.client_name|snake_case }}_client_options_credentials_file(cl
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
)

{% if 'grpc' in opts.transport %}
if "grpc" in transport_name:
# test that the credentials from file are saved and used as the credentials.
with mock.patch.object(
google.auth, "load_credentials_from_file", autospec=True
) as load_creds, mock.patch.object(
google.auth, "default", autospec=True
) as adc, mock.patch.object(
grpc_helpers, "create_channel"
) as create_channel:
creds = ga_credentials.AnonymousCredentials()
file_creds = ga_credentials.AnonymousCredentials()
load_creds.return_value = (file_creds, None)
adc.return_value = (creds, None)
client = client_class(client_options=options, transport=transport_name)
{% with host = (service.host|default('localhost', true)) %}
create_channel.assert_called_with(
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
credentials=file_creds,
credentials_file=None,
quota_project_id=None,
default_scopes=(
{% for scope in service.oauth_scopes %}
'{{ scope }}',
{% endfor %}),
scopes=None,
default_host="{{ host }}",
ssl_credentials=None,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)
{% endwith %}
{% endif %}
{% if 'grpc' in opts.transport %}
{# TODO(dovs): genericize this function#}

Expand All @@ -450,6 +414,63 @@ def test_{{ service.client_name|snake_case }}_client_options_from_dict():
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
)


@pytest.mark.parametrize("client_class,transport_class,transport_name", [
({{ service.client_name }}, transports.{{ service.grpc_transport_name }}, "grpc"),
])
def test_{{ service.client_name|snake_case }}_create_channel_credentials_file(client_class, transport_class, transport_name):
# Check the case credentials file is provided.
options = client_options.ClientOptions(
credentials_file="credentials.json"
)

with mock.patch.object(transport_class, '__init__') as patched:
patched.return_value = None
client = client_class(client_options=options, transport=transport_name)
patched.assert_called_once_with(
credentials=None,
credentials_file="credentials.json",
host=client.DEFAULT_ENDPOINT,
scopes=None,
client_cert_source_for_mtls=None,
quota_project_id=None,
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
)

# test that the credentials from file are saved and used as the credentials.
with mock.patch.object(
google.auth, "load_credentials_from_file", autospec=True
) as load_creds, mock.patch.object(
google.auth, "default", autospec=True
) as adc, mock.patch.object(
grpc_helpers, "create_channel"
) as create_channel:
creds = ga_credentials.AnonymousCredentials()
file_creds = ga_credentials.AnonymousCredentials()
load_creds.return_value = (file_creds, None)
adc.return_value = (creds, None)
client = client_class(client_options=options, transport=transport_name)
{% with host = (service.host|default('localhost', true)) %}
create_channel.assert_called_with(
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
credentials=file_creds,
credentials_file=None,
quota_project_id=None,
default_scopes=(
{% for scope in service.oauth_scopes %}
'{{ scope }}',
{% endfor %}),
scopes=None,
default_host="{{ host }}",
ssl_credentials=None,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)
{% endwith %}
{% endif %}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,42 +476,6 @@ def test_{{ service.client_name|snake_case }}_client_options_credentials_file(cl
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
)

{% if 'grpc' in opts.transport %}
if "grpc" in transport_name:
# test that the credentials from file are saved and used as the credentials.
with mock.patch.object(
google.auth, "load_credentials_from_file", autospec=True
) as load_creds, mock.patch.object(
google.auth, "default", autospec=True
) as adc, mock.patch.object(
grpc_helpers, "create_channel"
) as create_channel:
creds = ga_credentials.AnonymousCredentials()
file_creds = ga_credentials.AnonymousCredentials()
load_creds.return_value = (file_creds, None)
adc.return_value = (creds, None)
client = client_class(client_options=options, transport=transport_name)
{% with host = (service.host|default('localhost', true)) %}
create_channel.assert_called_with(
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
credentials=file_creds,
credentials_file=None,
quota_project_id=None,
default_scopes=(
{% for scope in service.oauth_scopes %}
'{{ scope }}',
{% endfor %}),
scopes=None,
default_host="{{ host }}",
ssl_credentials=None,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)
{% endwith %}
{% endif %}
{% if 'grpc' in opts.transport %}
{# TODO(dovs): genericize this function#}

Expand All @@ -531,6 +495,64 @@ def test_{{ service.client_name|snake_case }}_client_options_from_dict():
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
)


@pytest.mark.parametrize("client_class,transport_class,transport_name,grpc_helpers", [
({{ service.client_name }}, transports.{{ service.grpc_transport_name }}, "grpc", grpc_helpers),
({{ service.async_client_name }}, transports.{{ service.grpc_asyncio_transport_name }}, "grpc_asyncio", grpc_helpers_async),
])
def test_{{ service.client_name|snake_case }}_create_channel_credentials_file(client_class, transport_class, transport_name, grpc_helpers):
# Check the case credentials file is provided.
options = client_options.ClientOptions(
credentials_file="credentials.json"
)

with mock.patch.object(transport_class, '__init__') as patched:
patched.return_value = None
client = client_class(client_options=options, transport=transport_name)
patched.assert_called_once_with(
credentials=None,
credentials_file="credentials.json",
host=client.DEFAULT_ENDPOINT,
scopes=None,
client_cert_source_for_mtls=None,
quota_project_id=None,
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
)

# test that the credentials from file are saved and used as the credentials.
with mock.patch.object(
google.auth, "load_credentials_from_file", autospec=True
) as load_creds, mock.patch.object(
google.auth, "default", autospec=True
) as adc, mock.patch.object(
grpc_helpers, "create_channel"
) as create_channel:
creds = ga_credentials.AnonymousCredentials()
file_creds = ga_credentials.AnonymousCredentials()
load_creds.return_value = (file_creds, None)
adc.return_value = (creds, None)
client = client_class(client_options=options, transport=transport_name)
{% with host = (service.host|default('localhost', true)) %}
create_channel.assert_called_with(
"{{ host }}{% if ":" not in service.host %}:443{% endif %}",
credentials=file_creds,
credentials_file=None,
quota_project_id=None,
default_scopes=(
{% for scope in service.oauth_scopes %}
'{{ scope }}',
{% endfor %}),
scopes=None,
default_host="{{ host }}",
ssl_credentials=None,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)
{% endwith %}
{% endif %}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,37 +418,6 @@ def test_asset_service_client_client_options_credentials_file(client_class, tran
always_use_jwt_access=True,
)

if "grpc" in transport_name:
# test that the credentials from file are saved and used as the credentials.
with mock.patch.object(
google.auth, "load_credentials_from_file", autospec=True
) as load_creds, mock.patch.object(
google.auth, "default", autospec=True
) as adc, mock.patch.object(
grpc_helpers, "create_channel"
) as create_channel:
creds = ga_credentials.AnonymousCredentials()
file_creds = ga_credentials.AnonymousCredentials()
load_creds.return_value = (file_creds, None)
adc.return_value = (creds, None)
client = client_class(client_options=options, transport=transport_name)
create_channel.assert_called_with(
"cloudasset.googleapis.com:443",
credentials=file_creds,
credentials_file=None,
quota_project_id=None,
default_scopes=(
'https://www.googleapis.com/auth/cloud-platform',
),
scopes=None,
default_host="cloudasset.googleapis.com",
ssl_credentials=None,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)

def test_asset_service_client_client_options_from_dict():
with mock.patch('google.cloud.asset_v1.services.asset_service.transports.AssetServiceGrpcTransport.__init__') as grpc_transport:
grpc_transport.return_value = None
Expand All @@ -467,6 +436,61 @@ def test_asset_service_client_client_options_from_dict():
)


@pytest.mark.parametrize("client_class,transport_class,transport_name,grpc_helpers", [
(AssetServiceClient, transports.AssetServiceGrpcTransport, "grpc", grpc_helpers),
(AssetServiceAsyncClient, transports.AssetServiceGrpcAsyncIOTransport, "grpc_asyncio", grpc_helpers_async),
])
def test_asset_service_client_create_channel_credentials_file(client_class, transport_class, transport_name, grpc_helpers):
# Check the case credentials file is provided.
options = client_options.ClientOptions(
credentials_file="credentials.json"
)

with mock.patch.object(transport_class, '__init__') as patched:
patched.return_value = None
client = client_class(client_options=options, transport=transport_name)
patched.assert_called_once_with(
credentials=None,
credentials_file="credentials.json",
host=client.DEFAULT_ENDPOINT,
scopes=None,
client_cert_source_for_mtls=None,
quota_project_id=None,
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
)

# test that the credentials from file are saved and used as the credentials.
with mock.patch.object(
google.auth, "load_credentials_from_file", autospec=True
) as load_creds, mock.patch.object(
google.auth, "default", autospec=True
) as adc, mock.patch.object(
grpc_helpers, "create_channel"
) as create_channel:
creds = ga_credentials.AnonymousCredentials()
file_creds = ga_credentials.AnonymousCredentials()
load_creds.return_value = (file_creds, None)
adc.return_value = (creds, None)
client = client_class(client_options=options, transport=transport_name)
create_channel.assert_called_with(
"cloudasset.googleapis.com:443",
credentials=file_creds,
credentials_file=None,
quota_project_id=None,
default_scopes=(
'https://www.googleapis.com/auth/cloud-platform',
),
scopes=None,
default_host="cloudasset.googleapis.com",
ssl_credentials=None,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)


@pytest.mark.parametrize("request_type", [
asset_service.ExportAssetsRequest,
dict,
Expand Down
Loading

0 comments on commit 16469c4

Please sign in to comment.