|
| 1 | +# Copyright 2021 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 | +# https://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 google.api_core.exceptions |
| 16 | +from google.cloud import bigquery_connection_v1 as bq_connection |
| 17 | +from google.cloud.bigquery_connection_v1.services import connection_service |
| 18 | +import pytest |
| 19 | +import test_utils.prefixer |
| 20 | + |
| 21 | +from . import create_mysql_connection |
| 22 | + |
| 23 | +connection_prefixer = test_utils.prefixer.Prefixer("py-bq-r", "snippets", separator="-") |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(scope="session") |
| 27 | +def location_path( |
| 28 | + connection_client: connection_service.ConnectionServiceClient(), |
| 29 | + project_id: str, |
| 30 | + location: str, |
| 31 | +) -> str: |
| 32 | + return connection_client.common_location_path(project_id, location) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture(scope="module", autouse=True) |
| 36 | +def cleanup_connection( |
| 37 | + connection_client: connection_service.ConnectionServiceClient, location_path: str |
| 38 | +) -> None: |
| 39 | + for connection in connection_client.list_connections(parent=location_path): |
| 40 | + connection_id = connection.name.split("/")[-1] |
| 41 | + if connection_prefixer.should_cleanup(connection_id): |
| 42 | + connection_client.delete_connection(name=connection.name) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture(scope="session") |
| 46 | +def connection_id( |
| 47 | + connection_client: connection_service.ConnectionServiceClient, |
| 48 | + project_id: str, |
| 49 | + location: str, |
| 50 | +) -> str: |
| 51 | + id_ = connection_prefixer.create_prefix() |
| 52 | + yield id_ |
| 53 | + |
| 54 | + connection_name = connection_client.connection_path(project_id, location, id_) |
| 55 | + try: |
| 56 | + connection_client.delete_connection(name=connection_name) |
| 57 | + except google.api_core.exceptions.NotFound: |
| 58 | + pass |
| 59 | + |
| 60 | + |
| 61 | +def test_create_mysql_connection( |
| 62 | + capsys: pytest.CaptureFixture, |
| 63 | + mysql_username: str, |
| 64 | + mysql_password: str, |
| 65 | + database: str, |
| 66 | + cloud_sql_conn_name: str, |
| 67 | + project_id: str, |
| 68 | + location: str, |
| 69 | +) -> None: |
| 70 | + cloud_sql_credential = bq_connection.CloudSqlCredential( |
| 71 | + { |
| 72 | + "username": mysql_username, |
| 73 | + "password": mysql_password, |
| 74 | + } |
| 75 | + ) |
| 76 | + cloud_sql_properties = bq_connection.CloudSqlProperties( |
| 77 | + { |
| 78 | + "type_": bq_connection.CloudSqlProperties.DatabaseType.MYSQL, |
| 79 | + "database": database, |
| 80 | + "instance_id": cloud_sql_conn_name, |
| 81 | + "credential": cloud_sql_credential, |
| 82 | + } |
| 83 | + ) |
| 84 | + create_mysql_connection.create_mysql_connection( |
| 85 | + project_id=project_id, |
| 86 | + location=location, |
| 87 | + cloud_sql_properties=cloud_sql_properties, |
| 88 | + ) |
| 89 | + out, _ = capsys.readouterr() |
| 90 | + assert "Created connection successfully:" in out |
0 commit comments