|
| 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 | +import datetime |
| 16 | +from sqlalchemy import create_engine, select |
| 17 | +from sqlalchemy.orm import Session |
| 18 | +from sqlalchemy.testing import eq_, is_instance_of |
| 19 | +from google.cloud.spanner_v1 import ( |
| 20 | + FixedSizePool, |
| 21 | + BatchCreateSessionsRequest, |
| 22 | + ExecuteSqlRequest, |
| 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 TestStaleReads(MockServerTestBase): |
| 33 | + def test_stale_read_multi_use(self): |
| 34 | + from test.mockserver_tests.stale_read_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 | + timestamp = datetime.datetime.fromtimestamp(1733328910) |
| 44 | + for i in range(2): |
| 45 | + with Session( |
| 46 | + engine.execution_options( |
| 47 | + read_only=True, |
| 48 | + staleness={"read_timestamp": timestamp}, |
| 49 | + ) |
| 50 | + ) as session: |
| 51 | + # Execute two queries in a read-only transaction. |
| 52 | + session.scalars(select(Singer)).all() |
| 53 | + session.scalars(select(Singer)).all() |
| 54 | + |
| 55 | + # Verify the requests that we got. |
| 56 | + requests = self.spanner_service.requests |
| 57 | + eq_(7, len(requests)) |
| 58 | + is_instance_of(requests[0], BatchCreateSessionsRequest) |
| 59 | + is_instance_of(requests[1], BeginTransactionRequest) |
| 60 | + is_instance_of(requests[2], ExecuteSqlRequest) |
| 61 | + is_instance_of(requests[3], ExecuteSqlRequest) |
| 62 | + is_instance_of(requests[4], BeginTransactionRequest) |
| 63 | + is_instance_of(requests[5], ExecuteSqlRequest) |
| 64 | + is_instance_of(requests[6], ExecuteSqlRequest) |
| 65 | + # Verify that the transaction is a read-only transaction. |
| 66 | + for index in [1, 4]: |
| 67 | + begin_request: BeginTransactionRequest = requests[index] |
| 68 | + eq_( |
| 69 | + TransactionOptions( |
| 70 | + dict( |
| 71 | + read_only=TransactionOptions.ReadOnly( |
| 72 | + dict( |
| 73 | + read_timestamp={"seconds": 1733328910}, |
| 74 | + return_read_timestamp=True, |
| 75 | + ) |
| 76 | + ) |
| 77 | + ) |
| 78 | + ), |
| 79 | + begin_request.options, |
| 80 | + ) |
| 81 | + |
| 82 | + def test_stale_read_single_use(self): |
| 83 | + from test.mockserver_tests.stale_read_model import Singer |
| 84 | + |
| 85 | + add_singer_query_result("SELECT singers.id, singers.name\n" + "FROM singers") |
| 86 | + engine = create_engine( |
| 87 | + "spanner:///projects/p/instances/i/databases/d", |
| 88 | + echo=True, |
| 89 | + connect_args={"client": self.client, "pool": FixedSizePool(size=10)}, |
| 90 | + ) |
| 91 | + |
| 92 | + with Session( |
| 93 | + engine.execution_options( |
| 94 | + isolation_level="AUTOCOMMIT", |
| 95 | + staleness={"max_staleness": {"seconds": 15}}, |
| 96 | + ) |
| 97 | + ) as session: |
| 98 | + # Execute two queries in autocommit. |
| 99 | + session.scalars(select(Singer)).all() |
| 100 | + session.scalars(select(Singer)).all() |
| 101 | + |
| 102 | + # Verify the requests that we got. |
| 103 | + requests = self.spanner_service.requests |
| 104 | + eq_(3, len(requests)) |
| 105 | + is_instance_of(requests[0], BatchCreateSessionsRequest) |
| 106 | + is_instance_of(requests[1], ExecuteSqlRequest) |
| 107 | + is_instance_of(requests[2], ExecuteSqlRequest) |
| 108 | + # Verify that the requests use a stale read. |
| 109 | + for index in [1, 2]: |
| 110 | + execute_request: ExecuteSqlRequest = requests[index] |
| 111 | + eq_( |
| 112 | + TransactionOptions( |
| 113 | + dict( |
| 114 | + read_only=TransactionOptions.ReadOnly( |
| 115 | + dict( |
| 116 | + max_staleness={"seconds": 15}, |
| 117 | + return_read_timestamp=True, |
| 118 | + ) |
| 119 | + ) |
| 120 | + ) |
| 121 | + ), |
| 122 | + execute_request.transaction.single_use, |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +def add_singer_query_result(sql: str): |
| 127 | + result = result_set.ResultSet( |
| 128 | + dict( |
| 129 | + metadata=result_set.ResultSetMetadata( |
| 130 | + dict( |
| 131 | + row_type=spanner_type.StructType( |
| 132 | + dict( |
| 133 | + fields=[ |
| 134 | + spanner_type.StructType.Field( |
| 135 | + dict( |
| 136 | + name="singers_id", |
| 137 | + type=spanner_type.Type( |
| 138 | + dict(code=spanner_type.TypeCode.INT64) |
| 139 | + ), |
| 140 | + ) |
| 141 | + ), |
| 142 | + spanner_type.StructType.Field( |
| 143 | + dict( |
| 144 | + name="singers_name", |
| 145 | + type=spanner_type.Type( |
| 146 | + dict(code=spanner_type.TypeCode.STRING) |
| 147 | + ), |
| 148 | + ) |
| 149 | + ), |
| 150 | + ] |
| 151 | + ) |
| 152 | + ) |
| 153 | + ) |
| 154 | + ), |
| 155 | + ) |
| 156 | + ) |
| 157 | + result.rows.extend( |
| 158 | + [ |
| 159 | + ( |
| 160 | + "1", |
| 161 | + "Jane Doe", |
| 162 | + ), |
| 163 | + ( |
| 164 | + "2", |
| 165 | + "John Doe", |
| 166 | + ), |
| 167 | + ] |
| 168 | + ) |
| 169 | + add_result(sql, result) |
0 commit comments