|
| 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 google.adk.features import FeatureName |
| 16 | +from google.adk.features._feature_registry import temporary_feature_override |
| 17 | +from google.adk.tools.retrieval.base_retrieval_tool import BaseRetrievalTool |
| 18 | +from google.genai import types |
| 19 | + |
| 20 | + |
| 21 | +class _TestRetrievalTool(BaseRetrievalTool): |
| 22 | + """Concrete implementation of BaseRetrievalTool for testing.""" |
| 23 | + |
| 24 | + def __init__(self): |
| 25 | + super().__init__( |
| 26 | + name='test_retrieval', |
| 27 | + description='A test retrieval tool.', |
| 28 | + ) |
| 29 | + |
| 30 | + async def run_async(self, *, args, tool_context): |
| 31 | + return {'result': 'test'} |
| 32 | + |
| 33 | + |
| 34 | +def test_get_declaration_with_json_schema_feature_disabled(): |
| 35 | + """Test that _get_declaration uses parameters when feature is disabled.""" |
| 36 | + tool = _TestRetrievalTool() |
| 37 | + |
| 38 | + with temporary_feature_override(FeatureName.JSON_SCHEMA_FOR_FUNC_DECL, False): |
| 39 | + declaration = tool._get_declaration() |
| 40 | + |
| 41 | + assert declaration.name == 'test_retrieval' |
| 42 | + assert declaration.description == 'A test retrieval tool.' |
| 43 | + assert declaration.parameters_json_schema is None |
| 44 | + assert isinstance(declaration.parameters, types.Schema) |
| 45 | + assert declaration.parameters.type == types.Type.OBJECT |
| 46 | + assert 'query' in declaration.parameters.properties |
| 47 | + |
| 48 | + |
| 49 | +def test_get_declaration_with_json_schema_feature_enabled(): |
| 50 | + """Test that _get_declaration uses parameters_json_schema when feature is enabled.""" |
| 51 | + tool = _TestRetrievalTool() |
| 52 | + |
| 53 | + with temporary_feature_override(FeatureName.JSON_SCHEMA_FOR_FUNC_DECL, True): |
| 54 | + declaration = tool._get_declaration() |
| 55 | + |
| 56 | + assert declaration.name == 'test_retrieval' |
| 57 | + assert declaration.description == 'A test retrieval tool.' |
| 58 | + assert declaration.parameters is None |
| 59 | + assert declaration.parameters_json_schema == { |
| 60 | + 'type': 'object', |
| 61 | + 'properties': { |
| 62 | + 'query': { |
| 63 | + 'type': 'string', |
| 64 | + 'description': 'The query to retrieve.', |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
0 commit comments