refactor: Calculator Component updating tool implementation and fix deprecation warnings (#5442)
* refactor(calculator): update tool implementation Replace legacy tool mode implementation using CalculatorToolSchema with simplified tool_mode=True approach. * refactor(calculator): fix deprecation warnings Fix ast.Num deprecation warnings by supporting ast.Constant while maintaining backwards compatibility. * Update isinstance check to use Python 3.10+ union operator (|) instead of tuple syntax * Update calculator.py Component name required; if not it would get None in Toolset * [autofix.ci] apply automated fixes * test(calculator): add unit tests for CalculatorToolComponent * revert(tools): restore Calculator component to its original implementation Due to potential breaking changes in the repository, reverting the Calculator component to its initial PR state to maintain compatibility and stability. * feat(tools): mark Calculator component as legacy and update display name - Set legacy flag to true for Calculator component - Update display name to "Calculator (Deprecated)" to clearly indicate deprecation status - Maintain backward compatibility by preserving class name and internal name * feat(tools)!: add new calculator core component BREAKING CHANGE: Introduces calculator_core.py as a replacement for the deprecated calculator.py * refactor(tools): rename calculator classes for better distinction * refactor(tools): update __init__.py to reflect new class names * [autofix.ci] apply automated fixes * test(tools): update calculator tests for core component --------- Co-authored-by: Edwin Jose <edwin.jose@datastax.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
7cf77d30ae
commit
3474259ca4
4 changed files with 176 additions and 1 deletions
84
src/backend/tests/unit/components/tools/test_calculator.py
Normal file
84
src/backend/tests/unit/components/tools/test_calculator.py
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import pytest
|
||||
from langflow.components.tools.calculator_core import CalculatorComponent
|
||||
|
||||
from tests.base import ComponentTestBaseWithoutClient
|
||||
|
||||
|
||||
class TestCalculatorComponent(ComponentTestBaseWithoutClient):
|
||||
@pytest.fixture
|
||||
def component_class(self):
|
||||
return CalculatorComponent
|
||||
|
||||
@pytest.fixture
|
||||
def default_kwargs(self):
|
||||
return {"expression": "2 + 2", "_session_id": "test_session"}
|
||||
|
||||
@pytest.fixture
|
||||
def file_names_mapping(self):
|
||||
return []
|
||||
|
||||
def test_basic_calculation(self, component_class, default_kwargs):
|
||||
# Arrange
|
||||
component = component_class(**default_kwargs)
|
||||
|
||||
# Act
|
||||
result = component.evaluate_expression()
|
||||
|
||||
# Assert
|
||||
assert result.data["result"] == "4"
|
||||
|
||||
def test_complex_calculation(self, component_class):
|
||||
# Arrange
|
||||
component = component_class(expression="4*4*(33/22)+12-20", _session_id="test_session")
|
||||
|
||||
# Act
|
||||
result = component.evaluate_expression()
|
||||
|
||||
# Assert
|
||||
assert float(result.data["result"]) == pytest.approx(16)
|
||||
|
||||
def test_division_by_zero(self, component_class):
|
||||
# Arrange
|
||||
component = component_class(expression="1/0", _session_id="test_session")
|
||||
|
||||
# Act
|
||||
result = component.evaluate_expression()
|
||||
|
||||
# Assert
|
||||
assert "error" in result.data
|
||||
assert result.data["error"] == "Error: Division by zero"
|
||||
|
||||
def test_invalid_expression(self, component_class):
|
||||
# Arrange
|
||||
component = component_class(expression="2 + *", _session_id="test_session")
|
||||
|
||||
# Act
|
||||
result = component.evaluate_expression()
|
||||
|
||||
# Assert
|
||||
assert "error" in result.data
|
||||
assert "Invalid expression" in result.data["error"]
|
||||
|
||||
def test_unsupported_operation(self, component_class):
|
||||
# Arrange
|
||||
component = component_class(expression="sqrt(16)", _session_id="test_session")
|
||||
|
||||
# Act
|
||||
result = component.evaluate_expression()
|
||||
|
||||
# Assert
|
||||
assert "error" in result.data
|
||||
assert "Unsupported operation" in result.data["error"]
|
||||
|
||||
def test_component_frontend_node(self, component_class, default_kwargs):
|
||||
# Arrange
|
||||
component = component_class(**default_kwargs)
|
||||
|
||||
# Act
|
||||
frontend_node = component.to_frontend_node()
|
||||
|
||||
# Assert
|
||||
node_data = frontend_node["data"]["node"]
|
||||
assert node_data["display_name"] == "Calculator"
|
||||
assert node_data["description"] == "Perform basic arithmetic operations on a given expression."
|
||||
assert node_data["icon"] == "calculator"
|
||||
Loading…
Add table
Add a link
Reference in a new issue