Skip to content
This repository was archived by the owner on Mar 13, 2026. It is now read-only.

Commit 5b07111

Browse files
authored
feat: User provided client (#311)
* chore: auto-release * feat:pass client object direcrly * read me * read me * read me * changes
1 parent 942e699 commit 5b07111

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

README.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ on this step in a dialect prefix part:
7676
# for SQLAlchemy 1.4:
7777
spanner+spanner:///projects/project-id/instances/instance-id/databases/database-id
7878
79+
To pass your custom client object directly to be be used, create engine as following:
80+
81+
.. code:: python
82+
83+
engine = create_engine(
84+
"spanner+spanner:///projects/project-id/instances/instance-id/databases/database-id",
85+
connect_args={'client': spanner.Client(project="project-id")}
86+
)
87+
7988
Create a table
8089
~~~~~~~~~~~~~~
8190

test/_helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def use_test_ot_exporter():
7171
_TEST_OT_PROVIDER_INITIALIZED = True
7272

7373

74+
def get_project():
75+
return PROJECT
76+
77+
7478
class OpenTelemetryBase(fixtures.TestBase):
7579
@classmethod
7680
def setup_class(cls):

test/test_suite_13.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import time
2525
from unittest import mock
2626

27-
from google.cloud.spanner_v1 import RequestOptions
27+
from google.cloud.spanner_v1 import RequestOptions, Client
2828

2929
import sqlalchemy
3030
from sqlalchemy import create_engine
@@ -120,7 +120,7 @@
120120
UnicodeVarcharTest as _UnicodeVarcharTest,
121121
UnicodeTextTest as _UnicodeTextTest,
122122
)
123-
from test._helpers import get_db_url
123+
from test._helpers import get_db_url, get_project
124124

125125
config.test_schema = ""
126126

@@ -1961,3 +1961,30 @@ def test_round_trip_none_as_json_null(self):
19611961
)
19621962
def test_round_trip_none_as_sql_null(self):
19631963
pass
1964+
1965+
1966+
class CreateEngineWithClientObjectTest(fixtures.TestBase):
1967+
def test_create_engine_w_valid_client_object(self):
1968+
"""
1969+
SPANNER TEST:
1970+
1971+
Check that we can connect to SqlAlchemy
1972+
by passing custom Client object.
1973+
"""
1974+
client = Client(project=get_project())
1975+
engine = create_engine(get_db_url(), connect_args={"client": client})
1976+
with engine.connect() as connection:
1977+
assert connection.connection.instance._client == client
1978+
1979+
def test_create_engine_w_invalid_client_object(self):
1980+
"""
1981+
SPANNER TEST:
1982+
1983+
Check that if project id in url and custom Client
1984+
Object passed to enginer mismatch, error is thrown.
1985+
"""
1986+
client = Client(project="project_id")
1987+
engine = create_engine(get_db_url(), connect_args={"client": client})
1988+
1989+
with pytest.raises(ValueError):
1990+
engine.connect()

test/test_suite_14.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import time
2525
from unittest import mock
2626

27-
from google.cloud.spanner_v1 import RequestOptions
27+
from google.cloud.spanner_v1 import RequestOptions, Client
2828

2929
import sqlalchemy
3030
from sqlalchemy import create_engine
@@ -134,7 +134,7 @@
134134
UnicodeTextTest as _UnicodeTextTest,
135135
_UnicodeFixture as __UnicodeFixture,
136136
)
137-
from test._helpers import get_db_url
137+
from test._helpers import get_db_url, get_project
138138

139139
config.test_schema = ""
140140

@@ -2193,3 +2193,30 @@ def test_request_priority(self):
21932193
engine = create_engine("sqlite:///database")
21942194
with engine.connect() as connection:
21952195
pass
2196+
2197+
2198+
class CreateEngineWithClientObjectTest(fixtures.TestBase):
2199+
def test_create_engine_w_valid_client_object(self):
2200+
"""
2201+
SPANNER TEST:
2202+
2203+
Check that we can connect to SqlAlchemy
2204+
by passing custom Client object.
2205+
"""
2206+
client = Client(project=get_project())
2207+
engine = create_engine(get_db_url(), connect_args={"client": client})
2208+
with engine.connect() as connection:
2209+
assert connection.connection.instance._client == client
2210+
2211+
def test_create_engine_w_invalid_client_object(self):
2212+
"""
2213+
SPANNER TEST:
2214+
2215+
Check that if project id in url and custom Client
2216+
Object passed to enginer mismatch, error is thrown.
2217+
"""
2218+
client = Client(project="project_id")
2219+
engine = create_engine(get_db_url(), connect_args={"client": client})
2220+
2221+
with pytest.raises(ValueError):
2222+
engine.connect()

0 commit comments

Comments
 (0)