|
| 1 | +# Copyright 2025 Google LLC All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from sqlalchemy import create_engine |
| 16 | +from sqlalchemy.orm import Session |
| 17 | +from sqlalchemy.testing import eq_, is_instance_of |
| 18 | +from google.cloud.spanner_v1 import ( |
| 19 | + FixedSizePool, |
| 20 | + BatchCreateSessionsRequest, |
| 21 | + ExecuteSqlRequest, |
| 22 | + CommitRequest, |
| 23 | + BeginTransactionRequest, |
| 24 | + TransactionOptions, |
| 25 | +) |
| 26 | + |
| 27 | +from test.mockserver_tests.mock_server_test_base import ( |
| 28 | + MockServerTestBase, |
| 29 | + add_result, |
| 30 | +) |
| 31 | +import google.cloud.spanner_v1.types.type as spanner_type |
| 32 | +import google.cloud.spanner_v1.types.result_set as result_set |
| 33 | + |
| 34 | + |
| 35 | +class TestIsolationLevel(MockServerTestBase): |
| 36 | + def test_default_isolation_level(self): |
| 37 | + from test.mockserver_tests.isolation_level_model import Singer |
| 38 | + |
| 39 | + self.add_insert_result( |
| 40 | + "INSERT INTO singers (name) VALUES (@a0) THEN RETURN singers.id" |
| 41 | + ) |
| 42 | + engine = create_engine( |
| 43 | + "spanner:///projects/p/instances/i/databases/d", |
| 44 | + connect_args={"client": self.client, "pool": FixedSizePool(size=10)}, |
| 45 | + ) |
| 46 | + |
| 47 | + with Session(engine) as session: |
| 48 | + singer = Singer(name="Test") |
| 49 | + session.add(singer) |
| 50 | + session.commit() |
| 51 | + self.verify_isolation_level( |
| 52 | + TransactionOptions.IsolationLevel.ISOLATION_LEVEL_UNSPECIFIED |
| 53 | + ) |
| 54 | + |
| 55 | + def test_engine_isolation_level(self): |
| 56 | + from test.mockserver_tests.isolation_level_model import Singer |
| 57 | + |
| 58 | + self.add_insert_result( |
| 59 | + "INSERT INTO singers (name) VALUES (@a0) THEN RETURN singers.id" |
| 60 | + ) |
| 61 | + engine = create_engine( |
| 62 | + "spanner:///projects/p/instances/i/databases/d", |
| 63 | + connect_args={"client": self.client, "pool": FixedSizePool(size=10)}, |
| 64 | + isolation_level="REPEATABLE READ", |
| 65 | + ) |
| 66 | + |
| 67 | + with Session(engine) as session: |
| 68 | + singer = Singer(name="Test") |
| 69 | + session.add(singer) |
| 70 | + session.commit() |
| 71 | + self.verify_isolation_level(TransactionOptions.IsolationLevel.REPEATABLE_READ) |
| 72 | + |
| 73 | + def test_execution_options_isolation_level(self): |
| 74 | + from test.mockserver_tests.isolation_level_model import Singer |
| 75 | + |
| 76 | + self.add_insert_result( |
| 77 | + "INSERT INTO singers (name) VALUES (@a0) THEN RETURN singers.id" |
| 78 | + ) |
| 79 | + engine = create_engine( |
| 80 | + "spanner:///projects/p/instances/i/databases/d", |
| 81 | + connect_args={"client": self.client, "pool": FixedSizePool(size=10)}, |
| 82 | + ) |
| 83 | + |
| 84 | + with Session( |
| 85 | + engine.execution_options(isolation_level="repeatable read") |
| 86 | + ) as session: |
| 87 | + singer = Singer(name="Test") |
| 88 | + session.add(singer) |
| 89 | + session.commit() |
| 90 | + self.verify_isolation_level(TransactionOptions.IsolationLevel.REPEATABLE_READ) |
| 91 | + |
| 92 | + def test_override_engine_isolation_level(self): |
| 93 | + from test.mockserver_tests.isolation_level_model import Singer |
| 94 | + |
| 95 | + self.add_insert_result( |
| 96 | + "INSERT INTO singers (name) VALUES (@a0) THEN RETURN singers.id" |
| 97 | + ) |
| 98 | + engine = create_engine( |
| 99 | + "spanner:///projects/p/instances/i/databases/d", |
| 100 | + connect_args={"client": self.client, "pool": FixedSizePool(size=10)}, |
| 101 | + isolation_level="REPEATABLE READ", |
| 102 | + ) |
| 103 | + |
| 104 | + with Session( |
| 105 | + engine.execution_options(isolation_level="SERIALIZABLE") |
| 106 | + ) as session: |
| 107 | + singer = Singer(name="Test") |
| 108 | + session.add(singer) |
| 109 | + session.commit() |
| 110 | + self.verify_isolation_level(TransactionOptions.IsolationLevel.SERIALIZABLE) |
| 111 | + |
| 112 | + def verify_isolation_level(self, level): |
| 113 | + # Verify the requests that we got. |
| 114 | + requests = self.spanner_service.requests |
| 115 | + eq_(4, len(requests)) |
| 116 | + is_instance_of(requests[0], BatchCreateSessionsRequest) |
| 117 | + is_instance_of(requests[1], BeginTransactionRequest) |
| 118 | + is_instance_of(requests[2], ExecuteSqlRequest) |
| 119 | + is_instance_of(requests[3], CommitRequest) |
| 120 | + begin_request: BeginTransactionRequest = requests[1] |
| 121 | + eq_( |
| 122 | + TransactionOptions( |
| 123 | + dict( |
| 124 | + isolation_level=level, |
| 125 | + read_write=TransactionOptions.ReadWrite(), |
| 126 | + ) |
| 127 | + ), |
| 128 | + begin_request.options, |
| 129 | + ) |
| 130 | + |
| 131 | + def add_insert_result(self, sql): |
| 132 | + result = result_set.ResultSet( |
| 133 | + dict( |
| 134 | + metadata=result_set.ResultSetMetadata( |
| 135 | + dict( |
| 136 | + row_type=spanner_type.StructType( |
| 137 | + dict( |
| 138 | + fields=[ |
| 139 | + spanner_type.StructType.Field( |
| 140 | + dict( |
| 141 | + name="id", |
| 142 | + type=spanner_type.Type( |
| 143 | + dict(code=spanner_type.TypeCode.INT64) |
| 144 | + ), |
| 145 | + ) |
| 146 | + ) |
| 147 | + ] |
| 148 | + ) |
| 149 | + ) |
| 150 | + ) |
| 151 | + ), |
| 152 | + stats=result_set.ResultSetStats( |
| 153 | + dict( |
| 154 | + row_count_exact=1, |
| 155 | + ) |
| 156 | + ), |
| 157 | + ) |
| 158 | + ) |
| 159 | + result.rows.extend([("987654321",)]) |
| 160 | + add_result(sql, result) |
0 commit comments