Skip to content

Commit

Permalink
web: More sql parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tbantle22 committed Dec 15, 2023
1 parent 2641663 commit d892169
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 5 deletions.
4 changes: 2 additions & 2 deletions web/components/HistoryTable/queryHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ function removeExtraWhereClause(q: string): string {
function useGetCols(q: string): string[] {
const { getColumns } = useSqlParser();
const columns = getColumns(q);
if (!columns) return [];
if (columns === "*") return ["*"];
if (!columns?.length) return [];
if (columns[0].expr.column === "*") return ["*"];
// Remove dolt_commit_diff_[table] specific columns and column to_ and from_ prefixes
const mappedCols = columns
.slice(1, columns.length - 4)
Expand Down
12 changes: 12 additions & 0 deletions web/hooks/useSqlBuilder/tests/mysql.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,15 @@ describe("test updateTableQuery", () => {
});
});
});

describe("test updateTableMakeNullQuery", () => {});

describe("test convertToSqlWithNewCols", () => {});

describe("test convertToSqlWithNewColNames", () => {});

describe("test alterTableDropColQuery", () => {});

describe("test hideRowQuery", () => {});

describe("test selectFromTable", () => {});
2 changes: 1 addition & 1 deletion web/hooks/useSqlParser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default function useSqlParser() {
}

// Extracts columns from query string
function getColumns(q: string): any[] | Column[] | "*" | undefined {
function getColumns(q: string): any[] | Column[] | undefined {
const ast = parseSelectQuery(q);
return ast?.columns;
}
Expand Down
101 changes: 100 additions & 1 deletion web/hooks/useSqlParser/tests/mysql.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import compareArray from "@lib/compareArray";
import { fallbackGetTableNamesForSelect } from "../util";
import { renderUseSqlParser } from "./render.test";
import {
getParserCol,
invalidQuery,
mutationExamples,
notMutationExamples,
Expand Down Expand Up @@ -126,7 +127,7 @@ describe("test isMutation", () => {
});
});

describe("test use regex to get table names from query", () => {
describe("test fallbackGetTableNamesForSelect", () => {
const tests = [
{
desc: "single table",
Expand Down Expand Up @@ -178,3 +179,101 @@ describe("test use regex to get table names from query", () => {
});
});
});

describe("test queryHasOrderBy", () => {
const tests = [
{
desc: "no order by",
query: "select * from `test`",
column: "test",
expectedDef: true,
expectedAsc: false,
expectedDesc: false,
},
{
desc: "order by, column doesn't match",
query: "select * from `test` ORDER BY `other-col` ASC",
column: "not-col",
expectedDef: true,
expectedAsc: false,
expectedDesc: false,
},
{
desc: "order by, column matches, desc",
query: "select * from `test` ORDER BY `my-col` DESC",
column: "my-col",
expectedDef: false,
expectedAsc: false,
expectedDesc: true,
},
{
desc: "order by, column matches, asc",
query: "select * from `test` ORDER BY `my-col` ASC",
column: "my-col",
expectedDef: false,
expectedAsc: true,
expectedDesc: false,
},
{
desc: "invalid query",
query: invalidQuery,
column: "my-col",
expectedDef: false,
expectedAsc: false,
expectedDesc: false,
},
];

tests.forEach(test => {
it(test.desc, async () => {
const { queryHasOrderBy } = await renderUseSqlParser();
// Default
expect(queryHasOrderBy(test.query, test.column)).toBe(test.expectedDef);
// ASC
expect(queryHasOrderBy(test.query, test.column, "ASC")).toBe(
test.expectedAsc,
);
// DESC
expect(queryHasOrderBy(test.query, test.column, "DESC")).toBe(
test.expectedDesc,
);
});
});
});

describe("test getColumns", () => {
const tests = [
{
desc: "select *",
query: "select * from `test`",
expected: [getParserCol("*")],
},
{
desc: "select one column",
query: "select `col1` from `test`",
expected: [getParserCol("col1")],
},
{
desc: "select two columns",
query: "select `col1`, `col2` from `test`",
expected: [getParserCol("col1"), getParserCol("col2")],
},
{
desc: "update query",
query: "update `test` set `col1` = 'val1', `col2` = 'val2'",
expected: undefined,
},
{
desc: "invalid query",
query: invalidQuery,
expected: undefined,
},
];

tests.forEach(test => {
it(test.desc, async () => {
const { getColumns } = await renderUseSqlParser();
expect(getColumns(test.query)).toEqual(test.expected);
});
});
});
106 changes: 105 additions & 1 deletion web/hooks/useSqlParser/tests/postgres.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import compareArray from "@lib/compareArray";
import { fallbackGetTableNamesForSelect } from "../util";
import { renderUseSqlParser } from "./render.test";
import {
getParserCol,
invalidQuery,
mutationExamples,
notMutationExamples,
Expand Down Expand Up @@ -128,7 +129,7 @@ describe("test isMutation", () => {
});
});

describe("test use regex to get table names from query", () => {
describe("test fallbackGetTableNamesForSelect", () => {
const tests = [
{
desc: "single table",
Expand Down Expand Up @@ -180,3 +181,106 @@ describe("test use regex to get table names from query", () => {
});
});
});

describe("test queryHasOrderBy", () => {
const tests = [
{
desc: "no order by",
query: "select * from `test`",
column: "test",
expectedDef: true,
expectedAsc: false,
expectedDesc: false,
},
{
desc: "order by, column doesn't match",
query: 'select * from "test" ORDER BY "other-col" ASC',
column: "not-col",
expectedDef: true,
expectedAsc: false,
expectedDesc: false,
},
{
desc: "order by, column matches, desc",
query: 'select * from "test" ORDER BY "my-col" DESC',
column: "my-col",
expectedDef: false,
expectedAsc: false,
expectedDesc: true,
},
{
desc: "order by, column matches, asc",
query: 'select * from "test" ORDER BY "my-col" ASC',
column: "my-col",
expectedDef: false,
expectedAsc: true,
expectedDesc: false,
},
{
desc: "invalid query",
query: invalidQuery,
column: "my-col",
expectedDef: false,
expectedAsc: false,
expectedDesc: false,
},
];

tests.forEach(test => {
it(test.desc, async () => {
const { queryHasOrderBy } = await renderUseSqlParserForPG();
// Default
expect(queryHasOrderBy(test.query, test.column)).toBe(test.expectedDef);
// ASC
expect(queryHasOrderBy(test.query, test.column, "ASC")).toBe(
test.expectedAsc,
);
// DESC
expect(queryHasOrderBy(test.query, test.column, "DESC")).toBe(
test.expectedDesc,
);
});
});
});

describe("test getColumns", () => {
const tests = [
{
desc: "select *",
query: 'select * from "test"',
expected: [getParserCol("*")],
},
{
desc: "select one column",
query: 'select "col1" from "test"',
expected: [getParserCol("col1", true)],
},
{
desc: "select one column, with schema name",
query: 'select "test"."col1" from "test"',
expected: [getParserCol("col1", true, "test")],
},
{
desc: "select two columns",
query: 'select "col1", "col2" from "test"',
expected: [getParserCol("col1", true), getParserCol("col2", true)],
},
{
desc: "update query",
query: 'update "test" set "col1" = \'val1\', "col2" = \'val2\'',
expected: undefined,
},
{
desc: "invalid query",
query: invalidQuery,
expected: undefined,
},
];

tests.forEach(test => {
it(test.desc, async () => {
const { getColumns } = await renderUseSqlParserForPG();
expect(getColumns(test.query)).toEqual(test.expected);
});
});
});
25 changes: 25 additions & 0 deletions web/hooks/useSqlParser/tests/testData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AggrFunc, ColumnRef } from "node-sql-parser";

export const invalidQuery = `this is not a valid query`;

// TODO: Translate to postgres
Expand Down Expand Up @@ -114,3 +116,26 @@ export const mutationExamples = [
"SET SESSION slow_query_log = 1",
"FLUSH PRIVILEGES",
];

// TODO: Replace with node-sql-parser column when fixed
type Column = {
expr: ColumnRef | AggrFunc;
as: string | null;
type?: string;
};

export function getParserCol(
name: string,
includeType?: boolean,
table?: string,
): Column {
return {
expr: {
column: name,
table: table ?? null,
type: "column_ref",
},
as: null,
type: includeType ? "expr" : undefined,
};
}

0 comments on commit d892169

Please sign in to comment.