|
| 1 | +# Copyright 2024 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, select |
| 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 | + GetSessionRequest, |
| 23 | + BeginTransactionRequest, |
| 24 | + TransactionOptions, |
| 25 | +) |
| 26 | +from test.mockserver_tests.mock_server_test_base import MockServerTestBase |
| 27 | +from test.mockserver_tests.mock_server_test_base import add_result |
| 28 | +import google.cloud.spanner_v1.types.type as spanner_type |
| 29 | +import google.cloud.spanner_v1.types.result_set as result_set |
| 30 | + |
| 31 | + |
| 32 | +class TestReadOnlyTransaction(MockServerTestBase): |
| 33 | + def test_read_only_transaction(self): |
| 34 | + from test.mockserver_tests.read_only_model import Singer |
| 35 | + |
| 36 | + add_singer_query_result("SELECT singers.id, singers.name \n" + "FROM singers") |
| 37 | + engine = create_engine( |
| 38 | + "spanner:///projects/p/instances/i/databases/d", |
| 39 | + echo=True, |
| 40 | + connect_args={"client": self.client, "pool": FixedSizePool(size=10)}, |
| 41 | + ) |
| 42 | + |
| 43 | + with Session(engine.execution_options(read_only=True)) as session: |
| 44 | + # Execute two queries in a read-only transaction. |
| 45 | + session.scalars(select(Singer)).all() |
| 46 | + session.scalars(select(Singer)).all() |
| 47 | + |
| 48 | + # Verify the requests that we got. |
| 49 | + requests = self.spanner_service.requests |
| 50 | + eq_(5, len(requests)) |
| 51 | + is_instance_of(requests[0], BatchCreateSessionsRequest) |
| 52 | + # We should get rid of this extra round-trip for GetSession.... |
| 53 | + is_instance_of(requests[1], GetSessionRequest) |
| 54 | + is_instance_of(requests[2], BeginTransactionRequest) |
| 55 | + is_instance_of(requests[3], ExecuteSqlRequest) |
| 56 | + is_instance_of(requests[4], ExecuteSqlRequest) |
| 57 | + # Verify that the transaction is a read-only transaction. |
| 58 | + begin_request: BeginTransactionRequest = requests[2] |
| 59 | + eq_( |
| 60 | + TransactionOptions( |
| 61 | + dict( |
| 62 | + read_only=TransactionOptions.ReadOnly( |
| 63 | + dict( |
| 64 | + strong=True, |
| 65 | + return_read_timestamp=True, |
| 66 | + ) |
| 67 | + ) |
| 68 | + ) |
| 69 | + ), |
| 70 | + begin_request.options, |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def add_singer_query_result(sql: str): |
| 75 | + result = result_set.ResultSet( |
| 76 | + dict( |
| 77 | + metadata=result_set.ResultSetMetadata( |
| 78 | + dict( |
| 79 | + row_type=spanner_type.StructType( |
| 80 | + dict( |
| 81 | + fields=[ |
| 82 | + spanner_type.StructType.Field( |
| 83 | + dict( |
| 84 | + name="singers_id", |
| 85 | + type=spanner_type.Type( |
| 86 | + dict(code=spanner_type.TypeCode.INT64) |
| 87 | + ), |
| 88 | + ) |
| 89 | + ), |
| 90 | + spanner_type.StructType.Field( |
| 91 | + dict( |
| 92 | + name="singers_name", |
| 93 | + type=spanner_type.Type( |
| 94 | + dict(code=spanner_type.TypeCode.STRING) |
| 95 | + ), |
| 96 | + ) |
| 97 | + ), |
| 98 | + ] |
| 99 | + ) |
| 100 | + ) |
| 101 | + ) |
| 102 | + ), |
| 103 | + ) |
| 104 | + ) |
| 105 | + result.rows.extend( |
| 106 | + [ |
| 107 | + ( |
| 108 | + "1", |
| 109 | + "Jane Doe", |
| 110 | + ), |
| 111 | + ( |
| 112 | + "2", |
| 113 | + "John Doe", |
| 114 | + ), |
| 115 | + ] |
| 116 | + ) |
| 117 | + add_result(sql, result) |
0 commit comments