From b5f977ebf61527914af3c8356aeeae9418114215 Mon Sep 17 00:00:00 2001 From: Ilya Gurov Date: Mon, 13 Sep 2021 13:39:13 +0300 Subject: [PATCH] feat: set a separate user agent for the DB API (#566) * feat: set a separate user agent for the DB API * fix test error * fix the test --- google/cloud/spanner_dbapi/version.py | 5 +++-- tests/system/test_dbapi.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/google/cloud/spanner_dbapi/version.py b/google/cloud/spanner_dbapi/version.py index b0e48cff0b..63bd687feb 100644 --- a/google/cloud/spanner_dbapi/version.py +++ b/google/cloud/spanner_dbapi/version.py @@ -12,8 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import pkg_resources import platform PY_VERSION = platform.python_version() -VERSION = "2.2.0a1" -DEFAULT_USER_AGENT = "django_spanner/" + VERSION +VERSION = pkg_resources.get_distribution("google-cloud-spanner").version +DEFAULT_USER_AGENT = "dbapi/" + VERSION diff --git a/tests/system/test_dbapi.py b/tests/system/test_dbapi.py index 210a4f5e90..c47aeebd82 100644 --- a/tests/system/test_dbapi.py +++ b/tests/system/test_dbapi.py @@ -14,11 +14,12 @@ import hashlib import pickle +import pkg_resources import pytest from google.cloud import spanner_v1 -from google.cloud.spanner_dbapi.connection import Connection +from google.cloud.spanner_dbapi.connection import connect, Connection from . import _helpers DATABASE_NAME = "dbapi-txn" @@ -357,3 +358,12 @@ def test_ping(shared_instance, dbapi_database): conn = Connection(shared_instance, dbapi_database) conn.validate() conn.close() + + +def test_user_agent(shared_instance, dbapi_database): + """Check that DB API uses an appropriate user agent.""" + conn = connect(shared_instance.name, dbapi_database.name) + assert ( + conn.instance._client._client_info.user_agent + == "dbapi/" + pkg_resources.get_distribution("google-cloud-spanner").version + )