|
| 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 | + ResultSet, |
| 21 | + BatchCreateSessionsRequest, |
| 22 | + ExecuteSqlRequest, |
| 23 | + CommitRequest, |
| 24 | + BeginTransactionRequest, |
| 25 | +) |
| 26 | +from test.mockserver_tests.mock_server_test_base import ( |
| 27 | + MockServerTestBase, |
| 28 | + add_result, |
| 29 | +) |
| 30 | +from google.cloud.spanner_admin_database_v1 import UpdateDatabaseDdlRequest |
| 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 TestAutoIncrement(MockServerTestBase): |
| 36 | + def test_create_table(self): |
| 37 | + from test.mockserver_tests.auto_increment_model import Base |
| 38 | + |
| 39 | + add_result( |
| 40 | + """SELECT true |
| 41 | +FROM INFORMATION_SCHEMA.TABLES |
| 42 | +WHERE TABLE_SCHEMA="" AND TABLE_NAME="singers" |
| 43 | +LIMIT 1 |
| 44 | +""", |
| 45 | + ResultSet(), |
| 46 | + ) |
| 47 | + engine = create_engine( |
| 48 | + "spanner:///projects/p/instances/i/databases/d", |
| 49 | + connect_args={"client": self.client, "pool": FixedSizePool(size=10)}, |
| 50 | + ) |
| 51 | + Base.metadata.create_all(engine) |
| 52 | + requests = self.database_admin_service.requests |
| 53 | + eq_(1, len(requests)) |
| 54 | + is_instance_of(requests[0], UpdateDatabaseDdlRequest) |
| 55 | + eq_(1, len(requests[0].statements)) |
| 56 | + eq_( |
| 57 | + "CREATE TABLE singers (\n" |
| 58 | + "\tid INT64 NOT NULL " |
| 59 | + "GENERATED BY DEFAULT AS IDENTITY (BIT_REVERSED_POSITIVE), \n" |
| 60 | + "\tname STRING(MAX) NOT NULL\n" |
| 61 | + ") PRIMARY KEY (id)", |
| 62 | + requests[0].statements[0], |
| 63 | + ) |
| 64 | + |
| 65 | + def test_insert_row(self): |
| 66 | + from test.mockserver_tests.auto_increment_model import Singer |
| 67 | + |
| 68 | + self.add_insert_result( |
| 69 | + "INSERT INTO singers (name) VALUES (@a0) THEN RETURN singers.id" |
| 70 | + ) |
| 71 | + engine = create_engine( |
| 72 | + "spanner:///projects/p/instances/i/databases/d", |
| 73 | + connect_args={"client": self.client, "pool": FixedSizePool(size=10)}, |
| 74 | + ) |
| 75 | + |
| 76 | + with Session(engine) as session: |
| 77 | + singer = Singer(name="Test") |
| 78 | + session.add(singer) |
| 79 | + # Flush the session to send the insert statement to the database. |
| 80 | + session.flush() |
| 81 | + eq_(987654321, singer.id) |
| 82 | + session.commit() |
| 83 | + # Verify the requests that we got. |
| 84 | + requests = self.spanner_service.requests |
| 85 | + eq_(4, len(requests)) |
| 86 | + is_instance_of(requests[0], BatchCreateSessionsRequest) |
| 87 | + is_instance_of(requests[1], BeginTransactionRequest) |
| 88 | + is_instance_of(requests[2], ExecuteSqlRequest) |
| 89 | + is_instance_of(requests[3], CommitRequest) |
| 90 | + |
| 91 | + def add_insert_result(self, sql): |
| 92 | + result = result_set.ResultSet( |
| 93 | + dict( |
| 94 | + metadata=result_set.ResultSetMetadata( |
| 95 | + dict( |
| 96 | + row_type=spanner_type.StructType( |
| 97 | + dict( |
| 98 | + fields=[ |
| 99 | + spanner_type.StructType.Field( |
| 100 | + dict( |
| 101 | + name="id", |
| 102 | + type=spanner_type.Type( |
| 103 | + dict(code=spanner_type.TypeCode.INT64) |
| 104 | + ), |
| 105 | + ) |
| 106 | + ) |
| 107 | + ] |
| 108 | + ) |
| 109 | + ) |
| 110 | + ) |
| 111 | + ), |
| 112 | + stats=result_set.ResultSetStats( |
| 113 | + dict( |
| 114 | + row_count_exact=1, |
| 115 | + ) |
| 116 | + ), |
| 117 | + ) |
| 118 | + ) |
| 119 | + result.rows.extend([("987654321",)]) |
| 120 | + add_result(sql, result) |
0 commit comments