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。

3.1 KB
Raw Blame
// @vitest-environment happy-dom

import { beforeEach, describe, expect, it } from "vitest";
import { loadVirtualRowIdentifier, removeVirtualRowIdentifier, saveVirtualRowIdentifier, tableDataVirtualRowIdentifierScope, virtualRowIdentifierScopeKey } from "@/lib/table/virtualRowIdentifier";
import type { ColumnInfo } from "@/types/database";

const scope = {
  connectionId: "db2-1",
  database: "MAXIMO",
  schema: "APP",
  tableName: "MAFAPPDATA",
};

function column(name: string): ColumnInfo {
  return { name, data_type: "VARCHAR", is_nullable: true, column_default: null, is_primary_key: false, extra: null };
}

describe("virtualRowIdentifier", () => {
  beforeEach(() => localStorage.clear());

  it("persists a composite key for one exact table scope", () => {
    const columns = [column("TENANT_ID"), column("APP_ID"), column("APP")];

    expect(saveVirtualRowIdentifier(scope, ["TENANT_ID", "APP_ID"], columns)).toBe(true);
    expect(loadVirtualRowIdentifier(scope, columns)).toEqual(["TENANT_ID", "APP_ID"]);
    expect(loadVirtualRowIdentifier({ ...scope, tableName: "OTHER_TABLE" }, columns)).toEqual([]);
    expect(virtualRowIdentifierScopeKey(scope)).not.toBe(virtualRowIdentifierScopeKey({ ...scope, catalog: "ARCHIVE" }));
  });

  it("canonicalizes unambiguous casing and removes a key when a column disappears", () => {
    expect(saveVirtualRowIdentifier(scope, ["app_id"], [column("app_id")])).toBe(true);
    expect(loadVirtualRowIdentifier(scope, [column("APP_ID")])).toEqual(["APP_ID"]);
    expect(loadVirtualRowIdentifier(scope, [column("OTHER_ID")])).toEqual([]);
    expect(loadVirtualRowIdentifier(scope, [column("APP_ID")])).toEqual([]);
  });

  it("rejects empty, duplicate, and unknown column selections", () => {
    const columns = [column("ID"), column("NAME")];

    expect(saveVirtualRowIdentifier(scope, [], columns)).toBe(false);
    expect(saveVirtualRowIdentifier(scope, ["ID", "ID"], columns)).toBe(false);
    expect(saveVirtualRowIdentifier(scope, ["MISSING"], columns)).toBe(false);
    expect(loadVirtualRowIdentifier(scope, columns)).toEqual([]);
  });

  it("saves a MySQL table data tab key under the scope its metadata load reads", () => {
    const tabScope = { connectionId: "mysql-1", database: "shop", tableName: "orders" };
    const columns = [column("k1"), column("k2")];

    expect(saveVirtualRowIdentifier(tableDataVirtualRowIdentifierScope({ db_type: "mysql" }, tabScope), ["k1"], columns)).toBe(true);

    // Data tab metadata loads send the MySQL database name as the schema.
    expect(loadVirtualRowIdentifier({ ...tabScope, schema: "shop" }, columns)).toEqual(["k1"]);
  });

  it("keeps an explicit schema for schema-aware table data tabs", () => {
    const tabScope = { connectionId: "pg-1", database: "app", schema: "sales", tableName: "orders" };

    expect(tableDataVirtualRowIdentifierScope({ db_type: "postgres" }, tabScope)).toEqual(tabScope);
  });

  it("clears a persisted key", () => {
    const columns = [column("ID")];
    expect(saveVirtualRowIdentifier(scope, ["ID"], columns)).toBe(true);

    removeVirtualRowIdentifier(scope);

    expect(loadVirtualRowIdentifier(scope, columns)).toEqual([]);
  });
});