feat: update sql component to support toolkit (#7652)

* update sql component

* cache

* Update src/backend/base/langflow/components/data/sql_executor.py

Co-authored-by: Edwin Jose <edwin.jose@datastax.com>

* Update src/backend/base/langflow/components/data/sql_executor.py

Co-authored-by: Edwin Jose <edwin.jose@datastax.com>

* [autofix.ci] apply automated fixes

* fix: sql query component (#7700)

* feat: Sql toolkit tests (#7842)

Co-authored-by: Edwin Jose <edwin.jose@datastax.com>

---------

Co-authored-by: Edwin Jose <edwin.jose@datastax.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Gustavo Costa <gsantosaero@gmail.com>
This commit is contained in:
Sebastián Estévez 2025-05-06 12:08:33 -04:00 committed by GitHub
commit a04e5f56b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 203 additions and 57 deletions

View file

@ -0,0 +1,115 @@
import sqlite3
from pathlib import Path
import pytest
from langflow.components.data.sql_executor import SQLComponent
from langflow.schema import Data, DataFrame, Message
from tests.base import ComponentTestBaseWithoutClient
class TestSQLComponent(ComponentTestBaseWithoutClient):
@pytest.fixture
def test_db(self):
"""Fixture that creates a temporary SQLite database for testing."""
test_data_dir = Path(__file__).parent.parent.parent.parent / "data"
db_path = test_data_dir / "test.db"
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS test (
id INTEGER PRIMARY KEY,
name TEXT
)
""")
cursor.execute("""
INSERT INTO test (id, name)
VALUES (1, 'name_test')
""")
conn.commit()
conn.close()
yield str(db_path)
Path(db_path).unlink()
@pytest.fixture
def component_class(self):
"""Return the component class to test."""
return SQLComponent
@pytest.fixture
def default_kwargs(self, test_db):
"""Return the default kwargs for the component."""
return {
"database_url": f"sqlite:///{test_db}",
"query": "SELECT * FROM test",
"include_columns": True,
"add_error": False,
}
@pytest.fixture
def file_names_mapping(self):
"""Return an empty list since this component doesn't have version-specific files."""
return []
def test_successful_query_with_columns(self, component_class: type[SQLComponent], default_kwargs):
"""Test a successful SQL query with columns included."""
component = component_class(**default_kwargs)
result = component.build_component()
assert isinstance(result, Message)
assert isinstance(result.text, str)
assert result.text == "[{'id': 1, 'name': 'name_test'}]"
def test_successful_query_without_columns(self, component_class: type[SQLComponent], default_kwargs):
"""Test a successful SQL query without columns included."""
default_kwargs["include_columns"] = False
component = component_class(**default_kwargs)
result = component.build_component()
assert isinstance(result, Message)
assert isinstance(result.text, str)
assert result.text == "[(1, 'name_test')]"
assert component.status == "[(1, 'name_test')]"
assert component.query == "SELECT * FROM test"
def test_query_error_with_add_error(self, component_class: type[SQLComponent], default_kwargs):
"""Test a SQL query that raises an error with add_error=True."""
default_kwargs["add_error"] = True
default_kwargs["query"] = "SELECT * FROM non_existent_table"
component = component_class(**default_kwargs)
result = component.build_component()
assert isinstance(result, Message)
assert isinstance(result.text, str)
assert "no such table: non_existent_table" in result.text
assert "Error:" in result.text
assert "Query: SELECT * FROM non_existent_table" in result.text
def test_build_dataframe(self, component_class: type[SQLComponent], default_kwargs):
"""Test building a DataFrame from a SQL query."""
component = component_class(**default_kwargs)
result = component.build_dataframe()
assert isinstance(result, DataFrame)
assert len(result) == 1
assert "id" in result.columns
assert "name" in result.columns
assert result.iloc[0]["id"] == 1
assert result.iloc[0]["name"] == "name_test"
def test_build_data(self, component_class: type[SQLComponent], default_kwargs):
"""Test building a Data object from a SQL query."""
component = component_class(**default_kwargs)
result = component.build_data()
assert isinstance(result, Data)
assert "result" in result.data
assert len(result.data["result"]) == 1
assert result.data["result"][0]["id"] == 1
assert result.data["result"][0]["name"] == "name_test"