|
| 1 | +# Copyright 2026 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 | +from unittest import mock |
| 16 | + |
| 17 | +import bigquery_query_manifest_external_table |
| 18 | +from google.api_core import exceptions |
| 19 | +import pytest |
| 20 | + |
| 21 | + |
| 22 | +def test_query_manifest_external_table_success( |
| 23 | + monkeypatch: pytest.MonkeyPatch, |
| 24 | + capsys: pytest.CaptureFixture[str], |
| 25 | +) -> None: |
| 26 | + mock_client = mock.MagicMock() |
| 27 | + mock_job = mock.MagicMock() |
| 28 | + mock_job.job_id = "example-job-123" |
| 29 | + mock_job.statement_type = "SCRIPT" |
| 30 | + mock_row = mock.MagicMock() |
| 31 | + mock_job.result.return_value = [mock_row] |
| 32 | + mock_client.query.return_value = mock_job |
| 33 | + monkeypatch.setattr( |
| 34 | + bigquery_query_manifest_external_table.bigquery, |
| 35 | + "Client", |
| 36 | + lambda project=None: mock_client, |
| 37 | + ) |
| 38 | + |
| 39 | + bigquery_query_manifest_external_table.query_manifest_external_table( |
| 40 | + project_id="example-project", |
| 41 | + dataset_id="example_dataset", |
| 42 | + table_id="example_manifest_table", |
| 43 | + manifest_uri="gs://example-bucket/manifests/symlink.manifest", |
| 44 | + ) |
| 45 | + |
| 46 | + captured = capsys.readouterr() |
| 47 | + assert "Resource ID:" in captured.out |
| 48 | + assert "example-project" in captured.out |
| 49 | + assert mock_client.query.called |
| 50 | + |
| 51 | + |
| 52 | +def test_query_manifest_external_table_error( |
| 53 | + monkeypatch: pytest.MonkeyPatch, |
| 54 | + capsys: pytest.CaptureFixture[str], |
| 55 | +) -> None: |
| 56 | + mock_client = mock.MagicMock() |
| 57 | + mock_client.query.side_effect = exceptions.NotFound("resource error") |
| 58 | + monkeypatch.setattr( |
| 59 | + bigquery_query_manifest_external_table.bigquery, |
| 60 | + "Client", |
| 61 | + lambda project=None: mock_client, |
| 62 | + ) |
| 63 | + |
| 64 | + bigquery_query_manifest_external_table.query_manifest_external_table( |
| 65 | + project_id="example-project", |
| 66 | + dataset_id="example_dataset", |
| 67 | + table_id="example_manifest_table", |
| 68 | + manifest_uri="gs://example-bucket/manifests/symlink.manifest", |
| 69 | + ) |
| 70 | + |
| 71 | + captured = capsys.readouterr() |
| 72 | + assert "error:" in captured.err |
| 73 | + assert "example-project" in captured.err |
0 commit comments