The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Use lib for MBQL construction in Workspace tests (#70532)
Chris Truter47c037a3f87c
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/api_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/api_test.cljindex d16ff0a64e3..2f54fbae89c 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/api_test.clj@@ -38,9 +38,13 @@"Alias for ws.tu/ws-url for convenience."ws.tu/ws-url)+(def ^:private q+ "Alias for ws.tu/q for convenience."+ ws.tu/q)+(def ^:private ->native- "It's convenient to construct queries using MBQL helper, but only native queries can be used in workspaces."- (comp mt/native-query ws.tu/mbql->native))+ "Alias for ws.tu/->native for convenience."+ ws.tu/->native)(deftest workspace-crud-flow-test(let [workspace-name (str "Workspace " (random-uuid))@@ -672,7 +676,7 @@(mt/user-http-request :crowberto :post 400 (ws-url (:id workspace) "/transform"){:name "Should Fail":source {:type "query"- :query (mt/mbql-query venues)}+ :query (q :venues)}:target {:type "table":name "should_fail"}}))))))@@ -703,7 +707,7 @@(let [response (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform"){:name "New Transform":source {:type "query"- :query (->native (mt/mbql-query venues))}+ :query (->native (q :venues))}:target {:type "table":name "new_transform_output"}})]@@ -726,7 +730,7 @@(mt/user-http-request :crowberto :post 400 (ws-url ws-id "/transform"){:name "Should Fail":source {:type "query"- :query (->native (mt/mbql-query venues))}+ :query (->native (q :venues))}:target {:type "table":name "should_fail"}})))))))))@@ -743,7 +747,7 @@(mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform"){:name "Workspace Transform":source {:type "query"- :query (->native (mt/mbql-query transforms_products))}+ :query (->native (q :transforms_products))}:target {:type "table":name table-name}})))(is (=? {:id ws-id, :status "ready"}@@ -765,7 +769,7 @@(with-transform-cleanup! [orig-name "ws_tables_not_run_test"](mt/with-temp [:model/Transform x1 {:name "My X1":source {:type "query"- :query (mt/native-query (ws.tu/mbql->native (mt/mbql-query orders {:limit 10})))}+ :query (->native (-> (q :orders) (lib/limit 10)))}:target {:type "table":database (mt/id):schema "public"@@ -985,7 +989,7 @@(let [transform (mt/user-http-request :crowberto :post 200 (ws-url (:id ws) "/transform"){:name "New Transform":source {:type "query"- :query (mt/native-query (ws.tu/mbql->native (mt/mbql-query venues)))}+ :query (->native (q :venues))}:target {:type "table":database (mt/id):schema "public"@@ -1072,14 +1076,14 @@(let [tx1 (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/transform"){:name "Transform 1":source {:type "query"- :query (->native (mt/mbql-query orders {:aggregation [[:count]]}))}+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}:target {:type "table":schema target-schema:name "target_table_1"}})_tx2 (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/transform"){:name "Transform 2":source {:type "query"- :query (->native (mt/mbql-query orders {:aggregation [[:count]]}))}+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}:target {:type "table":schema target-schema:name "target_table_2"}})]@@ -1114,7 +1118,7 @@(ws-url (:id workspace) "/transform" new-ref-id){:name "New Transform via Upsert":source {:type "query"- :query (->native (mt/mbql-query orders {:aggregation [[:count]]}))}+ :query (-> (q :orders) (lib/aggregate (lib/count)) ->native)}:target {:type "table":schema target-schema:name "upsert_test_table"}})]@@ -1144,7 +1148,7 @@(mt/user-http-request :crowberto :put 400(ws-url (:id workspace) "/transform" ref-id){:source {:type "query"- :query (->native (mt/mbql-query orders {:aggregation [[:count]]}))}+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}:target {:type "table":schema target-schema:name "missing_name_test"}})))))@@ -1164,7 +1168,7 @@(ws-url (:id workspace) "/transform" ref-id){:name "Test Transform":source {:type "query"- :query (->native (mt/mbql-query orders {:aggregation [[:count]]}))}})))))))))+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}})))))))))(deftest upsert-transform-archived-workspace-test(testing "PUT /api/ee/workspace/:id/transform/:txid returns 400 for archived workspace"@@ -1178,7 +1182,7 @@(ws-url (:id workspace) "/transform" new-ref-id){:name "New Transform":source {:type "query"- :query (->native (mt/mbql-query orders {:aggregation [[:count]]}))}+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}:target {:type "table":schema target-schema:name "archived_ws_test"}})))))))@@ -1191,7 +1195,7 @@(mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/transform"){:name "Existing Transform":source {:type "query"- :query (->native (mt/mbql-query orders {:aggregation [[:count]]}))}+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}:target {:type "table":schema target-schema:name "conflict_target_table"}})@@ -1202,7 +1206,7 @@(ws-url (:id workspace) "/transform" new-ref-id){:name "Conflicting Transform":source {:type "query"- :query (->native (mt/mbql-query orders {:aggregation [[:count]]}))}+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}:target {:type "table":schema target-schema:name "conflict_target_table"}}))))))))@@ -1218,7 +1222,7 @@(ws-url (:id workspace) "/transform" new-ref-id){:name "First Transform":source {:type "query"- :query (->native (mt/mbql-query orders {:aggregation [[:count]]}))}+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}:target {:type "table":database (mt/id):schema target-schema@@ -1238,7 +1242,7 @@(ws-url (:id workspace) "/transform" new-ref-id){:name "First Transform":source {:type "query"- :query (->native (mt/mbql-query orders {:aggregation [[:count]]}))}+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}:target {:type "table":schema target-schema:name "status_transition_test"}})@@ -1289,9 +1293,7 @@(let [target-schema (t2/select-one-fn :schema :model/Table (mt/id :orders))](mt/with-temp [:model/Transform x1 {:name "Transform":source {:type "query"- :query (-> (mt/mbql-query orders {:aggregation [[:count]]})- ws.tu/mbql->native- mt/native-query)}+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}:target {:type "table":database (mt/id):schema target-schema@@ -1388,7 +1390,7 @@ws-2 {:name "Workspace 2"}](let [body {:name "Transform for execute test":source {:type "query"- :query (mt/native-query (ws.tu/mbql->native (mt/mbql-query orders {:aggregation [[:count]]})))}+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}:target {:type "table":database (mt/id):schema nil@@ -1688,19 +1690,15 @@:schema (driver.sql/default-schema driver/*driver*):name (str/replace (str "t_" (random-uuid)) "-" "_")})-(defn- my-native-query [db-id sql & [card-mapping]]- ;; TODO (Chris 2025-12-11) -- don't build MBQL manually- ;; For some reason, when is use mt/native-query the transforms hook thinks this is MBQL.- ;; It's probably a dialect version issue.- {:database db-id- :lib/type :mbql/query- :stages [{:lib/type :mbql.stage/native- :native sql- :template-tags (u/for-map [[tag card-id] card-mapping]- [tag {:name tag- :display-name tag- :type :card- :card-id card-id}])}]})+(defn- my-native-query [_db-id sql & [card-mapping]]+ (let [mp (mt/metadata-provider)]+ (cond-> (lib/native-query mp sql)+ card-mapping (lib/with-template-tags+ (u/for-map [[tag card-id] card-mapping]+ [tag {:name tag+ :display-name tag+ :type :card+ :card-id card-id}])))))(deftest external-transforms-test(testing "GET /api/ee/workspace/id/external/transform"@@ -1719,7 +1717,7 @@:target (random-target db-1)}:model/Transform {xf4-id :id} {:name "Not checked out - mbql":source {:type :query- :query (mt/mbql-query venues)}+ :query (q :venues)}:target (random-target db-1)}:model/Transform {xf5-id :id} {:name "Not checked out - native":source {:type "query"@@ -1728,7 +1726,7 @@;; Native transform referencing a card - should be disabled once BOT-694 is implemented:model/Card {card-id :id} {:name "Source Card":database_id db-1- :dataset_query (mt/mbql-query venues)}+ :dataset_query (q :venues)}:model/Transform {xf6-id :id} {:name "Not checked out - native with card dep":source {:type "query":query (my-native-query@@ -2110,7 +2108,7 @@(testing "MBQL transforms cannot be checked out"(mt/with-temp [:model/Transform tx {:name "MBQL Transform":source {:type :query- :query (mt/mbql-query venues)}+ :query (q :venues)}:target {:type "table":database (mt/id):schema "public"@@ -2139,7 +2137,7 @@(testing "POST /api/ee/workspace/:id/transform blocks MBQL transform checkout"(mt/with-temp [:model/Transform tx {:name "MBQL Transform":source {:type :query- :query (mt/mbql-query venues)}+ :query (q :venues)}:target {:type "table":database (mt/id):schema "public"@@ -2277,7 +2275,7 @@(ws.tu/with-workspaces! [ws {:name "Ungranted Run Test"}](let [transform {:name "Transform with input":source {:type "query"- :query (->native (mt/mbql-query orders {:aggregation [[:count]]}))}+ :query (->native (-> (q :orders) (lib/aggregate (lib/count))))}:target {:type "table":database (mt/id):schema "public"diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.cljindex 69f89d5e4be..69213bbb722 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj@@ -5,6 +5,7 @@[metabase-enterprise.workspaces.common :as ws.common][metabase-enterprise.workspaces.impl :as ws.impl][metabase-enterprise.workspaces.test-util :as ws.tu]+ [metabase.lib.core :as lib][metabase.sync.core :as sync][metabase.test :as mt][metabase.transforms.test-util :as transforms.tu]@@ -19,7 +20,7 @@(mt/with-model-cleanup [:model/Transform :model/Workspace](mt/with-temp [:model/Transform {transform-id :id} {:name "Transform 1":source {:type "query"- :query (mt/native-query (ws.tu/mbql->native (mt/mbql-query orders {:limit 3})))}+ :query (-> (ws.tu/q :orders) (lib/limit 3) ws.tu/->native)}:target {:type "table":name output-table-name:database (mt/id)@@ -57,7 +58,7 @@(t2/update! :model/WorkspaceTransform{:workspace_id (:id workspace) :ref_id (:ref_id isolated-transform)}{:source {:type "query"- :query (mt/native-query (ws.tu/mbql->native (mt/mbql-query venues {:limit 1})))}})+ :query (ws.tu/->native (-> (ws.tu/q :venues) (lib/limit 1)))}})(let [ref-id (:ref_id isolated-transform)transform (t2/select-one :model/WorkspaceTransform :workspace_id (:id workspace) :ref_id ref-id)](is (=? {:status :failed}diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/isolation_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/isolation_test.cljindex 3fc005d042d..5a52452fb53 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/isolation_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/isolation_test.clj@@ -8,6 +8,7 @@[metabase.driver :as driver][metabase.driver.sql-jdbc.connection :as sql-jdbc.conn][metabase.driver.util :as driver.u]+ [metabase.lib.core :as lib][metabase.test :as mt][toucan2.core :as t2]))@@ -103,7 +104,8 @@(ws.common/add-to-changeset! (mt/user->id :crowberto) workspace:transform nil{:name "Transform A"- :source {:type "query" :query (mt/mbql-query orders {:limit 1})}+ :source {:type "query"+ :query (-> (ws.tu/q :orders) (lib/limit 1))}:target {:database (mt/id):schema "analytics":name "table_a"}})diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj b/enterprise/backend/test/metabase_enterprise/workspaces/test_util.cljindex f788c5ef459..a3a77e694e9 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj@@ -14,6 +14,8 @@[metabase.driver :as driver][metabase.driver.sql.normalize :as sql.normalize][metabase.driver.sql.query-processor :as sql.qp]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata][metabase.query-processor.preprocess :as qp.preprocess]^{:clj-kondo/ignore [:deprecated-namespace]}[metabase.query-processor.store :as qp.store]@@ -68,13 +70,26 @@;;;; Query helpers+(defn q+ "Create a lib query against a test database table, e.g. `(q :orders)`.+ Can be threaded with lib functions: `(-> (q :orders) (lib/limit 10))`."+ [table-kw]+ (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/table mp (mt/id table-kw)))))+(defn mbql->native- "Convert an MBQL query to a native query map suitable for use in transform tests.+ "Compile a query to a native SQL map. Works with both legacy MBQL and lib (pMBQL) queries.This generates driver-specific SQL with properly qualified table names."[query](qp.store/with-metadata-provider (mt/id)(sql.qp/mbql->native driver/*driver* (qp.preprocess/preprocess query))))+(defn ->native+ "Compile a query to native SQL form wrapped as a native query map.+ Useful when building test transforms, which need to be SQL flavored."+ [query]+ (mt/native-query (mbql->native query)))+;;;; Building blocks for test resource creation(defn- build-source-query