Skip to content
Snippets Groups Projects
Unverified Commit a0025d0d authored by Alexander Polyankin's avatar Alexander Polyankin Committed by GitHub
Browse files

Fix native query preview for mongo (#48960)

* Fix native query preview for mongo

* Add tests

* Add tests

* Do not rely on join identity
parent 5df7d88d
No related branches found
No related tags found
No related merge requests found
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import {
type NativeQuestionDetails,
createNativeQuestion,
createQuestion,
getNotebookStep,
modal,
......@@ -229,3 +231,65 @@ describe("issue 39487", () => {
return popover().get("button[data-previous]");
}
});
const MONGO_DB_ID = 2;
describe("issue 47793", () => {
const questionDetails: NativeQuestionDetails = {
database: MONGO_DB_ID,
native: {
query: `[
{ $match: { quantity: {{quantity}} }},
{
"$project": {
"_id": "$_id",
"id": "$id",
"user_id": "$user_id",
"product_id": "$product_id",
"subtotal": "$subtotal",
"tax": "$tax",
"total": "$total",
"created_at": "$created_at",
"quantity": "$quantity",
"discount": "$discount"
}
},
{
"$limit": 1048575
}
]`,
"template-tags": {
quantity: {
type: "number",
name: "quantity",
id: "754ae827-661c-4fc9-b511-c0fb7b6bae2b",
"display-name": "Quantity",
default: "10",
},
},
collection: "orders",
},
};
beforeEach(() => {
restore("mongo-5");
cy.signInAsAdmin();
});
it(
"should be able to preview queries for mongodb (metabase#47793)",
{ tags: ["@external", "@mongo"] },
() => {
createNativeQuestion(questionDetails, { visitQuestion: true });
cy.findByTestId("visibility-toggler")
.findByText(/open editor/i)
.click();
cy.findByTestId("native-query-editor-container")
.findByLabelText("Preview the query")
.click();
modal()
.should("contain.text", "$project")
.and("contain.text", "quantity: 10");
},
);
});
......@@ -41,8 +41,8 @@ export function formatNativeQuery(query?: string | JSONQuery, engine?: string) {
return formatSQL(query);
}
if (typeof query === "object" && engineType === "json") {
return formatJsonQuery(query);
if (engineType === "json") {
return typeof query === "object" ? formatJsonQuery(query) : query;
}
return undefined;
......
......@@ -85,10 +85,6 @@ describe("formatNativeQuery", () => {
});
it("should return `undefined` when the query and the engine don't match", () => {
expect(formatNativeQuery("select 1", "mongo")).toBeUndefined();
expect(formatNativeQuery("foo bar baz", "mongo")).toBeUndefined();
expect(formatNativeQuery("", "mongo")).toBeUndefined();
expect(formatNativeQuery({}, "postgres")).toBeUndefined();
expect(formatNativeQuery([], "postgres")).toBeUndefined();
expect(formatNativeQuery([{}], "postgres")).toBeUndefined();
......@@ -117,6 +113,7 @@ describe("formatNativeQuery", () => {
expect(formatNativeQuery([], "mongo")).toEqual("[]");
expect(formatNativeQuery(["foo"], "mongo")).toEqual('[\n "foo"\n]');
expect(formatNativeQuery({ a: 1 }, "mongo")).toEqual('{\n "a": 1\n}');
expect(formatNativeQuery('["foo"]', "mongo")).toEqual('["foo"]');
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment