Skip to content

Commit b818992

Browse files
shollymanLinchin
andauthored
fix: supplementary fix to env-based universe resolution (#1844)
* fix: supplementary fix to env-based universe resolution There's a corner case where conversion from dict to a ClientOptions will return a universe_domain value as None that wasn't covered by initial testing. This updates the resolution code and adds tests to exercise the new path. * formatting --------- Co-authored-by: Lingqing Gan <[email protected]>
1 parent 86a45c9 commit b818992

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

google/cloud/bigquery/_helpers.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,13 @@ def _get_client_universe(
8181
if isinstance(client_options, dict):
8282
client_options = client_options_lib.from_dict(client_options)
8383
universe = _DEFAULT_UNIVERSE
84-
if hasattr(client_options, "universe_domain"):
85-
options_universe = getattr(client_options, "universe_domain")
86-
if options_universe is not None and len(options_universe) > 0:
87-
universe = options_universe
84+
options_universe = getattr(client_options, "universe_domain", None)
85+
if (
86+
options_universe
87+
and isinstance(options_universe, str)
88+
and len(options_universe) > 0
89+
):
90+
universe = options_universe
8891
else:
8992
env_universe = os.getenv(_UNIVERSE_DOMAIN_ENV)
9093
if isinstance(env_universe, str) and len(env_universe) > 0:

tests/unit/test__helpers.py

+15
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ def test_with_environ(self):
6060

6161
self.assertEqual("foo.com", _get_client_universe(None))
6262

63+
@mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"})
64+
def test_with_environ_and_dict(self):
65+
from google.cloud.bigquery._helpers import _get_client_universe
66+
67+
options = ({"credentials_file": "file.json"},)
68+
self.assertEqual("foo.com", _get_client_universe(options))
69+
70+
@mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"})
71+
def test_with_environ_and_empty_options(self):
72+
from google.cloud.bigquery._helpers import _get_client_universe
73+
from google.api_core import client_options
74+
75+
options = client_options.from_dict({})
76+
self.assertEqual("foo.com", _get_client_universe(options))
77+
6378
@mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": ""})
6479
def test_with_environ_empty(self):
6580
from google.cloud.bigquery._helpers import _get_client_universe

0 commit comments

Comments
 (0)