From a91ca968e1367ef61bbdc0f19673b52b58584f54 Mon Sep 17 00:00:00 2001 From: Sijun Liu Date: Wed, 8 Jan 2025 23:57:03 +0000 Subject: [PATCH] update --- .../%sub/services/%service/client.py.j2 | 2 +- .../%name_%version/%sub/test_%service.py.j2 | 72 +------------------ 2 files changed, 4 insertions(+), 70 deletions(-) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index 446c82acc..bb15778d1 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -441,7 +441,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): if not hasattr(cred, "get_cred_info"): return - cred_info = cred.get_cred_info() # type: ignore + cred_info = cred.get_cred_info() if cred_info and hasattr(error._details, "append"): error._details.append(json.dumps(cred_info)) diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 9507357fb..d0b8cf51b 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -116,7 +116,6 @@ async def mock_async_gen(data, chunk_size=1): chunk = data[i : i + chunk_size] yield chunk.encode("utf-8") - def client_cert_source_callback(): return b"cert bytes", b"key bytes" @@ -294,14 +293,14 @@ def test__add_cred_info_for_auth_errors(error_code, cred_info_json, show_cred_in client = {{ service.client_name }}(credentials=cred) client._transport._credentials = cred - error = core_exceptions.GoogleAPICallError("message", details=[]) + error = core_exceptions.GoogleAPICallError("message", details=["foo"]) error.code = error_code client._add_cred_info_for_auth_errors(error) if show_cred_info: - assert error.details == [CRED_INFO_STRING] + assert error.details == ["foo", CRED_INFO_STRING] else: - assert error.details == [] + assert error.details == ["foo"] @pytest.mark.parametrize("error_code", [401,403,404,500]) def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): @@ -316,71 +315,6 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): client._add_cred_info_for_auth_errors(error) assert error.details == [] -@pytest.mark.parametrize("client_class,transport_class,transport_name", [ - {% if 'grpc' in opts.transport %} - ({{ service.client_name }}, transports.{{ service.grpc_transport_name }}, "grpc"), - {% endif %} - {% if 'rest' in opts.transport %} - ({{ service.client_name }}, transports.{{ service.rest_transport_name }}, "rest"), - {% endif %} -]) -def test__validate_universe_domain(client_class, transport_class, transport_name): - client = client_class( - transport=transport_class( - credentials=ga_credentials.AnonymousCredentials() - ) - ) - assert client._validate_universe_domain() == True - - # Test the case when universe is already validated. - assert client._validate_universe_domain() == True - - if transport_name == "grpc": - # Test the case where credentials are provided by the - # `local_channel_credentials`. The default universes in both match. - channel = grpc.secure_channel('http://localhost/', grpc.local_channel_credentials()) - client = client_class(transport=transport_class(channel=channel)) - assert client._validate_universe_domain() == True - - # Test the case where credentials do not exist: e.g. a transport is provided - # with no credentials. Validation should still succeed because there is no - # mismatch with non-existent credentials. - channel = grpc.secure_channel('http://localhost/', grpc.local_channel_credentials()) - transport=transport_class(channel=channel) - transport._credentials = None - client = client_class(transport=transport) - assert client._validate_universe_domain() == True - - # TODO: This is needed to cater for older versions of google-auth - # Make this test unconditional once the minimum supported version of - # google-auth becomes 2.23.0 or higher. - google_auth_major, google_auth_minor = [int(part) for part in google.auth.__version__.split(".")[0:2]] - if google_auth_major > 2 or (google_auth_major == 2 and google_auth_minor >= 23): - credentials = ga_credentials.AnonymousCredentials() - credentials._universe_domain = "foo.com" - # Test the case when there is a universe mismatch from the credentials. - client = client_class( - transport=transport_class(credentials=credentials) - ) - with pytest.raises(ValueError) as excinfo: - client._validate_universe_domain() - assert str(excinfo.value) == "The configured universe domain (googleapis.com) does not match the universe domain found in the credentials (foo.com). If you haven't configured the universe domain explicitly, `googleapis.com` is the default." - - # Test the case when there is a universe mismatch from the client. - # - # TODO: Make this test unconditional once the minimum supported version of - # google-api-core becomes 2.15.0 or higher. - api_core_major, api_core_minor = [int(part) for part in api_core_version.__version__.split(".")[0:2]] - if api_core_major > 2 or (api_core_major == 2 and api_core_minor >= 15): - client = client_class(client_options={"universe_domain": "bar.com"}, transport=transport_class(credentials=ga_credentials.AnonymousCredentials(),)) - with pytest.raises(ValueError) as excinfo: - client._validate_universe_domain() - assert str(excinfo.value) == "The configured universe domain (bar.com) does not match the universe domain found in the credentials (googleapis.com). If you haven't configured the universe domain explicitly, `googleapis.com` is the default." - - # Test that ValueError is raised if universe_domain is provided via client options and credentials is None - with pytest.raises(ValueError): - client._compare_universes("foo.bar", None) - @pytest.mark.parametrize("client_class,transport_name", [ {% if 'grpc' in opts.transport %} ({{ service.client_name }}, "grpc"),