|
| 1 | +// Copyright 2023 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 | +using Google.Apis.Bigquery.v2.Data; |
| 16 | +using Xunit; |
| 17 | + |
| 18 | +namespace Google.Cloud.BigQuery.V2.IntegrationTests; |
| 19 | + |
| 20 | +[Collection(nameof(BigQueryFixture))] |
| 21 | +public class SnapshotCloneTest |
| 22 | +{ |
| 23 | + private readonly BigQueryFixture _fixture; |
| 24 | + public SnapshotCloneTest(BigQueryFixture fixture) |
| 25 | + { |
| 26 | + _fixture = fixture; |
| 27 | + } |
| 28 | + |
| 29 | + private TableReference GetTableReference(string datasetId, string tableId) => |
| 30 | + new TableReference { ProjectId = _fixture.ProjectId, DatasetId = datasetId, TableId = tableId }; |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void CreateTableSnapshot() |
| 34 | + { |
| 35 | + var client = BigQueryClient.Create(_fixture.ProjectId); |
| 36 | + var datasetId = _fixture.DatasetId; |
| 37 | + var tableId = _fixture.PeopleTableId; |
| 38 | + |
| 39 | + var sourceTableReference = GetTableReference(datasetId, tableId); |
| 40 | + var destinationTableReference = GetTableReference(datasetId, tableId + "SnapshotTable"); |
| 41 | + var jobOptions = new CreateCopyJobOptions |
| 42 | + { |
| 43 | + OperationType = CopyOperationType.Snapshot |
| 44 | + }; |
| 45 | + var job = client.CreateCopyJob(sourceTableReference, destinationTableReference, jobOptions).PollUntilCompleted(); |
| 46 | + Assert.Null(job.Status.ErrorResult); |
| 47 | + |
| 48 | + var destTable = client.GetTable(_fixture.DatasetId, destinationTableReference.TableId); |
| 49 | + Assert.NotNull(destTable.Resource.SnapshotDefinition); |
| 50 | + |
| 51 | + var baseTableReference = destTable.Resource.SnapshotDefinition.BaseTableReference; |
| 52 | + Assert.Equal(sourceTableReference.TableId, baseTableReference.TableId); |
| 53 | + Assert.Equal(sourceTableReference.ProjectId, baseTableReference.ProjectId); |
| 54 | + Assert.Equal(sourceTableReference.DatasetId, baseTableReference.DatasetId); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void CreateTableClone() |
| 59 | + { |
| 60 | + var client = BigQueryClient.Create(_fixture.ProjectId); |
| 61 | + var datasetId = _fixture.DatasetId; |
| 62 | + var tableId = _fixture.PeopleTableId; |
| 63 | + |
| 64 | + var sourceTableReference = GetTableReference(datasetId, tableId); |
| 65 | + var destinationTableReference = GetTableReference(datasetId, tableId + "CloneTable"); |
| 66 | + var jobOptions = new CreateCopyJobOptions |
| 67 | + { |
| 68 | + OperationType = CopyOperationType.Clone |
| 69 | + }; |
| 70 | + var job = client.CreateCopyJob(sourceTableReference, destinationTableReference, jobOptions).PollUntilCompleted(); |
| 71 | + Assert.Null(job.Status.ErrorResult); |
| 72 | + |
| 73 | + var destTable = client.GetTable(_fixture.DatasetId, destinationTableReference.TableId); |
| 74 | + Assert.NotNull(destTable.Resource.CloneDefinition); |
| 75 | + |
| 76 | + var baseTableReference = destTable.Resource.CloneDefinition.BaseTableReference; |
| 77 | + Assert.Equal(sourceTableReference.TableId, baseTableReference.TableId); |
| 78 | + Assert.Equal(sourceTableReference.ProjectId, baseTableReference.ProjectId); |
| 79 | + Assert.Equal(sourceTableReference.DatasetId, baseTableReference.DatasetId); |
| 80 | + } |
| 81 | +} |
0 commit comments