|
| 1 | +# Copyright 2025 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 | +from __future__ import annotations |
| 16 | + |
| 17 | +import os |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +from google.adk.tools.bigquery import metadata_tool |
| 21 | +from google.auth.exceptions import DefaultCredentialsError |
| 22 | +from google.cloud import bigquery |
| 23 | +from google.oauth2.credentials import Credentials |
| 24 | +import pytest |
| 25 | + |
| 26 | + |
| 27 | +@mock.patch.dict(os.environ, {}, clear=True) |
| 28 | +@mock.patch("google.cloud.bigquery.Client.list_datasets", autospec=True) |
| 29 | +@mock.patch("google.auth.default", autospec=True) |
| 30 | +def test_list_dataset_ids(mock_default_auth, mock_list_datasets): |
| 31 | + """Test list_dataset_ids tool invocation.""" |
| 32 | + project = "my_project_id" |
| 33 | + mock_credentials = mock.create_autospec(Credentials, instance=True) |
| 34 | + |
| 35 | + # Simulate the behavior of default auth - on purpose throw exception when |
| 36 | + # the default auth is called |
| 37 | + mock_default_auth.side_effect = DefaultCredentialsError( |
| 38 | + "Your default credentials were not found" |
| 39 | + ) |
| 40 | + |
| 41 | + mock_list_datasets.return_value = [ |
| 42 | + bigquery.DatasetReference(project, "dataset1"), |
| 43 | + bigquery.DatasetReference(project, "dataset2"), |
| 44 | + ] |
| 45 | + result = metadata_tool.list_dataset_ids(project, mock_credentials) |
| 46 | + assert result == ["dataset1", "dataset2"] |
| 47 | + mock_default_auth.assert_not_called() |
| 48 | + |
| 49 | + |
| 50 | +@mock.patch.dict(os.environ, {}, clear=True) |
| 51 | +@mock.patch("google.cloud.bigquery.Client.get_dataset", autospec=True) |
| 52 | +@mock.patch("google.auth.default", autospec=True) |
| 53 | +def test_get_dataset_info(mock_default_auth, mock_get_dataset): |
| 54 | + """Test get_dataset_info tool invocation.""" |
| 55 | + mock_credentials = mock.create_autospec(Credentials, instance=True) |
| 56 | + |
| 57 | + # Simulate the behavior of default auth - on purpose throw exception when |
| 58 | + # the default auth is called |
| 59 | + mock_default_auth.side_effect = DefaultCredentialsError( |
| 60 | + "Your default credentials were not found" |
| 61 | + ) |
| 62 | + |
| 63 | + mock_get_dataset.return_value = mock.create_autospec( |
| 64 | + Credentials, instance=True |
| 65 | + ) |
| 66 | + result = metadata_tool.get_dataset_info( |
| 67 | + "my_project_id", "my_dataset_id", mock_credentials |
| 68 | + ) |
| 69 | + assert result != { |
| 70 | + "status": "ERROR", |
| 71 | + "error_details": "Your default credentials were not found", |
| 72 | + } |
| 73 | + mock_default_auth.assert_not_called() |
| 74 | + |
| 75 | + |
| 76 | +@mock.patch.dict(os.environ, {}, clear=True) |
| 77 | +@mock.patch("google.cloud.bigquery.Client.list_tables", autospec=True) |
| 78 | +@mock.patch("google.auth.default", autospec=True) |
| 79 | +def test_list_table_ids(mock_default_auth, mock_list_tables): |
| 80 | + """Test list_table_ids tool invocation.""" |
| 81 | + project = "my_project_id" |
| 82 | + dataset = "my_dataset_id" |
| 83 | + dataset_ref = bigquery.DatasetReference(project, dataset) |
| 84 | + mock_credentials = mock.create_autospec(Credentials, instance=True) |
| 85 | + |
| 86 | + # Simulate the behavior of default auth - on purpose throw exception when |
| 87 | + # the default auth is called |
| 88 | + mock_default_auth.side_effect = DefaultCredentialsError( |
| 89 | + "Your default credentials were not found" |
| 90 | + ) |
| 91 | + |
| 92 | + mock_list_tables.return_value = [ |
| 93 | + bigquery.TableReference(dataset_ref, "table1"), |
| 94 | + bigquery.TableReference(dataset_ref, "table2"), |
| 95 | + ] |
| 96 | + result = metadata_tool.list_table_ids(project, dataset, mock_credentials) |
| 97 | + assert result == ["table1", "table2"] |
| 98 | + mock_default_auth.assert_not_called() |
| 99 | + |
| 100 | + |
| 101 | +@mock.patch.dict(os.environ, {}, clear=True) |
| 102 | +@mock.patch("google.cloud.bigquery.Client.get_table", autospec=True) |
| 103 | +@mock.patch("google.auth.default", autospec=True) |
| 104 | +def test_get_table_info(mock_default_auth, mock_get_table): |
| 105 | + """Test get_table_info tool invocation.""" |
| 106 | + mock_credentials = mock.create_autospec(Credentials, instance=True) |
| 107 | + |
| 108 | + # Simulate the behavior of default auth - on purpose throw exception when |
| 109 | + # the default auth is called |
| 110 | + mock_default_auth.side_effect = DefaultCredentialsError( |
| 111 | + "Your default credentials were not found" |
| 112 | + ) |
| 113 | + |
| 114 | + mock_get_table.return_value = mock.create_autospec(Credentials, instance=True) |
| 115 | + result = metadata_tool.get_table_info( |
| 116 | + "my_project_id", "my_dataset_id", "my_table_id", mock_credentials |
| 117 | + ) |
| 118 | + assert result != { |
| 119 | + "status": "ERROR", |
| 120 | + "error_details": "Your default credentials were not found", |
| 121 | + } |
| 122 | + mock_default_auth.assert_not_called() |
0 commit comments