[go: up one dir, main page]

Skip to content

Commit

Permalink
fix: supplementary fix to env-based universe resolution (#1844)
Browse files Browse the repository at this point in the history
* 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 <lingqing.gan@gmail.com>
  • Loading branch information
shollyman and Linchin committed Mar 7, 2024
1 parent 86a45c9 commit b818992
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 7 additions & 4 deletions google/cloud/bigquery/_helpers.py
Expand Up @@ -81,10 +81,13 @@ def _get_client_universe(
if isinstance(client_options, dict):
client_options = client_options_lib.from_dict(client_options)
universe = _DEFAULT_UNIVERSE
if hasattr(client_options, "universe_domain"):
options_universe = getattr(client_options, "universe_domain")
if options_universe is not None and len(options_universe) > 0:
universe = options_universe
options_universe = getattr(client_options, "universe_domain", None)
if (
options_universe
and isinstance(options_universe, str)
and len(options_universe) > 0
):
universe = options_universe
else:
env_universe = os.getenv(_UNIVERSE_DOMAIN_ENV)
if isinstance(env_universe, str) and len(env_universe) > 0:
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test__helpers.py
Expand Up @@ -60,6 +60,21 @@ def test_with_environ(self):

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

@mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"})
def test_with_environ_and_dict(self):
from google.cloud.bigquery._helpers import _get_client_universe

options = ({"credentials_file": "file.json"},)
self.assertEqual("foo.com", _get_client_universe(options))

@mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"})
def test_with_environ_and_empty_options(self):
from google.cloud.bigquery._helpers import _get_client_universe
from google.api_core import client_options

options = client_options.from_dict({})
self.assertEqual("foo.com", _get_client_universe(options))

@mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": ""})
def test_with_environ_empty(self):
from google.cloud.bigquery._helpers import _get_client_universe
Expand Down

0 comments on commit b818992

Please sign in to comment.