Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db_api): allow file path for credentials #221

Merged
merged 5 commits into from Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions google/cloud/spanner_dbapi/connection.py
Expand Up @@ -339,6 +339,7 @@ def connect(
credentials=None,
pool=None,
user_agent=None,
credentials_uri=None,
):
"""Creates a connection to a Google Cloud Spanner database.

Expand Down Expand Up @@ -368,6 +369,11 @@ def connect(
:param user_agent: (Optional) User agent to be used with this connection's
requests.

:type credentials_uri: str
:param credentials_uri: (Optional) An optional string specifying where to retrieve
the service account JSON for the credentials to connect to
Cloud Spanner.

:rtype: :class:`google.cloud.spanner_dbapi.connection.Connection`
:returns: Connection object associated with the given Google Cloud Spanner
resource.
Expand All @@ -380,9 +386,14 @@ def connect(
user_agent=user_agent or DEFAULT_USER_AGENT, python_version=PY_VERSION
)

client = spanner.Client(
project=project, credentials=credentials, client_info=client_info
)
if credentials_uri:
client = spanner.Client.from_service_account_json(
credentials_uri, project=project, client_info=client_info
)
else:
client = spanner.Client(
project=project, credentials=credentials, client_info=client_info
)

instance = client.instance(instance_id)
if not instance.exists():
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/spanner_dbapi/test_connect.py
Expand Up @@ -139,3 +139,29 @@ def test_sessions_pool(self):
):
connect("test-instance", database_id, pool=pool)
database_mock.assert_called_once_with(database_id, pool=pool)

def test_connect_w_coonection_uri(self):
from google.cloud.spanner_dbapi import connect
from google.cloud.spanner_dbapi import Connection

PROJECT = "test-project"
USER_AGENT = "user-agent"
credentials_uri = "dummy/file/path.json"

with mock.patch(
"google.cloud.spanner_v1.Client.from_service_account_json"
) as client_mock:
connection = connect(
"test-instance",
"test-database",
PROJECT,
None,
user_agent=USER_AGENT,
credentials_uri=credentials_uri,
)

self.assertIsInstance(connection, Connection)

client_mock.assert_called_once_with(
credentials_uri, project=PROJECT, client_info=mock.ANY
)