mirror of
https://github.com/t8y2/dbx.git
synced 2026-10-21 10:28:44 +00:00
244 lines
6.8 KiB
TypeScript
Vendored
244 lines
6.8 KiB
TypeScript
Vendored
import { strict as assert } from "node:assert";
|
|
import { test } from "vitest";
|
|
import { defaultGeneratorParams, displayGeneratedValue, generateTableData, generateValue, supportsGeneratedMultiRowValues } from "../../apps/desktop/src/lib/dataGrid/dataGenerate.ts";
|
|
|
|
test("enables default values for columns with schema defaults", () => {
|
|
const params = defaultGeneratorParams(
|
|
"status",
|
|
{
|
|
dataType: "varchar(16)",
|
|
columnDefault: "active",
|
|
},
|
|
"text",
|
|
);
|
|
|
|
assert.equal(params.includeDefault, true);
|
|
assert.equal(params.defaultPercent, 100);
|
|
});
|
|
|
|
test("uses string column defaults instead of random generated values", () => {
|
|
const result = generateTableData(
|
|
{
|
|
tableName: "users",
|
|
schema: "public",
|
|
database: "app",
|
|
rowCount: 2,
|
|
columns: [
|
|
{
|
|
columnName: "status",
|
|
dataType: "varchar(16)",
|
|
rowCount: 2,
|
|
generatorKey: "text",
|
|
generatorParams: { includeDefault: true, defaultPercent: 100 },
|
|
columnDefault: "active",
|
|
},
|
|
],
|
|
},
|
|
"postgres",
|
|
);
|
|
|
|
assert.deepEqual(result.rows, [["active"], ["active"]]);
|
|
assert.match(result.sql, /VALUES\n\('active'\),\n\('active'\);$/);
|
|
});
|
|
|
|
test("unwraps quoted and casted PostgreSQL default literals", () => {
|
|
const result = generateTableData(
|
|
{
|
|
tableName: "users",
|
|
schema: "public",
|
|
database: "app",
|
|
rowCount: 1,
|
|
columns: [
|
|
{
|
|
columnName: "role",
|
|
dataType: "text",
|
|
rowCount: 1,
|
|
generatorKey: "text",
|
|
generatorParams: { includeDefault: true, defaultPercent: 100 },
|
|
columnDefault: "'guest'::text",
|
|
},
|
|
],
|
|
},
|
|
"postgres",
|
|
);
|
|
|
|
assert.deepEqual(result.rows, [["guest"]]);
|
|
assert.match(result.sql, /VALUES\n\('guest'\);$/);
|
|
});
|
|
|
|
test("keeps expression defaults as raw SQL", () => {
|
|
const result = generateTableData(
|
|
{
|
|
tableName: "events",
|
|
schema: "public",
|
|
database: "app",
|
|
rowCount: 2,
|
|
columns: [
|
|
{
|
|
columnName: "created_at",
|
|
dataType: "timestamp",
|
|
rowCount: 2,
|
|
generatorKey: "datetime",
|
|
generatorParams: { includeDefault: true, defaultPercent: 100 },
|
|
columnDefault: "CURRENT_TIMESTAMP",
|
|
},
|
|
],
|
|
},
|
|
"postgres",
|
|
);
|
|
|
|
assert.equal(displayGeneratedValue(result.rows[0][0]), "CURRENT_TIMESTAMP");
|
|
assert.match(result.sql, /VALUES\n\(CURRENT_TIMESTAMP\),\n\(CURRENT_TIMESTAMP\);$/);
|
|
assert.doesNotMatch(result.sql, /'CURRENT_TIMESTAMP'/);
|
|
});
|
|
|
|
test("includeDefault without a schema default no longer emits NULL", () => {
|
|
const value = generateValue("name", "varchar(32)", "text", 0, { includeDefault: true, defaultPercent: 100 });
|
|
|
|
assert.notEqual(value, null);
|
|
assert.notEqual(value, undefined);
|
|
});
|
|
|
|
test("generates Oracle-compatible single-row inserts with explicit temporal literals", () => {
|
|
const result = generateTableData(
|
|
{
|
|
tableName: "DBX_GENERATE_TEST",
|
|
schema: "APP",
|
|
database: "XE",
|
|
rowCount: 2,
|
|
columns: [
|
|
{
|
|
columnName: "ID",
|
|
dataType: "NUMBER",
|
|
rowCount: 2,
|
|
generatorKey: "sequence",
|
|
generatorParams: { startValue: 1, increment: 1 },
|
|
},
|
|
{
|
|
columnName: "CREATED_ON",
|
|
dataType: "DATE",
|
|
rowCount: 2,
|
|
generatorKey: "date",
|
|
},
|
|
{
|
|
columnName: "CREATED_AT",
|
|
dataType: "TIMESTAMP(6)",
|
|
rowCount: 2,
|
|
generatorKey: "datetime",
|
|
},
|
|
],
|
|
},
|
|
"oracle",
|
|
);
|
|
|
|
assert.equal(supportsGeneratedMultiRowValues("oracle"), false);
|
|
assert.equal(result.statements.length, 1);
|
|
assert.match(result.statements[0], /^INSERT ALL\n/);
|
|
assert.equal(result.statements[0].match(/\n INTO /g)?.length, 2);
|
|
assert.match(result.statements[0], /SELECT 1 FROM DUAL;$/);
|
|
assert.ok(result.statements.every((sql) => /TO_DATE\('[^']+', 'YYYY-MM-DD'\)/.test(sql)));
|
|
assert.ok(result.statements.every((sql) => /TO_TIMESTAMP\('[^']+', 'YYYY-MM-DD HH24:MI:SS'\)/.test(sql)));
|
|
assert.doesNotMatch(result.sql, /VALUES\s*\([^;]+\),\s*\(/s);
|
|
});
|
|
|
|
test("batches large Oracle data generation statements", () => {
|
|
const result = generateTableData(
|
|
{
|
|
tableName: "DBX_GENERATE_TEST",
|
|
schema: "APP",
|
|
database: "XE",
|
|
rowCount: 101,
|
|
columns: [
|
|
{
|
|
columnName: "ID",
|
|
dataType: "NUMBER",
|
|
rowCount: 101,
|
|
generatorKey: "sequence",
|
|
generatorParams: { startValue: 1, increment: 1 },
|
|
},
|
|
],
|
|
},
|
|
"oracle",
|
|
);
|
|
|
|
assert.equal(result.statements.length, 2);
|
|
assert.equal(result.statements[0].match(/\n INTO /g)?.length, 100);
|
|
assert.equal(result.statements[1].match(/\n INTO /g)?.length, 1);
|
|
});
|
|
|
|
test("generates TDengine stable rows with one child table identity and stable tag values", () => {
|
|
const result = generateTableData(
|
|
{
|
|
tableName: "sensor_data",
|
|
tableType: "STABLE",
|
|
schema: "dbx_issue4512",
|
|
database: "dbx_issue4512",
|
|
rowCount: 2,
|
|
columns: [
|
|
{
|
|
columnName: "ts",
|
|
dataType: "TIMESTAMP",
|
|
rowCount: 2,
|
|
generatorKey: "sequence",
|
|
generatorParams: { startValue: 1, increment: 1 },
|
|
},
|
|
{
|
|
columnName: "temperature",
|
|
dataType: "FLOAT",
|
|
rowCount: 2,
|
|
generatorKey: "sequence",
|
|
generatorParams: { startValue: 20, increment: 1 },
|
|
},
|
|
{
|
|
columnName: "device_id",
|
|
dataType: "BINARY(64)",
|
|
rowCount: 2,
|
|
generatorKey: "sequence",
|
|
generatorParams: { startValue: 100, increment: 1 },
|
|
isTag: true,
|
|
},
|
|
],
|
|
},
|
|
"tdengine",
|
|
);
|
|
|
|
assert.deepEqual(result.columns, ["tbname", "ts", "temperature", "device_id"]);
|
|
assert.match(String(result.rows[0][0]), /^dbx_gen_[a-z0-9]+_[a-z0-9]+$/);
|
|
assert.equal(result.rows[1][0], result.rows[0][0]);
|
|
assert.deepEqual(
|
|
result.rows.map((row) => row.slice(1)),
|
|
[
|
|
[1, 20, 100],
|
|
[2, 21, 100],
|
|
],
|
|
);
|
|
assert.match(result.sql, /^INSERT INTO `sensor_data` \(`tbname`, `ts`, `temperature`, `device_id`\) VALUES\n/);
|
|
assert.equal(result.sql.match(/'dbx_gen_[a-z0-9]+_[a-z0-9]+'/g)?.length, 2);
|
|
});
|
|
|
|
test("keeps ordinary TDengine table generation unchanged", () => {
|
|
const result = generateTableData(
|
|
{
|
|
tableName: "sensor_data_001",
|
|
tableType: "TABLE",
|
|
schema: "dbx_issue4512",
|
|
database: "dbx_issue4512",
|
|
rowCount: 1,
|
|
columns: [
|
|
{
|
|
columnName: "ts",
|
|
dataType: "TIMESTAMP",
|
|
rowCount: 1,
|
|
generatorKey: "sequence",
|
|
generatorParams: { startValue: 1, increment: 1 },
|
|
},
|
|
],
|
|
},
|
|
"tdengine",
|
|
);
|
|
|
|
assert.deepEqual(result.columns, ["ts"]);
|
|
assert.deepEqual(result.rows, [[1]]);
|
|
assert.doesNotMatch(result.sql, /tbname/);
|
|
});
|