Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit b45f1cf

Browse files
committed
update tests
1 parent d0d5fe9 commit b45f1cf

6 files changed

Lines changed: 76 additions & 51 deletions

File tree

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ except ImportError: # pragma: NO COVER
5858
import google.auth
5959
from google.auth import credentials as ga_credentials
6060

61+
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif %}
62+
{{- api.naming.versioned_module_name }}._compat import legacy_helpers
63+
6164

6265
from google.auth.exceptions import MutualTLSChannelError
6366
from google.oauth2 import service_account
@@ -148,35 +151,35 @@ def test__get_default_mtls_endpoint():
148151
assert {{ service.client_name }}._get_default_mtls_endpoint(non_googleapi) == non_googleapi
149152

150153
def test__read_environment_variables():
151-
assert {{ service.client_name }}._read_environment_variables() == (False, "auto", None)
154+
assert legacy_helpers._read_environment_variables() == (False, "auto", None)
152155

153156
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
154-
assert {{ service.client_name }}._read_environment_variables() == (True, "auto", None)
157+
assert legacy_helpers._read_environment_variables() == (True, "auto", None)
155158

156159
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
157-
assert {{ service.client_name }}._read_environment_variables() == (False, "auto", None)
160+
assert legacy_helpers._read_environment_variables() == (False, "auto", None)
158161

159162
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"}):
160163
with pytest.raises(ValueError) as excinfo:
161-
{{ service.client_name }}._read_environment_variables()
164+
legacy_helpers._read_environment_variables()
162165
assert str(excinfo.value) == "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
163166

164167
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
165-
assert {{ service.client_name }}._read_environment_variables() == (False, "never", None)
168+
assert legacy_helpers._read_environment_variables() == (False, "never", None)
166169

167170
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
168-
assert {{ service.client_name }}._read_environment_variables() == (False, "always", None)
171+
assert legacy_helpers._read_environment_variables() == (False, "always", None)
169172

170173
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}):
171-
assert {{ service.client_name }}._read_environment_variables() == (False, "auto", None)
174+
assert legacy_helpers._read_environment_variables() == (False, "auto", None)
172175

173176
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}):
174177
with pytest.raises(MutualTLSChannelError) as excinfo:
175-
{{ service.client_name }}._read_environment_variables()
178+
legacy_helpers._read_environment_variables()
176179
assert str(excinfo.value) == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
177180

178181
with mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}):
179-
assert {{ service.client_name }}._read_environment_variables() == (False, "auto", "foo.com")
182+
assert legacy_helpers._read_environment_variables() == (False, "auto", "foo.com")
180183

181184
def test__get_client_cert_source():
182185
mock_provided_cert_source = mock.Mock()

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from google.api_core import retry as retries
5656
from google.auth import credentials as ga_credentials
5757
from google.auth.exceptions import MutualTLSChannelError
58+
from google.cloud.asset_v1._compat import legacy_helpers
5859
from google.cloud.asset_v1.services.asset_service import AssetServiceAsyncClient
5960
from google.cloud.asset_v1.services.asset_service import AssetServiceClient
6061
from google.cloud.asset_v1.services.asset_service import pagers
@@ -117,35 +118,35 @@ def test__get_default_mtls_endpoint():
117118
assert AssetServiceClient._get_default_mtls_endpoint(non_googleapi) == non_googleapi
118119

119120
def test__read_environment_variables():
120-
assert AssetServiceClient._read_environment_variables() == (False, "auto", None)
121+
assert legacy_helpers._read_environment_variables() == (False, "auto", None)
121122

122123
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
123-
assert AssetServiceClient._read_environment_variables() == (True, "auto", None)
124+
assert legacy_helpers._read_environment_variables() == (True, "auto", None)
124125

125126
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
126-
assert AssetServiceClient._read_environment_variables() == (False, "auto", None)
127+
assert legacy_helpers._read_environment_variables() == (False, "auto", None)
127128

128129
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"}):
129130
with pytest.raises(ValueError) as excinfo:
130-
AssetServiceClient._read_environment_variables()
131+
legacy_helpers._read_environment_variables()
131132
assert str(excinfo.value) == "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
132133

133134
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
134-
assert AssetServiceClient._read_environment_variables() == (False, "never", None)
135+
assert legacy_helpers._read_environment_variables() == (False, "never", None)
135136

136137
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
137-
assert AssetServiceClient._read_environment_variables() == (False, "always", None)
138+
assert legacy_helpers._read_environment_variables() == (False, "always", None)
138139

139140
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}):
140-
assert AssetServiceClient._read_environment_variables() == (False, "auto", None)
141+
assert legacy_helpers._read_environment_variables() == (False, "auto", None)
141142

142143
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}):
143144
with pytest.raises(MutualTLSChannelError) as excinfo:
144-
AssetServiceClient._read_environment_variables()
145+
legacy_helpers._read_environment_variables()
145146
assert str(excinfo.value) == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
146147

147148
with mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}):
148-
assert AssetServiceClient._read_environment_variables() == (False, "auto", "foo.com")
149+
assert legacy_helpers._read_environment_variables() == (False, "auto", "foo.com")
149150

150151
def test__get_client_cert_source():
151152
mock_provided_cert_source = mock.Mock()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
import os
3+
4+
from google.auth.exceptions import MutualTLSChannelError # type: ignore
5+
6+
def _read_environment_variables():
7+
"""Returns the environment variables used by the client.
8+
9+
Returns:
10+
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
11+
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
12+
13+
Raises:
14+
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
15+
any of ["true", "false"].
16+
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
17+
is not any of ["auto", "never", "always"].
18+
"""
19+
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower()
20+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
21+
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
22+
if use_client_cert not in ("true", "false"):
23+
raise ValueError("Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`")
24+
if use_mtls_endpoint not in ("auto", "never", "always"):
25+
raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`")
26+
return use_client_cert == "true", use_mtls_endpoint, universe_domain_env

tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
ASYNC_REST_EXCEPTION = e
7070

7171

72+
from google.cloud.redis_v1._compat import legacy_helpers
73+
7274
class CloudRedisClientMeta(type):
7375
"""Metaclass for the CloudRedis client.
7476
@@ -281,29 +283,6 @@ def parse_common_location_path(path: str) -> Dict[str,str]:
281283
m = re.match(r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$", path)
282284
return m.groupdict() if m else {}
283285

284-
@staticmethod
285-
def _read_environment_variables():
286-
"""Returns the environment variables used by the client.
287-
288-
Returns:
289-
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
290-
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
291-
292-
Raises:
293-
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
294-
any of ["true", "false"].
295-
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
296-
is not any of ["auto", "never", "always"].
297-
"""
298-
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower()
299-
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
300-
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
301-
if use_client_cert not in ("true", "false"):
302-
raise ValueError("Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`")
303-
if use_mtls_endpoint not in ("auto", "never", "always"):
304-
raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`")
305-
return use_client_cert == "true", use_mtls_endpoint, universe_domain_env
306-
307286
@staticmethod
308287
def _get_client_cert_source(provided_cert_source, use_cert_flag):
309288
"""Return the client cert source to be used by the client.
@@ -494,7 +473,7 @@ def __init__(self, *,
494473

495474
universe_domain_opt = getattr(self._client_options, 'universe_domain', None)
496475

497-
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = CloudRedisClient._read_environment_variables()
476+
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = legacy_helpers._read_environment_variables()
498477
self._client_cert_source = CloudRedisClient._get_client_cert_source(self._client_options.client_cert_source, self._use_client_cert)
499478
self._universe_domain = CloudRedisClient._get_universe_domain(universe_domain_opt, self._universe_domain_env)
500479
self._api_endpoint = None # updated below, depending on `transport`

tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from google.auth import credentials as ga_credentials
6464
from google.auth.exceptions import MutualTLSChannelError
6565
from google.cloud.location import locations_pb2
66+
from google.cloud.redis_v1._compat import legacy_helpers
6667
from google.cloud.redis_v1.services.cloud_redis import CloudRedisAsyncClient
6768
from google.cloud.redis_v1.services.cloud_redis import CloudRedisClient
6869
from google.cloud.redis_v1.services.cloud_redis import pagers
@@ -125,35 +126,35 @@ def test__get_default_mtls_endpoint():
125126
assert CloudRedisClient._get_default_mtls_endpoint(non_googleapi) == non_googleapi
126127

127128
def test__read_environment_variables():
128-
assert CloudRedisClient._read_environment_variables() == (False, "auto", None)
129+
assert legacy_helpers._read_environment_variables() == (False, "auto", None)
129130

130131
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
131-
assert CloudRedisClient._read_environment_variables() == (True, "auto", None)
132+
assert legacy_helpers._read_environment_variables() == (True, "auto", None)
132133

133134
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
134-
assert CloudRedisClient._read_environment_variables() == (False, "auto", None)
135+
assert legacy_helpers._read_environment_variables() == (False, "auto", None)
135136

136137
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"}):
137138
with pytest.raises(ValueError) as excinfo:
138-
CloudRedisClient._read_environment_variables()
139+
legacy_helpers._read_environment_variables()
139140
assert str(excinfo.value) == "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
140141

141142
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
142-
assert CloudRedisClient._read_environment_variables() == (False, "never", None)
143+
assert legacy_helpers._read_environment_variables() == (False, "never", None)
143144

144145
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
145-
assert CloudRedisClient._read_environment_variables() == (False, "always", None)
146+
assert legacy_helpers._read_environment_variables() == (False, "always", None)
146147

147148
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}):
148-
assert CloudRedisClient._read_environment_variables() == (False, "auto", None)
149+
assert legacy_helpers._read_environment_variables() == (False, "auto", None)
149150

150151
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}):
151152
with pytest.raises(MutualTLSChannelError) as excinfo:
152-
CloudRedisClient._read_environment_variables()
153+
legacy_helpers._read_environment_variables()
153154
assert str(excinfo.value) == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
154155

155156
with mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}):
156-
assert CloudRedisClient._read_environment_variables() == (False, "auto", "foo.com")
157+
assert legacy_helpers._read_environment_variables() == (False, "auto", "foo.com")
157158

158159
def test__get_client_cert_source():
159160
mock_provided_cert_source = mock.Mock()

0 commit comments

Comments
 (0)