Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix narrow symbol downloads with tests (#69811)

Edward Pagetd59d58e61a59
diff --git a/src/metabase/query_processor/streaming/common.clj b/src/metabase/query_processor/streaming/common.clj
index 67bb7e33a1f..58bf2e8024d 100644
--- a/src/metabase/query_processor/streaming/common.clj
+++ b/src/metabase/query_processor/streaming/common.clj
@@ -103,6 +103,12 @@
;; Fall back to using code if symbol isn't supported on the Metabase frontend
currency-code)
+ "narrowSymbol"
+ (if (currency/supports-symbol? currency-code)
+ (get-in currency/currency [(keyword currency-code) :symbol_native])
+ ;; Fall back to using code if symbol_native isn't supported on the Metabase frontend
+ currency-code)
+
"code"
currency-code
diff --git a/src/metabase/query_processor/streaming/xlsx.clj b/src/metabase/query_processor/streaming/xlsx.clj
index 4e1e3ca0947..366a362c278 100644
--- a/src/metabase/query_processor/streaming/xlsx.clj
+++ b/src/metabase/query_processor/streaming/xlsx.clj
@@ -77,6 +77,11 @@
(str "[$" currency-identifier "]" base-string)
(str "[$" currency-identifier "] " base-string))
+ "narrowSymbol"
+ (if (currency/supports-symbol? currency-code)
+ (str "[$" currency-identifier "]" base-string)
+ (str "[$" currency-identifier "] " base-string))
+
"code"
(str "[$" currency-identifier "] " base-string)
diff --git a/test/metabase/query_processor/streaming/common_test.clj b/test/metabase/query_processor/streaming/common_test.clj
index cf45db3b568..2050dede833 100644
--- a/test/metabase/query_processor/streaming/common_test.clj
+++ b/test/metabase/query_processor/streaming/common_test.clj
@@ -19,6 +19,21 @@
filename-time-in-report-zone (t/zoned-date-time filename-time test-timezone)]
(is (= now-in-report-zone filename-time-in-report-zone))))))))
+(deftest currency-identifier-test
+ (testing "narrowSymbol style returns symbol_native"
+ (testing "USD symbol_native is $ (same as symbol)"
+ (is (= "$" (streaming.common/currency-identifier {::mb.viz/currency "USD"
+ ::mb.viz/currency-style "narrowSymbol"}))))
+ (testing "CAD symbol_native is $ (differs from symbol CA$)"
+ (is (= "$" (streaming.common/currency-identifier {::mb.viz/currency "CAD"
+ ::mb.viz/currency-style "narrowSymbol"}))))
+ (testing "EUR symbol_native is €"
+ (is (= "€" (streaming.common/currency-identifier {::mb.viz/currency "EUR"
+ ::mb.viz/currency-style "narrowSymbol"})))))
+ (testing "narrowSymbol falls back to code if symbol not supported"
+ (is (= "KGS" (streaming.common/currency-identifier {::mb.viz/currency "KGS"
+ ::mb.viz/currency-style "narrowSymbol"})))))
+
(deftest column-titles-test
(testing "column titles properly merge settings from multiple references to the same column"
(let [ordered-cols [{:name "CREATED_AT" :id 13 :display_name "Created At"}]
diff --git a/test/metabase/query_processor/streaming/xlsx_test.clj b/test/metabase/query_processor/streaming/xlsx_test.clj
index 48b3fae506c..8b06a2fae05 100644
--- a/test/metabase/query_processor/streaming/xlsx_test.clj
+++ b/test/metabase/query_processor/streaming/xlsx_test.clj
@@ -231,6 +231,30 @@
::mb.viz/currency-style "name"}
price-col))))))))
+(deftest format-settings->format-string-test-3d2
+ (mt/with-temporary-setting-values [custom-formatting {}]
+ (testing "Currency formatting"
+ (let [price-col {:semantic_type :type/Price, :effective_type :type/Float}]
+ (testing "narrowSymbol uses symbol_native and produces correct format strings"
+ (is (= "[$$]#,##0.00" (format-string {::mb.viz/currency-in-header false,
+ ::mb.viz/currency "USD",
+ ::mb.viz/currency-style "narrowSymbol"}
+ price-col)))
+ ;; CAD narrowSymbol is "$" (not "CA$" like symbol), so no space before number
+ (is (= "[$$]#,##0.00" (format-string {::mb.viz/currency-in-header false,
+ ::mb.viz/currency "CAD",
+ ::mb.viz/currency-style "narrowSymbol"}
+ price-col)))
+ (is (= "[$€]#,##0.00" (format-string {::mb.viz/currency-in-header false,
+ ::mb.viz/currency "EUR",
+ ::mb.viz/currency-style "narrowSymbol"}
+ price-col))))
+ (testing "narrowSymbol falls back to code with space if symbol not supported"
+ (is (= "[$KGS] #,##0.00" (format-string {::mb.viz/currency-in-header false,
+ ::mb.viz/currency "KGS",
+ ::mb.viz/currency-style "narrowSymbol"}
+ price-col))))))))
+
(deftest format-settings->format-string-test-3e
(mt/with-temporary-setting-values [custom-formatting {}]
(testing "Currency formatting"
@@ -243,11 +267,13 @@
(let [price-col {:semantic_type :type/Price, :effective_type :type/Float}]
(testing "Formatting options are ignored if currency-in-header is true or absent (defaults to true)"
(is (= "#,##0.00" (format-string {::mb.viz/currency-style "symbol"} price-col)))
+ (is (= "#,##0.00" (format-string {::mb.viz/currency-style "narrowSymbol"} price-col)))
(is (= "#,##0.00" (format-string {::mb.viz/currency-style "name"} price-col)))
(is (= "#,##0.00" (format-string {::mb.viz/currency-style "code"} price-col)))
(is (= "#,##0.00" (format-string {::mb.viz/currency "USD"} price-col)))
(is (= "#,##0.00" (format-string {::mb.viz/currency "EUR"} price-col)))
(is (= "#,##0.00" (format-string {::currency-in-header true, ::mb.viz/currency-style "symbol"} price-col)))
+ (is (= "#,##0.00" (format-string {::currency-in-header true, ::mb.viz/currency-style "narrowSymbol"} price-col)))
(is (= "#,##0.00" (format-string {::currency-in-header true, ::mb.viz/currency-style "name"} price-col)))
(is (= "#,##0.00" (format-string {::currency-in-header true, ::mb.viz/currency-style "code"} price-col)))
(is (= "#,##0.00" (format-string {::currency-in-header true, ::mb.viz/currency "USD"} price-col)))
@@ -515,6 +541,19 @@
(first (xlsx-export [{:id 0, :name "Col", :semantic_type :type/Cost :field_ref [:field 0]}]
{::mb.viz/column-settings {{::mb.viz/field-id 0} {::mb.viz/currency "EUR"}}}
[]))))
+ ;; narrowSymbol uses symbol_native (e.g. "$" for CAD instead of "CA$")
+ (is (= ["Col ($)"]
+ (first (xlsx-export [{:id 0, :name "Col", :semantic_type :type/Cost :field_ref [:field 0]}]
+ {::mb.viz/column-settings {{::mb.viz/field-id 0}
+ {::mb.viz/currency "CAD",
+ ::mb.viz/currency-style "narrowSymbol"}}}
+ []))))
+ ;; narrowSymbol falls back to code if symbol not supported
+ (is (= ["Col (KGS)"]
+ (first (xlsx-export [{:id 0, :name "Col", :semantic_type :type/Cost :field_ref [:field 0]}]
+ {::mb.viz/column-settings {{::mb.viz/field-id 0}
+ {::mb.viz/currency "KGS", ::mb.viz/currency-style "narrowSymbol"}}}
+ []))))
;; Falls back to code if native symbol is not supported
(is (= ["Col (KGS)"]
(first (xlsx-export [{:id 0, :name "Col", :semantic_type :type/Cost :field_ref [:field 0]}]