|
| 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 dataplex_get_data_profile_scan |
| 18 | +from google.api_core import exceptions |
| 19 | +import pytest |
| 20 | + |
| 21 | + |
| 22 | +def test_get_data_profile_scan_success( |
| 23 | + monkeypatch: pytest.MonkeyPatch, |
| 24 | + capsys: pytest.CaptureFixture[str], |
| 25 | +) -> None: |
| 26 | + mock_client = mock.MagicMock() |
| 27 | + mock_scan = mock.MagicMock() |
| 28 | + mock_scan.name = ( |
| 29 | + "projects/example-project/locations/us-central1/dataScans/example-profile-scan" |
| 30 | + ) |
| 31 | + mock_scan.data.resource = "//bigquery.googleapis.com/projects/bigquery-public-data/datasets/samples/tables/shakespeare" |
| 32 | + mock_scan.data_profile_result.row_count = 164656 |
| 33 | + mock_scan.state.name = "ACTIVE" |
| 34 | + mock_client.get_data_scan.return_value = mock_scan |
| 35 | + monkeypatch.setattr( |
| 36 | + dataplex_get_data_profile_scan.dataplex_v1, |
| 37 | + "DataScanServiceClient", |
| 38 | + lambda: mock_client, |
| 39 | + ) |
| 40 | + |
| 41 | + dataplex_get_data_profile_scan.get_data_profile_scan( |
| 42 | + project_id="example-project", |
| 43 | + location="us-central1", |
| 44 | + data_scan_id="example-profile-scan", |
| 45 | + ) |
| 46 | + |
| 47 | + captured = capsys.readouterr() |
| 48 | + assert ( |
| 49 | + "Resource ID: projects/example-project/locations/us-central1/dataScans/example-profile-scan" |
| 50 | + in captured.out |
| 51 | + ) |
| 52 | + assert "Row Count: 164656" in captured.out |
| 53 | + assert "Scan State: ACTIVE" in captured.out |
| 54 | + |
| 55 | + |
| 56 | +def test_get_data_profile_scan_error( |
| 57 | + monkeypatch: pytest.MonkeyPatch, |
| 58 | + capsys: pytest.CaptureFixture[str], |
| 59 | +) -> None: |
| 60 | + mock_client = mock.MagicMock() |
| 61 | + mock_client.get_data_scan.side_effect = exceptions.NotFound("scan not found") |
| 62 | + monkeypatch.setattr( |
| 63 | + dataplex_get_data_profile_scan.dataplex_v1, |
| 64 | + "DataScanServiceClient", |
| 65 | + lambda: mock_client, |
| 66 | + ) |
| 67 | + |
| 68 | + dataplex_get_data_profile_scan.get_data_profile_scan( |
| 69 | + project_id="example-project", |
| 70 | + location="us-central1", |
| 71 | + data_scan_id="example-profile-scan", |
| 72 | + ) |
| 73 | + |
| 74 | + captured = capsys.readouterr() |
| 75 | + assert "error:" in captured.err |
| 76 | + assert "example-profile-scan" in captured.err |
0 commit comments