Spaces:
Sleeping
Sleeping
File size: 20,684 Bytes
d8d14f1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 | from unittest.mock import MagicMock
import pytest
from pydantic import BaseModel
from swarms.tools.tool import (
BaseTool,
Runnable,
StructuredTool,
Tool,
tool,
)
# Define test data
test_input = {"key1": "value1", "key2": "value2"}
expected_output = "expected_output_value"
# Test with global variables
global_var = "global"
# Basic tests for BaseTool
def test_base_tool_init():
# Test BaseTool initialization
tool = BaseTool()
assert isinstance(tool, BaseTool)
def test_base_tool_invoke():
# Test BaseTool invoke method
tool = BaseTool()
result = tool.invoke(test_input)
assert result == expected_output
# Basic tests for Tool
def test_tool_init():
# Test Tool initialization
tool = Tool()
assert isinstance(tool, Tool)
def test_tool_invoke():
# Test Tool invoke method
tool = Tool()
result = tool.invoke(test_input)
assert result == expected_output
# Basic tests for StructuredTool
def test_structured_tool_init():
# Test StructuredTool initialization
tool = StructuredTool()
assert isinstance(tool, StructuredTool)
def test_structured_tool_invoke():
# Test StructuredTool invoke method
tool = StructuredTool()
result = tool.invoke(test_input)
assert result == expected_output
# Test additional functionality and edge cases as needed
def test_tool_creation():
tool = Tool(
name="test_tool", func=lambda x: x, description="Test tool"
)
assert tool.name == "test_tool"
assert tool.func is not None
assert tool.description == "Test tool"
def test_tool_ainvoke():
tool = Tool(
name="test_tool", func=lambda x: x, description="Test tool"
)
result = tool.ainvoke("input_data")
assert result == "input_data"
def test_tool_ainvoke_with_coroutine():
async def async_function(input_data):
return input_data
tool = Tool(
name="test_tool",
coroutine=async_function,
description="Test tool",
)
result = tool.ainvoke("input_data")
assert result == "input_data"
def test_tool_args():
def sample_function(input_data):
return input_data
tool = Tool(
name="test_tool",
func=sample_function,
description="Test tool",
)
assert tool.args == {"tool_input": {"type": "string"}}
# Basic tests for StructuredTool class
def test_structured_tool_creation():
class SampleArgsSchema:
pass
tool = StructuredTool(
name="test_tool",
func=lambda x: x,
description="Test tool",
args_schema=SampleArgsSchema,
)
assert tool.name == "test_tool"
assert tool.func is not None
assert tool.description == "Test tool"
assert tool.args_schema == SampleArgsSchema
def test_structured_tool_ainvoke():
class SampleArgsSchema:
pass
tool = StructuredTool(
name="test_tool",
func=lambda x: x,
description="Test tool",
args_schema=SampleArgsSchema,
)
result = tool.ainvoke({"tool_input": "input_data"})
assert result == "input_data"
def test_structured_tool_ainvoke_with_coroutine():
class SampleArgsSchema:
pass
async def async_function(input_data):
return input_data
tool = StructuredTool(
name="test_tool",
coroutine=async_function,
description="Test tool",
args_schema=SampleArgsSchema,
)
result = tool.ainvoke({"tool_input": "input_data"})
assert result == "input_data"
def test_structured_tool_args():
class SampleArgsSchema:
pass
def sample_function(input_data):
return input_data
tool = StructuredTool(
name="test_tool",
func=sample_function,
description="Test tool",
args_schema=SampleArgsSchema,
)
assert tool.args == {"tool_input": {"type": "string"}}
# Additional tests for exception handling
def test_tool_ainvoke_exception():
tool = Tool(name="test_tool", func=None, description="Test tool")
with pytest.raises(NotImplementedError):
tool.ainvoke("input_data")
def test_tool_ainvoke_with_coroutine_exception():
tool = Tool(
name="test_tool", coroutine=None, description="Test tool"
)
with pytest.raises(NotImplementedError):
tool.ainvoke("input_data")
def test_structured_tool_ainvoke_exception():
class SampleArgsSchema:
pass
tool = StructuredTool(
name="test_tool",
func=None,
description="Test tool",
args_schema=SampleArgsSchema,
)
with pytest.raises(NotImplementedError):
tool.ainvoke({"tool_input": "input_data"})
def test_structured_tool_ainvoke_with_coroutine_exception():
class SampleArgsSchema:
pass
tool = StructuredTool(
name="test_tool",
coroutine=None,
description="Test tool",
args_schema=SampleArgsSchema,
)
with pytest.raises(NotImplementedError):
tool.ainvoke({"tool_input": "input_data"})
def test_tool_description_not_provided():
tool = Tool(name="test_tool", func=lambda x: x)
assert tool.name == "test_tool"
assert tool.func is not None
assert tool.description == ""
def test_tool_invoke_with_callbacks():
def sample_function(input_data, callbacks=None):
if callbacks:
callbacks.on_start()
callbacks.on_finish()
return input_data
tool = Tool(name="test_tool", func=sample_function)
callbacks = MagicMock()
result = tool.invoke("input_data", callbacks=callbacks)
assert result == "input_data"
callbacks.on_start.assert_called_once()
callbacks.on_finish.assert_called_once()
def test_tool_invoke_with_new_argument():
def sample_function(input_data, callbacks=None):
return input_data
tool = Tool(name="test_tool", func=sample_function)
result = tool.invoke("input_data", callbacks=None)
assert result == "input_data"
def test_tool_ainvoke_with_new_argument():
async def async_function(input_data, callbacks=None):
return input_data
tool = Tool(name="test_tool", coroutine=async_function)
result = tool.ainvoke("input_data", callbacks=None)
assert result == "input_data"
def test_tool_description_from_docstring():
def sample_function(input_data):
"""Sample function docstring"""
return input_data
tool = Tool(name="test_tool", func=sample_function)
assert tool.description == "Sample function docstring"
def test_tool_ainvoke_with_exceptions():
async def async_function(input_data):
raise ValueError("Test exception")
tool = Tool(name="test_tool", coroutine=async_function)
with pytest.raises(ValueError):
tool.ainvoke("input_data")
# Additional tests for StructuredTool class
def test_structured_tool_infer_schema_false():
def sample_function(input_data):
return input_data
tool = StructuredTool(
name="test_tool",
func=sample_function,
args_schema=None,
infer_schema=False,
)
assert tool.args_schema is None
def test_structured_tool_ainvoke_with_callbacks():
class SampleArgsSchema:
pass
def sample_function(input_data, callbacks=None):
if callbacks:
callbacks.on_start()
callbacks.on_finish()
return input_data
tool = StructuredTool(
name="test_tool",
func=sample_function,
args_schema=SampleArgsSchema,
)
callbacks = MagicMock()
result = tool.ainvoke(
{"tool_input": "input_data"}, callbacks=callbacks
)
assert result == "input_data"
callbacks.on_start.assert_called_once()
callbacks.on_finish.assert_called_once()
def test_structured_tool_description_not_provided():
class SampleArgsSchema:
pass
tool = StructuredTool(
name="test_tool",
func=lambda x: x,
args_schema=SampleArgsSchema,
)
assert tool.name == "test_tool"
assert tool.func is not None
assert tool.description == ""
def test_structured_tool_args_schema():
class SampleArgsSchema:
pass
def sample_function(input_data):
return input_data
tool = StructuredTool(
name="test_tool",
func=sample_function,
args_schema=SampleArgsSchema,
)
assert tool.args_schema == SampleArgsSchema
def test_structured_tool_args_schema_inference():
def sample_function(input_data):
return input_data
tool = StructuredTool(
name="test_tool",
func=sample_function,
args_schema=None,
infer_schema=True,
)
assert tool.args_schema is not None
def test_structured_tool_ainvoke_with_new_argument():
class SampleArgsSchema:
pass
def sample_function(input_data, callbacks=None):
return input_data
tool = StructuredTool(
name="test_tool",
func=sample_function,
args_schema=SampleArgsSchema,
)
result = tool.ainvoke(
{"tool_input": "input_data"}, callbacks=None
)
assert result == "input_data"
def test_structured_tool_ainvoke_with_exceptions():
class SampleArgsSchema:
pass
async def async_function(input_data):
raise ValueError("Test exception")
tool = StructuredTool(
name="test_tool",
coroutine=async_function,
args_schema=SampleArgsSchema,
)
with pytest.raises(ValueError):
tool.ainvoke({"tool_input": "input_data"})
def test_base_tool_verbose_logging(caplog):
# Test verbose logging in BaseTool
tool = BaseTool(verbose=True)
result = tool.invoke(test_input)
assert result == expected_output
assert "Verbose logging" in caplog.text
def test_tool_exception_handling():
# Test exception handling in Tool
tool = Tool()
with pytest.raises(Exception):
tool.invoke(test_input, raise_exception=True)
def test_structured_tool_async_invoke():
# Test asynchronous invoke in StructuredTool
tool = StructuredTool()
result = tool.ainvoke(test_input)
assert result == expected_output
# Add more tests for specific functionalities and edge cases as needed
# Import necessary libraries and modules
# Example of a mock function to be used in testing
def mock_function(arg: str) -> str:
"""A simple mock function for testing."""
return f"Processed {arg}"
# Example of a Runnable class for testing
class MockRunnable(Runnable):
# Define necessary methods and properties
pass
# Fixture for creating a mock function
@pytest.fixture
def mock_func():
return mock_function
# Fixture for creating a Runnable instance
@pytest.fixture
def mock_runnable():
return MockRunnable()
# Basic functionality tests
def test_tool_with_callable(mock_func):
# Test creating a tool with a simple callable
tool_instance = tool(mock_func)
assert isinstance(tool_instance, BaseTool)
def test_tool_with_runnable(mock_runnable):
# Test creating a tool with a Runnable instance
tool_instance = tool(mock_runnable)
assert isinstance(tool_instance, BaseTool)
# ... more basic functionality tests ...
# Argument handling tests
def test_tool_with_invalid_argument():
# Test passing an invalid argument type
with pytest.raises(ValueError):
tool(
123
) # Using an integer instead of a string/callable/Runnable
def test_tool_with_multiple_arguments(mock_func):
# Test passing multiple valid arguments
tool_instance = tool("mock", mock_func)
assert isinstance(tool_instance, BaseTool)
# ... more argument handling tests ...
# Schema inference and application tests
class TestSchema(BaseModel):
arg: str
def test_tool_with_args_schema(mock_func):
# Test passing a custom args_schema
tool_instance = tool(mock_func, args_schema=TestSchema)
assert tool_instance.args_schema == TestSchema
# ... more schema tests ...
# Exception handling tests
def test_tool_function_without_docstring():
# Test that a ValueError is raised if the function lacks a docstring
def no_doc_func(arg: str) -> str:
return arg
with pytest.raises(ValueError):
tool(no_doc_func)
# Test suite starts here
class TestTool:
# Basic Functionality Tests
def test_tool_with_valid_callable_creates_base_tool(
self, mock_func
):
result = tool(mock_func)
assert isinstance(result, BaseTool)
def test_tool_returns_correct_function_name(self, mock_func):
result = tool(mock_func)
assert result.func.__name__ == "mock_function"
# Argument Handling Tests
def test_tool_with_string_and_runnable(self, mock_runnable):
result = tool("mock_runnable", mock_runnable)
assert isinstance(result, BaseTool)
def test_tool_raises_error_with_invalid_arguments(self):
with pytest.raises(ValueError):
tool(123)
def test_tool_with_infer_schema_true(self, mock_func):
tool(mock_func, infer_schema=True)
# Assertions related to schema inference
# Return Direct Feature Tests
def test_tool_with_return_direct_true(self, mock_func):
tool(mock_func, return_direct=True)
# Assertions for return_direct behavior
# Error Handling Tests
def test_tool_raises_error_without_docstring(self):
def no_doc_func(arg: str) -> str:
return arg
with pytest.raises(ValueError):
tool(no_doc_func)
def test_tool_raises_error_runnable_without_object_schema(
self, mock_runnable
):
with pytest.raises(ValueError):
tool(mock_runnable)
# Decorator Behavior Tests
@pytest.mark.asyncio
async def test_async_tool_function(self):
@tool
async def async_func(arg: str) -> str:
return arg
# Assertions for async behavior
# Integration with StructuredTool and Tool Classes
def test_integration_with_structured_tool(self, mock_func):
result = tool(mock_func)
assert isinstance(result, StructuredTool)
# Concurrency and Async Handling Tests
def test_concurrency_in_tool(self, mock_func):
# Test related to concurrency
pass
# Mocking and Isolation Tests
def test_mocking_external_dependencies(self, mocker):
# Use mocker to mock external dependencies
pass
def test_tool_with_different_return_types(self):
@tool
def return_int(arg: str) -> int:
return int(arg)
result = return_int("123")
assert isinstance(result, int)
assert result == 123
@tool
def return_bool(arg: str) -> bool:
return arg.lower() in ["true", "yes"]
result = return_bool("true")
assert isinstance(result, bool)
assert result is True
# Test with multiple arguments
def test_tool_with_multiple_args(self):
@tool
def concat_strings(a: str, b: str) -> str:
return a + b
result = concat_strings("Hello", "World")
assert result == "HelloWorld"
# Test handling of optional arguments
def test_tool_with_optional_args(self):
@tool
def greet(name: str, greeting: str = "Hello") -> str:
return f"{greeting} {name}"
assert greet("Alice") == "Hello Alice"
assert greet("Alice", greeting="Hi") == "Hi Alice"
# Test with variadic arguments
def test_tool_with_variadic_args(self):
@tool
def sum_numbers(*numbers: int) -> int:
return sum(numbers)
assert sum_numbers(1, 2, 3) == 6
assert sum_numbers(10, 20) == 30
# Test with keyword arguments
def test_tool_with_kwargs(self):
@tool
def build_query(**kwargs) -> str:
return "&".join(f"{k}={v}" for k, v in kwargs.items())
assert build_query(a=1, b=2) == "a=1&b=2"
assert build_query(foo="bar") == "foo=bar"
# Test with mixed types of arguments
def test_tool_with_mixed_args(self):
@tool
def mixed_args(a: int, b: str, *args, **kwargs) -> str:
return f"{a}{b}{len(args)}{'-'.join(kwargs.values())}"
assert mixed_args(1, "b", "c", "d", x="y", z="w") == "1b2y-w"
# Test error handling with incorrect types
def test_tool_error_with_incorrect_types(self):
@tool
def add_numbers(a: int, b: int) -> int:
return a + b
with pytest.raises(TypeError):
add_numbers("1", "2")
# Test with nested tools
def test_nested_tools(self):
@tool
def inner_tool(arg: str) -> str:
return f"Inner {arg}"
@tool
def outer_tool(arg: str) -> str:
return f"Outer {inner_tool(arg)}"
assert outer_tool("Test") == "Outer Inner Test"
def test_tool_with_global_variable(self):
@tool
def access_global(arg: str) -> str:
return f"{global_var} {arg}"
assert access_global("Var") == "global Var"
# Test with environment variables
def test_tool_with_env_variables(self, monkeypatch):
monkeypatch.setenv("TEST_VAR", "Environment")
@tool
def access_env_variable(arg: str) -> str:
import os
return f"{os.environ['TEST_VAR']} {arg}"
assert access_env_variable("Var") == "Environment Var"
# ... [Previous test cases] ...
# Test with complex data structures
def test_tool_with_complex_data_structures(self):
@tool
def process_data(data: dict) -> list:
return [data[key] for key in sorted(data.keys())]
result = process_data({"b": 2, "a": 1})
assert result == [1, 2]
# Test handling exceptions within the tool function
def test_tool_handling_internal_exceptions(self):
@tool
def function_that_raises(arg: str):
if arg == "error":
raise ValueError("Error occurred")
return arg
with pytest.raises(ValueError):
function_that_raises("error")
assert function_that_raises("ok") == "ok"
# Test with functions returning None
def test_tool_with_none_return(self):
@tool
def return_none(arg: str):
return None
assert return_none("anything") is None
# Test with lambda functions
def test_tool_with_lambda(self):
tool_lambda = tool(lambda x: x * 2)
assert tool_lambda(3) == 6
# Test with class methods
def test_tool_with_class_method(self):
class MyClass:
@tool
def method(self, arg: str) -> str:
return f"Method {arg}"
obj = MyClass()
assert obj.method("test") == "Method test"
# Test tool function with inheritance
def test_tool_with_inheritance(self):
class Parent:
@tool
def parent_method(self, arg: str) -> str:
return f"Parent {arg}"
class Child(Parent):
@tool
def child_method(self, arg: str) -> str:
return f"Child {arg}"
child_obj = Child()
assert child_obj.parent_method("test") == "Parent test"
assert child_obj.child_method("test") == "Child test"
# Test with decorators stacking
def test_tool_with_multiple_decorators(self):
def another_decorator(func):
def wrapper(*args, **kwargs):
return f"Decorated {func(*args, **kwargs)}"
return wrapper
@tool
@another_decorator
def decorated_function(arg: str):
return f"Function {arg}"
assert decorated_function("test") == "Decorated Function test"
# Test tool function when used in a multi-threaded environment
def test_tool_in_multithreaded_environment(self):
import threading
@tool
def threaded_function(arg: int) -> int:
return arg * 2
results = []
def thread_target():
results.append(threaded_function(5))
threads = [
threading.Thread(target=thread_target) for _ in range(10)
]
for t in threads:
t.start()
for t in threads:
t.join()
assert results == [10] * 10
# Test with recursive functions
def test_tool_with_recursive_function(self):
@tool
def recursive_function(n: int) -> int:
if n == 0:
return 0
else:
return n + recursive_function(n - 1)
assert recursive_function(5) == 15
# Additional tests can be added here to cover more scenarios
|