|
| 1 | +# Copyright 2026 Google LLC |
| 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 unittest.mock import MagicMock |
| 16 | +from sqlalchemy.testing import eq_ |
| 17 | +from sqlalchemy.testing.plugin.plugin_base import fixtures |
| 18 | +from google.cloud.sqlalchemy_spanner.sqlalchemy_spanner import SpannerDialect |
| 19 | + |
| 20 | + |
| 21 | +class TestSpannerDialect(fixtures.TestBase): |
| 22 | + def test_get_multi_indexes_excludes_search_indexes_sql(self): |
| 23 | + """Test that get_multi_indexes SQL query excludes SEARCH indexes.""" |
| 24 | + dialect = SpannerDialect() |
| 25 | + connection = MagicMock() |
| 26 | + mock_snapshot = MagicMock() |
| 27 | + mock_snapshot.execute_sql.return_value = [] |
| 28 | + connection.connection.database.snapshot.return_value.__enter__.return_value = ( |
| 29 | + mock_snapshot |
| 30 | + ) |
| 31 | + |
| 32 | + dialect.get_multi_indexes(connection) |
| 33 | + |
| 34 | + # Retrieve the SQL executed by snapshot |
| 35 | + executed_sql = mock_snapshot.execute_sql.call_args[0][0] |
| 36 | + assert "i.index_type != 'SEARCH'" in executed_sql |
| 37 | + |
| 38 | + def test_get_multi_indexes_handles_none_column_ordering(self): |
| 39 | + """Test get_multi_indexes with None column ordering.""" |
| 40 | + dialect = SpannerDialect() |
| 41 | + connection = MagicMock() |
| 42 | + mock_snapshot = MagicMock() |
| 43 | + # Mock row: schema, table, index_name, columns, |
| 44 | + # is_unique, column_orderings, storing_columns |
| 45 | + mock_row = [ |
| 46 | + "public", |
| 47 | + "my_table", |
| 48 | + "idx_search", |
| 49 | + ["col1"], |
| 50 | + False, |
| 51 | + [None], # column_ordering is None |
| 52 | + [], |
| 53 | + ] |
| 54 | + mock_snapshot.execute_sql.return_value = [mock_row] |
| 55 | + connection.connection.database.snapshot.return_value.__enter__.return_value = ( |
| 56 | + mock_snapshot |
| 57 | + ) |
| 58 | + |
| 59 | + res = dialect.get_multi_indexes(connection) |
| 60 | + assert ("public", "my_table") in res |
| 61 | + index_info = res[("public", "my_table")][0] |
| 62 | + eq_(index_info["column_sorting"], {}) |
| 63 | + |
| 64 | + def test_get_multi_indexes_handles_null_column_orderings_array(self): |
| 65 | + """Test get_multi_indexes when column_orderings array is None.""" |
| 66 | + dialect = SpannerDialect() |
| 67 | + connection = MagicMock() |
| 68 | + mock_snapshot = MagicMock() |
| 69 | + mock_row = [ |
| 70 | + "public", |
| 71 | + "my_table", |
| 72 | + "idx_test", |
| 73 | + ["col1"], |
| 74 | + False, |
| 75 | + None, # row[5] is None |
| 76 | + [], |
| 77 | + ] |
| 78 | + mock_snapshot.execute_sql.return_value = [mock_row] |
| 79 | + connection.connection.database.snapshot.return_value.__enter__.return_value = ( |
| 80 | + mock_snapshot |
| 81 | + ) |
| 82 | + |
| 83 | + res = dialect.get_multi_indexes(connection) |
| 84 | + assert ("public", "my_table") in res |
| 85 | + index_info = res[("public", "my_table")][0] |
| 86 | + eq_(index_info["column_sorting"], {}) |
0 commit comments