Файли
openidea/src/features/database/sql/snowflakeCommands.test.ts
T
pHтаvvnadolskyy 59eb6446d4 feat(database): add Snowflake data sources (#2972)
Snowflake is a built-in data source kind: connection dialog, key-pair, browser single sign-on, password or token sign-in, the database tree, SQL consoles, and Snowflake-aware SQL assistance and syntax colours. The driver speaks Snowflake's session protocol over the existing reqwest + rustls stack, with no JDBC/ODBC or native library and no new package. Metadata is read with SHOW / DESCRIBE, which need no running warehouse; introspected names are kept on disk for completion.

Co-authored-by: vvnadolskyy <[email protected]>
2026-11-12 23:06:10 +08:00

80 рядки
3.0 KiB
TypeScript

/**
* Every SQL command of Snowflake's reference through the console's SQL assistance. The samples
* are the ones the driver's tests run (`crates/ide-db/src/snowflake/commands.rs`): one statement
* per documented command.
*/
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { describe, expect, it } from "vitest";
import { formatted } from "./format";
import { lex } from "./lexer";
import { findParams } from "./params";
import { isReadOnlyQuery } from "./readOnly";
interface Command {
command: string;
sql: string;
/** Running it again for another page of its result changes nothing. */
rerunnable: boolean;
}
const fixture = resolve(process.cwd(), "crates/ide-db/tests/fixtures/snowflake/commands.json");
const commands = (JSON.parse(readFileSync(fixture, "utf8")) as { commands: Command[] }).commands;
/** What a statement is made of, whatever its layout and letter case. */
const shape = (sql: string) =>
lex(sql, "snowflake")
.tokens.map((t) => (t.kind === "word" ? t.upper : t.text))
.join(" ");
describe("Snowflake commands", () => {
it("has the whole reference", () => {
expect(commands.length).toBeGreaterThanOrEqual(786);
});
it("lexes every command to its end", () => {
for (const c of commands) {
const { tokens, comments } = lex(c.sql, "snowflake");
// The samples' only comments are inside `$$` bodies; `file://` is a path, not a comment.
expect(comments, c.command).toEqual([]);
expect(tokens.length, c.command).toBeGreaterThan(0);
expect(tokens[0]!.kind, c.command).toBe("word");
// No token runs past the statement or overlaps the one before.
let end = 0;
for (const t of tokens) {
expect(t.from, c.command).toBeGreaterThanOrEqual(end);
end = t.to;
}
expect(end, c.command).toBeLessThanOrEqual(c.sql.length);
}
});
it("pages only what is safe to run again", () => {
for (const c of commands) expect(isReadOnlyQuery(c.sql, "snowflake"), `${c.command}: ${c.sql}`).toBe(c.rerunnable);
});
it("asks for no parameter values", () => {
// Stage paths, variant paths, `$1`, `:=`, `file://` and `=>` are not parameters.
for (const c of commands) {
if (c.command === "CALL") continue; // `INTO :result` is a bind variable
expect(
findParams(c.sql, "snowflake").map((p) => p.name),
`${c.command}: ${c.sql}`,
).toEqual([]);
}
});
it("formats every command without changing it, and settles", () => {
for (const c of commands) {
const once = formatted(c.sql, "snowflake");
expect(shape(once), c.command).toBe(shape(c.sql));
expect(formatted(once, "snowflake"), c.command).toBe(once);
// Paths, casts, stages and scoped names stay in one piece: a space inside breaks them.
for (const piece of c.sql.split(/\s+/)) {
if (/[:!@$]/.test(piece) && !/['"(),;]/.test(piece) && !/^(:=|\$\$)$/.test(piece)) {
expect(once, `${c.command}: ${piece}`).toContain(piece);
}
}
}
});
});