|
| 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 | +from sqlalchemy import create_engine |
| 16 | +from sqlalchemy.testing import eq_, is_instance_of |
| 17 | +from google.cloud.spanner_v1 import ( |
| 18 | + FixedSizePool, |
| 19 | + ResultSet, |
| 20 | +) |
| 21 | +from test.mockserver_tests.mock_server_test_base import ( |
| 22 | + MockServerTestBase, |
| 23 | + add_result, |
| 24 | +) |
| 25 | +from google.cloud.spanner_admin_database_v1 import UpdateDatabaseDdlRequest |
| 26 | + |
| 27 | + |
| 28 | +class TestNullFilteredIndex(MockServerTestBase): |
| 29 | + """Ensure we emit correct DDL for not null filtered indexes.""" |
| 30 | + |
| 31 | + def test_create_table(self): |
| 32 | + from test.mockserver_tests.interleaved_index import Base |
| 33 | + |
| 34 | + add_result( |
| 35 | + """SELECT true |
| 36 | +FROM INFORMATION_SCHEMA.TABLES |
| 37 | +WHERE TABLE_SCHEMA="" AND TABLE_NAME="singers" |
| 38 | +LIMIT 1 |
| 39 | +""", |
| 40 | + ResultSet(), |
| 41 | + ) |
| 42 | + add_result( |
| 43 | + """SELECT true |
| 44 | +FROM INFORMATION_SCHEMA.TABLES |
| 45 | +WHERE TABLE_SCHEMA="" AND TABLE_NAME="albums" |
| 46 | +LIMIT 1 |
| 47 | +""", |
| 48 | + ResultSet(), |
| 49 | + ) |
| 50 | + add_result( |
| 51 | + """SELECT true |
| 52 | +FROM INFORMATION_SCHEMA.TABLES |
| 53 | +WHERE TABLE_SCHEMA="" AND TABLE_NAME="tracks" |
| 54 | +LIMIT 1 |
| 55 | +""", |
| 56 | + ResultSet(), |
| 57 | + ) |
| 58 | + engine = create_engine( |
| 59 | + "spanner:///projects/p/instances/i/databases/d", |
| 60 | + connect_args={"client": self.client, "pool": FixedSizePool(size=10)}, |
| 61 | + ) |
| 62 | + Base.metadata.create_all(engine) |
| 63 | + requests = self.database_admin_service.requests |
| 64 | + eq_(1, len(requests)) |
| 65 | + is_instance_of(requests[0], UpdateDatabaseDdlRequest) |
| 66 | + eq_(4, len(requests[0].statements)) |
| 67 | + eq_( |
| 68 | + "CREATE TABLE singers (\n" |
| 69 | + "\tsinger_id STRING(36) NOT NULL, \n" |
| 70 | + "\tfirst_name STRING(MAX) NOT NULL, \n" |
| 71 | + "\tlast_name STRING(MAX) NOT NULL\n" |
| 72 | + ") PRIMARY KEY (singer_id)", |
| 73 | + requests[0].statements[0], |
| 74 | + ) |
| 75 | + eq_( |
| 76 | + "CREATE TABLE albums (\n" |
| 77 | + "\tsinger_id STRING(36) NOT NULL, \n" |
| 78 | + "\talbum_id STRING(36) NOT NULL, \n" |
| 79 | + "\talbum_title STRING(MAX) NOT NULL, \n" |
| 80 | + "\tFOREIGN KEY(singer_id) REFERENCES singers (singer_id)\n" |
| 81 | + ") PRIMARY KEY (singer_id, album_id),\n" |
| 82 | + "INTERLEAVE IN PARENT singers ON DELETE CASCADE", |
| 83 | + requests[0].statements[1], |
| 84 | + ) |
| 85 | + eq_( |
| 86 | + "CREATE TABLE tracks (\n" |
| 87 | + "\tsinger_id STRING(36) NOT NULL, \n" |
| 88 | + "\talbum_id STRING(36) NOT NULL, \n" |
| 89 | + "\ttrack_id STRING(36) NOT NULL, \n" |
| 90 | + "\tsong_name STRING(MAX) NOT NULL, \n" |
| 91 | + "\tFOREIGN KEY(singer_id) REFERENCES singers (singer_id), \n" |
| 92 | + "\tFOREIGN KEY(album_id) REFERENCES albums (album_id)\n" |
| 93 | + ") PRIMARY KEY (singer_id, album_id, track_id),\n" |
| 94 | + "INTERLEAVE IN PARENT albums ON DELETE CASCADE", |
| 95 | + requests[0].statements[2], |
| 96 | + ) |
| 97 | + eq_( |
| 98 | + "CREATE INDEX idx_name ON tracks " |
| 99 | + "(singer_id, album_id, song_name), " |
| 100 | + "INTERLEAVE IN albums", |
| 101 | + requests[0].statements[3], |
| 102 | + ) |
0 commit comments