Skip to content

Building AI agents, atomically

more coverage!

KennyVaneetveldec4bbb96e68d8
diff --git a/.coveragerc b/.coveragerc
index fb8da48..5727e65 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -2,6 +2,8 @@
exclude_lines =
@abstractmethod
@abc.abstractmethod
+exclude_also =
+ if __name__ == .__main__.:
[run]
omit = */__init__.py
\ No newline at end of file
diff --git a/atomic_agents/lib/tools/calculator_tool.py b/atomic_agents/lib/tools/calculator_tool.py
index 9843b06..cdf3c4d 100644
--- a/atomic_agents/lib/tools/calculator_tool.py
+++ b/atomic_agents/lib/tools/calculator_tool.py
@@ -1,6 +1,6 @@
from pydantic import Field
from rich.console import Console
-from sympy import sympify
+from sympy import SympifyError, sympify
from atomic_agents.agents.base_chat_agent import BaseAgentIO
from atomic_agents.lib.tools.base import BaseTool, BaseToolConfig
@@ -70,18 +70,12 @@ class CalculatorTool(BaseTool):
Returns:
CalculatorToolOutputSchema: The output of the tool, adhering to the output schema.
-
- Raises:
- ValueError: If there is an error evaluating the expression.
"""
- try:
- # Explicitly convert the string form of the expression
- parsed_expr = sympify(str(params.expression))
- # Evaluate the expression numerically
- result = parsed_expr.evalf()
- return CalculatorToolOutputSchema(result=str(result))
- except Exception as e:
- raise ValueError(f"Error evaluating expression: {e}")
+ # Explicitly convert the string form of the expression
+ parsed_expr = sympify(str(params.expression))
+ # Evaluate the expression numerically
+ result = parsed_expr.evalf()
+ return CalculatorToolOutputSchema(result=str(result))
#################
diff --git a/tests/lib/tools/test_calculator_tool.py b/tests/lib/tools/test_calculator_tool.py
new file mode 100644
index 0000000..7140550
--- /dev/null
+++ b/tests/lib/tools/test_calculator_tool.py
@@ -0,0 +1,72 @@
+import pytest
+from atomic_agents.lib.tools.calculator_tool import CalculatorTool, CalculatorToolSchema, CalculatorToolOutputSchema, CalculatorToolConfig
+from atomic_agents.lib.tools.base import BaseTool, BaseToolConfig
+
+def test_calculator_tool_initialization():
+ tool = CalculatorTool()
+ assert isinstance(tool, BaseTool)
+ assert tool.tool_name == "CalculatorTool"
+ assert "Tool for performing calculations" in tool.tool_description
+
+def test_calculator_tool_with_custom_config():
+ config = CalculatorToolConfig(title="Custom Calculator", description="Custom description")
+ tool = CalculatorTool(config=config)
+ assert tool.tool_name == "Custom Calculator"
+ assert tool.tool_description == "Custom description"
+
+def test_calculator_tool_input_schema():
+ tool = CalculatorTool()
+ assert tool.input_schema == CalculatorToolSchema
+
+def test_calculator_tool_output_schema():
+ tool = CalculatorTool()
+ assert tool.output_schema == CalculatorToolOutputSchema
+
[email protected]("expression, expected_result", [
+ ("2 + 2", "4.00000000000000"),
+ ("3 * 4", "12.0000000000000"),
+ ("10 / 2", "5.00000000000000"),
+ ("2 ** 3", "8.00000000000000"),
+ ("sin(pi/2)", "1.00000000000000"),
+ ("log(E)", "1.00000000000000"), # Changed from "log(e)" to "log(E)"
+])
+def test_calculator_tool_run_valid_expressions(expression, expected_result):
+ tool = CalculatorTool()
+ result = tool.run(CalculatorToolSchema(expression=expression))
+ assert isinstance(result, CalculatorToolOutputSchema)
+ assert result.result == expected_result
+
+def test_calculator_tool_run_complex_expression():
+ tool = CalculatorTool()
+ expression = "sqrt(3**2 + 4**2)"
+ result = tool.run(CalculatorToolSchema(expression=expression))
+ assert isinstance(result, CalculatorToolOutputSchema)
+ assert result.result == "5.00000000000000"
+
+def test_calculator_tool_run_invalid_expression():
+ tool = CalculatorTool()
+ invalid_expression = "invalid + expression"
+ result = tool.run(CalculatorToolSchema(expression=invalid_expression))
+ assert isinstance(result, CalculatorToolOutputSchema)
+ assert set(result.result.split(' + ')) == set(invalid_expression.split(' + '))
+
+def test_calculator_tool_run_division_by_zero():
+ tool = CalculatorTool()
+ result = tool.run(CalculatorToolSchema(expression="1/0"))
+ assert isinstance(result, CalculatorToolOutputSchema)
+ assert result.result == "zoo" # SymPy returns "zoo" for complex infinity
+
+def test_calculator_tool_run_with_variables():
+ tool = CalculatorTool()
+ result = tool.run(CalculatorToolSchema(expression="x + y"))
+ assert isinstance(result, CalculatorToolOutputSchema)
+ assert result.result == "x + y" # The tool doesn't evaluate expressions with variables
+
+def test_calculator_tool_config_is_pydantic_model():
+ assert issubclass(CalculatorToolConfig, BaseToolConfig)
+
+def test_calculator_tool_config_optional_fields():
+ config = CalculatorToolConfig()
+ assert hasattr(config, 'title')
+ assert hasattr(config, 'description')
+