Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

8.2 KB
Raw Blame
import { strict as assert } from "node:assert";
import { test } from "vitest";
import { coerceDataGridCellValue, dataGridCellDisplayText, dataGridCellEditorText } from "../../apps/desktop/src/lib/dataGrid/dataGridCellCoercion.ts";

test("formats Postgres array cells with native brace syntax", () => {
  const columnInfo = { data_type: "_text" };

  assert.equal(
    dataGridCellDisplayText({
      value: ["draft", "发布", "needs space"],
      databaseType: "postgres",
      columnInfo,
    }),
    '{draft,发布,"needs space"}',
  );
  assert.equal(
    dataGridCellEditorText({
      value: ["draft", "发布", "needs space"],
      databaseType: "postgres",
      columnInfo,
    }),
    '{draft,发布,"needs space"}',
  );
});

test("coerces JSON style input for Postgres array columns", () => {
  assert.deepEqual(
    coerceDataGridCellValue({
      value: `["draft","发布"]`,
      oldValue: "{legacy}",
      databaseType: "postgres",
      columnInfo: { data_type: "_text" },
    }),
    ["draft", "发布"],
  );
});

test("coerces PG brace-style input for Postgres array columns", () => {
  assert.deepEqual(
    coerceDataGridCellValue({
      value: "{1,2,3}",
      oldValue: [1, 2, 3],
      databaseType: "postgres",
      columnInfo: { data_type: "_int4" },
    }),
    [1, 2, 3],
  );
});

test("preserves high precision numeric edits as text", () => {
  assert.equal(
    coerceDataGridCellValue({
      value: "144847503924137986",
      oldValue: 142189065666650,
      databaseType: "sqlserver",
      columnInfo: { data_type: "bigint" },
    }),
    "144847503924137986",
  );

  assert.equal(
    coerceDataGridCellValue({
      value: "12345678901234567890123456789012345678",
      oldValue: 1,
      databaseType: "postgres",
      columnInfo: { data_type: "numeric(38,0)" },
    }),
    "12345678901234567890123456789012345678",
  );
});

test("keeps safe numeric edits on the existing number path", () => {
  assert.equal(
    coerceDataGridCellValue({
      value: "42",
      oldValue: 1,
      databaseType: "sqlserver",
      columnInfo: { data_type: "int" },
    }),
    42,
  );

  assert.strictEqual(
    coerceDataGridCellValue({
      value: "42",
      oldValue: 42,
      databaseType: "sqlserver",
      columnInfo: { data_type: "bigint" },
    }),
    42,
  );
});

test("coerces PG brace-style string array", () => {
  assert.deepEqual(
    coerceDataGridCellValue({
      value: `{draft,"needs space",发布}`,
      oldValue: [],
      databaseType: "postgres",
      columnInfo: { data_type: "_text" },
    }),
    ["draft", "needs space", "发布"],
  );
});

test("preserves high precision numeric tokens in Postgres arrays", () => {
  assert.deepEqual(
    coerceDataGridCellValue({
      value: "{144847503924137986,2}",
      oldValue: [],
      databaseType: "postgres",
      columnInfo: { data_type: "_int8" },
    }),
    ["144847503924137986", 2],
  );
});

test("coerces PG brace-style array with NULL", () => {
  assert.deepEqual(
    coerceDataGridCellValue({
      value: "{1,NULL,3}",
      oldValue: [],
      databaseType: "postgres",
      columnInfo: { data_type: "_int4" },
    }),
    [1, null, 3],
  );
});

test("coerces empty PG brace-style array", () => {
  assert.deepEqual(
    coerceDataGridCellValue({
      value: "{}",
      oldValue: [],
      databaseType: "postgres",
      columnInfo: { data_type: "_int4" },
    }),
    [],
  );
});

test("PG array round-trip: format -> coerce -> same values", () => {
  const original = ["draft", "发布", "needs space"];
  const columnInfo = { data_type: "_text" };

  const formatted = dataGridCellEditorText({
    value: original,
    databaseType: "postgres",
    columnInfo,
  });

  const coerced = coerceDataGridCellValue({
    value: formatted,
    oldValue: undefined,
    databaseType: "postgres",
    columnInfo,
  });

  assert.deepEqual(coerced as string[], original);
});

test("unchanged PG array value returns oldValue reference to avoid false dirty", () => {
  const oldValue = [1, 2, 3];

  const formatted = dataGridCellEditorText({
    value: oldValue,
    databaseType: "postgres",
    columnInfo: { data_type: "_int4" },
  });

  const result = coerceDataGridCellValue({
    value: formatted,
    oldValue,
    databaseType: "postgres",
    columnInfo: { data_type: "_int4" },
  });

  assert.strictEqual(result, oldValue);
});

test("changed PG array value returns new array reference", () => {
  const oldValue = [1, 2, 3];
  const newText = "{1,2,3,4}";

  const result = coerceDataGridCellValue({
    value: newText,
    oldValue,
    databaseType: "postgres",
    columnInfo: { data_type: "_int4" },
  });

  assert.notStrictEqual(result, oldValue);
  assert.deepEqual(result, [1, 2, 3, 4]);
});

test("MySQL text BLOB editor decodes UTF-8 and writes edited bytes as hex", () => {
  const oldValue = "0x2332303035383035";
  const columnInfo = { data_type: "longblob" };

  assert.equal(dataGridCellEditorText({ value: oldValue, databaseType: "mysql", columnInfo }), "#2005805");
  assert.strictEqual(coerceDataGridCellValue({ value: "#2005805", oldValue, databaseType: "mysql", columnInfo }), oldValue);
  assert.equal(coerceDataGridCellValue({ value: "新表达式", oldValue, databaseType: "mysql", columnInfo }), "0xe696b0e8a1a8e8bebee5bc8f");
  assert.equal(coerceDataGridCellValue({ value: "", oldValue, databaseType: "mysql", columnInfo }), "0x");
});

test("MySQL binary BLOB editor keeps non-text bytes in hex", () => {
  const oldValue = "0x89504e470d0a1a0a";
  const columnInfo = { data_type: "longblob" };

  assert.equal(dataGridCellEditorText({ value: oldValue, databaseType: "mysql", columnInfo }), oldValue);
  assert.equal(coerceDataGridCellValue({ value: oldValue, oldValue, databaseType: "mysql", columnInfo }), oldValue);
});

test("Oracle and OceanBase-Oracle text BLOB editor decodes UTF-8 and writes edited bytes as hex", () => {
  const oldValue = "0x2332303035383035";
  const columnInfo = { data_type: "blob" };

  for (const databaseType of ["oracle", "oceanbase-oracle"] as const) {
    assert.equal(dataGridCellEditorText({ value: oldValue, databaseType, columnInfo }), "#2005805");
    assert.strictEqual(coerceDataGridCellValue({ value: "#2005805", oldValue, databaseType, columnInfo }), oldValue);
    assert.equal(coerceDataGridCellValue({ value: "新表达式", oldValue, databaseType, columnInfo }), "0xe696b0e8a1a8e8bebee5bc8f");
    assert.equal(coerceDataGridCellValue({ value: "", oldValue, databaseType, columnInfo }), "0x");
  }
});

test("PostgreSQL text BYTEA editor decodes UTF-8 and writes edited bytes as hex (#11987)", () => {
  const oldValueHex = "0x48656c6c6f";
  const oldValuePgEscape = "\\x48656c6c6f";
  const columnInfo = { data_type: "bytea" };

  for (const oldValue of [oldValueHex, oldValuePgEscape]) {
    assert.equal(dataGridCellEditorText({ value: oldValue, databaseType: "postgres", columnInfo }), "Hello");
    assert.strictEqual(coerceDataGridCellValue({ value: "Hello", oldValue, databaseType: "postgres", columnInfo }), oldValue);
    assert.equal(coerceDataGridCellValue({ value: "新内容", oldValue, databaseType: "postgres", columnInfo }), "0xe696b0e58685e5aeb9");
    assert.equal(coerceDataGridCellValue({ value: "", oldValue, databaseType: "postgres", columnInfo }), "0x");
    assert.equal(coerceDataGridCellValue({ value: "", oldValue, databaseType: "postgres", columnInfo, emptyStringAsNull: true }), null);
  }
});

test("PostgreSQL binary BYTEA editor keeps non-text bytes in hex (#11987)", () => {
  const oldValueHex = "0x89504e470d0a1a0a";
  const oldValuePgEscape = "\\x89504e470d0a1a0a";
  const columnInfo = { data_type: "bytea" };

  for (const oldValue of [oldValueHex, oldValuePgEscape]) {
    assert.equal(dataGridCellEditorText({ value: oldValue, databaseType: "postgres", columnInfo }), oldValue);
    assert.equal(coerceDataGridCellValue({ value: oldValue, oldValue, databaseType: "postgres", columnInfo }), oldValue);
  }
});

test("non-PostgreSQL BYTEA editor keeps bytes in hex", () => {
  const oldValue = "0x48656c6c6f";
  const columnInfo = { data_type: "bytea" };

  assert.equal(dataGridCellEditorText({ value: oldValue, databaseType: "sqlite", columnInfo }), oldValue);
  assert.equal(coerceDataGridCellValue({ value: "Hello", oldValue, databaseType: "sqlite", columnInfo }), "Hello");
});