Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pkg_resources
import re

from sqlalchemy import types, ForeignKeyConstraint
Expand Down Expand Up @@ -433,9 +434,10 @@ def create_connect_args(self, url):
),
url.database,
)
dist = pkg_resources.get_distribution("sqlalchemy-spanner")
return (
[match.group("instance"), match.group("database"), match.group("project")],
{},
{"user_agent": dist.project_name + "/" + dist.version},
)

@engine_to_connection
Expand Down
21 changes: 21 additions & 0 deletions test/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import decimal
import operator
import os
import pkg_resources
import pytest
from unittest import mock

Expand Down Expand Up @@ -1525,3 +1526,23 @@ def test_interleave_on_delete_cascade(self):
with mock.patch("google.cloud.spanner_dbapi.cursor.Cursor.execute") as execute:
client.create(self._engine)
execute.assert_called_once_with(EXP_QUERY, [])


class UserAgentTest(fixtures.TestBase):
"""Check that SQLAlchemy dialect uses correct user agent."""

def setUp(self):
self._engine = create_engine(
"spanner:///projects/appdev-soda-spanner-staging/instances/"
"sqlalchemy-dialect-test/databases/compliance-test"
)
self._metadata = MetaData(bind=self._engine)

def test_user_agent(self):
dist = pkg_resources.get_distribution("sqlalchemy-spanner")

with self._engine.connect() as connection:
assert (
connection.connection.instance._client._client_info.user_agent
== dist.project_name + "/" + dist.version
)