|
| 1 | +# Copyright 2019 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 | + |
| 16 | +import datetime |
| 17 | +import uuid |
| 18 | + |
| 19 | +import google.auth |
| 20 | +from google.cloud import bigquery |
| 21 | +from google.cloud import datacatalog_v1 |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | + |
| 26 | +def temp_suffix(): |
| 27 | + now = datetime.datetime.now() |
| 28 | + return "{}_{}".format( |
| 29 | + now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="session") |
| 34 | +def client(credentials): |
| 35 | + return datacatalog_v1.DataCatalogClient(credentials=credentials) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture(scope="session") |
| 39 | +def bigquery_client(credentials, project_id): |
| 40 | + return bigquery.Client(project=project_id, credentials=credentials) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture(scope="session") |
| 44 | +def default_credentials(): |
| 45 | + return google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"]) |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture(scope="session") |
| 49 | +def credentials(default_credentials): |
| 50 | + return default_credentials[0] |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture(scope="session") |
| 54 | +def project_id(default_credentials): |
| 55 | + return default_credentials[1] |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture |
| 59 | +def dataset_id(bigquery_client): |
| 60 | + dataset_id = f"python_data_catalog_sample_{temp_suffix()}" |
| 61 | + dataset = bigquery_client.create_dataset(dataset_id) |
| 62 | + yield dataset.dataset_id |
| 63 | + bigquery_client.delete_dataset(dataset, delete_contents=True, not_found_ok=True) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture |
| 67 | +def table_id(bigquery_client, project_id, dataset_id): |
| 68 | + table_id = f"python_data_catalog_sample_{temp_suffix()}" |
| 69 | + table = bigquery.Table("{}.{}.{}".format(project_id, dataset_id, table_id)) |
| 70 | + table = bigquery_client.create_table(table) |
| 71 | + yield table.table_id |
| 72 | + bigquery_client.delete_table(table, not_found_ok=True) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.fixture |
| 76 | +def random_tag_template_id(): |
| 77 | + random_tag_template_id = f"python_sample_{temp_suffix()}" |
| 78 | + yield random_tag_template_id |
0 commit comments