fix(grid): reuse prepared row copy statements
This commit is contained in:
3 files changed
+294
-62
No files matched your search
@@ -0,0 +1,169 @@
|
||||
import { computed, ref } from "vue";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { useDataGridExport, type UseDataGridExportOptions } from "@/composables/useDataGridExport";
|
||||
import { buildDataGridCopyInsertStatement, buildDataGridCopyUpdateStatements } from "@/lib/dataGrid/dataGridSql";
|
||||
import { copyToClipboard } from "@/lib/common/clipboard";
|
||||
import type { DataGridTableMeta } from "@/lib/dataGrid/dataGridSql";
|
||||
|
||||
const toast = vi.fn();
|
||||
|
||||
vi.mock("vue-i18n", () => ({
|
||||
useI18n: () => ({ t: (key: string, params?: { message?: string }) => (params?.message ? `${key}: ${params.message}` : key) }),
|
||||
}));
|
||||
|
||||
vi.mock("@/composables/useToast", () => ({
|
||||
useToast: () => ({ toast }),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/common/clipboard", () => ({
|
||||
copyToClipboard: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/dataGrid/dataGridSql", async (importOriginal) => {
|
||||
const original = await importOriginal<typeof import("@/lib/dataGrid/dataGridSql")>();
|
||||
return {
|
||||
...original,
|
||||
buildDataGridCopyInsertStatement: vi.fn(),
|
||||
buildDataGridCopyUpdateStatements: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
interface Deferred<T> {
|
||||
promise: Promise<T>;
|
||||
resolve: (value: T) => void;
|
||||
reject: (error: unknown) => void;
|
||||
}
|
||||
|
||||
function deferred<T>(): Deferred<T> {
|
||||
let resolve!: (value: T) => void;
|
||||
let reject!: (error: unknown) => void;
|
||||
const promise = new Promise<T>((resolvePromise, rejectPromise) => {
|
||||
resolve = resolvePromise;
|
||||
reject = rejectPromise;
|
||||
});
|
||||
return { promise, resolve, reject };
|
||||
}
|
||||
|
||||
function row(data: unknown[]) {
|
||||
return {
|
||||
id: 1,
|
||||
data,
|
||||
isNew: false,
|
||||
isDeleted: false,
|
||||
isDirtyCol: data.map(() => false),
|
||||
status: "",
|
||||
};
|
||||
}
|
||||
|
||||
function createExportState(tableMeta: DataGridTableMeta, columns = tableMeta.columns?.map((column) => column.name) ?? ["id", "name"]) {
|
||||
const item = row(columns.map((column, index) => (column === "id" ? 1 : `value-${index}`)));
|
||||
const options: UseDataGridExportOptions = {
|
||||
columns: computed(() => columns),
|
||||
displayItems: computed(() => [item]),
|
||||
sql: computed(() => undefined),
|
||||
tableMeta: computed(() => tableMeta),
|
||||
databaseType: computed(() => "mysql"),
|
||||
connectionId: computed(() => "connection-1"),
|
||||
database: computed(() => "dbx"),
|
||||
context: computed(() => "table-data"),
|
||||
sourceColumns: computed(() => columns),
|
||||
columnTypes: computed(() => columns.map(() => "varchar")),
|
||||
whereInput: computed(() => undefined),
|
||||
orderBy: computed(() => undefined),
|
||||
exportBatchSize: computed(() => 1000),
|
||||
hasCellSelection: computed(() => false),
|
||||
selectedCells: computed(() => ({ columns: [], rows: [] })),
|
||||
selectedRange: computed(() => null),
|
||||
contextCell: ref({ rowId: item.id, rowIndex: 0, col: -1 }),
|
||||
getRowItem: (rowId) => (rowId === item.id ? item : undefined),
|
||||
selectedRowIds: ref(new Set<number>()),
|
||||
hasRowSelection: computed(() => false),
|
||||
};
|
||||
return useDataGridExport(options);
|
||||
}
|
||||
|
||||
const editableTable: DataGridTableMeta = {
|
||||
tableName: "users",
|
||||
primaryKeys: ["id"],
|
||||
columns: [
|
||||
{ name: "id", data_type: "int", is_nullable: false, is_primary_key: true },
|
||||
{ name: "name", data_type: "varchar", is_nullable: false },
|
||||
],
|
||||
};
|
||||
|
||||
describe("useDataGridExport prepared row statements", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("reuses an in-flight INSERT prefetch when the copy action runs", async () => {
|
||||
const pending = deferred<string | undefined>();
|
||||
vi.mocked(buildDataGridCopyInsertStatement).mockReturnValueOnce(pending.promise);
|
||||
const state = createExportState(editableTable);
|
||||
|
||||
const prefetch = state.prefetchRowAsInsertStatement(false);
|
||||
const copy = state.copyRowAsInsert();
|
||||
await vi.waitFor(() => expect(buildDataGridCopyInsertStatement).toHaveBeenCalledTimes(1));
|
||||
pending.resolve("INSERT INTO users VALUES (1, 'Alice');");
|
||||
|
||||
await Promise.all([prefetch, copy]);
|
||||
expect(copyToClipboard).toHaveBeenCalledWith("INSERT INTO users VALUES (1, 'Alice');");
|
||||
});
|
||||
|
||||
it("reuses an in-flight UPDATE prefetch on the first copy action", async () => {
|
||||
const pending = deferred<string[]>();
|
||||
vi.mocked(buildDataGridCopyUpdateStatements).mockReturnValueOnce(pending.promise);
|
||||
const state = createExportState(editableTable);
|
||||
|
||||
const prefetch = state.prefetchRowAsUpdateStatement();
|
||||
const copy = state.copyRowAsUpdate();
|
||||
await vi.waitFor(() => expect(buildDataGridCopyUpdateStatements).toHaveBeenCalledTimes(1));
|
||||
pending.resolve(["UPDATE users SET name = 'Alice' WHERE id = 1;"]);
|
||||
|
||||
await Promise.all([prefetch, copy]);
|
||||
expect(copyToClipboard).toHaveBeenCalledWith("UPDATE users SET name = 'Alice' WHERE id = 1;");
|
||||
});
|
||||
|
||||
it.each(["GENERATED ALWAYS AS (1)", "IDENTITY(1, 1)"])("disables copy-as-insert when every result column is non-insertable (%s)", (extra) => {
|
||||
const state = createExportState(
|
||||
{
|
||||
tableName: "generated_values",
|
||||
primaryKeys: [],
|
||||
columns: [{ name: "computed_value", data_type: "int", is_nullable: true, extra }],
|
||||
},
|
||||
["computed_value"],
|
||||
);
|
||||
|
||||
expect(state.canCopyRowAsInsert.value).toBe(false);
|
||||
});
|
||||
|
||||
it("reports a shared builder failure when the user invokes copy", async () => {
|
||||
const pending = deferred<string | undefined>();
|
||||
vi.mocked(buildDataGridCopyInsertStatement).mockReturnValueOnce(pending.promise);
|
||||
const state = createExportState(editableTable);
|
||||
|
||||
const prefetch = state.prefetchRowAsInsertStatement(false);
|
||||
const copy = state.copyRowAsInsert();
|
||||
await vi.waitFor(() => expect(buildDataGridCopyInsertStatement).toHaveBeenCalledTimes(1));
|
||||
pending.reject(new Error("builder unavailable"));
|
||||
|
||||
await Promise.all([prefetch, copy]);
|
||||
expect(toast).toHaveBeenCalledWith("grid.copyFailed: builder unavailable", 5000);
|
||||
expect(copyToClipboard).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reports an UPDATE builder failure from the first copy action", async () => {
|
||||
const pending = deferred<string[]>();
|
||||
vi.mocked(buildDataGridCopyUpdateStatements).mockReturnValueOnce(pending.promise);
|
||||
const state = createExportState(editableTable);
|
||||
|
||||
const prefetch = state.prefetchRowAsUpdateStatement();
|
||||
const copy = state.copyRowAsUpdate();
|
||||
await vi.waitFor(() => expect(buildDataGridCopyUpdateStatements).toHaveBeenCalledTimes(1));
|
||||
pending.reject(new Error("update builder unavailable"));
|
||||
|
||||
await Promise.all([prefetch, copy]);
|
||||
expect(toast).toHaveBeenCalledWith("grid.copyFailed: update builder unavailable", 5000);
|
||||
expect(copyToClipboard).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user