A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
chore: add tests for sql function (#5189)
Shahmir Varqha13bad4c623fa
diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.pyindex 603183ee53..73f95d5702 100644--- a/marimo/_plugins/ui/_impl/table.py+++ b/marimo/_plugins/ui/_impl/table.py@@ -653,9 +653,7 @@ class table()@property- def data(- self,- ) -> TableData:+ def data(self) -> TableData:"""Get the original table data.Returns:diff --git a/marimo/_sql/engines/types.py b/marimo/_sql/engines/types.pyindex c376fee66c..84e505ef50 100644--- a/marimo/_sql/engines/types.py+++ b/marimo/_sql/engines/types.py@@ -124,7 +124,6 @@ class QueryEngine(BaseEngine[CONN], ABC):"""Execute a SQL query and return a dataframe."""pass- # TODO: Maybe this should be called during init of db'sdef sql_output_format(self) -> SqlOutputType:if runtime_context_installed():try:diff --git a/tests/_sql/test_clickhouse.py b/tests/_sql/test_clickhouse.pyindex 2f85db42da..5879779672 100644--- a/tests/_sql/test_clickhouse.py+++ b/tests/_sql/test_clickhouse.py@@ -1,3 +1,4 @@+# Copyright 2025 Marimo. All rights reserved.from __future__ import annotationsfrom typing import Anydiff --git a/tests/_sql/test_dbapi.py b/tests/_sql/test_dbapi.pyindex 07d4578097..f2f37e4095 100644--- a/tests/_sql/test_dbapi.py+++ b/tests/_sql/test_dbapi.py@@ -1,4 +1,4 @@-# Copyright 2024 Marimo. All rights reserved.+# Copyright 2025 Marimo. All rights reserved.from __future__ import annotationsimport sqlite3diff --git a/tests/_sql/test_duckdb.py b/tests/_sql/test_duckdb.pyindex 551d75bdbe..ea07851cda 100644--- a/tests/_sql/test_duckdb.py+++ b/tests/_sql/test_duckdb.py@@ -1,4 +1,4 @@-"""Tests for SQL engines."""+# Copyright 2025 Marimo. All rights reserved.from __future__ import annotationsdiff --git a/tests/_sql/test_engines.py b/tests/_sql/test_engines.pyindex c412818a95..9035bd4f0b 100644--- a/tests/_sql/test_engines.py+++ b/tests/_sql/test_engines.py@@ -1,8 +1,8 @@-"""General tests for the SQL engines."""+# Copyright 2025 Marimo. All rights reserved.from __future__ import annotations-from unittest.mock import MagicMock+from unittest.mock import MagicMock, patchimport pytest@@ -10,7 +10,11 @@ from marimo._dependencies.dependencies import DependencyManagerfrom marimo._sql.engines.clickhouse import ClickhouseEmbeddedfrom marimo._sql.engines.duckdb import DuckDBEnginefrom marimo._sql.engines.sqlalchemy import SQLAlchemyEngine-from marimo._sql.utils import raise_df_import_error, sql_type_to_data_type+from marimo._sql.utils import (+ raise_df_import_error,+ sql_type_to_data_type,+ try_convert_to_polars,+)HAS_DUCKDB = DependencyManager.duckdb.has()HAS_SQLALCHEMY = DependencyManager.sqlalchemy.has()@@ -358,3 +362,41 @@ def test_sqlalchemy_type_conversion() -> None:raise NotImplementedError("Not implemented")assert engine._get_python_type(CustomType()) is None+++ not HAS_POLARS and not HAS_SQLALCHEMY,+ reason="Polars and SQLAlchemy not installed",+)+def test_try_convert_to_polars() -> None:+ """Test try_convert_to_polars function."""+ import polars as pl+ import sqlalchemy as sa++ sqlite_engine = sa.create_engine("sqlite:///:memory:")+ engine = SQLAlchemyEngine(sqlite_engine)++ df, error = try_convert_to_polars(+ query="SELECT 1", connection=engine._connection, lazy=False+ )+ assert isinstance(df, pl.DataFrame)+ assert error is None++ # Lazy frame+ df, error = try_convert_to_polars(+ query="SELECT 1", connection=engine._connection, lazy=True+ )+ assert isinstance(df, pl.LazyFrame)+ assert error is None++ # Polars error+ with patch(+ "polars.read_database",+ side_effect=pl.exceptions.ComputeError("Test error"),+ ):+ df, error = try_convert_to_polars(+ query="SELECT 1", connection=engine._connection, lazy=False+ )+ assert df is None+ assert isinstance(error, pl.exceptions.ComputeError)+ assert str(error) == "Test error"diff --git a/tests/_sql/test_get_engines.py b/tests/_sql/test_get_engines.pyindex d2075f631a..45986625dc 100644--- a/tests/_sql/test_get_engines.py+++ b/tests/_sql/test_get_engines.py@@ -1,3 +1,4 @@+# Copyright 2025 Marimo. All rights reserved.from __future__ import annotationsfrom unittest.mock import MagicMock, patchdiff --git a/tests/_sql/test_ibis.py b/tests/_sql/test_ibis.pyindex 21dd04cf84..a699e022ff 100644--- a/tests/_sql/test_ibis.py+++ b/tests/_sql/test_ibis.py@@ -1,4 +1,4 @@-"""Tests for Ibis engines."""+# Copyright 2025 Marimo. All rights reserved.from __future__ import annotationsdiff --git a/tests/_sql/test_pyiceberg.py b/tests/_sql/test_pyiceberg.pyindex 9d5343020f..b45182b58f 100644--- a/tests/_sql/test_pyiceberg.py+++ b/tests/_sql/test_pyiceberg.py@@ -1,4 +1,4 @@-"""Tests for PyIceberg engine."""+# Copyright 2025 Marimo. All rights reserved.from __future__ import annotationsdiff --git a/tests/_sql/test_redshift.py b/tests/_sql/test_redshift.pyindex edf2323fc6..da88cff3b3 100644--- a/tests/_sql/test_redshift.py+++ b/tests/_sql/test_redshift.py@@ -1,4 +1,4 @@-"""Tests for Redshift engine."""+# Copyright 2025 Marimo. All rights reserved.from __future__ import annotationsdiff --git a/tests/_sql/test_sql.py b/tests/_sql/test_sql.pyindex 9a30749377..eeae3fc061 100644--- a/tests/_sql/test_sql.py+++ b/tests/_sql/test_sql.py@@ -1,4 +1,4 @@-"""Tests for SQL functionality."""+# Copyright 2025 Marimo. All rights reserved.from __future__ import annotationsdiff --git a/tests/_sql/test_sqlalchemy.py b/tests/_sql/test_sqlalchemy.pyindex 3665b06a11..1a617a45c9 100644--- a/tests/_sql/test_sqlalchemy.py+++ b/tests/_sql/test_sqlalchemy.py@@ -1,4 +1,4 @@-"""Tests for SQLAlchemy engines."""+# Copyright 2025 Marimo. All rights reserved.from __future__ import annotations