|
| 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 |
| 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 | + |
| 27 | +from test.mockserver_tests.bit_reversed_sequence_model import add_singer_query_result |
| 28 | +from test.mockserver_tests.mock_server_test_base import MockServerTestBase |
| 29 | + |
| 30 | + |
| 31 | +class TestReadOnlyTransaction(MockServerTestBase): |
| 32 | + def test_read_only_transaction(self): |
| 33 | + from test.mockserver_tests.bit_reversed_sequence_model import Singer |
| 34 | + |
| 35 | + add_singer_query_result( |
| 36 | + "SELECT singers.id AS singers_id, singers.name AS singers_name \n" |
| 37 | + "FROM singers" |
| 38 | + ) |
| 39 | + engine = create_engine( |
| 40 | + "spanner:///projects/p/instances/i/databases/d", |
| 41 | + connect_args={"client": self.client, "pool": FixedSizePool(size=10)}, |
| 42 | + ) |
| 43 | + |
| 44 | + with Session(engine.execution_options(read_only=True)) as session: |
| 45 | + # Execute two queries in a read-only transaction. |
| 46 | + session.query(Singer).all() |
| 47 | + session.query(Singer).all() |
| 48 | + |
| 49 | + # Verify the requests that we got. |
| 50 | + requests = self.spanner_service.requests |
| 51 | + eq_(5, len(requests)) |
| 52 | + is_instance_of(requests[0], BatchCreateSessionsRequest) |
| 53 | + # We should get rid of this extra round-trip for GetSession.... |
| 54 | + is_instance_of(requests[1], GetSessionRequest) |
| 55 | + is_instance_of(requests[2], BeginTransactionRequest) |
| 56 | + is_instance_of(requests[3], ExecuteSqlRequest) |
| 57 | + is_instance_of(requests[4], ExecuteSqlRequest) |
| 58 | + # Verify that the transaction is a read-only transaction. |
| 59 | + begin_request: BeginTransactionRequest = requests[2] |
| 60 | + eq_( |
| 61 | + TransactionOptions( |
| 62 | + dict( |
| 63 | + read_only=TransactionOptions.ReadOnly( |
| 64 | + dict( |
| 65 | + strong=True, |
| 66 | + return_read_timestamp=True, |
| 67 | + ) |
| 68 | + ) |
| 69 | + ) |
| 70 | + ), |
| 71 | + begin_request.options, |
| 72 | + ) |
0 commit comments