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
test(sql): remove flaky connection timing assertions (#10745)
Shahmir Varqha9753b9fad76b
diff --git a/tests/_sql/test_connection_utils.py b/tests/_sql/test_connection_utils.pyindex 33931d9146..b94770d898 100644--- a/tests/_sql/test_connection_utils.py+++ b/tests/_sql/test_connection_utils.py@@ -1,11 +1,6 @@# Copyright 2026 Marimo. All rights reserved.from __future__ import annotations-import time-from typing import TYPE_CHECKING--import pytest-from marimo._data.models import (Database,DataSourceConnection,@@ -21,11 +16,6 @@ from marimo._sql.connection_utils import (update_table_list_in_connection,)-if TYPE_CHECKING:- from collections.abc import Callable--TIME_THRESHOLD_SECONDS = 0.1-def create_test_table(name: str = "test_table") -> DataTable:"""Create a test DataTable."""@@ -299,288 +289,6 @@ class TestUpdateTableListInConnection:)-class TestPerformance:- """Performance tests for connection utils."""-- def _measure_performance(- self,- func: Callable[[], None],- iterations: int = 1000,- ) -> tuple[float, float]:- """Measure average and total execution time.-- Returns:- Tuple of (average_time_ms, total_time_ms)- """- start = time.perf_counter()- for _ in range(iterations):- func()- total_time = (time.perf_counter() - start) * 1000 # Convert to ms- avg_time = total_time / iterations- return avg_time, total_time-- def test_performance_small_hierarchy(self) -> None:- """Benchmark with small hierarchy (typical real-world case)."""- # 2 connections, 3 databases each, 5 schemas each, 20 tables each- connections = create_test_connections(- num_connections=2,- num_databases_per_conn=3,- num_schemas_per_db=5,- num_tables_per_schema=20,- )-- sql_metadata = SQLMetadata(- connection="connection_1",- database="database_2",- schema="schema_4",- )-- updated_table = create_test_table("table_19")- updated_table.num_rows = 999-- def update_func() -> None:- update_table_in_connection(- connections, sql_metadata, updated_table- )-- avg_time, total_time = self._measure_performance(- update_func, iterations=1000- )-- print(- f"\nSmall hierarchy (2x3x5x20 = 600 tables): "- f"{avg_time:.4f}ms avg, {total_time:.2f}ms total"- )-- # Assert reasonable performance: < 0.1ms per operation- assert avg_time < 0.1, f"Performance too slow: {avg_time:.4f}ms"-- def test_performance_medium_hierarchy(self) -> None:- """Benchmark with medium hierarchy."""- # 5 connections, 5 databases each, 10 schemas each, 50 tables each- connections = create_test_connections(- num_connections=5,- num_databases_per_conn=5,- num_schemas_per_db=10,- num_tables_per_schema=50,- )-- sql_metadata = SQLMetadata(- connection="connection_4",- database="database_4",- schema="schema_9",- )-- updated_table = create_test_table("table_49")- updated_table.num_rows = 999-- def update_func() -> None:- update_table_in_connection(- connections, sql_metadata, updated_table- )-- avg_time, total_time = self._measure_performance(- update_func, iterations=1000- )-- print(- f"\nMedium hierarchy (5x5x10x50 = 12,500 tables): "- f"{avg_time:.4f}ms avg, {total_time:.2f}ms total"- )-- # Assert reasonable performance: < 0.5ms per operation- assert avg_time < TIME_THRESHOLD_SECONDS, (- f"Performance too slow: {avg_time:.4f}ms"- )-- def test_performance_large_hierarchy(self) -> None:- """Benchmark with large hierarchy (stress test)."""- # 10 connections, 10 databases each, 10 schemas each, 100 tables each- connections = create_test_connections(- num_connections=10,- num_databases_per_conn=10,- num_schemas_per_db=10,- num_tables_per_schema=100,- )-- sql_metadata = SQLMetadata(- connection="connection_9",- database="database_9",- schema="schema_9",- )-- updated_table = create_test_table("table_99")- updated_table.num_rows = 999-- def update_func() -> None:- update_table_in_connection(- connections, sql_metadata, updated_table- )-- avg_time, total_time = self._measure_performance(- update_func, iterations=1000- )-- print(- f"\nLarge hierarchy (10x10x10x100 = 100,000 tables): "- f"{avg_time:.4f}ms avg, {total_time:.2f}ms total"- )-- # Assert reasonable performance: < 1ms per operation- assert avg_time < TIME_THRESHOLD_SECONDS, (- f"Performance too slow: {avg_time:.4f}ms"- )-- def test_performance_worst_case_early_exit(self) -> None:- """Benchmark best case: early exit on first connection."""- connections = create_test_connections(- num_connections=10,- num_databases_per_conn=10,- num_schemas_per_db=10,- num_tables_per_schema=100,- )-- sql_metadata = SQLMetadata(- connection="connection_0",- database="database_0",- schema="schema_0",- )-- updated_table = create_test_table("table_0")- updated_table.num_rows = 999-- def update_func() -> None:- update_table_in_connection(- connections, sql_metadata, updated_table- )-- avg_time, total_time = self._measure_performance(- update_func, iterations=1000- )-- print(- f"\nBest case (early exit): "- f"{avg_time:.4f}ms avg, {total_time:.2f}ms total"- )-- # Early exit should be very fast- assert avg_time < 0.1, f"Performance too slow: {avg_time:.4f}ms"-- def test_performance_update_schema_list(self) -> None:- """Benchmark update_schema_list_in_connection."""- connections = create_test_connections(- num_connections=5,- num_databases_per_conn=5,- num_schemas_per_db=10,- num_tables_per_schema=50,- )-- sql_db_metadata = SQLDatabaseMetadata(- connection="connection_4",- database="database_4",- )-- new_schemas = [- Schema(name=f"new_schema_{i}", tables=[]) for i in range(10)- ]-- def update_func() -> None:- update_schema_list_in_connection(- connections, sql_db_metadata, new_schemas- )-- avg_time, total_time = self._measure_performance(- update_func, iterations=1000- )-- print(- f"\nUpdate schema list (5x5x10 = 250 schemas): "- f"{avg_time:.4f}ms avg, {total_time:.2f}ms total"- )-- # Should be fast since we're just replacing a list reference- assert avg_time < TIME_THRESHOLD_SECONDS, (- f"Performance too slow: {avg_time:.4f}ms"- )-- def test_performance_update_table_list(self) -> None:- """Benchmark update_table_list_in_connection."""- connections = create_test_connections(- num_connections=5,- num_databases_per_conn=5,- num_schemas_per_db=10,- num_tables_per_schema=50,- )-- sql_metadata = SQLMetadata(- connection="connection_4",- database="database_4",- schema="schema_9",- )-- new_tables = [create_test_table(f"new_table_{i}") for i in range(50)]-- def update_func() -> None:- update_table_list_in_connection(- connections, sql_metadata, new_tables- )-- avg_time, total_time = self._measure_performance(- update_func, iterations=1000- )-- print(- f"\nUpdate table list (5x5x10x50 = 12,500 tables): "- f"{avg_time:.4f}ms avg, {total_time:.2f}ms total"- )-- # Should be fast since we're just replacing a list reference- assert avg_time < TIME_THRESHOLD_SECONDS, (- f"Performance too slow: {avg_time:.4f}ms"- )-- @pytest.mark.skip(- reason="Only run manually for detailed profiling analysis"- )- def test_profile_with_cprofile(self) -> None:- """Profile the function with cProfile for detailed analysis."""- import cProfile- import pstats-- connections = create_test_connections(- num_connections=10,- num_databases_per_conn=10,- num_schemas_per_db=10,- num_tables_per_schema=100,- )-- sql_metadata = SQLMetadata(- connection="connection_9",- database="database_9",- schema="schema_9",- )-- updated_table = create_test_table("table_99")-- profiler = cProfile.Profile()- profiler.enable()-- for _ in range(10000):- update_table_in_connection(- connections, sql_metadata, updated_table- )-- profiler.disable()-- stats = pstats.Stats(profiler)- stats.strip_dirs()- stats.sort_stats("cumulative")- print("\n" + "=" * 80)- print("cProfile Results (top 20):")- print("=" * 80)- stats.print_stats(20)--def _create_nested_connection() -> list[DataSourceConnection]:"""Connection with a top-level namespace ("top") holding a recursivesub-namespace tree: top -> nested -> deep."""