sadpandajoe commented on code in PR #44657:
URL: https://github.com/apache/superset/pull/44657#discussion_r4251996192
##########
tests/unit_tests/charts/test_client_processing.py:
##########
@@ -2144,6 +2172,166 @@ def
test_pivot_table_v2_actual_values_uses_database_rollups():
assert pivoted.loc[("US",), ("AVG(num)", "boy")] == 10
[email protected](
+ "aggregate_function,expected",
+ [
+ ("Sample Variance", 50.0),
+ ("Sample Standard Deviation", 50.0**0.5),
+ ],
+)
+def test_pivot_table_v2_sample_statistics_aggregations(
+ aggregate_function: str, expected: float
+):
+ """Sample variance/standard deviation must compute for two values at one
+ pivot address (10 and 20 give variance 50 and standard deviation ~7.071),
+ not raise on a misspelled ``pd.series`` or a non-callable tuple.
+ """
+ df = pd.DataFrame(
+ {
+ "nation": ["US", "US"],
+ "gender": ["boy", "boy"],
+ "SUM(num)": [10, 20],
+ }
+ )
+ form_data = {
+ "groupbyRows": ["nation"],
+ "groupbyColumns": ["gender"],
+ "metrics": ["SUM(num)"],
+ "aggregateFunction": aggregate_function,
+ }
+
+ pivoted = pivot_table_v2(df, form_data, apply_number_format=False)
+
+ assert pivoted.loc[("US",), ("SUM(num)", "boy")] == pytest.approx(expected)
+
+
[email protected](
+ "aggregate_function",
+ ["Sample Variance", "Sample Standard Deviation"],
+)
+def test_pivot_table_v2_sample_statistics_with_summaries(aggregate_function:
str):
+ """Row and column summaries invoke the reducer with ``axis``; the sample
+ statistics must handle the DataFrame/axis form, not just a Series.
+ """
+ df = pd.DataFrame(
+ {
+ "nation": ["US", "US", "FR", "FR"],
+ "gender": ["boy", "girl", "boy", "girl"],
+ "SUM(num)": [10, 20, 30, 50],
+ }
+ )
+ form_data = {
+ "groupbyRows": ["nation"],
+ "groupbyColumns": ["gender"],
+ "metrics": ["SUM(num)"],
+ "aggregateFunction": aggregate_function,
+ "rowTotals": True,
+ "colTotals": True,
+ }
+
+ pivoted = pivot_table_v2(df, form_data, apply_number_format=False)
+
+ assert not pivoted.empty
+
+
[email protected](
+ "aggregate_function",
+ [
+ "Count",
+ "Count Unique Values",
+ "List Unique Values",
+ "First",
+ "Last",
+ ],
+)
[email protected]("totals", [{"rowTotals": True}, {"colTotals": True}])
+def test_pivot_table_v2_series_only_reducers_with_summaries(
+ aggregate_function: str, totals: dict[str, bool]
+) -> None:
+ """Series-only reducers must support each row/column summary axis rather
+ than raising ``TypeError`` on the DataFrame/``axis`` invocation.
+ """
+ df = pd.DataFrame(
+ {
+ "nation": ["US", "US", "FR", "FR"],
+ "gender": ["boy", "girl", "boy", "girl"],
+ "SUM(num)": [10, 20, 30, 50],
+ }
+ )
+ form_data = {
+ "groupbyRows": ["nation"],
+ "groupbyColumns": ["gender"],
+ "metrics": ["SUM(num)"],
+ "aggregateFunction": aggregate_function,
+ **totals,
+ }
+
+ pivoted = pivot_table_v2(df, form_data, apply_number_format=False)
+
+ assert not pivoted.empty
Review Comment:
These parametrized cases only assert `not pivoted.empty`, so they pass even
if `First` and `Last` swap reducers or a summary axis is computed wrongly; they
only guard against the earlier `TypeError`. With the US/FR × boy/girl fixture
and `rowTotals: True`, could this assert the row summaries (US `First` = 10,
`Last` = 20) per aggregation so the reducer selection is actually pinned?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]