mirror of
https://github.com/apache/superset.git
synced 2026-04-24 02:25:13 +00:00
feat(Chart): Save Chart State globally (#35343)
This commit is contained in:
@@ -1135,3 +1135,133 @@ def test__temporal_range_operator_in_adhoc_filter(physical_dataset):
|
||||
)
|
||||
df = pd.DataFrame(index=[0], data={"col1": 4, "col2": "e"})
|
||||
assert df.equals(result.df)
|
||||
|
||||
|
||||
def test_generic_metric_filtering_without_chart_flag(login_as_admin):
|
||||
"""
|
||||
Test that filters on metrics work without chart-specific flags.
|
||||
|
||||
This ensures metric filtering is generic and works for any chart type.
|
||||
"""
|
||||
from superset import db
|
||||
from superset.connectors.sqla.models import SqlaTable, SqlMetric, TableColumn
|
||||
from superset.utils.database import get_example_database
|
||||
|
||||
database = get_example_database()
|
||||
table = SqlaTable(
|
||||
table_name="test_metric_filter",
|
||||
database=database,
|
||||
)
|
||||
|
||||
col = TableColumn(
|
||||
column_name="name",
|
||||
type="VARCHAR(255)",
|
||||
table=table,
|
||||
)
|
||||
table.columns = [col]
|
||||
|
||||
metric = SqlMetric(
|
||||
metric_name="count",
|
||||
expression="COUNT(*)",
|
||||
table=table,
|
||||
)
|
||||
table.metrics = [metric]
|
||||
|
||||
db.session.add(table)
|
||||
db.session.commit()
|
||||
|
||||
try:
|
||||
query_obj = {
|
||||
"granularity": None,
|
||||
"from_dttm": None,
|
||||
"to_dttm": None,
|
||||
"groupby": ["name"],
|
||||
"metrics": ["count"],
|
||||
"filter": [
|
||||
{
|
||||
"col": "count",
|
||||
"op": ">",
|
||||
"val": 0,
|
||||
}
|
||||
],
|
||||
"is_timeseries": False,
|
||||
"extras": {},
|
||||
}
|
||||
|
||||
sqla_query = table.get_sqla_query(**query_obj)
|
||||
sql = str(
|
||||
sqla_query.sqla_query.compile(compile_kwargs={"literal_binds": True})
|
||||
).lower()
|
||||
|
||||
assert "having" in sql, "Metric filter should use HAVING clause. SQL: " + sql
|
||||
finally:
|
||||
db.session.delete(table)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def test_column_ordering_without_chart_flag(login_as_admin):
|
||||
"""
|
||||
Test that column_order works without chart-specific flags.
|
||||
|
||||
This ensures column ordering is generic and works for any chart type.
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
from superset import db
|
||||
from superset.connectors.sqla.models import SqlaTable, SqlMetric, TableColumn
|
||||
from superset.utils.database import get_example_database
|
||||
|
||||
database = get_example_database()
|
||||
table = SqlaTable(
|
||||
table_name="test_column_order",
|
||||
database=database,
|
||||
)
|
||||
|
||||
col_a = TableColumn(column_name="col_a", type="VARCHAR(255)", table=table)
|
||||
col_b = TableColumn(column_name="col_b", type="VARCHAR(255)", table=table)
|
||||
table.columns = [col_a, col_b]
|
||||
|
||||
metric_x = SqlMetric(metric_name="metric_x", expression="COUNT(*)", table=table)
|
||||
metric_y = SqlMetric(metric_name="metric_y", expression="SUM(val)", table=table)
|
||||
table.metrics = [metric_x, metric_y]
|
||||
|
||||
db.session.add(table)
|
||||
db.session.commit()
|
||||
|
||||
try:
|
||||
mock_df = pd.DataFrame(
|
||||
{
|
||||
"col_a": [1, 2],
|
||||
"col_b": [3, 4],
|
||||
"metric_x": [10, 20],
|
||||
"metric_y": [100, 200],
|
||||
}
|
||||
)
|
||||
|
||||
def mock_get_df(sql, catalog=None, schema=None, mutator=None):
|
||||
"""Mock get_df that calls the mutator function if provided."""
|
||||
df = mock_df.copy()
|
||||
if mutator:
|
||||
df = mutator(df)
|
||||
return df
|
||||
|
||||
with patch.object(database, "get_df", side_effect=mock_get_df):
|
||||
query_obj = {
|
||||
"granularity": None,
|
||||
"from_dttm": None,
|
||||
"to_dttm": None,
|
||||
"groupby": ["col_a", "col_b"],
|
||||
"metrics": ["metric_x", "metric_y"],
|
||||
"filter": [],
|
||||
"is_timeseries": False,
|
||||
"extras": {"column_order": ["metric_y", "col_b", "metric_x", "col_a"]},
|
||||
}
|
||||
|
||||
result = table.query(query_obj)
|
||||
|
||||
expected_order = ["metric_y", "col_b", "metric_x", "col_a"]
|
||||
assert list(result.df.columns) == expected_order, (
|
||||
f"Expected {expected_order}, got {list(result.df.columns)}"
|
||||
)
|
||||
finally:
|
||||
db.session.delete(table)
|
||||
db.session.commit()
|
||||
|
||||
Reference in New Issue
Block a user