|
| 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 | +import uuid |
| 16 | +from unittest import mock |
| 17 | + |
| 18 | +import sqlalchemy |
| 19 | +from sqlalchemy.orm import Session |
| 20 | +from sqlalchemy.testing import eq_, is_instance_of |
| 21 | +from google.cloud.spanner_v1 import ( |
| 22 | + ExecuteSqlRequest, |
| 23 | + CommitRequest, |
| 24 | + RollbackRequest, |
| 25 | + BeginTransactionRequest, |
| 26 | + CreateSessionRequest, |
| 27 | +) |
| 28 | +from test.mockserver_tests.mock_server_test_base import ( |
| 29 | + MockServerTestBase, |
| 30 | + add_result, |
| 31 | +) |
| 32 | +import google.cloud.spanner_v1.types.type as spanner_type |
| 33 | +import google.cloud.spanner_v1.types.result_set as result_set |
| 34 | + |
| 35 | + |
| 36 | +class TestInsertmany(MockServerTestBase): |
| 37 | + @mock.patch.object(uuid, "uuid4", mock.MagicMock(side_effect=["a", "b"])) |
| 38 | + def test_insertmany_with_uuid_sentinels(self): |
| 39 | + """Ensures one bulk insert for ORM objects distinguished by uuid.""" |
| 40 | + from test.mockserver_tests.insertmany_model import SingerUUID |
| 41 | + |
| 42 | + self.add_uuid_insert_result( |
| 43 | + "INSERT INTO singers_uuid (id, name) " |
| 44 | + "VALUES (@a0, @a1), (@a2, @a3) " |
| 45 | + "THEN RETURN inserted_at, id" |
| 46 | + ) |
| 47 | + engine = self.create_engine() |
| 48 | + |
| 49 | + with Session(engine) as session: |
| 50 | + session.add(SingerUUID(name="a")) |
| 51 | + session.add(SingerUUID(name="b")) |
| 52 | + session.commit() |
| 53 | + |
| 54 | + # Verify the requests that we got. |
| 55 | + requests = self.spanner_service.requests |
| 56 | + eq_(4, len(requests)) |
| 57 | + is_instance_of(requests[0], CreateSessionRequest) |
| 58 | + is_instance_of(requests[1], BeginTransactionRequest) |
| 59 | + is_instance_of(requests[2], ExecuteSqlRequest) |
| 60 | + is_instance_of(requests[3], CommitRequest) |
| 61 | + |
| 62 | + def test_no_insertmany_with_bit_reversed_id(self): |
| 63 | + """Ensures we don't try to bulk insert rows with bit-reversed PKs. |
| 64 | +
|
| 65 | + SQLAlchemy's insertmany support requires either incrementing |
| 66 | + PKs or client-side supplied sentinel values such as UUIDs. |
| 67 | + Spanner's bit-reversed integer PKs don't meet the ordering |
| 68 | + requirement, so we need to make sure we don't try to bulk |
| 69 | + insert with them. |
| 70 | + """ |
| 71 | + from test.mockserver_tests.insertmany_model import SingerIntID |
| 72 | + |
| 73 | + self.add_int_id_insert_result( |
| 74 | + "INSERT INTO singers_int_id (name) " |
| 75 | + "VALUES (@a0) " |
| 76 | + "THEN RETURN id, inserted_at" |
| 77 | + ) |
| 78 | + engine = self.create_engine() |
| 79 | + |
| 80 | + with Session(engine) as session: |
| 81 | + session.add(SingerIntID(name="a")) |
| 82 | + session.add(SingerIntID(name="b")) |
| 83 | + try: |
| 84 | + session.commit() |
| 85 | + except sqlalchemy.exc.SAWarning: |
| 86 | + # This will fail because we're returning the same PK |
| 87 | + # for two rows. The mock server doesn't currently |
| 88 | + # support associating the same query with two |
| 89 | + # different results. For our purposes that's okay -- |
| 90 | + # we just want to ensure we generate two INSERTs, not |
| 91 | + # one. |
| 92 | + pass |
| 93 | + |
| 94 | + # Verify the requests that we got. |
| 95 | + requests = self.spanner_service.requests |
| 96 | + eq_(5, len(requests)) |
| 97 | + is_instance_of(requests[0], CreateSessionRequest) |
| 98 | + is_instance_of(requests[1], BeginTransactionRequest) |
| 99 | + is_instance_of(requests[2], ExecuteSqlRequest) |
| 100 | + is_instance_of(requests[3], ExecuteSqlRequest) |
| 101 | + is_instance_of(requests[4], RollbackRequest) |
| 102 | + |
| 103 | + def add_uuid_insert_result(self, sql): |
| 104 | + result = result_set.ResultSet( |
| 105 | + dict( |
| 106 | + metadata=result_set.ResultSetMetadata( |
| 107 | + dict( |
| 108 | + row_type=spanner_type.StructType( |
| 109 | + dict( |
| 110 | + fields=[ |
| 111 | + spanner_type.StructType.Field( |
| 112 | + dict( |
| 113 | + name="inserted_at", |
| 114 | + type=spanner_type.Type( |
| 115 | + dict( |
| 116 | + code=spanner_type.TypeCode.TIMESTAMP |
| 117 | + ) |
| 118 | + ), |
| 119 | + ) |
| 120 | + ), |
| 121 | + spanner_type.StructType.Field( |
| 122 | + dict( |
| 123 | + name="id", |
| 124 | + type=spanner_type.Type( |
| 125 | + dict(code=spanner_type.TypeCode.STRING) |
| 126 | + ), |
| 127 | + ) |
| 128 | + ), |
| 129 | + ] |
| 130 | + ) |
| 131 | + ) |
| 132 | + ) |
| 133 | + ), |
| 134 | + ) |
| 135 | + ) |
| 136 | + result.rows.extend( |
| 137 | + [ |
| 138 | + ( |
| 139 | + "2020-06-02T23:58:40Z", |
| 140 | + "a", |
| 141 | + ), |
| 142 | + ( |
| 143 | + "2020-06-02T23:58:41Z", |
| 144 | + "b", |
| 145 | + ), |
| 146 | + ] |
| 147 | + ) |
| 148 | + add_result(sql, result) |
| 149 | + |
| 150 | + def add_int_id_insert_result(self, sql): |
| 151 | + result = result_set.ResultSet( |
| 152 | + dict( |
| 153 | + metadata=result_set.ResultSetMetadata( |
| 154 | + dict( |
| 155 | + row_type=spanner_type.StructType( |
| 156 | + dict( |
| 157 | + fields=[ |
| 158 | + spanner_type.StructType.Field( |
| 159 | + dict( |
| 160 | + name="id", |
| 161 | + type=spanner_type.Type( |
| 162 | + dict(code=spanner_type.TypeCode.INT64) |
| 163 | + ), |
| 164 | + ) |
| 165 | + ), |
| 166 | + spanner_type.StructType.Field( |
| 167 | + dict( |
| 168 | + name="inserted_at", |
| 169 | + type=spanner_type.Type( |
| 170 | + dict( |
| 171 | + code=spanner_type.TypeCode.TIMESTAMP |
| 172 | + ) |
| 173 | + ), |
| 174 | + ) |
| 175 | + ), |
| 176 | + ] |
| 177 | + ) |
| 178 | + ) |
| 179 | + ) |
| 180 | + ), |
| 181 | + ) |
| 182 | + ) |
| 183 | + result.rows.extend( |
| 184 | + [ |
| 185 | + ( |
| 186 | + "1", |
| 187 | + "2020-06-02T23:58:40Z", |
| 188 | + ), |
| 189 | + ] |
| 190 | + ) |
| 191 | + add_result(sql, result) |
0 commit comments