|
1 |
| -""" |
2 |
| -Copyright 2021 Google LLC |
3 |
| -
|
4 |
| -Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| -you may not use this file except in compliance with the License. |
6 |
| -You may obtain a copy of the License at |
7 |
| -
|
8 |
| - https://www.apache.org/licenses/LICENSE-2.0 |
9 |
| -
|
10 |
| -Unless required by applicable law or agreed to in writing, software |
11 |
| -distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| -See the License for the specific language governing permissions and |
14 |
| -limitations under the License. |
15 |
| -""" |
16 |
| - |
17 | 1 | import asyncio
|
18 | 2 | import os
|
19 | 3 | from typing import Union
|
@@ -468,3 +452,42 @@ def test_configured_quota_project_env_var(
|
468 | 452 | assert connector._quota_project == quota_project
|
469 | 453 | # unset env var
|
470 | 454 | del os.environ["GOOGLE_CLOUD_QUOTA_PROJECT"]
|
| 455 | + |
| 456 | + |
| 457 | +@pytest.mark.asyncio |
| 458 | +async def test_connect_async_closed_connector( |
| 459 | + fake_credentials: Credentials, fake_client: CloudSQLClient |
| 460 | +) -> None: |
| 461 | + """Test that calling connect_async() on a closed connector raises an error.""" |
| 462 | + async with Connector( |
| 463 | + credentials=fake_credentials, loop=asyncio.get_running_loop() |
| 464 | + ) as connector: |
| 465 | + connector._client = fake_client |
| 466 | + await connector.close_async() |
| 467 | + with pytest.raises(RuntimeError) as exc_info: |
| 468 | + await connector.connect_async( |
| 469 | + "test-project:test-region:test-instance", |
| 470 | + "asyncpg", |
| 471 | + user="my-user", |
| 472 | + password="my-pass", |
| 473 | + db="my-db", |
| 474 | + ) |
| 475 | + assert exc_info.value.args[0] == "Cannot connect using a closed Connector." |
| 476 | + |
| 477 | + |
| 478 | +def test_connect_closed_connector( |
| 479 | + fake_credentials: Credentials, fake_client: CloudSQLClient |
| 480 | +) -> None: |
| 481 | + """Test that calling connect() on a closed connector raises an error.""" |
| 482 | + with Connector(credentials=fake_credentials) as connector: |
| 483 | + connector._client = fake_client |
| 484 | + connector.close() |
| 485 | + with pytest.raises(RuntimeError) as exc_info: |
| 486 | + connector.connect( |
| 487 | + "test-project:test-region:test-instance", |
| 488 | + "pg8000", |
| 489 | + user="my-user", |
| 490 | + password="my-pass", |
| 491 | + db="my-db", |
| 492 | + ) |
| 493 | + assert exc_info.value.args[0] == "Cannot connect using a closed Connector." |
0 commit comments