|
23 | 23 | from google.auth.credentials import Credentials |
24 | 24 | from google.auth.credentials import with_scopes_if_required |
25 | 25 | from google.oauth2 import service_account |
26 | | -from mock import patch |
27 | 26 | import pytest # noqa F401 Needed to run the tests |
28 | 27 | from unit.mocks import FakeCSQLInstance # type: ignore |
29 | 28 |
|
@@ -146,78 +145,79 @@ async def instance( |
146 | 145 | keys = asyncio.create_task(generate_keys()) |
147 | 146 | _, client_key = await keys |
148 | 147 |
|
149 | | - with patch("google.cloud.sql.connector.utils.default") as mock_auth: |
150 | | - mock_auth.return_value = fake_credentials, None |
151 | | - # mock Cloud SQL Admin API calls |
152 | | - with aioresponses() as mocked: |
153 | | - mocked.get( |
154 | | - f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{mock_instance.project}/instances/{mock_instance.name}/connectSettings", |
155 | | - status=200, |
156 | | - body=mock_instance.connect_settings(), |
157 | | - repeat=True, |
158 | | - ) |
159 | | - mocked.post( |
160 | | - f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{mock_instance.project}/instances/{mock_instance.name}:generateEphemeralCert", |
161 | | - status=200, |
162 | | - body=mock_instance.generate_ephemeral(client_key), |
163 | | - repeat=True, |
164 | | - ) |
165 | | - |
166 | | - instance = Instance( |
167 | | - f"{mock_instance.project}:{mock_instance.region}:{mock_instance.name}", |
168 | | - "pg8000", |
169 | | - keys, |
170 | | - loop, |
171 | | - ) |
172 | | - |
173 | | - yield instance |
174 | | - await instance.close() |
| 148 | + # mock Cloud SQL Admin API calls |
| 149 | + with aioresponses() as mocked: |
| 150 | + mocked.get( |
| 151 | + f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{mock_instance.project}/instances/{mock_instance.name}/connectSettings", |
| 152 | + status=200, |
| 153 | + body=mock_instance.connect_settings(), |
| 154 | + repeat=True, |
| 155 | + ) |
| 156 | + mocked.post( |
| 157 | + f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{mock_instance.project}/instances/{mock_instance.name}:generateEphemeralCert", |
| 158 | + status=200, |
| 159 | + body=mock_instance.generate_ephemeral(client_key), |
| 160 | + repeat=True, |
| 161 | + ) |
| 162 | + |
| 163 | + instance = Instance( |
| 164 | + f"{mock_instance.project}:{mock_instance.region}:{mock_instance.name}", |
| 165 | + "pg8000", |
| 166 | + keys, |
| 167 | + loop, |
| 168 | + fake_credentials, |
| 169 | + ) |
| 170 | + |
| 171 | + yield instance |
| 172 | + await instance.close() |
175 | 173 |
|
176 | 174 |
|
177 | 175 | @pytest.fixture |
178 | 176 | async def connector(fake_credentials: Credentials) -> AsyncGenerator[Connector, None]: |
179 | 177 | instance_connection_name = "my-project:my-region:my-instance" |
180 | 178 | project, region, instance_name = instance_connection_name.split(":") |
181 | 179 | # initialize connector |
182 | | - connector = Connector() |
183 | | - with patch("google.cloud.sql.connector.utils.default") as mock_auth: |
184 | | - mock_auth.return_value = fake_credentials, None |
185 | | - # mock Cloud SQL Admin API calls |
186 | | - mock_instance = FakeCSQLInstance(project, region, instance_name) |
187 | | - |
188 | | - async def wait_for_keys(future: asyncio.Future) -> Tuple[bytes, str]: |
189 | | - """ |
190 | | - Helper method to await keys of Connector in tests prior to |
191 | | - initializing an Instance object. |
192 | | - """ |
193 | | - return await future |
194 | | - |
195 | | - # converting asyncio.Future into concurrent.Future |
196 | | - # await keys in background thread so that .result() is set |
197 | | - # required because keys are needed for mocks, but are not awaited |
198 | | - # in the code until Instance() is initialized |
199 | | - _, client_key = asyncio.run_coroutine_threadsafe( |
200 | | - wait_for_keys(connector._keys), connector._loop |
201 | | - ).result() |
202 | | - with aioresponses() as mocked: |
203 | | - mocked.get( |
204 | | - f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance_name}/connectSettings", |
205 | | - status=200, |
206 | | - body=mock_instance.connect_settings(), |
207 | | - repeat=True, |
208 | | - ) |
209 | | - mocked.post( |
210 | | - f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance_name}:generateEphemeralCert", |
211 | | - status=200, |
212 | | - body=mock_instance.generate_ephemeral(client_key), |
213 | | - repeat=True, |
214 | | - ) |
215 | | - # initialize Instance using mocked API calls |
216 | | - instance = Instance( |
217 | | - instance_connection_name, "pg8000", connector._keys, connector._loop |
218 | | - ) |
219 | | - |
220 | | - connector._instances[instance_connection_name] = instance |
221 | | - |
222 | | - yield connector |
223 | | - connector.close() |
| 180 | + connector = Connector(credentials=fake_credentials) |
| 181 | + # mock Cloud SQL Admin API calls |
| 182 | + mock_instance = FakeCSQLInstance(project, region, instance_name) |
| 183 | + |
| 184 | + async def wait_for_keys(future: asyncio.Future) -> Tuple[bytes, str]: |
| 185 | + """ |
| 186 | + Helper method to await keys of Connector in tests prior to |
| 187 | + initializing an Instance object. |
| 188 | + """ |
| 189 | + return await future |
| 190 | + |
| 191 | + # converting asyncio.Future into concurrent.Future |
| 192 | + # await keys in background thread so that .result() is set |
| 193 | + # required because keys are needed for mocks, but are not awaited |
| 194 | + # in the code until Instance() is initialized |
| 195 | + _, client_key = asyncio.run_coroutine_threadsafe( |
| 196 | + wait_for_keys(connector._keys), connector._loop |
| 197 | + ).result() |
| 198 | + with aioresponses() as mocked: |
| 199 | + mocked.get( |
| 200 | + f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance_name}/connectSettings", |
| 201 | + status=200, |
| 202 | + body=mock_instance.connect_settings(), |
| 203 | + repeat=True, |
| 204 | + ) |
| 205 | + mocked.post( |
| 206 | + f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance_name}:generateEphemeralCert", |
| 207 | + status=200, |
| 208 | + body=mock_instance.generate_ephemeral(client_key), |
| 209 | + repeat=True, |
| 210 | + ) |
| 211 | + # initialize Instance using mocked API calls |
| 212 | + instance = Instance( |
| 213 | + instance_connection_name, |
| 214 | + "pg8000", |
| 215 | + connector._keys, |
| 216 | + connector._loop, |
| 217 | + fake_credentials, |
| 218 | + ) |
| 219 | + |
| 220 | + connector._instances[instance_connection_name] = instance |
| 221 | + |
| 222 | + yield connector |
| 223 | + connector.close() |
0 commit comments