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。

1.7 KB
Raw Blame
import { strict as assert } from "node:assert";
import { test } from "vitest";
import { formatMongoShellText, MAX_MONGO_FORMAT_CHARS } from "../../apps/desktop/src/lib/mongo/mongoFormatter.ts";

test("formats MongoDB find query objects", () => {
    assert.equal(
        formatMongoShellText('db.characters.find({name:"Ada",age:{$gte:18,$lt:30}}).sort({createdAt:-1}).limit(20)'),
        `db.characters.find({
  name: "Ada",
  age: {
    $gte: 18,
    $lt: 30
  }
})
  .sort({
    createdAt: -1
  })
  .limit(20)`,
    );
});

test("formats MongoDB aggregation pipelines", () => {
    assert.equal(
        formatMongoShellText('db.orders.aggregate([{$match:{status:"paid"}},{$group:{_id:"$userId",total:{$sum:"$amount"}}}])'),
        `db.orders.aggregate([
  {
    $match: {
      status: "paid"
    }
  },
  {
    $group: {
      _id: "$userId",
      total: {
        $sum: "$amount"
      }
    }
  }
])`,
    );
});

test("preserves strings, regex literals, and comments", () => {
    assert.equal(
        formatMongoShellText('db.users.find({name:/a,b\\/c/i,note:"x,y",active:true}) // keep comment'),
        `db.users.find({
  name: /a,b\\/c/i,
  note: "x,y",
  active: true
}) // keep comment`,
    );
});

test("respects tab indentation setting", () => {
    assert.equal(
        formatMongoShellText("db.users.find({name:'Ada'})", { useTabs: true, tabWidth: 4 }),
        "db.users.find({\n\tname: 'Ada'\n})",
    );
});

test("leaves blank MongoDB text unchanged", () => {
    assert.equal(formatMongoShellText("  \n\t"), "  \n\t");
});

test("rejects very large MongoDB queries", () => {
    assert.throws(() => formatMongoShellText("x".repeat(MAX_MONGO_FORMAT_CHARS + 1)), /too large/i);
});