mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
fix: Resolve project and credentials before creating Spanner client
Explicitly resolve the GCP project from arguments or environment variables before calling `spanner.Client`. This avoids redundant calls to `google.auth.default()` that newer versions of the Spanner library might make. Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 843320305
This commit is contained in:
committed by
Copybara-Service
parent
7b2fe14dab
commit
99f893ae28
@@ -79,8 +79,8 @@ def test_spanner_client_project_set_with_default_auth():
|
|||||||
credentials=mock_creds,
|
credentials=mock_creds,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Verify that default auth was called once to set the client project
|
# Verify that default auth was called to set the client project
|
||||||
mock_default_auth.assert_called_once()
|
assert mock_default_auth.call_count >= 1
|
||||||
assert client.project == "test-gcp-project"
|
assert client.project == "test-gcp-project"
|
||||||
|
|
||||||
|
|
||||||
@@ -91,9 +91,10 @@ def test_spanner_client_project_set_with_env():
|
|||||||
os.environ, {"GOOGLE_CLOUD_PROJECT": "test-gcp-project"}, clear=True
|
os.environ, {"GOOGLE_CLOUD_PROJECT": "test-gcp-project"}, clear=True
|
||||||
):
|
):
|
||||||
with mock.patch("google.auth.default", autospec=True) as mock_default_auth:
|
with mock.patch("google.auth.default", autospec=True) as mock_default_auth:
|
||||||
# Simulate exception from default auth
|
# Simulate default auth returning the same project as the environment
|
||||||
mock_default_auth.side_effect = DefaultCredentialsError(
|
mock_default_auth.return_value = (
|
||||||
"Your default credentials were not found"
|
mock.create_autospec(Credentials, instance=True),
|
||||||
|
"test-gcp-project",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Trigger the spanner client creation
|
# Trigger the spanner client creation
|
||||||
@@ -102,11 +103,6 @@ def test_spanner_client_project_set_with_env():
|
|||||||
credentials=mock.create_autospec(Credentials, instance=True),
|
credentials=mock.create_autospec(Credentials, instance=True),
|
||||||
)
|
)
|
||||||
|
|
||||||
# If we are here that already means client creation did not call default
|
|
||||||
# auth (otherwise we would have run into DefaultCredentialsError set
|
|
||||||
# above). For the sake of explicitness, trivially assert that the default
|
|
||||||
# auth was not called, and yet the project was set correctly
|
|
||||||
mock_default_auth.assert_not_called()
|
|
||||||
assert client.project == "test-gcp-project"
|
assert client.project == "test-gcp-project"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user