191 lines
6.0 KiB
TypeScript
191 lines
6.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import {
|
|
buildStructureTargetLabel,
|
|
combineDataTypeForDatabase,
|
|
createColumnDrafts,
|
|
createIndexDrafts,
|
|
getDataTypeOptions,
|
|
normalizeDataTypeParams,
|
|
parseExtraToColumnExtra,
|
|
toColumnNames,
|
|
} from "../../apps/desktop/src/lib/tableStructureEditorState.ts";
|
|
import type { ColumnInfo, IndexInfo } from "../../apps/desktop/src/types/database.ts";
|
|
|
|
const columns: ColumnInfo[] = [
|
|
{
|
|
name: "id",
|
|
data_type: "bigint",
|
|
is_nullable: false,
|
|
column_default: null,
|
|
is_primary_key: true,
|
|
extra: "auto_increment",
|
|
comment: "identifier",
|
|
},
|
|
{
|
|
name: "name",
|
|
data_type: "varchar(120)",
|
|
is_nullable: true,
|
|
column_default: "'guest'",
|
|
is_primary_key: false,
|
|
extra: null,
|
|
comment: null,
|
|
},
|
|
];
|
|
|
|
const indexes: IndexInfo[] = [
|
|
{ name: "PRIMARY", columns: ["id"], is_unique: true, is_primary: true },
|
|
{ name: "idx_name", columns: ["name"], is_unique: false, is_primary: false },
|
|
];
|
|
|
|
test("creates editable column drafts from column metadata", () => {
|
|
const drafts = createColumnDrafts(columns, "mysql");
|
|
|
|
assert.deepEqual(
|
|
drafts.map((draft) => ({
|
|
id: draft.id,
|
|
name: draft.name,
|
|
dataType: draft.dataType,
|
|
isNullable: draft.isNullable,
|
|
defaultValue: draft.defaultValue,
|
|
comment: draft.comment,
|
|
isPrimaryKey: draft.isPrimaryKey,
|
|
extra: draft.extra,
|
|
originalPosition: draft.originalPosition,
|
|
markedForDrop: draft.markedForDrop,
|
|
originalName: draft.original?.name,
|
|
})),
|
|
[
|
|
{
|
|
id: "existing:id",
|
|
name: "id",
|
|
dataType: "bigint",
|
|
isNullable: false,
|
|
defaultValue: "",
|
|
comment: "identifier",
|
|
isPrimaryKey: true,
|
|
extra: { autoIncrement: true },
|
|
originalPosition: 0,
|
|
markedForDrop: false,
|
|
originalName: "id",
|
|
},
|
|
{
|
|
id: "existing:name",
|
|
name: "name",
|
|
dataType: "varchar(120)",
|
|
isNullable: true,
|
|
defaultValue: "'guest'",
|
|
comment: "",
|
|
isPrimaryKey: false,
|
|
extra: {},
|
|
originalPosition: 1,
|
|
markedForDrop: false,
|
|
originalName: "name",
|
|
},
|
|
],
|
|
);
|
|
});
|
|
|
|
test("parses MySQL extra string to ColumnExtra", () => {
|
|
assert.deepEqual(parseExtraToColumnExtra("auto_increment", "mysql"), { autoIncrement: true });
|
|
assert.deepEqual(parseExtraToColumnExtra("on update CURRENT_TIMESTAMP", "mysql"), {
|
|
onUpdateCurrentTimestamp: true,
|
|
});
|
|
assert.deepEqual(parseExtraToColumnExtra("auto_increment on update current_timestamp", "mysql"), {
|
|
autoIncrement: true,
|
|
onUpdateCurrentTimestamp: true,
|
|
});
|
|
assert.deepEqual(parseExtraToColumnExtra(null, "mysql"), {});
|
|
assert.deepEqual(parseExtraToColumnExtra("", "mysql"), {});
|
|
});
|
|
|
|
test("parses PostgreSQL extra string to ColumnExtra", () => {
|
|
assert.deepEqual(parseExtraToColumnExtra("generated by default as identity", "postgres"), {
|
|
identity: { generation: "BY DEFAULT" },
|
|
});
|
|
assert.deepEqual(parseExtraToColumnExtra("generated by default as identity", "opengauss"), {
|
|
identity: { generation: "BY DEFAULT" },
|
|
});
|
|
assert.deepEqual(parseExtraToColumnExtra("generated by default as identity", "gaussdb"), {
|
|
identity: { generation: "BY DEFAULT" },
|
|
});
|
|
assert.deepEqual(parseExtraToColumnExtra("GENERATED ALWAYS AS IDENTITY", "postgres"), {
|
|
identity: { generation: "ALWAYS" },
|
|
});
|
|
assert.deepEqual(parseExtraToColumnExtra("generated always as identity (start with 10 increment by 2)", "postgres"), {
|
|
identity: { generation: "ALWAYS", seed: 10, increment: 2 },
|
|
});
|
|
});
|
|
|
|
test("parses SQL Server identity extra string to ColumnExtra", () => {
|
|
assert.deepEqual(parseExtraToColumnExtra("identity(1,1)", "sqlserver"), {
|
|
autoIncrement: true,
|
|
identity: { seed: 1, increment: 1 },
|
|
});
|
|
assert.deepEqual(parseExtraToColumnExtra("IDENTITY(100, 5)", "sqlserver"), {
|
|
autoIncrement: true,
|
|
identity: { seed: 100, increment: 5 },
|
|
});
|
|
});
|
|
|
|
test("creates editable index drafts and splits pasted column lists", () => {
|
|
const drafts = createIndexDrafts(indexes);
|
|
|
|
assert.deepEqual(
|
|
drafts.map((draft) => ({
|
|
id: draft.id,
|
|
name: draft.name,
|
|
columns: draft.columns,
|
|
isUnique: draft.isUnique,
|
|
isPrimary: draft.isPrimary,
|
|
originalName: draft.original?.name,
|
|
})),
|
|
[
|
|
{
|
|
id: "existing:PRIMARY",
|
|
name: "PRIMARY",
|
|
columns: ["id"],
|
|
isUnique: true,
|
|
isPrimary: true,
|
|
originalName: "PRIMARY",
|
|
},
|
|
{
|
|
id: "existing:idx_name",
|
|
name: "idx_name",
|
|
columns: ["name"],
|
|
isUnique: false,
|
|
isPrimary: false,
|
|
originalName: "idx_name",
|
|
},
|
|
],
|
|
);
|
|
assert.equal(toColumnNames(["id", "name"]), "id, name");
|
|
});
|
|
|
|
test("structure editor target label omits duplicate database and schema", () => {
|
|
assert.equal(
|
|
buildStructureTargetLabel("online-clickhouse", "testdb", "testdb", "users"),
|
|
"online-clickhouse / testdb / users",
|
|
);
|
|
assert.equal(
|
|
buildStructureTargetLabel("online-postgres", "app", "public", "users"),
|
|
"online-postgres / app / public / users",
|
|
);
|
|
});
|
|
|
|
test("normalizes temporal precision when combining data types", () => {
|
|
assert.equal(combineDataTypeForDatabase("mysql", "timestamp", "255"), "timestamp");
|
|
assert.equal(combineDataTypeForDatabase("mysql", "timestamp", "3"), "timestamp(3)");
|
|
assert.equal(combineDataTypeForDatabase("mysql", "varchar", "255"), "varchar(255)");
|
|
assert.equal(normalizeDataTypeParams("oracle", "timestamp", "9"), "9");
|
|
assert.equal(normalizeDataTypeParams("oracle", "timestamp", "10"), "");
|
|
});
|
|
|
|
test("returns data type options for compatible table structure editors", () => {
|
|
assert.deepEqual(getDataTypeOptions("dameng"), getDataTypeOptions("oracle"));
|
|
assert.deepEqual(getDataTypeOptions("gaussdb"), getDataTypeOptions("postgres"));
|
|
assert.deepEqual(getDataTypeOptions("doris"), getDataTypeOptions("mysql"));
|
|
assert.equal(getDataTypeOptions("dameng").includes("varchar2"), true);
|
|
assert.equal(getDataTypeOptions("sqlserver").includes("nvarchar"), true);
|
|
});
|