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

Commit 869c252

Browse files
committed
feat: Propagate client_context in Session and update tests
- Update Session.transaction to accept client_context. - Update unit tests to support client_context propagation. - Update mock objects in tests to match the expected attribute hierarchy. - Clean up nested imports in test files.
1 parent 55b213b commit 869c252

File tree

13 files changed

+280
-153
lines changed

13 files changed

+280
-153
lines changed

google/cloud/spanner_v1/database.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,8 +1100,8 @@ def run_in_transaction(self, func, *args, **kw):
11001100
the DDL option `allow_txn_exclusion` being false or unset.
11011101
"isolation_level" sets the isolation level for the transaction.
11021102
"read_lock_mode" sets the read lock mode for the transaction.
1103-
"client_context" (Optional) Client context to use for all requests made
1104-
by this transaction.
1103+
"client_context" (Optional) Client context to use for all requests
1104+
made by this transaction.
11051105
11061106
:rtype: Any
11071107
:returns: The return value of ``func``.
@@ -1526,7 +1526,9 @@ def __enter__(self):
15261526
transaction_type = TransactionType.READ_WRITE
15271527
self._session = self._database.sessions_manager.get_session(transaction_type)
15281528

1529-
return MutationGroups(session=self._session, client_context=self._client_context)
1529+
return MutationGroups(
1530+
session=self._session, client_context=self._client_context
1531+
)
15301532

15311533
def __exit__(self, exc_type, exc_val, exc_tb):
15321534
"""End ``with`` block."""

google/cloud/spanner_v1/session.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,14 @@ def batch(self):
472472

473473
return Batch(self)
474474

475-
def transaction(self) -> Transaction:
475+
def transaction(self, client_context=None) -> Transaction:
476476
"""Create a transaction to perform a set of reads with shared staleness.
477477
478+
:type client_context: :class:`~google.cloud.spanner_v1.types.ClientContext`
479+
or :class:`dict`
480+
:param client_context: (Optional) Client context to use for all requests made
481+
by this transaction.
482+
478483
:rtype: :class:`~google.cloud.spanner_v1.transaction.Transaction`
479484
:returns: a transaction bound to this session
480485
@@ -483,7 +488,7 @@ def transaction(self) -> Transaction:
483488
if self._session_id is None:
484489
raise ValueError("Session has not been created.")
485490

486-
return Transaction(self)
491+
return Transaction(self, client_context=client_context)
487492

488493
def run_in_transaction(self, func, *args, **kw):
489494
"""Perform a unit of work in a transaction, retrying on abort.
@@ -512,6 +517,8 @@ def run_in_transaction(self, func, *args, **kw):
512517
the DDL option `allow_txn_exclusion` being false or unset.
513518
"isolation_level" sets the isolation level for the transaction.
514519
"read_lock_mode" sets the read lock mode for the transaction.
520+
"client_context" (Optional) Client context to use for all requests
521+
made by this transaction.
515522
516523
:rtype: Any
517524
:returns: The return value of ``func``.
@@ -529,6 +536,7 @@ def run_in_transaction(self, func, *args, **kw):
529536
)
530537
isolation_level = kw.pop("isolation_level", None)
531538
read_lock_mode = kw.pop("read_lock_mode", None)
539+
client_context = kw.pop("client_context", None)
532540

533541
database = self._database
534542
log_commit_stats = database.log_commit_stats
@@ -554,7 +562,7 @@ def run_in_transaction(self, func, *args, **kw):
554562
previous_transaction_id: Optional[bytes] = None
555563

556564
while True:
557-
txn = self.transaction()
565+
txn = self.transaction(client_context=client_context)
558566
txn.transaction_tag = transaction_tag
559567
txn.exclude_txn_from_change_streams = exclude_txn_from_change_streams
560568
txn.isolation_level = isolation_level

tests/unit/spanner_dbapi/test_connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,7 @@ class _Client(object):
872872
def __init__(self, project="project_id"):
873873
self.project = project
874874
self.project_name = "projects/" + self.project
875+
self._client_context = None
875876

876877
def instance(self, instance_id="instance_id"):
877878
return _Instance(name=instance_id, client=self)

tests/unit/test_backup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ class _Client(object):
679679
def __init__(self, project=TestBackup.PROJECT_ID):
680680
self.project = project
681681
self.project_name = "projects/" + self.project
682+
self._client_context = None
682683

683684

684685
class _Instance(object):

tests/unit/test_batch.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,9 @@ class _Database(object):
806806

807807
def __init__(self, enable_end_to_end_tracing=False):
808808
self.name = "testing"
809+
self._instance = mock.Mock()
810+
self._instance._client = mock.Mock()
811+
self._instance._client._client_context = None
809812
self._route_to_leader_enabled = True
810813
if enable_end_to_end_tracing:
811814
self.observability_options = dict(enable_end_to_end_tracing=True)

0 commit comments

Comments
 (0)