Skip to content

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

Move test helper parser to lib (#69405)

Romeo Van Snick9f22d9434134
diff --git a/e2e/test/scenarios/question/summarization.cy.spec.js b/e2e/test/scenarios/question/summarization.cy.spec.js
index 610d35683b2..d578edab420 100644
--- a/e2e/test/scenarios/question/summarization.cy.spec.js
+++ b/e2e/test/scenarios/question/summarization.cy.spec.js
@@ -1,6 +1,7 @@
const { H } = cy;
import { dedent } from "ts-dedent";
+import { SAMPLE_DB_ID } from "e2e/support/cypress_data";
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import { ORDERS_QUESTION_ID } from "e2e/support/cypress_sample_instance_data";
@@ -215,22 +216,45 @@ describe("scenarios > question > summarize sidebar", () => {
});
it("should handle (removing) multiple metrics when one is sorted (metabase#12625)", () => {
- H.createQuestion(
- {
- name: "12625",
- query: {
- "source-table": ORDERS_ID,
- aggregation: [
- ["count"],
- ["sum", ["field", ORDERS.SUBTOTAL, null]],
- ["sum", ["field", ORDERS.TOTAL, null]],
+ H.createTestQuery({
+ database: SAMPLE_DB_ID,
+ stages: [
+ {
+ source: { type: "table", id: ORDERS_ID },
+ aggregations: [
+ { type: "operator", operator: "count" },
+ {
+ type: "operator",
+ operator: "sum",
+ args: [{ type: "column", name: "SUBTOTAL" }],
+ },
+ {
+ type: "operator",
+ operator: "sum",
+ args: [{ type: "column", name: "TOTAL" }],
+ },
+ ],
+ breakouts: [
+ {
+ type: "column",
+ name: "CREATED_AT",
+ sourceName: "ORDERS",
+ unit: "year",
+ },
+ ],
+ orderBys: [
+ {
+ direction: "desc",
+ type: "column",
+ name: "sum",
+ displayName: "Sum of Subtotal",
+ },
],
- breakout: [["field", ORDERS.CREATED_AT, { "temporal-unit": "year" }]],
- "order-by": [["desc", ["aggregation", 1]]],
},
- },
- { visitQuestion: true },
- );
+ ],
+ })
+ .then((dataset_query) => H.createCard({ name: "12625", dataset_query }))
+ .then((card) => H.visitQuestion(card.id));
H.summarize();
diff --git a/frontend/src/metabase-types/api/query.ts b/frontend/src/metabase-types/api/query.ts
index 453f06cb73e..684b784f654 100644
--- a/frontend/src/metabase-types/api/query.ts
+++ b/frontend/src/metabase-types/api/query.ts
@@ -482,7 +482,7 @@ export type TestLiteralSpec = {
export type TestOperatorSpec = {
type: "operator";
operator: string;
- args: TestExpressionSpec[];
+ args?: TestExpressionSpec[];
};
export type TestTemporalBucketSpec = {
diff --git a/src/metabase/lib/js.cljs b/src/metabase/lib/js.cljs
index 803316f0d30..24b78d15367 100644
--- a/src/metabase/lib/js.cljs
+++ b/src/metabase/lib/js.cljs
@@ -59,8 +59,6 @@
[clojure.set :as set]
[clojure.string :as str]
[goog.object :as gobject]
- [malli.core :as mc]
- [malli.transform :as mtx]
[medley.core :as m]
^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize]
[metabase.lib.aggregation :as lib.aggregation]
@@ -87,7 +85,6 @@
[metabase.lib.query :as lib.query]
[metabase.lib.query.test-spec :as lib.query.test-spec]
[metabase.lib.schema.ref :as lib.schema.ref]
- [metabase.lib.schema.test-spec :as lib.schema.test-spec]
[metabase.lib.types.isa :as lib.types.isa]
[metabase.lib.util :as lib.util]
[metabase.lib.util.unique-name-generator :as lib.util.unique-name-generator]
@@ -2789,24 +2786,9 @@
lib.native/validate-template-tags
clj->js))
-(defn- decode-js-key
- [cljs-key]
- (cond-> cljs-key
- (keyword? cljs-key) (name)
- true (js-key->cljs-key)))
-
-(def parse-query-spec
- "Parser for query-spec."
- (mc/coercer [:ref ::lib.schema.test-spec/test-query-spec]
- (mtx/transformer
- mtx/json-transformer
- (mtx/key-transformer {:decode decode-js-key})
- mtx/strip-extra-keys-transformer
- mtx/default-value-transformer)))
-
(defn ^:export test-query
"Creates a query from a test query spec."
[metadata-providerable js-query-spec]
- (let [query-spec (js->clj js-query-spec :keywordize-keys true)
- parsed-query-spec (parse-query-spec query-spec)]
- (lib.query.test-spec/test-query metadata-providerable parsed-query-spec)))
+ (-> js-query-spec
+ (js->clj :keywordize-keys true)
+ (->> (lib.query.test-spec/test-query metadata-providerable))))
diff --git a/src/metabase/lib/query/test_spec.cljc b/src/metabase/lib/query/test_spec.cljc
index 2c916b3f0c5..139324c059d 100644
--- a/src/metabase/lib/query/test_spec.cljc
+++ b/src/metabase/lib/query/test_spec.cljc
@@ -1,6 +1,8 @@
(ns metabase.lib.query.test-spec
(:refer-clojure :exclude [mapv name empty?])
(:require
+ [malli.core :as mc]
+ [malli.transform :as mtx]
[medley.core :as m]
[metabase.lib.aggregation :as lib.aggregation]
[metabase.lib.binning :as lib.binning]
@@ -25,6 +27,7 @@
[metabase.lib.schema.test-spec :as lib.schema.test-spec]
[metabase.lib.stage :as lib.stage]
[metabase.lib.temporal-bucket :as lib.temporal-bucket]
+ [metabase.util :as u]
[metabase.util.malli :as mu]
[metabase.util.performance :refer [mapv]]))
@@ -310,10 +313,20 @@
fields (append-fields stage-number fields)
limit (lib.limit/limit stage-number limit)))
+(def parse-query-spec
+ "Parser for query-spec."
+ (mc/decoder [:ref ::lib.schema.test-spec/test-query-spec]
+ (mtx/transformer
+ mtx/json-transformer
+ (mtx/key-transformer {:decode #(-> % u/->kebab-case-en keyword)})
+ mtx/strip-extra-keys-transformer
+ mtx/default-value-transformer)))
+
(mu/defn test-query :- ::lib.schema/query
"Creates a query from a test query spec."
[metadata-providerable :- ::lib.schema.metadata/metadata-providerable
- {:keys [stages]} :- ::lib.schema.test-spec/test-query-spec]
- (let [source (->> stages first :source (find-source metadata-providerable))
+ query-spec :- :any]
+ (let [{:keys [stages]} (parse-query-spec query-spec)
+ source (->> stages first :source (find-source metadata-providerable))
query (lib.query/query metadata-providerable source)]
(reduce-kv append-stage-clauses query stages)))
diff --git a/src/metabase/lib/schema/test_spec.cljc b/src/metabase/lib/schema/test_spec.cljc
index ad4dce61d9f..d2bbc526b55 100644
--- a/src/metabase/lib/schema/test_spec.cljc
+++ b/src/metabase/lib/schema/test_spec.cljc
@@ -56,7 +56,7 @@
(mr/def ::test-order-by-spec
[:merge
- ::test-column-spec
+ ::test-column-with-binning-spec
[:map
[:direction {:optional true} [:maybe [:ref ::lib.schema.order-by/direction]]]]])
@@ -73,7 +73,7 @@
[:map
[:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :operator]]
[:operator ::test-operator-spec]
- [:args [:sequential [:ref ::test-expression-spec]]]])
+ [:args {:default []} [:sequential [:ref ::test-expression-spec]]]])
(mr/def ::test-expression-spec
[:multi {:dispatch (comp keyword :type)}
diff --git a/test/metabase/lib/query/test_spec_test.cljc b/test/metabase/lib/query/test_spec_test.cljc
index e20c53fe1f2..68cd7dee5d7 100644
--- a/test/metabase/lib/query/test_spec_test.cljc
+++ b/test/metabase/lib/query/test_spec_test.cljc
@@ -111,8 +111,7 @@
{:stages [{:source {:type :table
:id (meta/id :venues)}
:aggregations [{:type :operator
- :operator :count
- :args []}]}]})]
+ :operator :count}]}]})]
(is (=? [[:count {}]]
(lib/aggregations query))))))
@@ -491,7 +490,7 @@
:name "PRICE"
:bins 10
:direction :asc}]}]})]
- (is (=? [[:asc {} [:field {:binning {:strategy :num-bins}} (meta/id :venues :price)]]]
+ (is (=? [[:asc {} [:field {:binning {:strategy :num-bins :num-bins 10}} (meta/id :venues :price)]]]
(lib/order-bys query))))))
(deftest ^:parallel test-query-expression-with-aggregation-test