Skip to content

Commit b2676bc

Browse files
committed
fix: convert unspecified to serializable
1 parent 8addfb2 commit b2676bc

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

google/cloud/sqlalchemy_spanner/requirements.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ def get_isolation_levels(self, _):
102102
Returns:
103103
dict: isolation levels description.
104104
"""
105-
return {"default": "SERIALIZABLE", "supported": ["SERIALIZABLE", "AUTOCOMMIT"]}
105+
return {
106+
"default": "SERIALIZABLE",
107+
"supported": ["SERIALIZABLE", "REPEATABLE READ", "AUTOCOMMIT"],
108+
}
106109

107110
@property
108111
def precision_numerics_enotation_large(self):

google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,9 @@ def default_isolation_level(self):
813813

814814
@default_isolation_level.setter
815815
def default_isolation_level(self, value):
816+
unspecified = TransactionOptions.IsolationLevel.ISOLATION_LEVEL_UNSPECIFIED
817+
if value == unspecified or value == unspecified.name.replace("_", " "):
818+
value = "SERIALIZABLE"
816819
self._default_isolation_level = value
817820

818821
def _check_unicode_returns(self, connection, additional_tests=None):
@@ -1707,9 +1710,9 @@ def get_isolation_level(self, conn_proxy):
17071710

17081711
def _string_to_isolation_level(self, name):
17091712
try:
1710-
return TransactionOptions.IsolationLevel[name.upper().replace(" ", "_")]
1713+
return TransactionOptions.IsolationLevel[name.replace(" ", "_")]
17111714
except KeyError:
1712-
return TransactionOptions.IsolationLevel.ISOLATION_LEVEL_UNSPECIFIED
1715+
raise ValueError("Invalid isolation level name '%s'" % name)
17131716

17141717
def _isolation_level_to_string(self, level):
17151718
return level.name.replace("_", " ")

test/mockserver_tests/test_isolation_level.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
14+
import pytest
1515
from sqlalchemy import create_engine
1616
from sqlalchemy.orm import Session
1717
from sqlalchemy.testing import eq_, is_instance_of
@@ -144,6 +144,19 @@ def test_auto_commit(self):
144144
execute_request.transaction.begin,
145145
)
146146

147+
def test_invalid_isolation_level(self):
148+
from test.mockserver_tests.isolation_level_model import Singer
149+
150+
engine = create_engine(
151+
"spanner:///projects/p/instances/i/databases/d",
152+
connect_args={"client": self.client, "pool": FixedSizePool(size=10)},
153+
)
154+
with pytest.raises(ValueError):
155+
with Session(engine.execution_options(isolation_level="foo")) as session:
156+
singer = Singer(name="Test")
157+
session.add(singer)
158+
session.commit()
159+
147160
def verify_isolation_level(self, level):
148161
# Verify the requests that we got.
149162
requests = self.spanner_service.requests

0 commit comments

Comments
 (0)