SEPURI-SAI-KRISHNA commented on code in PR #45439:
URL: https://github.com/apache/superset/pull/45439#discussion_r4320974130


##########
tests/unit_tests/charts/test_client_processing.py:
##########
@@ -2501,6 +2501,118 @@ 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.
+    """
+    df = pd.DataFrame.from_dict({"ratio": {0: 0.0523, 1: 12.5, 2: -0.5, 3: 
0.0}})
+    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%"]
+
+
+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"]
+
+
+def test_table_small_number_format_uses_the_column_config_currency() -> None:
+    """
+    ``getSmallNumberFormatter`` pairs the small format with the column config's
+    own currency, and the currency formatter drops percent signs.
+    """
+    df = pd.DataFrame.from_dict({"amount": {0: 0.5, 1: 12.5}})
+    form_data = {
+        "viz_type": "table",
+        "metrics": ["amount"],
+        "column_config": {
+            "amount": {
+                "d3NumberFormat": ",.2f",
+                "d3SmallNumberFormat": ".1%",
+                "currencyFormat": {"symbol": "USD", "symbolPosition": 
"prefix"},
+            }
+        },
+    }
+    formatted = table(df, form_data)

Review Comment:
   Good catch, that test does leave the pairing unpinned. Added 
`test_table_small_number_format_ignores_a_saved_currency` with the case you 
described:
   
   ```python
       datasource.data = {
           "column_formats": {},
           "verbose_map": {},
           "metrics": [
               {
                   "metric_name": "amount",
                   "currency": {"symbol": "USD", "symbolPosition": "prefix"},
               }
           ],
       }
       ...
       assert formatted["amount"].tolist() == ["50.0%", "$ 12.50"]
   ```
   
   The saved USD reaches `currency` through `currency_by_label` but not 
`configured_currency`, so swapping the small path over to the resolved currency 
turns the first cell into `$ 50.0` and the test fails. It passes on the parent 
commit as well, which is the point of it: it pins behavior the fix does not 
change rather than proving the fix.
   
   The percent-metric test also pins one thing worth naming. Its middle row has 
a null currency code and the query-wide detection is `GBP`, and the cell stays 
`0.250`. The small-number formatter never sees that detection in the browser, 
so it must not fall back to it.



-- 
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]

Reply via email to