|
| 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 | + |
| 16 | +from unittest.mock import AsyncMock, Mock |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from toolbox_core.tool import create_docstring, resolve_value |
| 21 | + |
| 22 | + |
| 23 | +def test_create_docstring_no_params(): |
| 24 | + """ |
| 25 | + Tests create_docstring when the params list is empty. |
| 26 | + """ |
| 27 | + description = "This is a tool description." |
| 28 | + params = [] |
| 29 | + |
| 30 | + result_docstring = create_docstring(description, params) |
| 31 | + |
| 32 | + assert result_docstring == description |
| 33 | + assert "\n\nArgs:" not in result_docstring |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.asyncio |
| 37 | +@pytest.mark.parametrize( |
| 38 | + "non_callable_source", |
| 39 | + [ |
| 40 | + "a simple string", |
| 41 | + 12345, |
| 42 | + True, |
| 43 | + False, |
| 44 | + None, |
| 45 | + [1, "two", 3.0], |
| 46 | + {"key": "value", "number": 100}, |
| 47 | + object(), |
| 48 | + ], |
| 49 | + ids=[ |
| 50 | + "string", |
| 51 | + "integer", |
| 52 | + "bool_true", |
| 53 | + "bool_false", |
| 54 | + "none", |
| 55 | + "list", |
| 56 | + "dict", |
| 57 | + "object", |
| 58 | + ], |
| 59 | +) |
| 60 | +async def test_resolve_value_non_callable(non_callable_source): |
| 61 | + """ |
| 62 | + Tests resolve_value when the source is not callable. |
| 63 | + """ |
| 64 | + resolved = await resolve_value(non_callable_source) |
| 65 | + |
| 66 | + assert resolved is non_callable_source |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.asyncio |
| 70 | +async def test_resolve_value_sync_callable(): |
| 71 | + """ |
| 72 | + Tests resolve_value with a synchronous callable. |
| 73 | + """ |
| 74 | + expected_value = "sync result" |
| 75 | + sync_callable = Mock(return_value=expected_value) |
| 76 | + |
| 77 | + # Call the async function |
| 78 | + resolved = await resolve_value(sync_callable) |
| 79 | + |
| 80 | + sync_callable.assert_called_once() |
| 81 | + assert resolved == expected_value |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.asyncio |
| 85 | +async def test_resolve_value_async_callable(): |
| 86 | + """ |
| 87 | + Tests resolve_value with an asynchronous callable (coroutine function). |
| 88 | + """ |
| 89 | + expected_value = "async result" |
| 90 | + async_callable = AsyncMock(return_value=expected_value) |
| 91 | + |
| 92 | + resolved = await resolve_value(async_callable) |
| 93 | + |
| 94 | + async_callable.assert_awaited_once() |
| 95 | + assert resolved == expected_value |
0 commit comments