diff --git a/google/cloud/bigquery/_helpers.py b/google/cloud/bigquery/_helpers.py index ec4ac9970..7198b60c2 100644 --- a/google/cloud/bigquery/_helpers.py +++ b/google/cloud/bigquery/_helpers.py @@ -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: diff --git a/tests/unit/test__helpers.py b/tests/unit/test__helpers.py index 019d2e7bd..7e8d815d2 100644 --- a/tests/unit/test__helpers.py +++ b/tests/unit/test__helpers.py @@ -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