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
Create MBQL queries with the lib in unit and E2E tests (#68719)
Alexander Polyankin868b69bc46ba
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 9750b14846e..14719e5fdd9 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -772,6 +772,7 @@metabase.lib.schema.settingsmetabase.lib.schema.template-tagmetabase.lib.schema.temporal-bucketing+ metabase.lib.schema.test-specmetabase.lib.schema.utilmetabase.lib.schema.validatemetabase.lib.temporal-bucket@@ -1527,6 +1528,8 @@apiapp-dbconfig+ lib+ lib-bepremium-featuressearchutil}}diff --git a/.github/workflows/e2e-stress-test-flake-fix.yml b/.github/workflows/e2e-stress-test-flake-fix.ymlindex 271a9fb1de6..520481802a6 100644--- a/.github/workflows/e2e-stress-test-flake-fix.yml+++ b/.github/workflows/e2e-stress-test-flake-fix.yml@@ -120,10 +120,6 @@ jobs:- name: Prepare Cypress environmentid: cypress-prepuses: ./.github/actions/prepare-cypress-- - name: Compile CLJS- run: yarn build-pure:cljs-- name: Run Metabaserun: node e2e/runner/run_cypress_ci.js startdiff --git a/.github/workflows/e2e-test.yml b/.github/workflows/e2e-test.ymlindex 951e46ef85f..01f11c791dd 100644--- a/.github/workflows/e2e-test.yml+++ b/.github/workflows/e2e-test.yml@@ -105,9 +105,6 @@ jobs:if: github.run_attempt != '1'run: echo "specs=$(cat failed-specs)" >> $GITHUB_ENV- - name: Compile CLJS- run: yarn build-pure:cljs-- name: Run Metabaserun: node e2e/runner/run_cypress_ci.js startdiff --git a/e2e/support/config.js b/e2e/support/config.jsindex 1b492f406a1..8103f22b783 100644--- a/e2e/support/config.js+++ b/e2e/support/config.js@@ -46,67 +46,6 @@ const assetsResolverPlugin = {},};-// Plugin to allow loading the ClojureScript bundle only when it exists-// on the filesystem. This is to avoid breaking the build when the bundle-// is not present.-//-// If the bundle is not present, tests relying on it will fail, but the Cypress-// process will be allowed to start.-//-// The error will look like this:-//-// Dynamic require of "cljs/metabase.types.core" is not supported-//-// This allows us to avoid building the ClojureScript bundle when we don't need to (like in certain CI tasks).-const virtualClojureScriptPlugin = {- name: "virtualClojureScriptBundle",- setup(build) {- let cljsDir = null;-- async function isDirectory(path) {- try {- const stat = await fs.promises.stat(path);- return stat.isDirectory();- } catch (e) {- return false;- }- }-- build.onStart(async () => {- const baseDir = path.join(__dirname, "../../target");- const devDir = path.join(baseDir, "cljs_dev");- const prodDir = path.join(baseDir, "cljs_prod");-- const [devDirExists, prodDirExists] = await Promise.all([- isDirectory(devDir),- isDirectory(prodDir),- ]);-- if (prodDirExists) {- console.log("ClojureScript production bundle found");- cljsDir = prodDir;- } else if (devDirExists) {- console.log("ClojureScript development bundle found");- cljsDir = devDir;- } else {- console.log("No ClojureScript bundle found, not loading bundle");- cljsDir = null;- }- });-- build.onResolve({ filter: /^cljs\// }, async (args) => {- if (cljsDir === null) {- return {- path: args.path,- external: true,- };- }- const unnested = args.path.split(path.sep).slice(1).join(path.sep);- return { path: path.join(cljsDir, unnested + ".js") };- });- },-};-const defaultConfig = {// This is the functionality of the old cypress-plugins.js filesetupNodeEvents(cypressOn, config) {@@ -136,11 +75,7 @@ const defaultConfig = {loader: {".svg": "text",},- plugins: [- NodeModulesPolyfillPlugin(),- assetsResolverPlugin,- virtualClojureScriptPlugin,- ],+ plugins: [NodeModulesPolyfillPlugin(), assetsResolverPlugin],sourcemap: "inline",}),);diff --git a/e2e/support/helpers/api/createCard.ts b/e2e/support/helpers/api/createCard.tsnew file mode 100644index 00000000000..493226ef29a--- /dev/null+++ b/e2e/support/helpers/api/createCard.ts@@ -0,0 +1,18 @@+import type { Card, CreateCardRequest } from "metabase-types/api";++export type CardDetails = Partial<+ Pick<CreateCardRequest, "name" | "display" | "visualization_settings">+> &+ Omit<CreateCardRequest, "name" | "display" | "visualization_settings">;++const DEFAULT_CARD_DETAILS: Partial<CreateCardRequest> = {+ name: "Test card",+ display: "table",+ visualization_settings: {},+};++export function createCard(details: CardDetails): Cypress.Chainable<Card> {+ return cy+ .request("POST", "/api/card", { ...DEFAULT_CARD_DETAILS, ...details })+ .then(({ body }) => body);+}diff --git a/e2e/support/helpers/api/createTestQuery.ts b/e2e/support/helpers/api/createTestQuery.tsnew file mode 100644index 00000000000..194b206efc5--- /dev/null+++ b/e2e/support/helpers/api/createTestQuery.ts@@ -0,0 +1,12 @@+import type {+ OpaqueDatasetQuery,+ TestQuerySpecWithDatabase,+} from "metabase-types/api";++export function createTestQuery(+ querySpec: TestQuerySpecWithDatabase,+): Cypress.Chainable<OpaqueDatasetQuery> {+ return cy+ .request<OpaqueDatasetQuery>("POST", "/api/testing/query", querySpec)+ .then(({ body }) => body);+}diff --git a/e2e/support/helpers/api/index.ts b/e2e/support/helpers/api/index.tsindex 5796ca82e2c..b65ab29678f 100644--- a/e2e/support/helpers/api/index.ts+++ b/e2e/support/helpers/api/index.ts@@ -4,6 +4,7 @@ export { archiveCollection } from "./archiveCollection";export { archiveDashboard } from "./archiveDashboard";export { archiveQuestion } from "./archiveQuestion";export { createApiKey } from "./createApiKey";+export { createCard } from "./createCard";export { createCollection } from "./createCollection";export { createComment } from "./createComment";export { createDashboard } from "./createDashboard";@@ -28,6 +29,7 @@ export { createQuestionAndDashboard } from "./createQuestionAndDashboard";export { createReaction } from "./createReaction";export { createSnippet } from "./createSnippet";export { createSnippetFolder } from "./createSnippetFolder";+export { createTestQuery } from "./createTestQuery";export { createTimeline } from "./createTimeline";export { createTimelineEvent } from "./createTimelineEvent";export { createTimelineWithEvents } from "./createTimelineWithEvents";diff --git a/e2e/support/helpers/index.ts b/e2e/support/helpers/index.tsindex d32a1aef7c6..64237614d2a 100644--- a/e2e/support/helpers/index.ts+++ b/e2e/support/helpers/index.ts@@ -1,5 +1,4 @@export * from "./api";-export * from "./mbql";export * from "./e2e-action-helpers";export * from "./e2e-ad-hoc-question-helpers";export * from "./e2e-api-key-helpers";diff --git a/e2e/support/helpers/mbql/index.ts b/e2e/support/helpers/mbql/index.tsdeleted file mode 100644index 5830aa52173..00000000000--- a/e2e/support/helpers/mbql/index.ts+++ /dev/null@@ -1,6 +0,0 @@-export {- getMetadataProvider,- createQuery,- createNativeQuery,-} from "./wrappers";-export { createCardWithQuery, createCardWithNativeQuery } from "./question";diff --git a/e2e/support/helpers/mbql/metadata-provider.ts b/e2e/support/helpers/mbql/metadata-provider.tsdeleted file mode 100644index 3b8953ae946..00000000000--- a/e2e/support/helpers/mbql/metadata-provider.ts+++ /dev/null@@ -1,86 +0,0 @@-// WARNING:-// This module should not be imported directly in cypress-// harnesses, use an dynamic import instead.-import { getMetadataWithoutSettings as getMetadataFromState } from "metabase/selectors/metadata";-import * as Lib from "metabase-lib";-import type {- Card,- CardId,- Database,- Table,- TableId,-} from "metabase-types/api";--import { createMockEntitiesState } from "../../../../frontend/test/__support__/store";-import { SAMPLE_DB_ID, SAMPLE_DB_TABLES } from "../../cypress_data";--import type { GetMetadataOpts } from "./types";--const DEFAULT_TABLE_IDS: TableId[] = [- SAMPLE_DB_TABLES.STATIC_ORDERS_ID,- SAMPLE_DB_TABLES.STATIC_PEOPLE_ID,- SAMPLE_DB_TABLES.STATIC_PRODUCTS_ID,- SAMPLE_DB_TABLES.STATIC_REVIEWS_ID,-];-const DEFAULT_CARD_IDS: CardId[] = [];--/**- * Returns a Lib.MetadataProvider instance containing metadata for the- * provided database, tables and cards.- *- * WARNING:- * DO NOT USE THIS DIRECTLY IN TESTS.- * Use the `getMetadataProvider` from ./wrappers" instead.- *- * NOTE:- * This function uses plain Promises and Promise.all to avoid type issues- * with Cypress. The result needs to wrapped in cy.wrap() to be correctly usable- * in Cypress tests.- */-export async function getMetadataProvider({- databaseId = SAMPLE_DB_ID,- ...rest-}: GetMetadataOpts = {}) {- const metadata = await getMetadata({ databaseId, ...rest });- return Lib.metadataProvider(databaseId, metadata);-}--async function getMetadata({- databaseId = SAMPLE_DB_ID,- tableIds = DEFAULT_TABLE_IDS,- cardIds = DEFAULT_CARD_IDS,-}: GetMetadataOpts = {}) {- // Just one database for now, but wrapped to get the types to work- const databaseIds = [databaseId];-- const [databases, tables, cards] = await Promise.all([- // databases- Promise.all(- databaseIds.map((databaseId) =>- GET<Database>(`/api/database/${databaseId}`),- ),- ),-- // cards- Promise.all(- tableIds.map((tableId) =>- GET<Table>(`/api/table/${tableId}/query_metadata`),- ),- ),-- // tables- Promise.all(cardIds.map((cardId) => GET<Card>(`/api/card/${cardId}`))),- ]);-- const entities = createMockEntitiesState({- databases,- tables,- questions: cards,- });- return getMetadataFromState({ entities });-}--async function GET<T>(url: string): Promise<T> {- const response = await fetch(url);- return response.json();-}diff --git a/e2e/support/helpers/mbql/question.ts b/e2e/support/helpers/mbql/question.tsdeleted file mode 100644index 7ad9196f4f5..00000000000--- a/e2e/support/helpers/mbql/question.ts+++ /dev/null@@ -1,104 +0,0 @@-import type * as Lib from "metabase-lib";-import type { CreateTestQueryOpts } from "metabase-lib/test-helpers";-import type { DatabaseId } from "metabase-types/api";--import type { QuestionDetails } from "../api";-import { type Options, question } from "../api/createQuestion";--import type { GetMetadataOpts } from "./types";-import { createNativeQuery, createQuery } from "./wrappers";--/**- * Create a card using the provided details and a MBQL query.- *- * @param details.metadata -- * The metadata provider to use for the query,- * or options used to create one on the fly.- * If no metadata is provided, the query will be created with- * the default metadata provider.- *- * @example- * // If no metadata is provided, the card will be created with- * // the default metadata provider.- * H.createTestQuestion({- * name: "My question",- * query: {- * databaseId: SAMPLE_DATABASE_ID,- * stages: [- * ...- * ]- * }- * })- * })- *- * @example- * // Provide custom metadata entities relevant to your query.- * H.createTestQuestion({- * name: "My question",- * databaseId: SAMPLE_DATABASE_ID,- * query: "SELECT * FROM orders"- * })- * })- *- */--type CardWithQueryDetails = Omit<QuestionDetails, "dataset_query"> & {- metadata?: GetMetadataOpts | Lib.MetadataProvider;- query: CreateTestQueryOpts;-};--export function createCardWithQuery(- { metadata = {}, query, ...details }: CardWithQueryDetails,- options: Options = {},-) {- return createQuery(metadata, query).then((dataset_query) =>- question({ ...details, dataset_query }, options),- );-}--type CardWithNativeQueryDetails = Omit<QuestionDetails, "dataset_query"> & {- metadata?: GetMetadataOpts | Lib.MetadataProvider;- databaseId: DatabaseId;- query: string;-};--/**- * Create a card using the provided details and a native query.- *- * @param details.metadata -- * The metadata provider to use for the query,- * or options used to create one on the fly.- * If no metadata is provided, the query will be created with- * the default metadata provider.- *- * @example- * // If no metadata is provided, the card will be created with- * // the default metadata provider.- * H.createCardWithNativeQuery({- * name: "My question",- * databaseId: SAMPLE_DATABASE_ID,- * query: "SELECT * FROM orders"- * })- * })- *- * @example- * // Provide custom metadata entities relevant to your query.- * H.createCardWithNativeQuery({- * name: "My question",- * databaseId: SAMPLE_DATABASE_ID,- * metadata: {- * databaseId: SAMPLE_DATABASE_ID,- * cardIds: [SAMPLE_CARD_ID],- * },- * query: "SELECT * FROM orders"- * })- * })- */-export function createCardWithNativeQuery(- { metadata = {}, databaseId, query, ...details }: CardWithNativeQueryDetails,- options: Options = {},-) {- return createNativeQuery(metadata, databaseId, query).then((dataset_query) =>- question({ ...details, dataset_query }, options),- );-}diff --git a/e2e/support/helpers/mbql/types.ts b/e2e/support/helpers/mbql/types.tsdeleted file mode 100644index adcc0c0b2f2..00000000000--- a/e2e/support/helpers/mbql/types.ts+++ /dev/null@@ -1,7 +0,0 @@-import type { CardId, DatabaseId, TableId } from "metabase-types/api";--export type GetMetadataOpts = {- databaseId?: DatabaseId;- tableIds?: TableId[];- cardIds?: CardId[];-};diff --git a/e2e/support/helpers/mbql/wrappers.ts b/e2e/support/helpers/mbql/wrappers.tsdeleted file mode 100644index 2c582938489..00000000000--- a/e2e/support/helpers/mbql/wrappers.ts+++ /dev/null@@ -1,103 +0,0 @@-import type * as Lib from "metabase-lib";-import type { CreateTestQueryOpts } from "metabase-lib/test-helpers";-import type { DatabaseId, OpaqueDatasetQuery } from "metabase-types/api";--import type { GetMetadataOpts } from "./types";--/**- * Returns a Lib.MetadataProvider instance containing metadata for the- * entities defined in opts.- *- * @param opts.databaseId - The database to fetch metadata for- * @param opts.tableIds - The tables to fetch metadata for- * @param opts.cardIds - Cards to fetch metadata for- */-export function getMetadataProvider(- opts?: GetMetadataOpts,-): Cypress.Chainable<Lib.MetadataProvider> {- return (- cy- // we need to log in before we can get metadata- .getCookie("metabase.SESSION_ID")- .then(() => _getMetadataProvider({ ...opts }))- );-}--// Helper function which calls getMetadataProvider from ./metadata-provider-// This module needs to imported lazily not to crash Cypress and to avoid importing-// cljs in tests that don't need it.-async function _getMetadataProvider(- opts?: GetMetadataOpts,-): Promise<Lib.MetadataProvider> {- const { getMetadataProvider } = await import("./metadata-provider");- return getMetadataProvider(opts);-}--/**- * Build an MBQL query using a custom DSL that simplifies field selection and creation.- *- * @param metadataProvider - A Lib.MetadataProvider instance or a GetMetadataOpts object that will be user to instantiate a Lib.MetadataProvider- * @param opts - The details of the query to create- */-export function createQuery(- metadataProvider: Lib.MetadataProvider | GetMetadataOpts,- opts: CreateTestQueryOpts,-): Cypress.Chainable<OpaqueDatasetQuery> {- return ensureProvider(metadataProvider).then((provider) => {- return createTestJsQuery(provider, opts);- });-}--// Helper function which calls createTestJsQuery from metabase-lib/test-helpers.-// This module needs to imported lazily not to crash Cypress and to avoid importing-// cljs in tests that don't need it.-async function createTestJsQuery(- metadataProvider: Lib.MetadataProvider,- opts: CreateTestQueryOpts,-): Promise<OpaqueDatasetQuery> {- const { createTestJsQuery } = await import("metabase-lib/test-helpers");- return createTestJsQuery(metadataProvider, opts);-}--/**- * Build a native MBQL query using a custom DSL that simplifies field selection and creation.- *- * @param metadataProvider - A Lib.MetadataProvider instance or a GetMetadataOpts object that will be user to instantiate a Lib.MetadataProvider- * @param databaseId - The database the query is for- * @param query - The native query string- */-export function createNativeQuery(- metadataProvider: Lib.MetadataProvider | GetMetadataOpts,- databaseId: DatabaseId,- query: string,-): Cypress.Chainable<OpaqueDatasetQuery> {- return ensureProvider(metadataProvider).then((provider) =>- createTestNativeJsQuery(provider, databaseId, query),- );-}--// Helper function which calls createTestNativeJsQuery from metabase-lib/test-helpers.-// This module needs to imported lazily not to crash Cypress and to avoid importing-// cljs in tests that don't need it.-async function createTestNativeJsQuery(- metadataProvider: Lib.MetadataProvider,- databaseId: DatabaseId,- query: string,-) {- const { createTestNativeJsQuery } = await import("metabase-lib/test-helpers");- return createTestNativeJsQuery(metadataProvider, databaseId, query);-}--function ensureProvider(- providerOrOpts: GetMetadataOpts | Lib.MetadataProvider,-): Cypress.Chainable<Lib.MetadataProvider> {- return isMetadataProvider(providerOrOpts)- ? cy.wrap(providerOrOpts)- : getMetadataProvider(providerOrOpts);-}--function isMetadataProvider(- opts: GetMetadataOpts | Lib.MetadataProvider,-): opts is Lib.MetadataProvider {- return "cache" in opts;-}diff --git a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.tsindex 7cad21764e9..5b21462d834 100644--- a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts+++ b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts@@ -1,6 +1,6 @@const { H } = cy;-import { SAMPLE_DB_ID, WRITABLE_DB_ID } from "e2e/support/cypress_data";+import { WRITABLE_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";import type {@@ -833,16 +833,25 @@ describe("54205", () => {});});- H.createQuestion(- {- database: WRITABLE_DB_ID,- name: "Q 54205",- query: {- "source-table": tableId,+ H.createTestQuery({+ database: WRITABLE_DB_ID,+ stages: [+ {+ source: {+ type: "table",+ id: tableId,+ },},- },- { wrapId: true, visitQuestion: true },- );+ ],+ }).then((query) => {+ H.createCard({+ name: "Q 54205",+ dataset_query: query,+ }).then((card) => {+ cy.wrap(card.id).as("questionId");+ H.visitQuestion(card.id);+ });+ });});cy.findByTestId("query-visualization-root").contains("Name").click();@@ -1420,36 +1429,17 @@ describe("issue #47005", () => {H.restore("postgres-12");cy.signInAsNormalUser();- H.createCardWithQuery({+ H.createQuestion({name: "Question A",query: {- databaseId: SAMPLE_DB_ID,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- },- ],+ "source-table": ORDERS_ID,},}).then(({ body: question }) => {- H.createCardWithQuery(+ H.createQuestion({name: "Question B",- metadata: {- cardIds: [question.id],- },query: {- databaseId: SAMPLE_DB_ID,- stages: [- {- source: {- type: "card",- id: question.id,- },- },- ],+ "source-table": "card__" + question.id,},},{ visitQuestion: true },@@ -1472,30 +1462,13 @@ describe("issue 66210", () => {H.restore();cy.signInAsAdmin();- H.createCardWithQuery({+ H.createQuestion({name: METRIC_NAME,- type: "metric",query: {- databaseId: SAMPLE_DB_ID,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: {- type: "operator",- operator: "count",- args: [],- },- },- ],- },- ],+ "source-table": ORDERS_ID,+ aggregation: [["count"]],},+ type: "metric",});cy.visit("/");diff --git a/frontend/src/metabase-lib/comparison.unit.spec.ts b/frontend/src/metabase-lib/comparison.unit.spec.tsindex b4f67f998e7..dfb846c9c00 100644--- a/frontend/src/metabase-lib/comparison.unit.spec.ts+++ b/frontend/src/metabase-lib/comparison.unit.spec.ts@@ -1,31 +1,22 @@import * as Lib from "metabase-lib";-import { ORDERS_ID } from "metabase-types/api/mocks/presets";-import {- SAMPLE_DATABASE,- SAMPLE_PROVIDER,- createTestQuery,-} from "./test-helpers";+import { createQueryWithClauses } from "./test-helpers";describe("findColumnIndexesFromLegacyRefs", () => {const stageIndex = -1;it("should match columns that differ only by temporal buckets", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [+ const query = createQueryWithClauses({+ breakouts: [{- source: { type: "table", id: ORDERS_ID },- breakouts: [- {- name: "CREATED_AT",- unit: "year",- },- {- name: "CREATED_AT",- unit: "month",- },- ],+ tableName: "ORDERS",+ columnName: "CREATED_AT",+ temporalBucketName: "Year",+ },+ {+ tableName: "ORDERS",+ columnName: "CREATED_AT",+ temporalBucketName: "Month",},],});@@ -40,21 +31,17 @@ describe("findColumnIndexesFromLegacyRefs", () => {});it("should match columns that differ only by binning", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [+ const query = createQueryWithClauses({+ breakouts: [+ {+ tableName: "ORDERS",+ columnName: "TOTAL",+ binningStrategyName: "10 bins",+ },{- source: { type: "table", id: ORDERS_ID },- breakouts: [- {- name: "TOTAL",- binningCount: 10,- },- {- name: "TOTAL",- binningCount: 50,- },- ],+ tableName: "ORDERS",+ columnName: "TOTAL",+ binningStrategyName: "50 bins",},],});diff --git a/frontend/src/metabase-lib/query.ts b/frontend/src/metabase-lib/query.tsindex 92c22b189c6..125ef2655be 100644--- a/frontend/src/metabase-lib/query.ts+++ b/frontend/src/metabase-lib/query.ts@@ -8,6 +8,7 @@ import type {LegacyDatasetQuery,OpaqueDatasetQuery,TableId,+ TestQuerySpec,} from "metabase-types/api";import type {@@ -156,3 +157,10 @@ export function fromJsQueryAndMetadata(export function toJsQuery(query: Query): OpaqueDatasetQuery {return ML.to_js_query(query);}++export function createTestQuery(+ metadataProvider: MetadataProvider,+ querySpec: TestQuerySpec,+): Query {+ return ML.test_query(metadataProvider, querySpec);+}diff --git a/frontend/src/metabase-lib/query.unit.spec.ts b/frontend/src/metabase-lib/query.unit.spec.tsindex b0f330009f1..e9e7ba31003 100644--- a/frontend/src/metabase-lib/query.unit.spec.ts+++ b/frontend/src/metabase-lib/query.unit.spec.ts@@ -1,9 +1,15 @@import * as Lib from "metabase-lib";+import {+ ORDERS_ID,+ PEOPLE_ID,+ PRODUCTS_ID,+} from "metabase-types/api/mocks/presets";import {DEFAULT_QUERY,SAMPLE_DATABASE,SAMPLE_METADATA,+ SAMPLE_PROVIDER,createQuery,} from "./test-helpers";@@ -45,3 +51,427 @@ describe("stageIndexes", () => {expect(Lib.stageIndexes(query)).toEqual([0, 1, 2]);});});++describe("createTestQuery", () => {+ describe("source", () => {+ it("should create a query with a table source", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ },+ ],+ });+ expect(Lib.sourceTableOrCardId(query)).toBe(PRODUCTS_ID);+ });+ });++ describe("fields", () => {+ it("should create a query with fields", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ fields: [+ {+ type: "column",+ name: "ID",+ },+ {+ type: "column",+ name: "VENDOR",+ },+ ],+ },+ ],+ });++ const fields = Lib.fields(query, 0);+ expect(fields).toHaveLength(2);++ expect(Lib.displayInfo(query, 0, fields[0])).toMatchObject({+ name: "ID",+ });+ expect(Lib.displayInfo(query, 0, fields[1])).toMatchObject({+ name: "VENDOR",+ });+ });+ });++ describe("expressions", () => {+ it("should create a query with expressions", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ expressions: [+ {+ name: "Foo",+ value: {+ type: "operator",+ operator: "+",+ args: [+ { type: "column", name: "ID" },+ { type: "literal", value: 42 },+ ],+ },+ },+ ],+ },+ ],+ });++ const expressions = Lib.expressions(query, 0);+ expect(expressions).toHaveLength(1);++ expect(Lib.displayInfo(query, 0, expressions[0])).toMatchObject({+ displayName: "Foo",+ });+ });+ });++ describe("breakouts", () => {+ it("should create a query with breakouts", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ breakouts: [{ type: "column", name: "CATEGORY" }],+ },+ ],+ });++ const breakouts = Lib.breakouts(query, 0);+ expect(breakouts).toHaveLength(1);+ expect(Lib.displayInfo(query, 0, breakouts[0])).toMatchObject({+ displayName: "Category",+ });+ });++ it("should create a query with temporal breakouts", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ breakouts: [{ type: "column", name: "CREATED_AT", unit: "month" }],+ },+ ],+ });++ const breakouts = Lib.breakouts(query, 0);+ expect(breakouts).toHaveLength(1);+ expect(Lib.displayInfo(query, 0, breakouts[0])).toMatchObject({+ displayName: "Created At: Month",+ });+ });++ it("should create a query with breakouts with binning based on count", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ breakouts: [+ {+ type: "column",+ name: "RATING",+ bins: 10,+ },+ ],+ },+ ],+ });++ const breakouts = Lib.breakouts(query, 0);+ expect(breakouts).toHaveLength(1);+ expect(Lib.displayInfo(query, 0, breakouts[0])).toMatchObject({+ displayName: "Rating: 10 bins",+ });+ });++ it("should create a query with breakouts with binning based on width", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PEOPLE_ID,+ },+ breakouts: [+ {+ type: "column",+ name: "LATITUDE",+ sourceName: "PEOPLE",+ binWidth: 10,+ },+ ],+ },+ ],+ });++ const breakouts = Lib.breakouts(query, 0);+ expect(breakouts).toHaveLength(1);+ expect(Lib.displayInfo(query, 0, breakouts[0])).toMatchObject({+ displayName: "Latitude: 10°",+ });+ });+ });++ describe("joins", () => {+ it("should create a query with joins", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ joins: [+ {+ source: {+ type: "table",+ id: PEOPLE_ID,+ },+ strategy: "inner-join",+ conditions: [+ {+ operator: "=",+ left: {+ type: "column",+ name: "VENDOR",+ },+ right: {+ type: "operator",+ operator: "+",+ args: [+ {+ type: "column",+ name: "ID",+ },+ {+ type: "literal",+ value: 1,+ },+ ],+ },+ },+ ],+ },+ ],+ },+ ],+ });++ const joins = Lib.joins(query, -1);+ expect(joins).toHaveLength(1);++ const conditions = Lib.joinConditions(joins[0]);+ expect(conditions).toHaveLength(1);++ expect(Lib.displayInfo(query, -1, conditions[0])).toMatchObject({+ displayName: "Vendor is ID + 1",+ });+ });++ it("should create a query with joins that have binning", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ joins: [+ {+ source: {+ type: "table",+ id: PEOPLE_ID,+ },+ strategy: "inner-join",+ conditions: [+ {+ operator: "=",+ left: {+ type: "column",+ name: "CREATED_AT",+ unit: "month",+ },+ right: {+ type: "column",+ name: "CREATED_AT",+ unit: "month",+ },+ },+ ],+ },+ ],+ },+ ],+ });++ const joins = Lib.joins(query, -1);+ expect(joins).toHaveLength(1);++ const conditions = Lib.joinConditions(joins[0]);+ expect(conditions).toHaveLength(1);++ expect(Lib.displayInfo(query, -1, conditions[0])).toMatchObject({+ displayName: "Created At: Month is Created At: Month",+ });+ });++ it("should create a query with suggested joins", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: ORDERS_ID,+ },+ joins: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ strategy: "inner-join",+ },+ ],+ },+ ],+ });++ const joins = Lib.joins(query, -1);+ expect(joins).toHaveLength(1);++ const conditions = Lib.joinConditions(joins[0]);+ expect(conditions).toHaveLength(1);++ expect(Lib.displayInfo(query, -1, conditions[0])).toMatchObject({+ displayName: "Product ID is ID",+ });+ });+ });++ describe("filters", () => {+ it("should create a query with filters", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ filters: [+ {+ type: "operator",+ operator: ">=",+ args: [+ { type: "column", name: "PRICE" },+ { type: "literal", value: 100 },+ ],+ },+ ],+ },+ ],+ });++ const filters = Lib.filters(query, -1);+ expect(filters).toHaveLength(1);++ expect(Lib.displayInfo(query, -1, filters[0])).toMatchObject({+ displayName: "Price is greater than or equal to 100",+ });+ });+ });++ describe("aggregations", () => {+ it("should create a query with aggregations", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ aggregations: [+ {+ type: "operator",+ operator: ">=",+ args: [+ { type: "column", name: "PRICE" },+ { type: "literal", value: 100 },+ ],+ },+ ],+ },+ ],+ });++ const aggregations = Lib.aggregations(query, -1);+ expect(aggregations).toHaveLength(1);++ expect(Lib.displayInfo(query, -1, aggregations[0])).toMatchObject({+ displayName: "Price is greater than or equal to 100",+ });+ });+ });++ describe("order bys", () => {+ it("should create a query with order bys", () => {+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ orderBys: [{ type: "column", name: "CATEGORY" }],+ },+ ],+ });++ const orderBys = Lib.orderBys(query, 0);+ expect(orderBys).toHaveLength(1);+ expect(Lib.displayInfo(query, 0, orderBys[0])).toMatchObject({+ displayName: "Category",+ });+ });+ });++ describe("limit", () => {+ it("should create a query with a limit", () => {+ const LIMIT = 42;+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, {+ stages: [+ {+ source: {+ type: "table",+ id: PRODUCTS_ID,+ },+ limit: LIMIT,+ },+ ],+ });++ expect(Lib.currentLimit(query, 0)).toBe(LIMIT);+ });+ });+});diff --git a/frontend/src/metabase-lib/test-helpers.ts b/frontend/src/metabase-lib/test-helpers.tsindex beb39b73974..44dbbfb9d3f 100644--- a/frontend/src/metabase-lib/test-helpers.ts+++ b/frontend/src/metabase-lib/test-helpers.ts@@ -5,11 +5,9 @@ import { checkNotNull } from "metabase/lib/types";import * as Lib from "metabase-lib";import type Metadata from "metabase-lib/v1/metadata/Metadata";import type {- CardId,DatabaseId,DatasetColumn,DatasetQuery,- JoinStrategy,RowValue,TableId,} from "metabase-types/api";@@ -22,10 +20,7 @@ const SAMPLE_DATABASE = createSampleDatabase();const SAMPLE_METADATA = createMockMetadata({ databases: [SAMPLE_DATABASE] });-const SAMPLE_PROVIDER = createMetadataProvider({- databaseId: SAMPLE_DATABASE.id,- metadata: SAMPLE_METADATA,-});+const SAMPLE_PROVIDER = createMetadataProvider();export { SAMPLE_DATABASE, SAMPLE_METADATA, SAMPLE_PROVIDER };@@ -34,7 +29,7 @@ type MetadataProviderOpts = {metadata?: Metadata;};-export function createMetadataProvider({+function createMetadataProvider({databaseId = SAMPLE_DATABASE.id,metadata = SAMPLE_METADATA,}: MetadataProviderOpts = {}) {@@ -53,9 +48,6 @@ type QueryOpts = MetadataProviderOpts & {query?: DatasetQuery;};-/**- * @deprecated: Use createTestQuery, createTestJsQuery, createTestNativeQuery or createTestNativeJsQuery instead.- */export function createQuery({databaseId = SAMPLE_DATABASE.id,metadata = SAMPLE_METADATA,@@ -210,9 +202,6 @@ interface QueryWithClausesOpts {orderBys?: OrderByClauseOpts[];}-/**- * @deprecated: Use createTestQuery or createTestJsQuery instead.- */export function createQueryWithClauses({query = createQuery(),expressions = [],@@ -421,526 +410,3 @@ export function createLegendItemClickObject() {return { dimensions: [dimension] };}--export type CreateTestQueryOpts = {- databaseId: DatabaseId;- stages: [TestQueryStageWithSource, ...TestQueryStage[]];-};--type TestQueryStage = {- limit?: Lib.Limit;- fields?: TestQueryColumnExpression[] | "all";- joins?: TestQueryJoin[];- filters?: TestQueryFilter[];- aggregations?: TestQueryAggregation[];- expressions?: TestQueryNamedExpression[];- breakouts?: TestQueryBreakout[];- orderBy?: TestQueryOrderBy[];-};--type TestQueryStageWithSource = TestQueryStage & {- source: TestQuerySource;-};--type TestQueryTableSource = { type: "table"; id: TableId };-type TestQueryCardSource = { type: "card"; id: CardId };-type TestQuerySource = TestQueryTableSource | TestQueryCardSource;--type TestQueryJoin = {- source: TestQuerySource;- strategy: JoinStrategy;-- // If not set we will used the suggested join conditions- conditions?: TestQueryJoinCondition[];-};--type TestQueryJoinCondition = {- operator: Lib.JoinConditionOperator;- left: TestQueryExpression;- right: TestQueryExpression;-};--type TestQueryNamedExpression = {- name: string;- value: TestQueryExpression;-};--type TestQueryFilter = TestQueryExpression;--type TestQueryAggregation = TestQueryExpression | TestQueryNamedExpression;--type TestQueryExpression =- | TestQueryLiteralExpression- | TestQueryColumnExpression- | TestQueryOperatorExpression;--type TestQueryLiteralExpression = {- type: "literal";- value: number | bigint | string | boolean;-};--type TestQueryColumnExpression = {- type: "column";- groupName?: string;- name: string;-};--type TestQueryOperatorExpression = {- type: "operator";- operator: Lib.ExpressionOperator;- args: TestQueryExpression[];-};--type TestQueryBreakout = {- name: string;- groupName?: string;- unit?: string;- binningCount?: number | "auto";-};--type TestQueryOrderBy = {- name: string;- groupName?: string;- direction: Lib.OrderByDirection;-};--export function createTestQuery(- metadataProvider: Lib.MetadataProvider,- { stages }: CreateTestQueryOpts,-): Lib.Query {- if (stages.length === 0) {- throw new Error("query must have at least one stage");- }-- if (!stages[0].source) {- throw new Error("query must have a source in the first stage");- }-- const firstSource = findSource(metadataProvider, stages[0].source);-- const initialQuery = Lib.queryFromTableOrCardMetadata(- metadataProvider,- firstSource,- );-- return stages.reduce(- (query: Lib.Query, stage, stageIndex) =>- appendTestQueryStage(metadataProvider, stage, query, stageIndex),- initialQuery,- );-}--export function createTestJsQuery(- metadataProvider: Lib.MetadataProvider,- opts: CreateTestQueryOpts,-) {- return Lib.toJsQuery(createTestQuery(metadataProvider, opts));-}--function appendTestQueryStage(- metadataProvider: Lib.MetadataProvider,- stage: TestQueryStage,- query: Lib.Query,- stageIndex: number,-): Lib.Query {- const {- limit,- joins = [],- expressions = [],- filters = [],- aggregations = [],- breakouts = [],- orderBy = [],- } = stage;-- const queryWithStage = Lib.appendStage(query);-- const queryWithLimit =- limit == null- ? queryWithStage- : Lib.limit(queryWithStage, stageIndex, limit);-- // Add joins to stage- const queryWithJoins = joins.reduce((query, join) => {- const joinClause = createTestJoinClause(- metadataProvider,- query,- stageIndex,- join,- );- return Lib.join(query, stageIndex, joinClause);- }, queryWithLimit);-- // Add expressions- const queryWithExpressions = expressions.reduce((query, expression) => {- const expressionClause = expressionToExpressionClause(- query,- stageIndex,- Lib.visibleColumns(query, stageIndex),- expression.value,- );- return Lib.expression(query, stageIndex, expression.name, expressionClause);- }, queryWithJoins);-- // Limit query fields- const visibleColumns = Lib.visibleColumns(queryWithJoins, stageIndex);- const queryWithFields =- stage.fields == null || stage.fields === "all"- ? // If no fields are specified, we use all the visible fields- queryWithExpressions- : // If fields are specified, we use only those fields- Lib.withFields(- queryWithExpressions,- stageIndex,- stage.fields.map((field) =>- findColumn(queryWithJoins, stageIndex, visibleColumns, field),- ),- );-- // Add filters- const queryWithFilters = filters.reduce((query, filter) => {- const filterClause = createTestFilterClause(- metadataProvider,- query,- stageIndex,- filter,- );- return Lib.filter(query, stageIndex, filterClause);- }, queryWithFields);-- // Add aggregations- const queryWithAggregations = aggregations.reduce((query, aggregation) => {- const aggregationClause = createTestAggregationClause(- metadataProvider,- query,- stageIndex,- aggregation,- );- return Lib.aggregate(query, stageIndex, aggregationClause);- }, queryWithFilters);-- // Add breakouts- const queryWithBreakouts = breakouts.reduce((query, breakout) => {- const clause = createTestBreakoutClause(- metadataProvider,- query,- stageIndex,- breakout,- );- return Lib.breakout(query, stageIndex, clause);- }, queryWithAggregations);-- const queryWithOrderBys = orderBy.reduce((query, orderBy) => {- const columns = Lib.orderableColumns(query, stageIndex);- const column = findColumn(query, stageIndex, columns, {- type: "column",- name: orderBy.name,- groupName: orderBy.groupName,- });- return Lib.orderBy(query, stageIndex, column, orderBy.direction);- }, queryWithBreakouts);-- return queryWithOrderBys;-}--function findSource(- provider: Lib.MetadataProvider,- source: TestQuerySource,-): Lib.TableMetadata | Lib.CardMetadata {- const id = source.type === "card" ? `card__${source.id}` : source.id;- const metadata = Lib.tableOrCardMetadata(provider, id);-- if (!metadata) {- throw new Error(`Could not find source metadata for source: ${id}`);- }-- return metadata;-}--function getGroupColumns(- query: Lib.Query,- stageIndex: number,- columns: Lib.ColumnMetadata[],- column: TestQueryColumnExpression,-) {- if (column.groupName) {- const groups = Lib.groupColumns(columns);- const group = groups.find((group) => {- const info = Lib.displayInfo(query, stageIndex, group);- return info.displayName === column.groupName;- });- if (!group) {- const groupNames = groups.map(- (group) => Lib.displayInfo(query, 0, group).displayName,- );- throw new Error(- `Could not find group named ${column.groupName}, available names are: ${groupNames.join(", ")}`,- );- }- return Lib.getColumnsFromColumnGroup(group);- }- return columns;-}--function findColumn(- query: Lib.Query,- stageIndex: number,- columns: Lib.ColumnMetadata[],- column: TestQueryColumnExpression,-) {- const columnsFromGroup = getGroupColumns(query, stageIndex, columns, column);- const candidates = columnsFromGroup.filter((candidate) => {- const displayInfo = Lib.displayInfo(query, stageIndex, candidate);- return column.name === displayInfo.name;- });-- if (candidates.length === 0) {- throw new Error(`Could not find column named "${column.name}"`);- } else if (candidates.length > 1 && column.groupName == null) {- // If there is only one candidate that is not from an (implicit) join, return that one- const filteredCandidates = candidates.filter((candidate) => {- const displayInfo = Lib.displayInfo(query, stageIndex, candidate);- return !displayInfo.isFromJoin && !displayInfo.isImplicitlyJoinable;- });-- if (filteredCandidates.length === 1) {- return filteredCandidates[0];- }-- throw new Error(- `More than one column named "${column.name}", please disambiguate using groupName`,- );- } else {- // Just one candidate found, or they are all from the same table,- // so they represent the same column. Return the first candidate.- return candidates[0];- }-}--function createTestJoinClause(- metadataProvider: Lib.MetadataProvider,- query: Lib.Query,- stageIndex: number,- join: TestQueryJoin,-) {- const targetMetadata = findSource(metadataProvider, join.source);-- // Pick join strategy- const joinStrategy = findJoinStrategy(query, stageIndex, join.strategy);-- // Build join conditions- const conditions =- join.conditions == null- ? // If no conditions are specified, use the suggested conditions- Lib.suggestedJoinConditions(query, stageIndex, targetMetadata)- : // If conditions are specified, use them- join.conditions.map((condition) => {- const lhs = expressionToJoinConditionExpression(- query,- stageIndex,- Lib.joinConditionLHSColumns(query, stageIndex),- condition.left,- );-- const rhs = expressionToJoinConditionExpression(- query,- stageIndex,- Lib.joinConditionRHSColumns(query, stageIndex, targetMetadata, lhs),- condition.right,- );-- return Lib.joinConditionClause(condition.operator, lhs, rhs);- });-- return Lib.joinClause(targetMetadata, conditions, joinStrategy);-}--function findJoinStrategy(- query: Lib.Query,- stageIndex: number,- strategyName: JoinStrategy,-) {- const availableJoinStrategies = Lib.availableJoinStrategies(- query,- stageIndex,- );- const joinStrategy = availableJoinStrategies.find(- (strategy) =>- Lib.displayInfo(query, stageIndex, strategy).shortName === strategyName,- );- if (!joinStrategy) {- throw new Error(`Could not find join strategy "${strategyName}"`);- }- return joinStrategy;-}--function expressionToExpressionParts(- query: Lib.Query,- stageIndex: number,- availableColumns: Lib.ColumnMetadata[],- expression: TestQueryExpression,-): Lib.ExpressionArg | Lib.ExpressionParts {- switch (expression.type) {- case "literal":- return expression.value;- case "column":- return findColumn(query, stageIndex, availableColumns, expression);- case "operator":- return {- operator: expression.operator,- options: {},- args: expression.args.map((arg) =>- expressionToExpressionParts(query, stageIndex, availableColumns, arg),- ),- };- }-}--function expressionToExpressionClause(- query: Lib.Query,- stageIndex: number,- availableColumns: Lib.ColumnMetadata[],- expression: TestQueryExpression,-): Lib.ExpressionClause {- const expressionParts = expressionToExpressionParts(- query,- stageIndex,- availableColumns,- expression,- );- return Lib.expressionClause(expressionParts);-}--function expressionToJoinConditionExpression(- query: Lib.Query,- stageIndex: number,- joinableColumns: Lib.ColumnMetadata[],- expression: TestQueryExpression,-) {- switch (expression.type) {- case "column":- return findColumn(query, stageIndex, joinableColumns, expression);- case "operator":- case "literal": {- return expressionToExpressionClause(- query,- stageIndex,- joinableColumns,- expression,- );- }- }-}--function createTestFilterClause(- _metadataProvider: Lib.MetadataProvider,- query: Lib.Query,- stageIndex: number,- filter: TestQueryFilter,-): Lib.ExpressionClause {- return expressionToExpressionClause(- query,- stageIndex,- Lib.filterableColumns(query, stageIndex),- filter,- );-}--function createTestAggregationClause(- metadataProvider: Lib.MetadataProvider,- query: Lib.Query,- stageIndex: number,- aggregation: TestQueryAggregation,-): Lib.ExpressionClause {- if ("name" in aggregation && "value" in aggregation) {- const clause = createTestAggregationClause(- metadataProvider,- query,- stageIndex,- aggregation.value,- );- return Lib.withExpressionName(clause, aggregation.name);- }-- const clause = expressionToExpressionClause(- query,- stageIndex,- Lib.aggregableColumns(query, stageIndex),- aggregation,- );- return clause;-}--function createTestBreakoutClause(- _metadataProvider: Lib.MetadataProvider,- query: Lib.Query,- stageIndex: number,- breakout: TestQueryBreakout,-): Lib.ColumnMetadata {- const columns = Lib.breakoutableColumns(query, stageIndex);- const column = findColumn(query, stageIndex, columns, {- type: "column",- name: breakout.name,- groupName: breakout.groupName,- });-- if (breakout.unit) {- const buckets = Lib.availableTemporalBuckets(query, stageIndex, column);- const bucket = buckets.find((candidate) => {- const info = Lib.displayInfo(query, stageIndex, candidate);- return info.shortName === breakout.unit;- });-- if (!bucket) {- throw new Error(`Could not find temporal bucket ${breakout.unit}`);- }-- return Lib.withTemporalBucket(column, bucket);- }-- if (breakout.binningCount) {- const strategies = Lib.availableBinningStrategies(- query,- stageIndex,- column,- );- const strategy = strategies.find((candidate) => {- const info = Lib.displayInfo(query, stageIndex, candidate);- return (- (breakout.binningCount === "auto" && info.displayName === "Auto bin") ||- info.displayName === `${breakout.binningCount} bins`- );- });-- if (!strategy) {- throw new Error(- `Could not find binning strategy: ${breakout.binningCount}`,- );- }-- return Lib.withBinning(column, strategy);- }-- return column;-}--export function createTestNativeQuery(- metadataProvider: Lib.MetadataProvider,- databaseId: DatabaseId,- query: string,-): Lib.Query {- return Lib.nativeQuery(databaseId, metadataProvider, query);-}--export function createTestNativeJsQuery(- metadataProvider: Lib.MetadataProvider,- databaseId: DatabaseId,- query: string,-) {- const nativeQuery = createTestNativeQuery(- metadataProvider,- databaseId,- query,- );- return Lib.toJsQuery(nativeQuery);-}diff --git a/frontend/src/metabase-lib/test-helpers.unit.spec.ts b/frontend/src/metabase-lib/test-helpers.unit.spec.tsdeleted file mode 100644index 3ed2bb9b874..00000000000--- a/frontend/src/metabase-lib/test-helpers.unit.spec.ts+++ /dev/null@@ -1,182 +0,0 @@-import * as Lib from "metabase-lib";-import {- SAMPLE_DATABASE,- SAMPLE_METADATA,- createTestQuery,-} from "metabase-lib/test-helpers";-import { ORDERS_ID, PEOPLE_ID } from "metabase-types/api/mocks/presets";--describe("createTestQuery", () => {- it("should create a query with stages", () => {- const provider = Lib.metadataProvider(SAMPLE_DATABASE.id, SAMPLE_METADATA);- const query = createTestQuery(provider, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- fields: [- {- type: "column",- groupName: "People",- name: "ID",- },- {- type: "column",- groupName: "Orders",- name: "ID",- },- ],- joins: [- {- source: {- type: "table",- id: PEOPLE_ID,- },- strategy: "inner-join",- conditions: [- {- operator: "=",- left: {- type: "column",- name: "USER_ID",- },- right: {- type: "operator",- operator: "+",- args: [- {- type: "column",- name: "ID",- },- {- type: "literal",- value: 1,- },- ],- },- },- ],- },- ],- filters: [- {- type: "operator",- operator: ">=",- args: [- {- type: "column",- name: "TOTAL",- },- {- type: "literal",- value: 10,- },- ],- },- ],- aggregations: [- {- name: "Foo",- value: {- type: "operator",- operator: "avg",- args: [- {- type: "column",- name: "TOTAL",- },- ],- },- },- ],- breakouts: [- {- name: "CREATED_AT",- unit: "month",- },- ],- expressions: [- {- name: "Bar",- value: {- type: "operator",- operator: "+",- args: [- { type: "column", name: "SUBTOTAL" },- { type: "literal", value: 50 },- ],- },- },- ],- orderBy: [- {- name: "CREATED_AT",- direction: "desc",- },- ],- limit: 101,- },- {- expressions: [- {- name: "FooPlusOne",- value: {- type: "operator",- operator: "+",- args: [- { type: "column", name: "Foo" },- { type: "literal", value: 1 },- ],- },- },- ],- },- ],- });-- const customExpressions = Lib.expressions(query, 0).map(- (expression) => Lib.displayInfo(query, 0, expression).displayName,- );- expect(customExpressions).toEqual(["Bar"]);-- const fields = Lib.fields(query, 0).map(- (field) => Lib.displayInfo(query, -1, field).displayName,- );- expect(fields).toEqual(["People → ID", "ID", "Bar"]);-- const joins = Lib.joins(query, 0).map((join) => {- const joinConditions = Lib.joinConditions(join);- return joinConditions.map(- (condition) => Lib.displayInfo(query, 0, condition).longDisplayName,- );- });- expect(joins).toEqual([["User ID is ID + 1"]]);-- const filters = Lib.filters(query, 0).map(- (filter) => Lib.displayInfo(query, 0, filter).displayName,- );- expect(filters).toEqual(["Total is greater than or equal to 10"]);-- const aggregations = Lib.aggregations(query, 0).map(- (aggregation) => Lib.displayInfo(query, 0, aggregation).displayName,- );- expect(aggregations).toEqual(["Foo"]);-- const orderBys = Lib.orderBys(query, 0).map((orderBy) => {- const info = Lib.displayInfo(query, 0, orderBy);- return {- name: info.displayName,- direction: info.direction,- };- });- expect(orderBys).toEqual([- { name: "Created At: Month", direction: "desc" },- ]);-- const limit = Lib.currentLimit(query, 0);- expect(limit).toEqual(101);- });-});diff --git a/frontend/src/metabase-lib/viz/display.unit.spec.ts b/frontend/src/metabase-lib/viz/display.unit.spec.tsindex 53f5baa7942..a6585e9879f 100644--- a/frontend/src/metabase-lib/viz/display.unit.spec.ts+++ b/frontend/src/metabase-lib/viz/display.unit.spec.ts@@ -2,20 +2,11 @@ import { createMockMetadata } from "__support__/metadata";import type { Field, Table } from "metabase-types/api";import { createMockField, createMockTable } from "metabase-types/api/mocks";import {- ORDERS_ID,SAMPLE_DB_ID,- createOrdersTable,- createPeopleTable,- createProductsTable,- createReviewsTable,createSampleDatabase,} from "metabase-types/api/mocks/presets";-import {- createMetadataProvider,- createTestNativeQuery,- createTestQuery,-} from "../test-helpers";+import { createQuery, createQueryWithClauses } from "../test-helpers";import { defaultDisplay } from "./display";@@ -47,90 +38,45 @@ const createAccountsCountryField = (opts?: Partial<Field>): Field =>});const SAMPLE_DATABASE = createSampleDatabase({- tables: [- createProductsTable(),- createReviewsTable(),- createOrdersTable(),- createPeopleTable(),- createAccountsTable(),- ],-});--const SAMPLE_METADATA = createMockMetadata({- databases: [SAMPLE_DATABASE],+ tables: [createAccountsTable()],});-const SAMPLE_PROVIDER = createMetadataProvider({- databaseId: SAMPLE_DATABASE.id,- metadata: SAMPLE_METADATA,-});+const SAMPLE_METADATA = createMockMetadata({ databases: [SAMPLE_DATABASE] });describe("defaultDisplay", () => {it("returns 'table' display for native queries", () => {- const query = createTestNativeQuery(- SAMPLE_PROVIDER,- SAMPLE_DATABASE.id,- "SELECT * FROM ACCOUNTS",- );+ const query = createQuery({+ metadata: SAMPLE_METADATA,+ query: {+ database: SAMPLE_DATABASE.id,+ type: "native",+ native: {+ query: "SELECT * FROM ACCOUNTS",+ },+ },+ });+expect(defaultDisplay(query)).toEqual({ display: "table" });});it("returns 'table' display for queries with no aggregations and no breakouts", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- },- ],- });+ const query = createQuery();expect(defaultDisplay(query)).toEqual({ display: "table" });});it("returns 'scalar' display for queries with 1 aggregation and no breakouts", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- },- ],+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],});expect(defaultDisplay(query)).toEqual({ display: "scalar" });});it("returns 'map' display for queries with 1 aggregation and 1 breakout by state", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [{ name: "STATE" }],- },- ],+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],+ breakouts: [{ columnName: "STATE", tableName: "PEOPLE" }],});expect(defaultDisplay(query)).toEqual({@@ -143,27 +89,19 @@ describe("defaultDisplay", () => {});it("returns 'map' display for queries with 1 aggregation and 1 breakout by country", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ACCOUNTS_ID,+ const query = createQueryWithClauses({+ query: createQuery({+ metadata: SAMPLE_METADATA,+ query: {+ database: SAMPLE_DATABASE.id,+ type: "query",+ query: {+ "source-table": ACCOUNTS_ID,},- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [- {- name: "COUNTRY",- },- ],},- ],+ }),+ aggregations: [{ operatorName: "count" }],+ breakouts: [{ columnName: "COUNTRY", tableName: "ACCOUNTS" }],});expect(defaultDisplay(query)).toEqual({@@ -176,27 +114,13 @@ describe("defaultDisplay", () => {});it("returns 'bar' display for queries with aggregations and 1 breakout by date with temporal bucketing", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],+ breakouts: [{- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [- {- name: "CREATED_AT",- groupName: "Orders",- unit: "day-of-month",- },- ],+ columnName: "CREATED_AT",+ tableName: "ORDERS",+ temporalBucketName: "Day of month",},],});@@ -205,134 +129,53 @@ describe("defaultDisplay", () => {});it("returns 'line' display for queries with aggregations and 1 breakout by date without temporal bucketing", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [- {- name: "CREATED_AT",- groupName: "Orders",- },- ],- },- ],+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],+ breakouts: [{ columnName: "CREATED_AT", tableName: "ORDERS" }],});expect(defaultDisplay(query)).toEqual({ display: "line" });});it("returns 'bar' display for queries with aggregations and 1 breakout with binning", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],+ breakouts: [{- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [- {- name: "TOTAL",- groupName: "Orders",- binningCount: 10,- },- ],+ columnName: "TOTAL",+ tableName: "ORDERS",+ binningStrategyName: "10 bins",},],});+expect(defaultDisplay(query)).toEqual({ display: "bar" });});it("returns 'table' display for queries with aggregations and 1 breakout without binning", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [- {- name: "TOTAL",- groupName: "Orders",- },- ],- },- ],+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],+ breakouts: [{ columnName: "TOTAL", tableName: "ORDERS" }],});expect(defaultDisplay(query)).toEqual({ display: "table" });});it("returns 'bar' display for queries with aggregations and 1 breakout by category", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [- {- name: "CATEGORY",- groupName: "Product",- },- ],- },- ],+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],+ breakouts: [{ columnName: "CATEGORY", tableName: "PRODUCTS" }],});expect(defaultDisplay(query)).toEqual({ display: "bar" });});it("returns 'line' display for queries with 1 aggregation and 2 breakouts, at least 1 of which is by date", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [{ name: "CREATED_AT" }, { name: "TOTAL" }],- },+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],+ breakouts: [+ { columnName: "CREATED_AT", tableName: "ORDERS" },+ { columnName: "TOTAL", tableName: "ORDERS" },],});@@ -340,30 +183,18 @@ describe("defaultDisplay", () => {});it("returns 'map' display with 'grid' type for queries with 1 aggregation and 2 binned breakouts by coordinates", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],+ breakouts: [{- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [- {- name: "LATITUDE",- binningCount: "auto",- },- {- name: "LONGITUDE",- binningCount: "auto",- },- ],+ columnName: "LATITUDE",+ tableName: "PEOPLE",+ binningStrategyName: "Auto bin",+ },+ {+ columnName: "LONGITUDE",+ tableName: "PEOPLE",+ binningStrategyName: "Auto bin",},],});@@ -377,22 +208,11 @@ describe("defaultDisplay", () => {});it("returns 'map' display with 'pin' type for queries with 1 aggregation and 2 un-binned breakouts by coordinates", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [{ name: "LATITUDE" }, { name: "LONGITUDE" }],- },+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],+ breakouts: [+ { columnName: "LATITUDE", tableName: "PEOPLE" },+ { columnName: "LONGITUDE", tableName: "PEOPLE" },],});@@ -405,28 +225,15 @@ describe("defaultDisplay", () => {});it("returns 'map' display with 'pin' type for queries with 1 aggregation and 2 breakouts by coordinates - 1 binned, 1 unbinned", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],+ breakouts: [{- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [- {- name: "LATITUDE",- binningCount: "auto",- },- { name: "LONGITUDE" },- ],+ columnName: "LATITUDE",+ tableName: "PEOPLE",+ binningStrategyName: "Auto bin",},+ { columnName: "LONGITUDE", tableName: "PEOPLE" },],});@@ -439,25 +246,11 @@ describe("defaultDisplay", () => {});it("returns 'bar' display for queries with 1 aggregation and 2 breakouts by category", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- ],- breakouts: [- { name: "CATEGORY", groupName: "Product" },- { name: "VENDOR", groupName: "Product" },- ],- },+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }],+ breakouts: [+ { columnName: "CATEGORY", tableName: "PRODUCTS" },+ { columnName: "VENDOR", tableName: "PRODUCTS" },],});@@ -465,29 +258,11 @@ describe("defaultDisplay", () => {});it("returns 'table' display by default", () => {- const query = createTestQuery(SAMPLE_PROVIDER, {- databaseId: SAMPLE_DATABASE.id,- stages: [- {- source: {- type: "table",- id: ORDERS_ID,- },- aggregations: [- {- name: "Count",- value: { type: "operator", operator: "count", args: [] },- },- {- name: "Cumulative Count",- value: { type: "operator", operator: "cum-count", args: [] },- },- ],- breakouts: [- { name: "LATITUDE", groupName: "User" },- { name: "LONGITUDE", groupName: "User" },- ],- },+ const query = createQueryWithClauses({+ aggregations: [{ operatorName: "count" }, { operatorName: "cum-count" }],+ breakouts: [+ { columnName: "LATITUDE", tableName: "PEOPLE" },+ { columnName: "LONGITUDE", tableName: "PEOPLE" },],});diff --git a/frontend/src/metabase-types/api/query.ts b/frontend/src/metabase-types/api/query.tsindex 1a80f2350d3..453f06cb73e 100644--- a/frontend/src/metabase-types/api/query.ts+++ b/frontend/src/metabase-types/api/query.ts@@ -1,12 +1,12 @@+import type * as Lib from "metabase-lib";import type { UiParameter } from "metabase-lib/v1/parameters/types";-import type {- CardId,- DatabaseId,- FieldId,- SegmentId,- TableId,- TemplateTags,-} from "metabase-types/api";++import type { CardId } from "./card";+import type { DatabaseId } from "./database";+import type { TemplateTags, TemporalUnit } from "./dataset";+import type { FieldId } from "./field";+import type { SegmentId } from "./segment";+import type { TableId } from "./table";export interface NativeQuery {query: string;@@ -440,3 +440,109 @@ export type DimensionReferenceWithOptions =export type DimensionReference =| DimensionReferenceWithOptions| TemplateTagReference;++export type TestTableSourceSpec = {+ type: "table";+ id: TableId;+};++export type TestCardSourceSpec = {+ type: "card";+ id: CardId;+};++export type TestSourceSpec = TestTableSourceSpec | TestCardSourceSpec;++export type TestColumnSpec = {+ type: "column";+ name: string;+ sourceName?: string;+ displayName?: string;+};++export type TestExpressionSpec =+ | TestLiteralSpec+ | TestOperatorSpec+ | TestColumnSpec;++export type TestFilterSpec = TestExpressionSpec;++export type TestAggregationSpec = TestExpressionSpec | TestNamedExpressionSpec;++export type TestNamedExpressionSpec = {+ name: string;+ value: TestExpressionSpec;+};++export type TestLiteralSpec = {+ type: "literal";+ value: NumericLiteral | StringLiteral | BooleanLiteral | DatetimeLiteral;+};++export type TestOperatorSpec = {+ type: "operator";+ operator: string;+ args: TestExpressionSpec[];+};++export type TestTemporalBucketSpec = {+ unit?: TemporalUnit;+};++export type TestBinCountSpec = {+ bins?: number;+};++export type TestBinWidthSpec = {+ binWidth?: number;+};++type TestBinningSpec =+ | TestTemporalBucketSpec+ | TestBinCountSpec+ | TestBinWidthSpec;++export type TestColumnWithBinningSpec = TestColumnSpec & TestBinningSpec;++export type TestBreakoutSpec = TestColumnWithBinningSpec;++export type TestJoinSpec = {+ source: TestSourceSpec;+ strategy: JoinStrategy;++ // If not set we will use the suggested join conditions+ conditions?: TestJoinConditionSpec[];+};++type TestJoinConditionSpec = {+ operator: Lib.JoinConditionOperator;+ left: TestColumnWithBinningSpec | TestExpressionSpec;+ right: TestColumnWithBinningSpec | TestExpressionSpec;+};++export type TestOrderBySpec = TestColumnSpec & {+ direction?: "asc" | "desc";+};++export type TestStageSpec = {+ fields?: TestColumnSpec[];+ expressions?: TestNamedExpressionSpec[];+ joins?: TestJoinSpec[];+ filters?: TestFilterSpec[];+ aggregations?: TestAggregationSpec[];+ breakouts?: TestBreakoutSpec[];+ orderBys?: TestOrderBySpec[];+ limit?: number;+};++export type TestStageWithSourceSpec = TestStageSpec & {+ source: TestSourceSpec;+};++export type TestQuerySpec = {+ stages: [TestStageWithSourceSpec, ...TestStageSpec[]];+};++export type TestQuerySpecWithDatabase = TestQuerySpec & {+ database: DatabaseId;+};diff --git a/frontend/src/metabase/entities/containers/index.jsx b/frontend/src/metabase/entities/containers/index.jssimilarity index 100%rename from frontend/src/metabase/entities/containers/index.jsxrename to frontend/src/metabase/entities/containers/index.jsdiff --git a/frontend/src/metabase/schema.js b/frontend/src/metabase/schema.jsindex c5dc0db0824..3ac6c38a99d 100644--- a/frontend/src/metabase/schema.js+++ b/frontend/src/metabase/schema.js@@ -159,19 +159,3 @@ export const QueryMetadataSchema = {cards: [QuestionSchema],dashboards: [DashboardSchema],};--export const EntitiesSchema = {- actions: [ActionSchema],- collections: [CollectionSchema],- dashboards: [DashboardSchema],- databases: [DatabaseSchema],- documents: [DocumentSchema],- schemas: [SchemaSchema],- tables: [TableSchema],- fields: [FieldSchema],- segments: [SegmentSchema],- measures: [MeasureSchema],- snippets: [SnippetSchema],- indexedEntities: [IndexedEntitySchema],- questions: [QuestionSchema],-};diff --git a/frontend/src/metabase/selectors/metadata.ts b/frontend/src/metabase/selectors/metadata.tsindex cd08860f83b..3484aee93ad 100644--- a/frontend/src/metabase/selectors/metadata.ts+++ b/frontend/src/metabase/selectors/metadata.ts@@ -28,8 +28,6 @@ import type {} from "metabase-types/api";import type { State } from "metabase-types/store";-import type { EntitiesState } from "../../metabase-types/store/entities";-import { getSettings } from "./settings";type TableSelectorOpts = {@@ -42,18 +40,13 @@ type FieldSelectorOpts = {export type MetadataSelectorOpts = TableSelectorOpts & FieldSelectorOpts;-const getNormalizedDatabases = (state: StateWithEntities) =>- state.entities.databases;-const getNormalizedSchemas = (state: StateWithEntities) =>- state.entities.schemas;+const getNormalizedDatabases = (state: State) => state.entities.databases;+const getNormalizedSchemas = (state: State) => state.entities.schemas;-const getNormalizedTablesUnfiltered = (state: StateWithEntities) =>- state.entities.tables;+const getNormalizedTablesUnfiltered = (state: State) => state.entities.tables;-const getIncludeHiddenTables = (- _state: StateWithEntities,- props?: TableSelectorOpts,-) => !!props?.includeHiddenTables;+const getIncludeHiddenTables = (_state: State, props?: TableSelectorOpts) =>+ !!props?.includeHiddenTables;const getNormalizedTables = createSelector([getNormalizedTablesUnfiltered, getIncludeHiddenTables],@@ -67,12 +60,9 @@ const getNormalizedTables = createSelector(),);-const getNormalizedFieldsUnfiltered = (state: StateWithEntities) =>- state.entities.fields;-const getIncludeSensitiveFields = (- _state: StateWithEntities,- props?: FieldSelectorOpts,-) => !!props?.includeSensitiveFields;+const getNormalizedFieldsUnfiltered = (state: State) => state.entities.fields;+const getIncludeSensitiveFields = (_state: State, props?: FieldSelectorOpts) =>+ !!props?.includeSensitiveFields;const getNormalizedFields = createSelector([@@ -97,14 +87,10 @@ const getNormalizedFields = createSelector(),);-const getNormalizedSegments = (state: StateWithEntities) =>- state.entities.segments;-const getNormalizedMeasures = (state: StateWithEntities) =>- state.entities.measures ?? {};-const getNormalizedQuestions = (state: StateWithEntities) =>- state.entities.questions;-const getNormalizedSnippets = (state: StateWithEntities) =>- state.entities.snippets;+const getNormalizedSegments = (state: State) => state.entities.segments;+const getNormalizedMeasures = (state: State) => state.entities.measures ?? {};+const getNormalizedQuestions = (state: State) => state.entities.questions;+const getNormalizedSnippets = (state: State) => state.entities.snippets;export const getShallowDatabases = getNormalizedDatabases;export const getShallowTables = getNormalizedTables;@@ -112,12 +98,8 @@ export const getShallowFields = getNormalizedFields;export const getShallowSegments = getNormalizedSegments;export const getShallowMeasures = getNormalizedMeasures;-type StateWithEntities = {- entities: EntitiesState;-};--export const getMetadataWithoutSettings: (- state: StateWithEntities,+export const getMetadata: (+ state: State,props?: MetadataSelectorOpts,) => Metadata = createSelector([@@ -129,6 +111,7 @@ export const getMetadataWithoutSettings: (getNormalizedMeasures,getNormalizedQuestions,getNormalizedSnippets,+ getSettings,],(databases,@@ -139,8 +122,9 @@ export const getMetadataWithoutSettings: (measures,questions,snippets,+ settings,) => {- const metadata = new Metadata({});+ const metadata = new Metadata({ settings });metadata.databases = Object.fromEntries(Object.values(databases).map((d) => [d.id, createDatabase(d, metadata)]),@@ -207,17 +191,6 @@ export const getMetadataWithoutSettings: (},);-export const getMetadata: (- state: State,- props?: MetadataSelectorOpts,-) => Metadata = createSelector(- [getMetadataWithoutSettings, getSettings],- (metadata, settings) => {- metadata.settings = settings;- return metadata;- },-);-export const getMetadataUnfiltered = (state: State) => {return getMetadata(state, {includeHiddenTables: true,diff --git a/frontend/test/__support__/store.ts b/frontend/test/__support__/store.tsindex d3794829f0a..849bce2f7e2 100644--- a/frontend/test/__support__/store.ts+++ b/frontend/test/__support__/store.ts@@ -1,6 +1,21 @@+import type { Schema as NormalizrSchema } from "normalizr";import { normalize } from "normalizr";-import { EntitiesSchema } from "metabase/schema";+import {+ ActionSchema,+ CollectionSchema,+ DashboardSchema,+ DatabaseSchema,+ DocumentSchema,+ FieldSchema,+ IndexedEntitySchema,+ MeasureSchema,+ QuestionSchema,+ SchemaSchema,+ SegmentSchema,+ SnippetSchema,+ TableSchema,+} from "metabase/schema";import type {Card,Collection,@@ -32,6 +47,22 @@ export interface EntitiesStateOpts {questions?: Card[];}+const EntitiesSchema: Record<keyof EntitiesState, NormalizrSchema<any>> = {+ actions: [ActionSchema],+ collections: [CollectionSchema],+ dashboards: [DashboardSchema],+ databases: [DatabaseSchema],+ documents: [DocumentSchema],+ schemas: [SchemaSchema],+ tables: [TableSchema],+ fields: [FieldSchema],+ segments: [SegmentSchema],+ measures: [MeasureSchema],+ snippets: [SnippetSchema],+ indexedEntities: [IndexedEntitySchema],+ questions: [QuestionSchema],+};+export const createMockEntitiesState = (opts: EntitiesStateOpts,): EntitiesState => {diff --git a/src/metabase/lib/core.cljc b/src/metabase/lib/core.cljcindex 474be8a8e18..6c5fab9c2a5 100644--- a/src/metabase/lib/core.cljc+++ b/src/metabase/lib/core.cljc@@ -47,6 +47,7 @@[metabase.lib.parameters.parse :as lib.parameters.parse][metabase.lib.parse :as lib.parse][metabase.lib.query :as lib.query]+ [metabase.lib.query.test-spec :as lib.query.test-spec][metabase.lib.ref :as lib.ref][metabase.lib.remove-replace :as lib.remove-replace][metabase.lib.schema]@@ -104,6 +105,7 @@metabase.lib.parameters/keep-melib.parse/keep-melib.query/keep-me+ lib.query.test-spec/keep-melib.ref/keep-melib.remove-replace/keep-memetabase.lib.schema/keep-me@@ -438,6 +440,8 @@with-different-tablewith-wrapped-native-querywrap-native-query-with-mbql]+ [lib.query.test-spec+ test-query][lib.reffield-ref-idfield-ref-namediff --git a/src/metabase/lib/js.cljs b/src/metabase/lib/js.cljsindex 1e1acdbc417..f3c5084ae3d 100644--- a/src/metabase/lib/js.cljs+++ b/src/metabase/lib/js.cljs@@ -59,6 +59,8 @@[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.cache :as lib.cache]@@ -79,7 +81,9 @@[metabase.lib.normalize :as lib.normalize][metabase.lib.order-by :as lib.order-by][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]@@ -2735,3 +2739,25 @@js->cljlib.native/validate-template-tagsclj->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)))diff --git a/src/metabase/lib/query/test_spec.cljc b/src/metabase/lib/query/test_spec.cljcnew file mode 100644index 00000000000..2c916b3f0c5--- /dev/null+++ b/src/metabase/lib/query/test_spec.cljc@@ -0,0 +1,319 @@+(ns metabase.lib.query.test-spec+ (:refer-clojure :exclude [mapv name empty?])+ (:require+ [medley.core :as m]+ [metabase.lib.aggregation :as lib.aggregation]+ [metabase.lib.binning :as lib.binning]+ [metabase.lib.breakout :as lib.breakout]+ [metabase.lib.expression :as lib.expression]+ [metabase.lib.fe-util :as lib.fe-util]+ [metabase.lib.field :as lib.field]+ [metabase.lib.filter :as lib.filter]+ [metabase.lib.join :as lib.join]+ [metabase.lib.limit :as lib.limit]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.metadata.calculation :as lib.metadata.calculation]+ [metabase.lib.order-by :as lib.order-by]+ [metabase.lib.query :as lib.query]+ [metabase.lib.schema :as lib.schema]+ [metabase.lib.schema.binning :as lib.schema.binning]+ [metabase.lib.schema.expression :as lib.schema.expression]+ [metabase.lib.schema.join :as lib.schema.join]+ [metabase.lib.schema.metadata :as lib.schema.metadata]+ [metabase.lib.schema.ref :as lib.schema.ref]+ [metabase.lib.schema.temporal-bucketing :as lib.schema.temporal-bucketing]+ [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.malli :as mu]+ [metabase.util.performance :refer [mapv]]))++(mu/defn- find-source :- [:or ::lib.schema.metadata/table ::lib.schema.metadata/card]+ [metadata-providerable :- ::lib.schema.metadata/metadata-providerable+ {spec-id :id spec-type :type} :- ::lib.schema.test-spec/test-source-spec]+ (case spec-type+ :table (lib.metadata/table metadata-providerable spec-id)+ :card (lib.metadata/card metadata-providerable spec-id)))++(mu/defn- matches-column? :- :boolean+ [query :- ::lib.schema/query+ _stage-number :- :int+ {:keys [name source-name display-name]} :- ::lib.schema.test-spec/test-column-spec+ column :- ::lib.schema.metadata/column]+ (cond-> (= name (:name column))+ (some? source-name) (and (= source-name (some->> column :table-id (lib.metadata/table query) :name)))+ (some? display-name) (and (= display-name (:display-name column)))))++(mu/defn- find-column :- ::lib.schema.metadata/column+ [query :- ::lib.schema/query+ stage-number :- :int+ available-columns :- [:sequential ::lib.schema.metadata/column]+ column-spec :- ::lib.schema.test-spec/test-column-spec]+ (let [columns (filterv (partial matches-column? query stage-number column-spec) available-columns)]+ (case (count columns)+ 0 (throw (ex-info "No column found" {:columns available-columns, :column-spec column-spec}))+ 1 (first columns)+ (throw (ex-info "Multiple columns found" {:columns columns, :column-spec column-spec})))))++(mu/defn- append-fields :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ field-specs :- [:sequential ::lib.schema.test-spec/test-column-spec]]+ (let [visible (lib.metadata.calculation/visible-columns query stage-number)]+ (->> field-specs+ (mapv (partial find-column query stage-number visible))+ (lib.field/with-fields query stage-number))))++(mu/defn- matches-temporal-bucket? :- :boolean+ [unit :- ::lib.schema.temporal-bucketing/unit+ option :- ::lib.schema.temporal-bucketing/option]+ (= unit (:unit option)))++(mu/defn- find-temporal-bucket :- ::lib.schema.temporal-bucketing/option+ [query :- ::lib.schema/query+ stage-number :- :int+ column :- [:or ::lib.schema.metadata/column ::lib.schema.ref/ref]+ unit :- ::lib.schema.temporal-bucketing/unit]+ (let [temporal-buckets (lib.temporal-bucket/available-temporal-buckets query stage-number column)+ matches (filterv (partial matches-temporal-bucket? unit) temporal-buckets)]+ (case (count matches)+ 0 (throw (ex-info "No temporal bucket found" {:temporal-buckets temporal-buckets, :unit unit}))+ 1 (first matches)+ (throw (ex-info "Multiple temporal buckets found" {:matches matches, :unit unit})))))++(mu/defn- add-temporal-bucket :- [:or ::lib.schema.metadata/column ::lib.schema.ref/ref]+ [query :- ::lib.schema/query+ stage-number :- :int+ unit :- ::lib.schema.temporal-bucketing/unit+ column :- [:or ::lib.schema.metadata/column ::lib.schema.ref/ref]]+ (->> (find-temporal-bucket query stage-number column unit)+ (lib.temporal-bucket/with-temporal-bucket column)))++(mu/defn- matches-binning? :- :boolean+ [strategy :- ::lib.schema.binning/strategy+ value :- [:or ::lib.schema.binning/num-bins ::lib.schema.binning/bin-width]+ {:keys [mbql]} :- ::lib.schema.binning/binning-option]+ (and+ (= strategy (:strategy mbql))+ (== value (strategy mbql))))++(mu/defn- find-binning-strategy :- ::lib.schema.binning/binning-option+ [query :- ::lib.schema/query+ stage-number :- :int+ strategy :- ::lib.schema.binning/strategy+ value :- [:or ::lib.schema.binning/num-bins ::lib.schema.binning/bin-width]+ column :- [:or ::lib.schema.metadata/column ::lib.schema.ref/ref]]+ (let [binning-strategies (lib.binning/available-binning-strategies query stage-number column)+ matches (filterv (partial matches-binning? strategy value) binning-strategies)]+ (case (count matches)+ 0 (throw (ex-info "No binning strategy found" {:binning-strategies binning-strategies strategy value}))+ 1 (first matches)+ (throw (ex-info "Multiple binning strategies found" {:matches matches strategy value})))))++(mu/defn- add-binning :- [:or ::lib.schema.metadata/column ::lib.schema.ref/ref]+ [query :- ::lib.schema/query+ stage-number :- :int+ strategy :- ::lib.schema.binning/strategy+ value :- [:or ::lib.schema.binning/num-bins ::lib.schema.binning/bin-width]+ column :- [:or ::lib.schema.metadata/column ::lib.schema.ref/ref]]+ (->> (find-binning-strategy query stage-number strategy value column)+ (lib.binning/with-binning column)))++(mu/defn- apply-binning :- [:or ::lib.schema.metadata/column ::lib.schema.ref/ref]+ [query :- ::lib.schema/query+ stage-number :- :int+ {:keys [unit bins bin-width]} :- ::lib.schema.test-spec/test-column-with-binning-spec+ column :- [:or ::lib.schema.metadata/column ::lib.schema.ref/ref]]+ (cond->> column+ unit (add-temporal-bucket query stage-number unit)+ bins (add-binning query stage-number :num-bins bins)+ bin-width (add-binning query stage-number :bin-width bin-width)))++(mu/defn- append-breakout :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ columns :- [:sequential ::lib.schema.metadata/column]+ breakout-spec :- ::lib.schema.test-spec/test-breakout-spec]+ (->> (find-column query stage-number columns breakout-spec)+ (apply-binning query stage-number breakout-spec)+ (lib.breakout/breakout query stage-number)))++(mu/defn- append-breakouts :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ breakout-specs :- [:sequential ::lib.schema.test-spec/test-breakout-spec]]+ (let [columns (lib.breakout/breakoutable-columns query stage-number)]+ (reduce #(append-breakout %1 stage-number columns %2)+ query+ breakout-specs)))++(mu/defn- expression-spec->expression-parts+ [query :- ::lib.schema/query+ stage-number :- :int+ expression-spec :- ::lib.schema.test-spec/test-expression-spec+ available-columns :- [:sequential ::lib.schema.metadata/column]]+ (case (:type expression-spec)+ :literal (:value expression-spec)+ :column (find-column query stage-number available-columns expression-spec)+ :operator {:lib/type :mbql/expression-parts+ :operator (:operator expression-spec)+ :options {}+ :args (mapv #(expression-spec->expression-parts query stage-number % available-columns)+ (:args expression-spec))}))++(mu/defn- named-expression-spec? :- :boolean+ [expression-spec :- [:or ::lib.schema.test-spec/test-expression-spec ::lib.schema.test-spec/test-named-expression-spec]]+ (and+ (contains? expression-spec :name)+ (contains? expression-spec :value)+ (not (contains? expression-spec :type))))++(mu/defn- expression-spec->expression-clause :- ::lib.schema.expression/expression+ [query :- ::lib.schema/query+ stage-number :- :int+ {:keys [name value] :as expression-spec} :- [:or ::lib.schema.test-spec/test-expression-spec ::lib.schema.test-spec/test-named-expression-spec]+ available-columns :- [:sequential ::lib.schema.metadata/column]]+ (if (named-expression-spec? expression-spec)+ (-> (expression-spec->expression-clause query stage-number value available-columns)+ (lib.expression/with-expression-name name))+ (-> (expression-spec->expression-parts query stage-number expression-spec available-columns)+ lib.fe-util/expression-clause)))++(mu/defn- append-expression :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ {:keys [name value]} :- ::lib.schema.test-spec/test-named-expression-spec]+ ;; NOTE: expressionable-columns needs to calculated inside the loop+ ;; able to reference each other.+ (->> (lib.expression/expressionable-columns query stage-number nil)+ (expression-spec->expression-clause query stage-number value)+ (lib.expression/expression query stage-number name)))++(mu/defn- append-expressions :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ expression-specs :- [:sequential ::lib.schema.test-spec/test-named-expression-spec]]+ (reduce #(append-expression %1 stage-number %2)+ query+ expression-specs))++(mu/defn- matches-strategy? :- :boolean+ [strategy-name :- ::lib.schema.join/strategy+ {:keys [strategy]} :- ::lib.schema.join/strategy.option]+ (= strategy-name strategy))++(mu/defn- find-join-strategy :- ::lib.schema.join/strategy.option+ [query :- ::lib.schema/query+ stage-number :- :int+ strategy :- ::lib.schema.join/strategy]+ (let [available-strategies (lib.join/available-join-strategies query stage-number)]+ (if-let [found-strategy (m/find-first (partial matches-strategy? strategy) available-strategies)]+ found-strategy+ (throw (ex-info "No join strategy found" {:available-strategies available-strategies, :strategy strategy})))))++(mu/defn- join-condition-spec->join-condition :- ::lib.schema.join/condition+ [query :- ::lib.schema/query+ stage-number :- :int+ target :- [:or ::lib.schema.metadata/table ::lib.schema.metadata/card]+ {:keys [operator left right]} :- ::lib.schema.test-spec/test-join-condition-spec]+ (let [lhs (->> (lib.join/join-condition-lhs-columns query stage-number nil nil nil)+ (expression-spec->expression-clause query stage-number left)+ (apply-binning query stage-number left))+ rhs (->> (lib.join/join-condition-rhs-columns query stage-number target lhs nil)+ (expression-spec->expression-clause query stage-number right)+ (apply-binning query stage-number right))]+ (lib.fe-util/join-condition-clause operator lhs rhs)))++(mu/defn- append-join :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ {:keys [source strategy conditions]} :- ::lib.schema.test-spec/test-join-spec]+ (let [join-target (find-source query source)+ join-strategy (find-join-strategy query stage-number strategy)+ join-conditions (if conditions+ (mapv (partial join-condition-spec->join-condition query stage-number join-target) conditions)+ (lib.join/suggested-join-conditions query stage-number join-target))+ join-clause (lib.join/join-clause join-target join-conditions join-strategy)]+ (lib.join/join query stage-number join-clause)))++(mu/defn- append-joins :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ join-specs :- [:sequential ::lib.schema.test-spec/test-join-spec]]+ (reduce #(append-join %1 stage-number %2)+ query+ join-specs))++(mu/defn- append-filter :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ filter-spec :- ::lib.schema.test-spec/test-expression-spec]+ (->> (lib.filter/filterable-columns query stage-number)+ (expression-spec->expression-clause query stage-number filter-spec)+ (lib.filter/filter query stage-number)))++(mu/defn- append-filters :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ filter-specs :- [:sequential ::lib.schema.test-spec/test-expression-spec]]+ (reduce #(append-filter %1 stage-number %2)+ query+ filter-specs))++(mu/defn- append-aggregation :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ aggregation-spec :- ::lib.schema.test-spec/test-aggregation-spec]+ (->> (lib.aggregation/aggregable-columns query stage-number)+ (expression-spec->expression-clause query stage-number aggregation-spec)+ (lib.aggregation/aggregate query stage-number)))++(mu/defn- append-aggregations :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ aggregation-specs :- [:sequential ::lib.schema.test-spec/test-aggregation-spec]]+ (reduce #(append-aggregation %1 stage-number %2)+ query+ aggregation-specs))++(mu/defn- append-order-by :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ orderable-columns :- [:sequential ::lib.schema.metadata/column]+ {:keys [direction]+ :as order-by-spec} :- ::lib.schema.test-spec/test-order-by-spec]+ (as-> (find-column query stage-number orderable-columns order-by-spec) column+ (apply-binning query stage-number order-by-spec column)+ (lib.order-by/order-by query column direction)))++(mu/defn- append-order-bys :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ order-by-specs :- [:sequential ::lib.schema.test-spec/test-order-by-spec]]+ (let [orderable-columns (lib.order-by/orderable-columns query stage-number)]+ (reduce #(append-order-by %1 stage-number orderable-columns %2)+ query+ order-by-specs)))++(mu/defn- append-stage-clauses :- ::lib.schema/query+ [query :- ::lib.schema/query+ stage-number :- :int+ {:keys [fields expressions filters joins aggregations breakouts order-bys limit]} :- ::lib.schema.test-spec/test-stage-spec]+ (cond-> query+ (> stage-number 0) (lib.stage/append-stage)+ joins (append-joins stage-number joins)+ expressions (append-expressions stage-number expressions)+ breakouts (append-breakouts stage-number breakouts)+ aggregations (append-aggregations stage-number aggregations)+ order-bys (append-order-bys stage-number order-bys)+ filters (append-filters stage-number filters)+ fields (append-fields stage-number fields)+ limit (lib.limit/limit stage-number limit)))++(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 (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.cljcnew file mode 100644index 00000000000..ad4dce61d9f--- /dev/null+++ b/src/metabase/lib/schema/test_spec.cljc@@ -0,0 +1,126 @@+(ns metabase.lib.schema.test-spec+ "Schemas for creating a query for testing purposes."+ (:require+ [metabase.lib.schema.binning :as lib.schema.binning]+ [metabase.lib.schema.common :as lib.schema.common]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.lib.schema.join :as lib.schema.join]+ [metabase.lib.schema.literal :as literal]+ [metabase.lib.schema.order-by :as lib.schema.order-by]+ [metabase.lib.schema.temporal-bucketing :as lib.schema.temporal-bucketing]+ [metabase.util.malli.registry :as mr]))++(mr/def ::test-table-source-spec+ [:map+ [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :table]]+ [:id [:ref ::lib.schema.id/table]]])++(mr/def ::test-card-source-spec+ [:map+ [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :card]]+ [:id [:ref ::lib.schema.id/card]]])++(mr/def ::test-source-spec+ [:multi {:dispatch (comp keyword :type)}+ [:table ::test-table-source-spec]+ [:card ::test-card-source-spec]])++(mr/def ::test-column-spec+ [:map+ [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :column]]+ [:name string?]+ [:source-name {:optional true} [:maybe string?]]+ [:display-name {:optional true} [:maybe string?]]])++(mr/def ::test-temporal-bucket-spec+ [:map+ [:unit {:optional true} [:maybe ::lib.schema.temporal-bucketing/unit]]])++(mr/def ::test-bin-count-bucket-spec+ [:map+ [:bins {:optional true} [:maybe ::lib.schema.binning/num-bins]]])++(mr/def ::test-bin-width-bucket-spec+ [:map+ [:bin-width {:optional true} [:maybe ::lib.schema.binning/bin-width]]])++(mr/def ::test-column-with-binning-spec+ [:merge+ ::test-column-spec+ ::test-temporal-bucket-spec+ ::test-bin-count-bucket-spec+ ::test-bin-width-bucket-spec])++(mr/def ::test-breakout-spec+ [:ref ::test-column-with-binning-spec])++(mr/def ::test-order-by-spec+ [:merge+ ::test-column-spec+ [:map+ [:direction {:optional true} [:maybe [:ref ::lib.schema.order-by/direction]]]]])++(mr/def ::test-literal-expression-spec+ [:map+ [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :literal]]+ [:value [:ref ::literal/literal]]])++(mr/def ::test-operator-spec+ ;; TODO(@romeovs): be more specific here and limit to all valid operators?+ keyword?)++(mr/def ::test-operator-expression-spec+ [:map+ [:type [:= {:decode/normalize lib.schema.common/normalize-keyword} :operator]]+ [:operator ::test-operator-spec]+ [:args [:sequential [:ref ::test-expression-spec]]]])++(mr/def ::test-expression-spec+ [:multi {:dispatch (comp keyword :type)}+ [:column [:ref ::test-column-spec]]+ [:literal [:ref ::test-literal-expression-spec]]+ [:operator [:ref ::test-operator-expression-spec]]])++(mr/def ::test-named-expression-spec+ [:map+ [:name string?]+ [:value [:ref ::test-expression-spec]]])++(mr/def ::test-join-spec+ [:map+ [:source [:ref ::test-source-spec]]+ [:strategy ::lib.schema.join/strategy]+ [:conditions {:optional true} [:maybe [:sequential ::test-join-condition-spec]]]])++(mr/def ::test-join-source-spec+ [:multi {:dispatch (comp keyword :type)}+ [:column [:ref ::test-column-with-binning-spec]]+ [:literal [:ref ::test-literal-expression-spec]]+ [:operator [:ref ::test-operator-expression-spec]]])++(mr/def ::test-join-condition-spec+ [:map+ [:operator {:decode/normalize lib.schema.common/normalize-keyword} ::test-operator-spec]+ [:left [:ref ::test-join-source-spec]]+ [:right [:ref ::test-join-source-spec]]])++(mr/def ::test-aggregation-spec+ [:or+ [:ref ::test-expression-spec]+ [:ref ::test-named-expression-spec]])++(mr/def ::test-stage-spec+ [:map+ [:source {:optional true} [:maybe ::test-source-spec]]+ [:fields {:optional true} [:maybe [:sequential ::test-column-spec]]]+ [:expressions {:optional true} [:maybe [:sequential ::test-named-expression-spec]]]+ [:joins {:optional true} [:maybe [:sequential ::test-join-spec]]]+ [:filters {:optional true} [:maybe [:sequential ::test-expression-spec]]]+ [:aggregations {:optional true} [:maybe [:sequential ::test-aggregation-spec]]]+ [:breakouts {:optional true} [:maybe [:sequential ::test-breakout-spec]]]+ [:order-bys {:optional true} [:maybe [:sequential ::test-order-by-spec]]]+ [:limit {:optional true} [:maybe number?]]])++(mr/def ::test-query-spec+ [:map+ [:stages [:sequential ::test-stage-spec]]])diff --git a/src/metabase/testing_api/api.clj b/src/metabase/testing_api/api.cljindex bf66092104a..c643221fa06 100644--- a/src/metabase/testing_api/api.clj+++ b/src/metabase/testing_api/api.clj@@ -10,6 +10,11 @@[metabase.api.macros :as api.macros][metabase.app-db.core :as mdb][metabase.config.core :as config]+ [metabase.lib-be.core :as lib-be]+ [metabase.lib.core :as lib]+ [metabase.lib.schema :as lib.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.lib.schema.test-spec :as lib.schema.test-spec][metabase.premium-features.core :refer [defenterprise]][metabase.search.core :as search][metabase.search.ingestion :as search.ingestion]@@ -237,3 +242,14 @@"Manually triggers the cache refresh task, if Enterprise code is available."[](refresh-cache-configs!))++(api.macros/defendpoint :post "/query" :- ::lib.schema/query+ "Creates a query from a test query spec."+ [_route-params+ _query-params+ {:keys [database], :as query-spec} :- [:merge+ [:map+ [:database ::lib.schema.id/database]]+ [:ref ::lib.schema.test-spec/test-query-spec]]]+ (-> (lib-be/application-database-metadata-provider database)+ (lib/test-query query-spec)))diff --git a/test/metabase/lib/query/test_spec_test.cljc b/test/metabase/lib/query/test_spec_test.cljcnew file mode 100644index 00000000000..e20c53fe1f2--- /dev/null+++ b/test/metabase/lib/query/test_spec_test.cljc@@ -0,0 +1,1007 @@+(ns metabase.lib.query.test-spec-test+ (:require+ #?@(:cljs+ [[metabase.test-runner.assert-exprs.approximately-equal]])+ [clojure.test :refer [deftest is testing]]+ [metabase.lib.core :as lib]+ [metabase.lib.query.test-spec :as lib.query.test-spec]+ [metabase.lib.test-metadata :as meta]+ [metabase.lib.test-util :as lib.tu]))++#?(:cljs (comment metabase.test-runner.assert-exprs.approximately-equal/keep-me))++(def missing-value (symbol "nil #_\"key is not present.\""))++(deftest ^:parallel test-query-basic-table-source-test+ (testing "test-query creates a basic query from a table source"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}}]})]+ (is (=? {:lib/type :mbql/query+ :database (meta/id)+ :stages [{:lib/type :mbql.stage/mbql+ :source-table (meta/id :venues)}]}+ query)))))++(deftest ^:parallel test-query-with-card-source-test+ (testing "test-query creates a query from a card source"+ (let [query (lib.query.test-spec/test-query+ lib.tu/metadata-provider-with-card+ {:stages [{:source {:type :card+ :id 1}}]})]+ (is (=? {:lib/type :mbql/query+ :database (meta/id)+ :stages [{:lib/type :mbql.stage/mbql+ :source-card 1}]}+ query)))))++(deftest ^:parallel test-query-with-fields-test+ (testing "test-query adds fields to the query"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :orders)}+ :fields [{:type :column+ :name "ID"+ :source-name "ORDERS"}++ ;; column without source-name can be found if it is unambiguous+ {:type :column+ :name "TOTAL"}++ ;; implicitly joined column+ {:type :column+ :name "NAME"+ :source-name "PEOPLE"}]}]})]+ (is (=? [[:field {} (meta/id :orders :id)]+ [:field {} (meta/id :orders :total)]+ [:field {} (meta/id :people :name)]]+ (:fields (first (:stages query))))))))++(deftest ^:parallel test-query-with-expressions-test+ (testing "test-query adds expressions to the query"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :expressions [{:name "double-price"+ :value {:type :operator+ :operator :*+ :args [{:type :column+ :name "PRICE"}+ {:type :literal+ :value 2}]}}+ {:name "half-price"+ :value {:type :operator+ :operator :/+ :args [{:type :column+ :name "double-price"}+ {:type :literal+ :value 4}]}}]}]})]+ (is (=? [[:*+ {:lib/expression-name "double-price"}+ [:field {} (meta/id :venues :price)]+ 2]+ [:/+ {:lib/expression-name "half-price"}+ [:expression {} "double-price"]+ 4]]+ (lib/expressions query))))))++(deftest ^:parallel test-query-with-filters-test+ (testing "test-query adds filters to the query"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :filters [{:type :operator+ :operator :>+ :args [{:type :column+ :name "PRICE"}+ {:type :literal+ :value 3}]}]}]})]+ (is (=? [[:> {} [:field {} (meta/id :venues :price)] 3]]+ (lib/filters query))))))++(deftest ^:parallel test-query-with-aggregations-test+ (testing "test-query adds aggregations to the query"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :aggregations [{:type :operator+ :operator :count+ :args []}]}]})]+ (is (=? [[:count {}]]+ (lib/aggregations query))))))++(deftest ^:parallel test-query-with-breakouts-test+ (testing "test-query adds breakouts to the query"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :breakouts [{:type :column+ :name "CATEGORY_ID"}]}]})]+ (is (=? [[:field {} (meta/id :venues :category-id)]]+ (lib/breakouts query))))))++(deftest ^:parallel test-query-with-temporal-bucket-breakout-test+ (testing "test-query adds breakouts with temporal bucketing"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :checkins)}+ :breakouts [{:type :column+ :name "DATE"+ :unit :month}]}]})]+ (is (=? [[:field+ {:temporal-unit :month}+ (meta/id :checkins :date)]]+ (lib/breakouts query))))))++(deftest ^:parallel test-query-with-bin-count-breakout-test+ (testing "test-query adds breakouts with bin count binning"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :breakouts [{:type :column+ :name "PRICE"+ :bins 10}]}]})]+ (is (=? [[:field+ {:binning {:strategy :num-bins :num-bins 10}}+ (meta/id :venues :price)]]+ (lib/breakouts query))))))++(deftest ^:parallel test-query-with-bin-width-breakout-test+ (testing "test-query adds breakouts with bin width binning"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :breakouts [{:type :column+ :name "LATITUDE"+ :bin-width 20}]}]})]+ (is (=? [[:field+ {:binning {:strategy :bin-width :bin-width 20.0}}+ (meta/id :venues :latitude)]]+ (lib/breakouts query))))))++(deftest ^:parallel test-query-with-order-bys-test+ (testing "test-query adds order-bys to the query"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :order-bys [{:type :column+ :name "PRICE"+ :direction :asc}]}]})]+ (is (=? [[:asc {} [:field {} (meta/id :venues :price)]]]+ (lib/order-bys query))))))++(deftest ^:parallel test-query-with-limit-test+ (testing "test-query adds a limit to the query"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :limit 100}]})]+ (is (= 100 (lib/current-limit query))))))++(deftest ^:parallel test-query-with-joins-test+ (testing "test-query adds joins to the query"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :joins [{:source {:type :table+ :id (meta/id :categories)}+ :strategy :left-join}]}]})]+ (is (=? [{:strategy :left-join}]+ (lib/joins query))))))++(deftest ^:parallel test-query-with-join-conditions-test+ (testing "test-query adds joins with explicit conditions"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :joins [{:source {:type :table+ :id (meta/id :categories)}+ :strategy :left-join+ :conditions [{:operator :=+ :left {:type :column+ :name "CATEGORY_ID"}+ :right {:type :column+ :name "ID"}}]}]}]})]+ (is (=? [{:alias "Categories"+ :strategy :left-join+ :conditions [[:= {}+ [:field {:join-alias missing-value} (meta/id :venues :category-id)]+ [:field {:join-alias "Categories"} (meta/id :categories :id)]]]}]+ (lib/joins query))))))++(deftest ^:parallel test-query-with-join-conditions-with-binning-test+ (testing "test-query adds joins with binned conditions"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :users)}+ :joins [{:source {:type :table+ :id (meta/id :checkins)}+ :strategy :left-join+ :conditions [{:operator :=+ :left {:type :column+ :name "LAST_LOGIN"+ :source-name "USERS"+ :unit :month}+ :right {:type :column+ :name "DATE"+ :source-name "CHECKINS"+ :unit :month}}]}]}]})]+ (is (=? [{:strategy :left-join+ :conditions [[:= {}+ [:field {:temporal-unit :month :join-alias missing-value}+ (meta/id :users :last-login)]+ [:field {:temporal-unit :month :join-alias "Checkins - Last Login"}+ (meta/id :checkins :date)]]]}]+ (lib/joins query))))))++(deftest ^:parallel test-query-multi-stage-test+ (testing "test-query handles multiple stages"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :aggregations [{:type :operator+ :operator :count+ :args []}]+ :breakouts [{:type :column+ :name "CATEGORY_ID"}]}+ {:filters [{:type :operator+ :operator :>+ :args [{:type :column+ :name "count"}+ {:type :literal+ :value 10}]}]}]})]+ (is (= 2 (count (:stages query))))+ (is (= 1 (count (lib/aggregations query 0))))+ (is (= 1 (count (lib/breakouts query 0))))+ (is (= 1 (count (lib/filters query 1))))+ (is (empty? (lib/filters query 0)))+ (is (empty? (lib/aggregations query 1)))+ (is (empty? (lib/breakouts query 1))))))++(deftest ^:parallel test-query-complex-expression-test+ (testing "test-query handles complex nested expressions"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :filters [{:type :operator+ :operator :and+ :args [{:type :operator+ :operator :>+ :args [{:type :column+ :name "PRICE"}+ {:type :literal+ :value 2}]}+ {:type :operator+ :operator :<+ :args [{:type :column+ :name "PRICE"}+ {:type :literal+ :value 4}]}]}]}]})]+ (is (=? [[:and+ {}+ [:> {} [:field {} (meta/id :venues :price)] 2]+ [:< {} [:field {} (meta/id :venues :price)] 4]]]+ (lib/filters query))))))++(deftest ^:parallel test-query-named-aggregation-test+ (testing "test-query handles named aggregations"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :aggregations [{:name "my-count"+ :value {:type :operator+ :operator :count+ :args []}}]}]})]+ (is (=? [[:count {:display-name "my-count"}]]+ (lib/aggregations query))))))++(deftest ^:parallel test-query-error-no-column-found-test+ (testing "test-query throws when column is not found"+ (is (thrown-with-msg?+ #?(:clj Exception :cljs js/Error)+ #"No column found"+ (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :fields [{:type :column+ :name "NONEXISTENT"}]}]})))))++(deftest ^:parallel test-query-error-multiple-columns-found-test+ (testing "test-query throws when multiple columns match"+ ;; This test would need a scenario where the same column name appears multiple times+ ;; For example, after a join. Let's test this with a join scenario+ (is (thrown-with-msg?+ #?(:clj Exception :cljs js/Error)+ #"Multiple columns found"+ (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :joins [{:source {:type :table+ :id (meta/id :categories)}+ :strategy :left-join}]+ :fields [{:type :column+ :name "ID"}]}]})))))++(deftest ^:parallel test-query-aggregation-with-args-test+ (testing "test-query handles aggregations with column arguments"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :aggregations [{:type :operator+ :operator :sum+ :args [{:type :column+ :name "PRICE"}]}]}]})]+ (is (=? [[:sum {} [:field {} (meta/id :venues :price)]]]+ (lib/aggregations query))))))++(deftest ^:parallel test-query-aggregation-avg-test+ (testing "test-query handles avg aggregations"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :aggregations [{:type :operator+ :operator :avg+ :args [{:type :column+ :name "PRICE"}]}]}]})]+ (is (=? [[:avg {} [:field {} (meta/id :venues :price)]]]+ (lib/aggregations query))))))++(deftest ^:parallel test-query-named-aggregation-with-args-test+ (testing "test-query handles named aggregations with arguments"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :aggregations [{:name "total-price"+ :value {:type :operator+ :operator :sum+ :args [{:type :column+ :name "PRICE"}]}}]}]})]+ (is (=? [[:sum {:display-name "total-price"} [:field {} (meta/id :venues :price)]]]+ (lib/aggregations query))))))++(deftest ^:parallel test-query-with-join-card-source-test+ (testing "test-query supports joins with card sources"+ (let [query (lib.query.test-spec/test-query+ lib.tu/metadata-provider-with-card+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :joins [{:source {:type :card+ :id 1}+ :strategy :left-join+ :conditions [{:operator :=+ :left {:type :column+ :name "ID"}+ :right {:type :column+ :name "USER_ID"}}]}]}]})]+ (is (=? [{:strategy :left-join}]+ (-> query lib/joins))))))++(deftest ^:parallel test-query-with-join-conditions-with-bin-width-test+ (testing "test-query adds joins with bin-width binned conditions"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :joins [{:source {:type :table+ :id (meta/id :venues)}+ :strategy :left-join+ :conditions [{:operator :=+ :left {:type :column+ :name "LATITUDE"+ :bin-width 20}+ :right {:type :column+ :name "LONGITUDE"+ :bin-width 20}}]}]}]})]+ (is (=? [{:lib/type :mbql/join+ :strategy :left-join+ :conditions [[:= {}+ [:field {:join-alias missing-value+ :binning {:bin-width 20.0+ :strategy :bin-width}}+ (meta/id :venues :latitude)]+ [:field {:join-alias "Venues - Latitude"+ :binning {:bin-width 20.0+ :strategy :bin-width}}+ (meta/id :venues :longitude)]]]}]+ (lib/joins query))))))++(deftest ^:parallel test-query-order-by-with-temporal-bucket-test+ (testing "test-query adds order-by with temporal bucketing"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :checkins)}+ :order-bys [{:type :column+ :name "DATE"+ :unit :month+ :direction :desc}]}]})]+ (is (=? [[:desc {}+ [:field {:temporal-unit :month} (meta/id :checkins :date)]]]+ (lib/order-bys query))))))++(deftest ^:parallel test-query-order-by-with-temporal-bucket-test-with-duplicate-column+ (testing "test-query adds order-by with temporal bucketing when selecting the first column"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :checkins)}+ :breakouts [{:type :column+ :name "DATE"+ :unit :month}+ {:type :column+ :name "DATE"+ :unit :year}]+ :order-bys [{:type :column+ :name "DATE"+ :display-name "Date: Month"+ :direction :desc}]}]})]+ (is (=? [[:desc {}+ [:field {:temporal-unit :month} (meta/id :checkins :date)]]]+ (lib/order-bys query)))))++ (testing "test-query adds order-by with temporal bucketing when selecting the second column"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :checkins)}+ :breakouts [{:type :column+ :name "DATE"+ :unit :month}+ {:type :column+ :name "DATE"+ :unit :year}]+ :order-bys [{:type :column+ :name "DATE"+ :display-name "Date: Year"+ :direction :desc}]}]})]+ (is (=? [[:desc {}+ [:field {:temporal-unit :year} (meta/id :checkins :date)]]]+ (lib/order-bys query))))))++(deftest ^:parallel test-query-order-by-with-binning-test+ (testing "test-query adds order-by with binning"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :order-bys [{:type :column+ :name "PRICE"+ :bins 10+ :direction :asc}]}]})]+ (is (=? [[:asc {} [:field {:binning {:strategy :num-bins}} (meta/id :venues :price)]]]+ (lib/order-bys query))))))++(deftest ^:parallel test-query-expression-with-aggregation-test+ (testing "test-query handles expressions that reference aggregations from earlier stages"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :aggregations [{:name "total-price"+ :value {:type :operator+ :operator :sum+ :args [{:type :column+ :name "PRICE"}]}}]+ :breakouts [{:type :column+ :name "CATEGORY_ID"}]}+ {:expressions [{:name "double-total"+ :value {:type :operator+ :operator :*+ :args [{:type :column+ :name "total-price"}+ {:type :literal+ :value 2}]}}]}]})]+ (is (= 2 (count (:stages query))))+ (is (= 1 (count (lib/aggregations query 0))))+ (is (= 1 (count (lib/expressions query 1)))))))++(deftest ^:parallel test-query-multiple-filters-test+ (testing "test-query handles multiple independent filters"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :filters [{:type :operator+ :operator :>+ :args [{:type :column+ :name "PRICE"}+ {:type :literal+ :value 2}]}+ {:type :operator+ :operator :<+ :args [{:type :column+ :name "LATITUDE"}+ {:type :literal+ :value 40}]}]}]})]+ (is (=? [[:> {} [:field {} (meta/id :venues :price)] 2]+ [:< {} [:field {} (meta/id :venues :latitude)] 40]]+ (-> query lib/filters))))))++(deftest ^:parallel test-query-multiple-joins-test+ (testing "test-query handles multiple joins"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :joins [{:source {:type :table+ :id (meta/id :categories)}+ :strategy :left-join}+ {:source {:type :table+ :id (meta/id :checkins)}+ :strategy :right-join}]}]})]+ (is (=? [{:strategy :left-join+ :alias "Categories"}+ {:strategy :right-join+ :alias "Checkins"}]+ (lib/joins query))))))++(deftest ^:parallel test-query-multiple-dependent-joins-test+ (testing "test-query handles multiple joins"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :orders)}+ :joins [{:source {:type :table+ :id (meta/id :products)}+ :strategy :left-join}+ {:source {:type :table+ :id (meta/id :reviews)}+ :strategy :right-join+ :conditions [{:operator :=+ :left {:type :column+ :name "ID"+ :source-name "PRODUCTS"}+ :right {:type :column+ :name "PRODUCT_ID"}}]}]}]})]++ (is (=? [{:strategy :left-join+ :alias "Products"}+ {:strategy :right-join+ :alias "Reviews"+ :conditions [[:= {}+ [:field {:join-alias "Products"} (meta/id :products :id)]+ [:field {:join-alias "Reviews"} (meta/id :reviews :product-id)]]]}]+ (lib/joins query))))))++(deftest ^:parallel test-query-with-implicit-join-test+ (testing "test-query handles implicit joins in most clauses"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :orders)}+ :filters [{:type :operator+ :operator :=+ :args [{:type :literal+ :value "Gadget"}+ {:type :column+ :source-name "PRODUCTS"+ :name "CATEGORY"}]}]++ :aggregations [{:type :operator+ :operator :sum+ :args [{:type :column+ :source-name "PRODUCTS"+ :name "PRICE"}]}]+ :breakouts [{:type :column+ :source-name "PRODUCTS"+ :name "CREATED_AT"}]++ :expressions [{:name "Custom"+ :value {:type :operator+ :operator :++ :args [{:type :literal+ :value 42}+ {:type :column+ :name "PRICE"}]}}]}]})]++ (is (=? [[:=+ {} "Gadget"+ [:field+ {:source-field (meta/id :orders :product-id)}+ (meta/id :products :category)]]]+ (lib/filters query)))++ (is (=? [[:sum {}+ [:field+ {:source-field (meta/id :orders :product-id)}+ (meta/id :products :price)]]]+ (lib/aggregations query)))++ (is (=? [[:++ {:lib/expression-name "Custom"}+ 42+ [:field+ {:source-field (meta/id :orders :product-id)}+ (meta/id :products :price)]]]+ (lib/expressions query)))++ (is (=? [[:field+ {:source-field (meta/id :orders :product-id)}+ (meta/id :products :created-at)]]+ (lib/breakouts query)))++ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :orders)}+ :order-bys [{:type :column+ :name "PRICE"}]}]})]++ (is (=? [[:asc {} [:field+ {:source-field (meta/id :orders :product-id)}+ (meta/id :products :price)]]]+ (lib/order-bys query)))))))++(deftest ^:parallel test-query-three-stage-test+ (testing "test-query handles three stages"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [{:source {:type :table+ :id (meta/id :venues)}+ :aggregations [{:type :operator+ :operator :count+ :args []}]+ :breakouts [{:type :column+ :name "CATEGORY_ID"}]}+ {:filters [{:type :operator+ :operator :>+ :args [{:type :column+ :name "count"}+ {:type :literal+ :value 10}]}]}+ {:limit 5}]})]+ (is (= 3 (count (:stages query))))+ (is (= 1 (count (lib/aggregations query 0))))+ (is (= 1 (count (lib/breakouts query 0))))+ (is (= 1 (count (lib/filters query 1))))+ (is (= 5 (lib/current-limit query 2))))))++#_{:clj-kondo/ignore [:metabase/i-like-making-cams-eyes-bleed-with-horrifically-long-tests]}+(deftest ^:parallel test-query-comprehensive-all-features-test+ (testing "test-query exercises all functionality in a comprehensive multi-stage query"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [;; Stage 0+ {:source {:type :table+ :id (meta/id :orders)}++ :joins [{:source {:type :table+ :id (meta/id :products)}+ :strategy :left-join}+ {:source {:type :table+ :id (meta/id :people)}+ :strategy :inner-join+ :conditions [{:operator :=+ :left {:type :column+ :name "USER_ID"}+ :right {:type :column+ :name "ID"}}]}]++ :expressions [{:name "discounted-price"+ :value {:type :operator+ :operator :*+ :args [{:type :column+ :name "PRICE"+ :source-name "PRODUCTS"}+ {:type :literal+ :value 0.9}]}}+ {:name "double-discount"+ :value {:type :operator+ :operator :/+ :args [{:type :column+ :name "discounted-price"}+ {:type :literal+ :value 2}]}}]++ :filters [{:type :operator+ :operator :and+ :args [{:type :operator+ :operator :>+ :args [{:type :column+ :name "TOTAL"}+ {:type :literal+ :value 50}]}+ {:type :operator+ :operator :or+ :args [{:type :operator+ :operator :=+ :args [{:type :column+ :name "CATEGORY"+ :source-name "PRODUCTS"}+ {:type :literal+ :value "Widget"}]}+ {:type :operator+ :operator :<+ :args [{:type :column+ :name "double-discount"}+ {:type :literal+ :value 10}]}]}]}]++ :aggregations [{:name "total-count"+ :value {:type :operator+ :operator :count+ :args []}}+ {:name "total-revenue"+ :value {:type :operator+ :operator :sum+ :args [{:type :column+ :name "TOTAL"}]}}+ {:type :operator+ :operator :avg+ :args [{:type :column+ :name "PRICE"+ :source-name "PRODUCTS"}]}]++ :breakouts [{:type :column+ :name "CREATED_AT"+ :source-name "ORDERS"+ :unit :month}+ {:type :column+ :name "QUANTITY"+ :bins 10}]++ :order-bys [{:type :column+ :name "CREATED_AT"+ :source-name "ORDERS"+ :direction :desc}]}++ ;; Stage 1+ {:expressions [{:name "doubled-count"+ :value {:type :operator+ :operator :*+ :args [{:type :column+ :name "total-count"}+ {:type :literal+ :value 2}]}}]++ :filters [{:type :operator+ :operator :>+ :args [{:type :column+ :name "total-revenue"}+ {:type :literal+ :value 1000}]}]++ :order-bys [{:type :column+ :name "total-revenue"+ :direction :asc}]}++ ;; Stage 2+ {:filters [{:type :operator+ :operator :<+ :args [{:type :column+ :name "doubled-count"}+ {:type :literal+ :value 500}]}]++ :limit 25}]})]++ (is (= 3 (lib/stage-count query)))++ ;; Stage 0+ (is (=? [{:strategy :left-join+ :alias "Products"}+ {:strategy :inner-join+ :alias "People - User"}]+ (lib/joins query 0)))++ (is (=? [[:* {:lib/expression-name "discounted-price"} [:field {} (meta/id :products :price)] 0.9]+ [:/ {:lib/expression-name "double-discount"} [:expression {} "discounted-price"] 2]]+ (lib/expressions query 0)))++ (is (empty? (lib/fields query 0)))++ (is (=? [[:and {}+ [:> {} [:field {} (meta/id :orders :total)] 50]+ [:or {}+ [:= {} [:field {:join-alias "Products"} (meta/id :products :category)] "Widget"]+ [:< {} [:expression {} "double-discount"] 10]]]]+ (lib/filters query 0)))++ (is (=? [[:count {:display-name "total-count"}]+ [:sum {:display-name "total-revenue"} [:field {} (meta/id :orders :total)]]+ [:avg {} [:field {:join-alias "Products"} (meta/id :products :price)]]]+ (lib/aggregations query 0)))++ (is (=? [[:field {:temporal-unit :month} (meta/id :orders :created-at)]+ [:field {:binning {:strategy :num-bins :num-bins 10}} (meta/id :orders :quantity)]]+ (lib/breakouts query 0)))++ (is (=? [[:desc {} [:field {:temporal-unit :month} (meta/id :orders :created-at)]]]+ (lib/order-bys query 0)))++ ;; Stage 1+ (is (=? [[:* {:lib/expression-name "doubled-count"}+ [:field {} "total-count"]+ 2]]+ (lib/expressions query 1)))++ (is (=? [[:> {} [:field {} "total-revenue"] 1000]]+ (lib/filters query 1)))++ (is (=? [[:asc {} [:field {} "total-revenue"]]]+ (lib/order-bys query 1)))++ (is (empty? (lib/fields query 1)))++ ;; Stage 2+ (is (=? [[:< {} [:field {} "doubled-count"] 500]]+ (lib/filters query 2)))++ (is (empty? (lib/fields query 2)))++ (is (= 25 (lib/current-limit query 2))))))++#_{:clj-kondo/ignore [:metabase/i-like-making-cams-eyes-bleed-with-horrifically-long-tests]}+(deftest ^:parallel test-query-comprehensive-all-features-test-with-fields+ (testing "test-query exercises all functionality in a comprehensive multi-stage query, with fields clause"+ (let [query (lib.query.test-spec/test-query+ meta/metadata-provider+ {:stages [;; Stage 0+ {:source {:type :table+ :id (meta/id :orders)}++ :joins [{:source {:type :table+ :id (meta/id :products)}+ :strategy :left-join}+ {:source {:type :table+ :id (meta/id :people)}+ :strategy :inner-join+ :conditions [{:operator :=+ :left {:type :column+ :name "USER_ID"}+ :right {:type :column+ :name "ID"}}]}]++ :expressions [{:name "discounted-price"+ :value {:type :operator+ :operator :*+ :args [{:type :column+ :name "PRICE"+ :source-name "PRODUCTS"}+ {:type :literal+ :value 0.9}]}}+ {:name "double-discount"+ :value {:type :operator+ :operator :/+ :args [{:type :column+ :name "discounted-price"}+ {:type :literal+ :value 2}]}}]++ :filters [{:type :operator+ :operator :and+ :args [{:type :operator+ :operator :>+ :args [{:type :column+ :name "TOTAL"}+ {:type :literal+ :value 50}]}+ {:type :operator+ :operator :or+ :args [{:type :operator+ :operator :=+ :args [{:type :column+ :name "CATEGORY"+ :source-name "PRODUCTS"}+ {:type :literal+ :value "Widget"}]}+ {:type :operator+ :operator :<+ :args [{:type :column+ :name "double-discount"}+ {:type :literal+ :value 10}]}]}]}]++ :order-bys [{:type :column+ :name "CREATED_AT"+ :source-name "ORDERS"+ :direction :desc}]++ :fields [{:type :column+ :name "TOTAL"}+ {:type :column+ :name "CATEGORY"+ :source-name "PRODUCTS"}+ {:type :column+ :name "double-discount"}]}++ ;; Stage 1+ {:expressions [{:name "half-discount"+ :value {:type :operator+ :operator :/+ :args [{:type :column+ :name "double-discount"}+ {:type :literal+ :value 4}]}}]++ :filters [{:type :operator+ :operator :>+ :args [{:type :column+ :name "half-discount"}+ {:type :literal+ :value 1000}]}]}++ ;; Stage 2+ {:filters [{:type :operator+ :operator :<+ :args [{:type :column+ :name "half-discount"}+ {:type :literal+ :value 500}]}]++ :limit 25}]})]++ (is (= 3 (lib/stage-count query)))++ ;; Stage 0+ (is (=? [{:strategy :left-join+ :alias "Products"}+ {:strategy :inner-join+ :alias "People - User"}]+ (lib/joins query 0)))++ (is (=? [[:* {:lib/expression-name "discounted-price"} [:field {} (meta/id :products :price)] 0.9]+ [:/ {:lib/expression-name "double-discount"} [:expression {} "discounted-price"] 2]]+ (lib/expressions query 0)))++ (is (=? [[:field {} (meta/id :orders :total)]+ [:field {:join-alias "Products"} (meta/id :products :category)]+ [:expression {} "double-discount"]+ [:expression {} "discounted-price"]]+ (lib/fields query 0)))++ (is (=? [[:and {}+ [:> {} [:field {} (meta/id :orders :total)] 50]+ [:or {}+ [:= {} [:field {:join-alias "Products"} (meta/id :products :category)] "Widget"]+ [:< {} [:expression {} "double-discount"] 10]]]]+ (lib/filters query 0)))++ (is (empty? (lib/aggregations query 0)))+ (is (empty? (lib/breakouts query 0)))++ (is (=? [[:desc {} [:field {} (meta/id :orders :created-at)]]]+ (lib/order-bys query 0)))++ ;; Stage 1+ (is (=? [[:/ {:lib/expression-name "half-discount"}+ [:field {} "double-discount"]+ 4]]+ (lib/expressions query 1)))++ (is (=? [[:> {} [:expression {} "half-discount"] 1000]]+ (lib/filters query 1)))++ (is (empty? (lib/order-bys query 1)))+ (is (empty? (lib/fields query 1)))++ ;; Stage 2+ (is (=? [[:< {} [:field {} "half-discount"] 500]]+ (lib/filters query 2)))++ (is (empty? (lib/fields query 2)))++ (is (= 25 (lib/current-limit query 2))))))