sadpandajoe commented on code in PR #45439:
URL: https://github.com/apache/superset/pull/45439#discussion_r4325985562
##########
tests/unit_tests/charts/test_client_processing.py:
##########
@@ -2501,6 +2501,318 @@ def test_table_applies_si_number_format() -> None:
assert formatted["amount"].tolist() == ["1.23k"]
+def test_table_applies_small_number_format() -> None:
+ """
+ A cell below one in absolute value takes the column's small-number format.
+
+ ``formatColumnValue`` in the Table plugin picks the formatter per value, so
+ the report has to switch per cell rather than per column.
+
+ The switch point itself is covered on both sides and in both signs. The
+ browser's test is ``Math.abs(value) < 1``, so exactly one is a large cell
+ and a large negative stays large: widening the comparison to ``<= 1`` or
+ comparing the signed value would render one of these as a percentage.
+ """
+ df = pd.DataFrame.from_dict(
+ {
+ "ratio": {
+ 0: 0.0523,
+ 1: 12.5,
+ 2: -0.5,
+ 3: 0.0,
+ 4: 1.0,
+ 5: -1.0,
+ 6: -12.5,
+ }
+ }
+ )
+ form_data = {
+ "viz_type": "table",
+ "metrics": ["ratio"],
+ "column_config": {
+ "ratio": {"d3NumberFormat": ",.2f", "d3SmallNumberFormat": ".1%"}
+ },
+ }
+ formatted = table(df, form_data)
+ assert formatted["ratio"].tolist() == [
+ "5.2%",
+ "12.50",
+ "-50.0%",
+ "0.0%",
+ "1.00",
+ "-1.00",
+ "-12.50",
+ ]
+
+
+def test_table_ignores_blank_small_number_format() -> None:
+ """
+ A cleared Select leaves an empty string behind, which
+ ``getSmallNumberFormatter`` treats as unset.
+ """
+ df = pd.DataFrame.from_dict({"ratio": {0: 0.0523, 1: 12.5}})
+ form_data = {
+ "viz_type": "table",
+ "metrics": ["ratio"],
+ "column_config": {
+ "ratio": {"d3NumberFormat": ",.2f", "d3SmallNumberFormat": " "}
+ },
+ }
+ formatted = table(df, form_data)
+ assert formatted["ratio"].tolist() == ["0.05", "12.50"]
+
+
+def test_table_small_number_format_without_a_normal_format() -> None:
+ """
+ A small-number format on its own is enough to format the small cells.
+
+ The other cells follow the browser: a metric falls back to SMART_NUMBER,
+ while a numeric dimension gets no formatter at all and is printed as is.
+ """
+ df = pd.DataFrame.from_dict({"ratio": {0: 0.0523, 1: 1200.0}})
+ form_data = {
+ "viz_type": "table",
+ "metrics": ["ratio"],
+ "column_config": {"ratio": {"d3SmallNumberFormat": ".1%"}},
+ }
+ assert table(df, form_data)["ratio"].tolist() == ["5.2%", "1.2k"]
+
+ df = pd.DataFrame.from_dict({"ratio": {0: 0.0523, 1: 12.5}})
+ form_data = {
+ "viz_type": "table",
+ "metrics": [],
+ "column_config": {"ratio": {"d3SmallNumberFormat": ".1%"}},
+ }
+ assert table(df, form_data)["ratio"].tolist() == ["5.2%", "12.5"]
Review Comment:
The numeric-dimension half of this test uses `12.5`, which SMART_NUMBER also
renders as `12.5`. If someone drops the new `column in metric_labels` condition
so a plain numeric dimension with only a small-number format picks up
SMART_NUMBER, this assertion still passes, even though the PR describes that
case as left unformatted. Would a larger value such as `1200.0` (expected
`'1200'`, versus `'1.2k'` for the metric case above) pin that condition?
--
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]