Skip to content
This repository was archived by the owner on Mar 13, 2026. It is now read-only.
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
3 changes: 3 additions & 0 deletions google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def reset_connection(dbapi_conn, connection_record):
if getattr(dbapi_conn.connection, "staleness", None) is not None:
dbapi_conn.connection.staleness = None

if getattr(dbapi_conn.connection, "read_only", None) is not None:
dbapi_conn.connection.read_only = False

Comment thread
vi3k6i5 marked this conversation as resolved.

# register a method to get a single value of a JSON object
OPERATORS[json_getitem_op] = operator_lookup["json_getitem_op"]
Expand Down
68 changes: 46 additions & 22 deletions test/test_suite_13.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import pkg_resources
import pytest
import random
import unittest
import time
from unittest import mock

import sqlalchemy
Expand Down Expand Up @@ -1579,45 +1579,32 @@ def test_user_agent(self):
)


class ExecutionOptionsTest(fixtures.TestBase, unittest.TestCase):
class ExecutionOptionsReadOnlyTest(fixtures.TestBase):
"""
Check that `execution_options()` method correctly
sets parameters on the underlying DB API connection.
"""

@classmethod
def setUpClass(cls):
cls._engine = create_engine(get_db_url(), pool_size=1)
cls._metadata = MetaData(bind=cls._engine)
def setUp(self):
self._engine = create_engine(get_db_url(), pool_size=1)
metadata = MetaData(bind=self._engine)

cls._table = Table(
self._table = Table(
"execution_options",
cls._metadata,
metadata,
Column("opt_id", Integer, primary_key=True),
Column("opt_name", String(16), nullable=False),
)

cls._metadata.create_all(cls._engine)
metadata.create_all(self._engine)

def test_read_only(self):
with self._engine.connect().execution_options(read_only=True) as connection:
connection.execute(select(["*"], from_obj=self._table)).fetchall()
assert connection.connection.read_only is True

def test_staleness(self):
with self._engine.connect().execution_options(
read_only=True, staleness={"exact_staleness": datetime.timedelta(seconds=5)}
) as connection:
connection.execute(select(["*"], from_obj=self._table)).fetchall()
assert connection.connection.staleness == {
"exact_staleness": datetime.timedelta(seconds=5)
}

with self._engine.connect() as connection:
assert connection.connection.staleness is None

with self._engine.connect() as connection:
del connection.staleness
assert connection.connection.read_only is False


class LimitOffsetTest(fixtures.TestBase):
Expand Down Expand Up @@ -1645,6 +1632,43 @@ def test_offset_only(self):
list(connection.execute(self._table.select().offset(offset)).fetchall())


class ExecutionOptionsStalenessTest(fixtures.TestBase):
"""
Check that `execution_options()` method correctly
sets parameters on the underlying DB API connection.
"""

def setUp(self):
self._engine = create_engine(get_db_url(), pool_size=1)
metadata = MetaData(bind=self._engine)

self._table = Table(
"execution_options",
metadata,
Column("opt_id", Integer, primary_key=True),
Column("opt_name", String(16), nullable=False),
)

metadata.create_all(self._engine)
time.sleep(1)

def test_staleness(self):
with self._engine.connect().execution_options(
read_only=True, staleness={"exact_staleness": datetime.timedelta(seconds=1)}
) as connection:
connection.execute(select(["*"], from_obj=self._table)).fetchall()
assert connection.connection.staleness == {
"exact_staleness": datetime.timedelta(seconds=1)
}

with self._engine.connect() as connection:
assert connection.connection.staleness == {}

engine = create_engine("sqlite:///database")
with engine.connect() as connection:
pass


class TemporaryTableTest(fixtures.TestBase):
"""
Check that temporary tables raise an error on creation.
Expand Down