Skip to content

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

e2e2unit: native subquery (#85843)

Nemanja Glumac18d7711bacee
diff --git a/e2e/test/scenarios/native/native_subquery.cy.spec.js b/e2e/test/scenarios/native/native_subquery.cy.spec.js
deleted file mode 100644
index f43f90e227d..00000000000
--- a/e2e/test/scenarios/native/native_subquery.cy.spec.js
+++ /dev/null
@@ -1,219 +0,0 @@
-const { H } = cy;
-import { ADMIN_PERSONAL_COLLECTION_ID } from "e2e/support/cypress_sample_instance_data";
-
-describe("scenarios > question > native subquery", () => {
- beforeEach(() => {
- H.restore();
- cy.signInAsAdmin();
- });
-
- it("typing a card tag should open the data reference", () => {
- H.createNativeQuestion({
- name: "A People Question",
- native: { query: "SELECT id AS a_unique_column_name FROM PEOPLE" },
- }).then(({ body: { id: questionId1 } }) => {
- H.createNativeQuestion({
- name: "A People Model",
- native: {
- query: "SELECT id AS another_unique_column_name FROM PEOPLE",
- },
- type: "model",
- }).then(({ body: { id: questionId2 } }) => {
- const tagName1 = `#${questionId1}-a-people-question`;
- const queryText = `{{${tagName1}}}`;
- // create a question with a template tag
- H.createNativeQuestion({
- name: "Count of People",
- native: { query: queryText },
- }).then(({ body: { id: questionId3 } }) => {
- cy.visit(`/question/${questionId3}`);
- // Refresh the state, so previously created questions need to be loaded again.
- cy.reload();
- cy.findByText("Open Editor").click();
- // placing the cursor inside an existing template tag should open the data reference
- H.NativeEditor.focus().type("{leftarrow}{leftarrow}");
- cy.findByText("A People Question");
- // moving the cursor out of the tag should keep the data reference open
- H.NativeEditor.type("{rightarrow}{rightarrow}", { focus: false });
- cy.findByTestId("sidebar-right")
- .findByText("A People Question")
- .should("be.visible");
- // typing after the tag, outside of it, should keep the data reference open
- H.NativeEditor.type(" ", { focus: false });
- H.NativeEditor.value().should("eq", `${queryText} `);
- cy.findByTestId("sidebar-right")
- .findByText("A People Question")
- .should("be.visible");
- // typing a template tag id should open the editor
- H.NativeEditor.type(`{{#${questionId2}`);
- cy.findByText("A People Model");
- });
- });
- });
- });
-
- it("autocomplete should complete question slugs inside template tags", () => {
- // Create a question and a model.
- H.createNativeQuestion({
- name: "A People Question",
- native: {
- query: "SELECT id FROM PEOPLE",
- },
- }).then(({ body: { id: questionId1 } }) => {
- H.createNativeQuestion({
- name: "A People Model",
- native: {
- query: "SELECT id FROM PEOPLE",
- },
- type: "model",
- collection_id: ADMIN_PERSONAL_COLLECTION_ID,
- }).then(({ body: { id: questionId2 } }) => {
- // Move question 2 from the personal collection to Our analytics
- cy.visit(`/question/${questionId2}`);
- H.openQuestionActions();
- cy.findByTestId("move-button").click();
- H.entityPickerModal().within(() => {
- cy.findByText(/Our analytics/).click();
- cy.button("Move").click();
- });
-
- H.startNewNativeQuestion();
- cy.reload(); // Refresh the state, so previously created questions need to be loaded again.
- H.NativeEditor.focus();
-
- cy.wait(200); // This reduces flakiness
-
- H.NativeEditor.focus().type(" {{#people");
-
- // Wait until another explicit autocomplete is triggered
- // (slightly longer than AUTOCOMPLETE_DEBOUNCE_DURATION)
- // See https://github.com/metabase/metabase/pull/20970
- cy.wait(1000);
-
- H.NativeEditor.completions().within(() => {
- H.NativeEditor.completion(`${questionId2}-a-`)
- .should("be.visible")
- .should("contain", "Model in Our analytics");
- H.NativeEditor.completion(`${questionId1}-a-`)
- .should("be.visible")
- .should("contain", "Question in Our analytics");
- });
- });
- });
- });
-
- it("autocomplete should work for columns from referenced questions", () => {
- // Create two saved questions, the first will be referenced in the query when it is opened, and the second will be added to the query after it is opened.
- H.createNativeQuestion({
- name: "A People Question 1",
- native: {
- query: "SELECT id AS a_unique_column_name FROM PEOPLE",
- },
- }).then(({ body: { id: questionId1 } }) => {
- H.createNativeQuestion({
- name: "A People Question 2",
- native: {
- query: "SELECT id AS another_unique_column_name FROM PEOPLE",
- },
- }).then(({ body: { id: questionId2 } }) => {
- const tagID = `#${questionId1}`;
-
- // create a question with a template tag
- H.createNativeQuestion({
- name: "Count of People",
- native: {
- query: `select COUNT(*) from {{#${questionId1}}}`,
- "template-tags": {
- [tagID]: {
- id: "10422a0f-292d-10a3-fd90-407cc9e3e20e",
- name: tagID,
- "display-name": tagID,
- type: "card",
- "card-id": questionId1,
- },
- },
- },
- }).then(({ body: { id: questionId3 } }) => {
- cy.visit(`/question/${questionId3}`);
-
- // Refresh the state, so previously created questions need to be loaded again.
- cy.reload();
- cy.findByText("Open Editor").click();
- H.NativeEditor.focus().type(" ").type("a_unique");
-
- // Wait until another explicit autocomplete is triggered
- // (slightly longer than AUTOCOMPLETE_DEBOUNCE_DURATION)
- // See https://github.com/metabase/metabase/pull/20970
- cy.wait(1000);
-
- H.NativeEditor.completion("A_UNIQUE_COLUMN_NAME").should(
- "be.visible",
- );
-
- H.NativeEditor.focus().type(` {{#${questionId2}}}`);
-
- // Again, typing in in one go doesn't always work
- // so type it in two parts
- H.NativeEditor.focus().type(" ").type("another");
-
- H.NativeEditor.completion("ANOTHER_UNIQUE_COLUMN_NAME").should(
- "be.visible",
- );
- });
- });
- });
- });
-
- it("card reference tags should update when the name of the card changes", () => {
- H.createNativeQuestion({
- name: "A People Question 1",
- native: {
- query: "SELECT id AS a_unique_column_name FROM PEOPLE",
- },
- }).then(({ body: { id: questionId1 } }) => {
- cy.wrap(questionId1).as("questionId");
- const tagID = `#${questionId1}`;
- H.createNativeQuestion({
- name: "Count of People",
- native: {
- query: `select COUNT(*) from {{#${questionId1}}}`,
- "template-tags": {
- [tagID]: {
- id: "10422a0f-292d-10a3-fd90-407cc9e3e20e",
- name: tagID,
- "display-name": tagID,
- type: "card",
- "card-id": questionId1,
- },
- },
- },
- }).then(({ body: { id: questionId2 } }) => {
- // check the original name is in the query
- cy.visit(`/question/${questionId2}`);
- cy.findByText("Open Editor").click();
- cy.get("@questionId").then((questionId) => {
- H.NativeEditor.get()
- .should("be.visible")
- .and("contain", `{{#${questionId}-a-people-question-1}}`);
- });
-
- // change the name
- cy.intercept("PUT", "/api/card/*").as("updateCard");
- cy.visit(`/question/${questionId1}`);
- cy.findByText("A People Question 1").type(" changed");
- // unfocus the input
- cy.findByText("Open Editor").click();
- cy.wait("@updateCard");
-
- // check the name has changed
- cy.visit(`/question/${questionId2}`);
- cy.findByText("Open Editor").click();
- cy.get("@questionId").then((questionId) => {
- H.NativeEditor.get()
- .should("be.visible")
- .and("contain", `{{#${questionId}-a-people-question-1-changed}}`);
- });
- });
- });
- });
-});
diff --git a/frontend/src/metabase-types/api/autocomplete.ts b/frontend/src/metabase-types/api/autocomplete.ts
index 3c2a1947fda..e60e86349bd 100644
--- a/frontend/src/metabase-types/api/autocomplete.ts
+++ b/frontend/src/metabase-types/api/autocomplete.ts
@@ -18,5 +18,5 @@ export type CardAutocompleteSuggestion = {
id: number;
name: string;
type: CardType;
- collection_name: string;
+ collection_name: string | null;
};
diff --git a/frontend/src/metabase/query_builder/actions/native.unit.spec.ts b/frontend/src/metabase/query_builder/actions/native.unit.spec.ts
new file mode 100644
index 00000000000..9f0ed73ca28
--- /dev/null
+++ b/frontend/src/metabase/query_builder/actions/native.unit.spec.ts
@@ -0,0 +1,57 @@
+import { cardApi } from "metabase/api";
+import * as rtkEndpointUtils from "metabase/api/utils/run-rtk-endpoint";
+import type { Card } from "metabase-types/api";
+import { createMockCard } from "metabase-types/api/mocks";
+
+import { queryBuilderReducer } from "../store/reducer";
+
+import { openDataReferenceAtQuestion } from "./native";
+
+type SetupOpts = {
+ card: Card | undefined;
+};
+
+async function setup({ card }: SetupOpts) {
+ const runRtkEndpointSpy = jest
+ .spyOn(rtkEndpointUtils, "runRtkEndpoint")
+ .mockResolvedValue(card);
+
+ const action = await openDataReferenceAtQuestion(card?.id ?? 1)(
+ jest.fn(),
+ jest.fn(),
+ );
+ const initialState = queryBuilderReducer(undefined, { type: "@@INIT" });
+ const state = queryBuilderReducer(initialState, action);
+
+ return { runRtkEndpointSpy, initialState, state };
+}
+
+describe("openDataReferenceAtQuestion", () => {
+ afterEach(() => {
+ jest.restoreAllMocks();
+ });
+
+ it("opens the data reference at the referenced card", async () => {
+ const card = createMockCard({ id: 10, database_id: 2 });
+ const { runRtkEndpointSpy, initialState, state } = await setup({ card });
+
+ expect(runRtkEndpointSpy).toHaveBeenCalledWith(
+ { id: 10, ignore_error: true },
+ expect.any(Function),
+ cardApi.endpoints.getCard,
+ { forceRefetch: false },
+ );
+ expect(initialState.uiControls.isShowingDataReference).toBe(false);
+ expect(state.uiControls.isShowingDataReference).toBe(true);
+ expect(state.uiControls.dataReferenceStack).toEqual([
+ { type: "database", id: 2 },
+ { type: "question", id: 10 },
+ ]);
+ });
+
+ it("keeps the data reference closed when the card cannot be loaded", async () => {
+ const { initialState, state } = await setup({ card: undefined });
+
+ expect(state.uiControls).toEqual(initialState.uiControls);
+ });
+});
diff --git a/frontend/src/metabase/querying/components/CodeMirrorEditor/CodeMirrorEditor.unit.spec.tsx b/frontend/src/metabase/querying/components/CodeMirrorEditor/CodeMirrorEditor.unit.spec.tsx
index de05b073197..6427a122ad2 100644
--- a/frontend/src/metabase/querying/components/CodeMirrorEditor/CodeMirrorEditor.unit.spec.tsx
+++ b/frontend/src/metabase/querying/components/CodeMirrorEditor/CodeMirrorEditor.unit.spec.tsx
@@ -15,7 +15,11 @@ import { waitForRequestsToSettle } from "__support__/utils";
import * as Browser from "metabase/utils/browser";
import * as Lib from "metabase-lib";
import { createMetadataProvider } from "metabase-lib/test-helpers";
-import type { NativeQuerySnippet } from "metabase-types/api";
+import type {
+ CardAutocompleteSuggestion,
+ NativeQuerySnippet,
+} from "metabase-types/api";
+import { createMockCard, createMockField } from "metabase-types/api/mocks";
import { createSampleDatabase } from "metabase-types/api/mocks/presets";
import {
@@ -271,6 +275,67 @@ describe("CodemirrorEditor", () => {
expect(onRunQuery).toHaveBeenCalledTimes(1);
});
+ it("reports the card id when the cursor moves into a card tag", async () => {
+ const { onCursorMoveOverCardTag } = setup({
+ text: "SELECT {{#42-foo}} FROM t",
+ });
+
+ await waitForRequestsToSettle();
+ await userEvent.click(screen.getByRole("textbox"));
+ await userEvent.keyboard("{ArrowRight>3/}");
+ expect(onCursorMoveOverCardTag).not.toHaveBeenCalled();
+
+ await userEvent.keyboard("{ArrowRight>8/}");
+ expect(onCursorMoveOverCardTag).toHaveBeenLastCalledWith(42);
+
+ await userEvent.keyboard("{ArrowRight>9/}");
+ const callsUntilAfterTag = onCursorMoveOverCardTag.mock.calls.length;
+ await userEvent.keyboard("{ArrowRight>3/}");
+ expect(onCursorMoveOverCardTag).toHaveBeenCalledTimes(callsUntilAfterTag);
+ });
+
+ it("completes columns of the cards referenced in the query", async () => {
+ fetchMock.get("path:/api/database/1/autocomplete_suggestions", []);
+ fetchMock.get(
+ "path:/api/card/42",
+ createMockCard({
+ id: 42,
+ name: "Referenced Question",
+ result_metadata: [createMockField({ name: "A_UNIQUE_COLUMN_NAME" })],
+ }),
+ );
+ setup({ text: "SELECT * FROM {{#42-referenced-question}}" });
+
+ await waitForRequestsToSettle();
+ await userEvent.click(screen.getByRole("textbox"));
+ await userEvent.keyboard("{Control>}{End}{/Control} a_uni");
+
+ expect(
+ await screen.findByRole("listbox", { name: "Completions" }),
+ ).toHaveTextContent("A_UNIQUE_COLUMN_NAME");
+ });
+
+ it("completes card names inside a card tag", async () => {
+ const suggestion: CardAutocompleteSuggestion = {
+ id: 51,
+ name: "A People Question",
+ type: "question",
+ collection_name: null,
+ };
+ fetchMock.get("path:/api/database/1/card_autocomplete_suggestions", [
+ suggestion,
+ ]);
+ setup({ text: "SELECT * FROM " });
+
+ await waitForRequestsToSettle();
+ await userEvent.click(screen.getByRole("textbox"));
+ await userEvent.keyboard("{Control>}{End}{/Control}{{{{#peo");
+
+ expect(
+ await screen.findByRole("listbox", { name: "Completions" }),
+ ).toHaveTextContent("#51-a-people-question");
+ });
+
it("keeps read-only proposals read-only", async () => {
setup({ text: "SELECT 1", proposedText: "SELECT 2", readOnly: true });
diff --git a/frontend/src/metabase/querying/components/CodeMirrorEditor/completers.unit.spec.tsx b/frontend/src/metabase/querying/components/CodeMirrorEditor/completers.unit.spec.tsx
index 75c1302ec42..04cd37478a2 100644
--- a/frontend/src/metabase/querying/components/CodeMirrorEditor/completers.unit.spec.tsx
+++ b/frontend/src/metabase/querying/components/CodeMirrorEditor/completers.unit.spec.tsx
@@ -511,6 +511,28 @@ describe("useCardTagCompletion", () => {
expect(fetchMock.callHistory.calls(url)).toHaveLength(1);
});
+ it("should describe models in the root collection", async () => {
+ const { complete } = setup({
+ results: [
+ { id: 7, name: "People Model", type: "model", collection_name: null },
+ ],
+ });
+ const results = await complete("SELECT {{ #people|");
+
+ expect(results).toEqual({
+ from: 10,
+ to: 17,
+ validFor: expect.any(Function),
+ options: [
+ {
+ label: "#7-people-model",
+ apply: "#7-people-model }}",
+ detail: "Model in Our analytics",
+ },
+ ],
+ });
+ });
+
it("should autocomplete cards when inside a closed card tag, inside a word", async () => {
const { complete, url } = setup();
const results = await complete("SELECT {{ #ba|r }}");
diff --git a/frontend/src/metabase/querying/components/CodeMirrorEditor/util.tsx b/frontend/src/metabase/querying/components/CodeMirrorEditor/util.tsx
index e06529f44d6..9d9a4fc3a08 100644
--- a/frontend/src/metabase/querying/components/CodeMirrorEditor/util.tsx
+++ b/frontend/src/metabase/querying/components/CodeMirrorEditor/util.tsx
@@ -43,7 +43,7 @@ export function convertSelectionToRange(
export const getCardAutocompleteResultMeta = (
type: CardType,
- collectionName: string = t`Our analytics`,
+ collectionName: string | null,
) => {
const collection = collectionName ?? t`Our analytics`;
if (type === "question") {
diff --git a/frontend/src/metabase/querying/components/CodeMirrorEditor/util.unit.spec.tsx b/frontend/src/metabase/querying/components/CodeMirrorEditor/util.unit.spec.tsx
index 6ac602e2e17..43afd63df0f 100644
--- a/frontend/src/metabase/querying/components/CodeMirrorEditor/util.unit.spec.tsx
+++ b/frontend/src/metabase/querying/components/CodeMirrorEditor/util.unit.spec.tsx
@@ -1,6 +1,35 @@
import { EditorState } from "@codemirror/state";
-import { getPlaceholderText, matchTagAtCursor } from "./util";
+import { createMockMetadata } from "__support__/metadata";
+import * as Lib from "metabase-lib";
+import { createMetadataProvider } from "metabase-lib/test-helpers";
+import { createSampleDatabase } from "metabase-types/api/mocks/presets";
+
+import {
+ getPlaceholderText,
+ getReferencedCardIds,
+ matchTagAtCursor,
+} from "./util";
+
+describe("getReferencedCardIds", () => {
+ const database = createSampleDatabase();
+ const provider = createMetadataProvider({
+ databaseId: database.id,
+ metadata: createMockMetadata({ databases: [database] }),
+ });
+
+ function createQuery(text: string) {
+ return Lib.createTestNativeQuery(provider, { query: text });
+ }
+
+ it("should return the ids of the cards referenced in the query", () => {
+ const query = createQuery("SELECT * FROM {{#1-foo}}");
+ expect(getReferencedCardIds(query)).toEqual([1]);
+
+ const updatedQuery = createQuery("SELECT * FROM {{#1-foo}}, {{#2-bar}}");
+ expect(getReferencedCardIds(updatedQuery)).toEqual([1, 2]);
+ });
+});
describe("matchTagAtCursor", () => {
function setup(