From 5b0711102bb45f5775addbda61cb4da5231c96d7 Mon Sep 17 00:00:00 2001 From: Astha Mohta <35952883+asthamohta@users.noreply.github.com> Date: Thu, 6 Apr 2023 15:42:50 +0530 Subject: [PATCH 1/2] feat: User provided client (#311) * chore: auto-release * feat:pass client object direcrly * read me * read me * read me * changes --- README.rst | 9 +++++++++ test/_helpers.py | 4 ++++ test/test_suite_13.py | 31 +++++++++++++++++++++++++++++-- test/test_suite_14.py | 31 +++++++++++++++++++++++++++++-- 4 files changed, 71 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 464402e2..85fecf20 100644 --- a/README.rst +++ b/README.rst @@ -76,6 +76,15 @@ on this step in a dialect prefix part: # for SQLAlchemy 1.4: spanner+spanner:///projects/project-id/instances/instance-id/databases/database-id +To pass your custom client object directly to be be used, create engine as following: + +.. code:: python + + engine = create_engine( + "spanner+spanner:///projects/project-id/instances/instance-id/databases/database-id", + connect_args={'client': spanner.Client(project="project-id")} + ) + Create a table ~~~~~~~~~~~~~~ diff --git a/test/_helpers.py b/test/_helpers.py index 8ef795f2..2aae11c7 100644 --- a/test/_helpers.py +++ b/test/_helpers.py @@ -71,6 +71,10 @@ def use_test_ot_exporter(): _TEST_OT_PROVIDER_INITIALIZED = True +def get_project(): + return PROJECT + + class OpenTelemetryBase(fixtures.TestBase): @classmethod def setup_class(cls): diff --git a/test/test_suite_13.py b/test/test_suite_13.py index 879e7964..e519dd4b 100644 --- a/test/test_suite_13.py +++ b/test/test_suite_13.py @@ -24,7 +24,7 @@ import time from unittest import mock -from google.cloud.spanner_v1 import RequestOptions +from google.cloud.spanner_v1 import RequestOptions, Client import sqlalchemy from sqlalchemy import create_engine @@ -120,7 +120,7 @@ UnicodeVarcharTest as _UnicodeVarcharTest, UnicodeTextTest as _UnicodeTextTest, ) -from test._helpers import get_db_url +from test._helpers import get_db_url, get_project config.test_schema = "" @@ -1961,3 +1961,30 @@ def test_round_trip_none_as_json_null(self): ) def test_round_trip_none_as_sql_null(self): pass + + +class CreateEngineWithClientObjectTest(fixtures.TestBase): + def test_create_engine_w_valid_client_object(self): + """ + SPANNER TEST: + + Check that we can connect to SqlAlchemy + by passing custom Client object. + """ + client = Client(project=get_project()) + engine = create_engine(get_db_url(), connect_args={"client": client}) + with engine.connect() as connection: + assert connection.connection.instance._client == client + + def test_create_engine_w_invalid_client_object(self): + """ + SPANNER TEST: + + Check that if project id in url and custom Client + Object passed to enginer mismatch, error is thrown. + """ + client = Client(project="project_id") + engine = create_engine(get_db_url(), connect_args={"client": client}) + + with pytest.raises(ValueError): + engine.connect() diff --git a/test/test_suite_14.py b/test/test_suite_14.py index 8965eebc..bd77a63f 100644 --- a/test/test_suite_14.py +++ b/test/test_suite_14.py @@ -24,7 +24,7 @@ import time from unittest import mock -from google.cloud.spanner_v1 import RequestOptions +from google.cloud.spanner_v1 import RequestOptions, Client import sqlalchemy from sqlalchemy import create_engine @@ -134,7 +134,7 @@ UnicodeTextTest as _UnicodeTextTest, _UnicodeFixture as __UnicodeFixture, ) -from test._helpers import get_db_url +from test._helpers import get_db_url, get_project config.test_schema = "" @@ -2193,3 +2193,30 @@ def test_request_priority(self): engine = create_engine("sqlite:///database") with engine.connect() as connection: pass + + +class CreateEngineWithClientObjectTest(fixtures.TestBase): + def test_create_engine_w_valid_client_object(self): + """ + SPANNER TEST: + + Check that we can connect to SqlAlchemy + by passing custom Client object. + """ + client = Client(project=get_project()) + engine = create_engine(get_db_url(), connect_args={"client": client}) + with engine.connect() as connection: + assert connection.connection.instance._client == client + + def test_create_engine_w_invalid_client_object(self): + """ + SPANNER TEST: + + Check that if project id in url and custom Client + Object passed to enginer mismatch, error is thrown. + """ + client = Client(project="project_id") + engine = create_engine(get_db_url(), connect_args={"client": client}) + + with pytest.raises(ValueError): + engine.connect() From c9ce478d3434e19ee6e64892e604b8abf24f80fa Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Mon, 10 Apr 2023 11:06:47 +0530 Subject: [PATCH 2/2] chore(main): release 1.4.0 (#313) Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com> --- CHANGELOG.md | 7 +++++++ version.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eebf3d1..11b0136c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [1.4.0](https://github.com/googleapis/python-spanner-sqlalchemy/compare/v1.3.0...v1.4.0) (2023-04-06) + + +### Features + +* User provided client ([#311](https://github.com/googleapis/python-spanner-sqlalchemy/issues/311)) ([5b07111](https://github.com/googleapis/python-spanner-sqlalchemy/commit/5b0711102bb45f5775addbda61cb4da5231c96d7)) + ## [1.3.0](https://github.com/googleapis/python-spanner-sqlalchemy/compare/v1.2.2...v1.3.0) (2023-03-20) diff --git a/version.py b/version.py index 75fcf6b3..108c5d32 100644 --- a/version.py +++ b/version.py @@ -4,4 +4,4 @@ # license that can be found in the LICENSE file or at # https://developers.google.com/open-source/licenses/bsd -__version__ = "1.3.0" +__version__ = "1.4.0"