|
| 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 os |
| 16 | +import unittest |
| 17 | + |
| 18 | +from django.db import connection |
| 19 | +from google.cloud.spanner_dbapi.parsed_statement import AutocommitDmlMode |
| 20 | +import google.cloud.spanner_v1.types.type as spanner_type |
| 21 | +import google.cloud.spanner_v1.types.result_set as result_set |
| 22 | +from google.api_core.client_options import ClientOptions |
| 23 | +from google.auth.creden
EF5E
tials import AnonymousCredentials |
| 24 | +from google.cloud.spanner_v1 import ( |
| 25 | + Client, |
| 26 | + ResultSet, |
| 27 | + PingingPool, |
| 28 | + TypeCode, |
| 29 | +) |
| 30 | +from google.cloud.spanner_v1.database import Database |
| 31 | +from google.cloud.spanner_v1.instance import Instance |
| 32 | +import grpc |
| 33 | + |
| 34 | +# TODO: Replace this with the mock server in the Spanner client lib |
| 35 | +from tests.mockserver_tests.mock_spanner import ( |
| 36 | + SpannerServicer, |
| 37 | + start_mock_server, |
| 38 | +) |
| 39 | +from tests.mockserver_tests.mock_database_admin import DatabaseAdminServicer |
| 40 | + |
| 41 | + |
| 42 | +def add_result(sql: str, result: ResultSet): |
| 43 | + MockServerTestBase.spanner_service.mock_spanner.add_result(sql, result) |
| 44 | + |
| 45 | + |
| 46 | +def add_update_count( |
| 47 | + sql: str, |
| 48 | + count: int, |
| 49 | + dml_mode: AutocommitDmlMode = AutocommitDmlMode.TRANSACTIONAL, |
| 50 | +): |
| 51 | + if dml_mode == AutocommitDmlMode.PARTITIONED_NON_ATOMIC: |
| 52 | + stats = dict(row_count_lower_bound=count) |
| 53 | + else: |
| 54 | + stats = dict(row_count_exact=count) |
| 55 | + result = result_set.ResultSet(dict(stats=result_set.ResultSetStats(stats))) |
| 56 | + add_result(sql, result) |
| 57 | + |
| 58 | + |
| 59 | +def add_select1_result(): |
| 60 | + add_single_result("select 1", "c", TypeCode.INT64, [("1",)]) |
| 61 | + |
| 62 | + |
| 63 | +def add_single_result( |
| 64 | + sql: str, column_name: str, type_code: spanner_type.TypeCode, row |
| 65 | +): |
| 66 | + result = result_set.ResultSet( |
| 67 | + dict( |
| 68 | + metadata=result_set.ResultSetMetadata( |
| 69 | + dict( |
| 70 | + row_type=spanner_type.StructType( |
| 71 | + dict( |
| 72 | + fields=[ |
| 73 | + spanner_type.StructType.Field( |
| 74 | + dict( |
| 75 | + name=column_name, |
| 76 | + type=spanner_type.Type( |
| 77 | + dict(code=type_code) |
| 78 | + ), |
| 79 | + ) |
| 80 | + ) |
| 81 | + ] |
| 82 | + ) |
| 83 | + ) |
| 84 | + ) |
| 85 | + ), |
| 86 | + ) |
| 87 | + ) |
| 88 | + result.rows.extend(row) |
| 89 | + MockServerTestBase.spanner_service.mock_spanner.add_result(sql, result) |
| 90 | + |
| 91 | + |
| 92 | +def add_singer_query_result(sql: str): |
| 93 | + result = result_set.ResultSet( |
| 94 | + dict( |
| 95 | + metadata=result_set.ResultSetMetadata( |
| 96 | + dict( |
| 97 | + row_type=spanner_type.StructType( |
| 98 | + dict( |
| 99 | + fields=[ |
| 100 | + spanner_type.StructType.Field( |
| 101 | + dict( |
| 102 | + name="id", |
| 103 | + type=spanner_type.Type( |
| 104 | + dict( |
| 105 | + code=spanner_type.TypeCode.INT64 |
| 106 | + ) |
| 107 | + ), |
| 108 | + ) |
| 109 | + ), |
| 110 | + spanner_type.StructType.Field( |
| 111 | + dict( |
| 112 | + name="first_name", |
| 113 | + type=spanner_type.Type( |
| 114 | + dict( |
| 115 | + code=spanner_type.TypeCode.STRING |
| 116 | + ) |
| 117 | + ), |
| 118 | + ) |
| 119 | + ), |
| 120 | + spanner_type.StructType.Field( |
| 121 | + dict( |
| 122 | + name="last_name", |
| 123 | + type=spanner_type.Type( |
| 124 | + dict( |
| 125 | + code=spanner_type.TypeCode.STRING |
| 126 | + ) |
| 127 | + ), |
| 128 | + ) |
| 129 | + ), |
| 130 | + ] |
| 131 | + ) |
| 132 | + ) |
| 133 | + ) |
| 134 | + ), |
| 135 | + ) |
| 136 | + ) |
| 137 | + result.rows.extend( |
| 138 | + [ |
| 139 | + ( |
| 140 | + "1", |
| 141 | + "Jane", |
| 142 | + "Doe", |
| 143 | + ), |
| 144 | + ( |
| 145 | + "2", |
| 146 | + "John", |
| 147 | + "Doe", |
| 148 | + ), |
| 149 | + ] |
| 150 | + ) |
| 151 | + add_result(sql, result) |
| 152 | + |
| 153 | + |
| 154 | +class MockServerTestBase(unittest.TestCase): |
| 155 | + server: grpc.Server = None |
| 156 | + spanner_service: SpannerServicer = None |
| 157 | + database_admin_service: DatabaseAdminServicer = None |
| 158 | + port: int = None |
| 159 | + |
| 160 | + @classmethod |
| 161 | + def setup_class(cls): |
| 162 | + os.environ["GOOGLE_CLOUD_PROJECT"] = "mockserver-project" |
| 163 | + ( |
| 164 | + MockServerTestBase.server, |
| 165 | + MockServerTestBase.spanner_service, |
| 166 | + MockServerTestBase.database_admin_service, |
| 167 | + MockServerTestBase.port, |
| 168 | + ) = start_mock_server() |
| 169 | + |
| 170 | + @classmethod |
| 171 | + def teardown_class(cls): |
| 172 | + if MockServerTestBase.server is not None: |
| 173 | + MockServerTestBase.server.stop(grace=None) |
| 174 | + MockServerTestBase.server = None |
| 175 | + |
| 176 | + def setup_method(self, test_method): |
| 177 | + self._client = None |
| 178 | + self._instance = None |
| 179 | + self._database = None |
| 180 | + connection.settings_dict["OPTIONS"]["client"] = self.client |
| 181 | + connection.settings_dict["OPTIONS"]["pool"] = PingingPool(size=10) |
| 182 | + |
| 183 | + def teardown_method(self, test_method): |
| 184 | + MockServerTestBase.spanner_service.clear_requests() |
| 185 | + MockServerTestBase.database_admin_service.clear_requests() |
| 186 | + |
| 187 | + @property |
| 188 | + def client(self) -> Client: |
| 189 | + if self._client is None: |
| 190 | + self._client = Client( |
| 191 | + project=os.environ["GOOGLE_CLOUD_PROJECT"], |
| 192 | + credentials=AnonymousCredentials(), |
| 193 | + client_options=ClientOptions( |
| 194 | + api_endpoint="localhost:" + str(MockServerTestBase.port), |
| 195 | + ), |
| 196 | + ) |
| 197 | + return self._client |
| 198 | + |
| 199 | + @property |
| 200 | + def instance(self) -> Instance: |
| 201 | + if self._instance is None: |
| 202 | + self._instance = self.client.instance("test-instance") |
| 203 | + return self._instance |
| 204 | + |
| 205 | + @property |
| 206 | + def database(self) -> Database: |
| 207 | + if self._database is None: |
| 208 | + self._database = self.instance.database( |
| 209 | + "test-database", pool=PingingPool(size=10) |
| 210 | + ) |
| 211 | + return self._database |
0 commit comments