|
| 1 | +# Copyright 2020 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 | +# 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 datetime |
| 16 | +import uuid |
| 17 | + |
| 18 | +import google.api_core.exceptions |
| 19 | +import google.auth |
| 20 | +from google.cloud import bigquery |
| 21 | +from google.cloud import bigquery_datatransfer |
| 22 | +import pytest |
| 23 | + |
| 24 | +from . import copy_dataset |
| 25 | + |
| 26 | + |
| 27 | +def temp_suffix(): |
| 28 | + now = datetime.datetime.now() |
| 29 | + return f"{now.strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}" |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(scope="session") |
| 33 | +def default_credentials(): |
| 34 | + return google.auth.default(["https://www.googleapis.com/auth/cloud-platform"]) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture(scope="session") |
| 38 | +def project_id(default_credentials): |
| 39 | + _, project_id = default_credentials |
| 40 | + return project_id |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture(scope="session") |
| 44 | +def bigquery_client(default_credentials): |
| 45 | + credentials, project_id = default_credentials |
| 46 | + return bigquery.Client(credentials=credentials, project=project_id) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture(scope="session") |
| 50 | +def transfer_client(default_credentials): |
| 51 | + credentials, _ = default_credentials |
| 52 | + return bigquery_datatransfer.DataTransferServiceClient(credentials=credentials) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def to_delete_configs(transfer_client): |
| 57 | + to_delete = [] |
| 58 | + yield to_delete |
| 59 | + for config_name in to_delete: |
| 60 | + try: |
| 61 | + transfer_client.delete_transfer_config(name=config_name) |
| 62 | + except google.api_core.exceptions.GoogleAPICallError: |
| 63 | + pass |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture(scope="module") |
| 67 | +def destination_dataset_id(bigquery_client, project_id): |
| 68 | + dataset_id = f"bqdts_dest_{temp_suffix()}" |
| 69 | + bigquery_client.create_dataset(f"{project_id}.{dataset_id}") |
| 70 | + yield dataset_id |
| 71 | + bigquery_client.delete_dataset(dataset_id, delete_contents=True) |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture(scope="module") |
| 75 | +def source_dataset_id(bigquery_client, project_id): |
| 76 | + dataset_id = f"bqdts_src_{temp_suffix()}" |
| 77 | + bigquery_client.create_dataset(f"{project_id}.{dataset_id}") |
| 78 | + yield dataset_id |
| 79 | + bigquery_client.delete_dataset(dataset_id, delete_contents=True) |
| 80 | + |
| 81 | + |
| 82 | +def test_copy_dataset( |
| 83 | + capsys, project_id, destination_dataset_id, source_dataset_id, to_delete_configs |
| 84 | +): |
| 85 | + transfer_config = copy_dataset.copy_dataset( |
| 86 | + { |
| 87 | + "destination_project_id": project_id, |
| 88 | + "destination_dataset_id": destination_dataset_id, |
| 89 | + "source_project_id": project_id, |
| 90 | + "source_dataset_id": source_dataset_id, |
| 91 | + } |
| 92 | + ) |
| 93 | + to_delete_configs.append(transfer_config.name) |
| 94 | + out, _ = capsys.readouterr() |
| 95 | + assert transfer_config.name in out |
0 commit comments