Skip to content

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

Cover Mongo `convert-to-native` on a unit test level (#83011)

Nemanja Glumaceedda27c892b
diff --git a/e2e/test/scenarios/question/notebook-native-preview-sidebar.cy.spec.ts b/e2e/test/scenarios/question/notebook-native-preview-sidebar.cy.spec.ts
index d8582691940..6c6a84b53ed 100644
--- a/e2e/test/scenarios/question/notebook-native-preview-sidebar.cy.spec.ts
+++ b/e2e/test/scenarios/question/notebook-native-preview-sidebar.cy.spec.ts
@@ -241,82 +241,6 @@ describe("converting question to SQL (metabase#12651, metabase#21615, metabase#3
});
});
-describe(
- "converting question to a native query (metabase#15946, metabase#32121, metabase#38181, metabase#40557)",
- { tags: "@mongo" },
- () => {
- const MONGO_DB_NAME = "QA Mongo";
-
- beforeEach(() => {
- H.restore("mongo-5");
- cy.signInAsAdmin();
- });
-
- it("should work for both simple and nested questions based on previously converted GUI query", () => {
- H.startNewQuestion();
- H.miniPicker().within(() => {
- cy.findByText(MONGO_DB_NAME).click();
- cy.findByText("Products").click();
- });
-
- cy.log("Simple question");
- openSidebar("native");
- cy.intercept("POST", "/api/dataset").as("dataset");
- cy.findByTestId("native-query-preview-sidebar").within(() => {
- cy.findByText("Native query for this question").should("exist");
- H.NativeEditor.get()
- .should("be.visible")
- .and("contain", "$project")
- .and("not.contain", "$limit");
-
- cy.button("Convert this question to a native query").click();
- });
- cy.wait("@dataset");
-
- cy.log("Database and table should be pre-selected (metabase#15946)");
- cy.findByTestId("selected-database").should("have.text", MONGO_DB_NAME);
- cy.findByTestId("selected-table").should("have.text", "Products");
- cy.get("[data-testid=cell-data]").should("contain", "Small Marble Shoes");
-
- cy.log("Nested question");
- cy.log(
- "should be possible to save a question and `Explore results` (metabase#32121)",
- );
- H.saveQuestion("foo", undefined, {
- path: ["Our analytics"],
- });
- cy.intercept("POST", "/api/dataset").as("exploreDataset");
- cy.findByTestId("qb-header").findByText("Explore results").click();
- cy.wait("@exploreDataset");
- cy.get("[data-testid=cell-data]").should("contain", "Small Marble Shoes");
-
- cy.log("The generated query should be valid (metabase#38181)");
- H.openNotebook();
- openSidebar("native");
- cy.intercept("POST", "/api/dataset").as("dataset2");
- cy.findByTestId("native-query-preview-sidebar").within(() => {
- cy.findByText("Native query for this question").should("exist");
- H.NativeEditor.get()
- .should("be.visible")
- .and("contain", "$project")
- .and("not.contain", "$limit")
- .and("not.contain", "BsonString")
- .and("not.contain", "BsonInt32");
-
- cy.button("Convert this question to a native query").click();
- });
- cy.wait("@dataset2");
-
- cy.log(
- "Database and table should be pre-selected (metabase#15946 and/or metabase#40557)",
- );
- cy.findByTestId("selected-database").should("have.text", MONGO_DB_NAME);
- cy.findByTestId("selected-table").should("have.text", "Products");
- cy.get("[data-testid=cell-data]").should("contain", "Small Marble Shoes");
- });
- },
-);
-
describe("scenarios > notebook > native query preview sidebar tracking events", () => {
beforeEach(() => {
H.resetSnowplow();
@@ -399,12 +323,10 @@ function resizeSidebar(amountX: number, cb: ResizeSidebarCallback) {
});
}
-function openSidebar(variant: "sql" | "native" = "sql") {
- const label = variant === "sql" ? "View SQL" : "View native query";
- cy.findByLabelText(label).should("be.visible").click();
+function openSidebar() {
+ cy.findByLabelText("View SQL").should("be.visible").click();
}
-function closeSidebar(variant: "sql" | "native" = "sql") {
- const label = variant === "sql" ? "Hide SQL" : "Hide native query";
- cy.findByLabelText(label).should("be.visible").click();
+function closeSidebar() {
+ cy.findByLabelText("Hide SQL").should("be.visible").click();
}
diff --git a/frontend/src/metabase/query_builder/components/view/ViewHeader/components/ToggleNativeQueryPreview/ToggleNativeQueryPreview.unit.spec.tsx b/frontend/src/metabase/query_builder/components/view/ViewHeader/components/ToggleNativeQueryPreview/ToggleNativeQueryPreview.unit.spec.tsx
new file mode 100644
index 00000000000..60bf3b07de7
--- /dev/null
+++ b/frontend/src/metabase/query_builder/components/view/ViewHeader/components/ToggleNativeQueryPreview/ToggleNativeQueryPreview.unit.spec.tsx
@@ -0,0 +1,38 @@
+import userEvent from "@testing-library/user-event";
+
+import { createMockMetadata } from "__support__/metadata";
+import { renderWithProviders, screen } from "__support__/ui";
+import Question from "metabase-lib/v1/Question";
+import { createMockDatabase } from "metabase-types/api/mocks";
+
+import { ToggleNativeQueryPreview } from "./ToggleNativeQueryPreview";
+
+const DB_ID = 2;
+
+function setup({ engine }: { engine: string }) {
+ const metadata = createMockMetadata({
+ databases: [createMockDatabase({ id: DB_ID, engine })],
+ });
+ const question = Question.create({
+ DEPRECATED_RAW_MBQL_databaseId: DB_ID,
+ metadata,
+ });
+
+ renderWithProviders(<ToggleNativeQueryPreview question={question} />);
+}
+
+describe("ToggleNativeQueryPreview", () => {
+ it.each([
+ { engine: "h2", show: "View SQL", hide: "Hide SQL" },
+ { engine: "mongo", show: "View native query", hide: "Hide native query" },
+ ])(
+ "should label the toggle for the $engine engine",
+ async ({ engine, show, hide }) => {
+ setup({ engine });
+
+ await userEvent.click(screen.getByRole("switch", { name: show }));
+
+ expect(screen.getByRole("switch", { name: hide })).toBeInTheDocument();
+ },
+ );
+});
diff --git a/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/NotebookNativePreview.unit.spec.tsx b/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/NotebookNativePreview.unit.spec.tsx
new file mode 100644
index 00000000000..ebfc503520d
--- /dev/null
+++ b/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/NotebookNativePreview.unit.spec.tsx
@@ -0,0 +1,109 @@
+import userEvent from "@testing-library/user-event";
+import fetchMock from "fetch-mock";
+
+import { createMockMetadata } from "__support__/metadata";
+import { setupNativeQuerySnippetEndpoints } from "__support__/server-mocks";
+import { createMockState } from "__support__/state";
+import { createMockEntitiesState } from "__support__/store";
+import { renderWithProviders, screen, waitFor } from "__support__/ui";
+import { checkNotNull } from "metabase/utils/types";
+import * as Lib from "metabase-lib";
+import type Question from "metabase-lib/v1/Question";
+import type { Database } from "metabase-types/api";
+import {
+ createMockDatabase,
+ createMockField,
+ createMockTable,
+} from "metabase-types/api/mocks";
+
+import { NotebookNativePreview } from "./NotebookNativePreview";
+
+const DB_ID = 2;
+const TABLE_ID = 20;
+
+function createDatabase(engine: string): Database {
+ return createMockDatabase({
+ id: DB_ID,
+ engine,
+ features:
+ engine === "mongo" ? ["native-requires-specified-collection"] : [],
+ tables: [
+ createMockTable({
+ id: TABLE_ID,
+ db_id: DB_ID,
+ name: "products",
+ fields: [createMockField({ id: 200, table_id: TABLE_ID })],
+ }),
+ ],
+ });
+}
+
+function setup({
+ engine,
+ collection,
+}: {
+ engine: string;
+ collection?: string;
+}) {
+ const database = createDatabase(engine);
+ const metadata = createMockMetadata({ databases: [database] });
+ const question = checkNotNull(metadata.table(TABLE_ID)).question();
+ const onConvertClick = jest.fn<void, [Question]>();
+
+ setupNativeQuerySnippetEndpoints();
+ fetchMock.post("path:/api/dataset/native", {
+ query: "native query",
+ collection,
+ params: null,
+ });
+
+ renderWithProviders(
+ <NotebookNativePreview
+ question={question}
+ onConvertClick={onConvertClick}
+ />,
+ {
+ storeInitialState: createMockState({
+ entities: createMockEntitiesState({ databases: [database] }),
+ }),
+ },
+ );
+
+ return { onConvertClick };
+}
+
+async function findConvertButton(name: string) {
+ const button = await screen.findByRole("button", { name });
+ await waitFor(() => expect(button).toBeEnabled());
+ return button;
+}
+
+describe("NotebookNativePreview", () => {
+ it("should use SQL labels for a SQL database", async () => {
+ setup({ engine: "h2" });
+
+ expect(screen.getByText("SQL for this question")).toBeInTheDocument();
+ expect(
+ await findConvertButton("Convert this question to SQL"),
+ ).toBeInTheDocument();
+ });
+
+ it("should use native query labels and keep the collection for a MongoDB database", async () => {
+ const { onConvertClick } = setup({
+ engine: "mongo",
+ collection: "products",
+ });
+
+ expect(
+ screen.getByText("Native query for this question"),
+ ).toBeInTheDocument();
+ await userEvent.click(
+ await findConvertButton("Convert this question to a native query"),
+ );
+
+ expect(onConvertClick).toHaveBeenCalledTimes(1);
+ const query = onConvertClick.mock.calls[0][0].query();
+ expect(Lib.databaseID(query)).toBe(DB_ID);
+ expect(Lib.nativeExtras(query)).toEqual({ collection: "products" });
+ });
+});
diff --git a/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/utils.unit.spec.ts b/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/utils.unit.spec.ts
new file mode 100644
index 00000000000..f0814214712
--- /dev/null
+++ b/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/utils.unit.spec.ts
@@ -0,0 +1,53 @@
+import { createMockMetadata } from "__support__/metadata";
+import { checkNotNull } from "metabase/utils/types";
+import * as Lib from "metabase-lib";
+import Question from "metabase-lib/v1/Question";
+import { createMockDatabase } from "metabase-types/api/mocks";
+
+import { createNativeQuestion } from "./utils";
+
+const MONGO_DB_ID = 2;
+const NATIVE_QUERY = '[{"$project":{"_id":"$_id","title":"$title"}}]';
+
+const METADATA = createMockMetadata({
+ databases: [
+ createMockDatabase({
+ id: MONGO_DB_ID,
+ engine: "mongo",
+ features: ["native-requires-specified-collection"],
+ }),
+ ],
+});
+
+function setup({ collection }: { collection?: string } = {}) {
+ const question = Question.create({
+ DEPRECATED_RAW_MBQL_databaseId: MONGO_DB_ID,
+ metadata: METADATA,
+ });
+ const metadataProvider = Lib.metadataProvider(MONGO_DB_ID, METADATA);
+ const nativeQuestion = checkNotNull(
+ createNativeQuestion(
+ question,
+ { query: NATIVE_QUERY, collection, params: null },
+ metadataProvider,
+ ),
+ );
+ return nativeQuestion.query();
+}
+
+describe("createNativeQuestion", () => {
+ it("should set the source collection returned by the backend", () => {
+ const query = setup({ collection: "products" });
+
+ expect(Lib.databaseID(query)).toBe(MONGO_DB_ID);
+ expect(Lib.rawNativeQuery(query)).toBe(NATIVE_QUERY);
+ expect(Lib.nativeExtras(query)).toEqual({ collection: "products" });
+ });
+
+ it("should not set a collection when the backend returns none", () => {
+ const query = setup();
+
+ expect(Lib.rawNativeQuery(query)).toBe(NATIVE_QUERY);
+ expect(Lib.nativeExtras(query)?.collection).toBeUndefined();
+ });
+});
diff --git a/modules/drivers/mongo/test/metabase/driver/mongo_test.clj b/modules/drivers/mongo/test/metabase/driver/mongo_test.clj
index 58dabd903b3..b868abdd57b 100644
--- a/modules/drivers/mongo/test/metabase/driver/mongo_test.clj
+++ b/modules/drivers/mongo/test/metabase/driver/mongo_test.clj
@@ -224,6 +224,40 @@
:collection "venues"}
(mt/user-http-request :crowberto :post 200 "dataset/native" query)))))))
+(deftest convert-to-native-omits-default-limit-test
+ (mt/test-driver :mongo
+ (testing "POST /api/dataset/native compiles without the default `$limit` stage"
+ (let [mp (mt/metadata-provider)
+ query (lib/query mp (lib.metadata/table mp (mt/id :venues)))]
+ (is (not (str/includes? (:query (mt/user-http-request :crowberto :post 200 "dataset/native" query))
+ "$limit")))
+ (testing "an explicit limit is kept"
+ (is (str/includes? (:query (mt/user-http-request :crowberto :post 200 "dataset/native" (lib/limit query 3)))
+ "$limit")))))))
+
+(deftest convert-to-native-nested-on-converted-card-test
+ (mt/test-driver :mongo
+ (testing "A question based on a converted-to-native Mongo card can be run and converted to native again"
+ (let [mp (mt/metadata-provider)
+ native (mt/user-http-request :crowberto :post 200 "dataset/native"
+ (lib/query mp (lib.metadata/table mp (mt/id :venues))))]
+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query (-> (lib/native-query mp (:query native))
+ (lib/with-native-extras {:collection (:collection native)}))}]
+ (let [mp (mt/metadata-provider)
+ query (lib/query mp (lib.metadata/card mp card-id))]
+ (is (=? {:status "completed"
+ :row_count 100}
+ (mt/user-http-request :crowberto :post 202 "dataset" query)))
+ (let [nested (mt/user-http-request :crowberto :post 200 "dataset/native" query)]
+ (is (=? {:collection "venues"} nested))
+ (is (not (str/includes? (:query nested) "$limit")))
+ (is (not (str/includes? (:query nested) "Bson")))
+ (is (=? {:status "completed"
+ :row_count 100}
+ (mt/user-http-request :crowberto :post 202 "dataset"
+ (-> (lib/native-query mp (:query nested))
+ (lib/with-native-extras {:collection (:collection nested)}))))))))))))
+
;; ## Tests for individual syncing functions
(deftest ^:parallel describe-database-test