test: v0.1.13 新增42项测试覆盖(事务回滚/子查询/外键级联/连接池)
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* v0.1.13 连接池测试
|
||||
* 覆盖 MetonaSqlark.connect / disconnect / disconnectAll / getActiveConnections
|
||||
*/
|
||||
|
||||
import { MetonaSqlark } from '../src/core';
|
||||
import '../src/connection-manager';
|
||||
|
||||
// ===================================================================
|
||||
// 连接池基础
|
||||
// ===================================================================
|
||||
|
||||
describe('v0.1.13 连接池', () => {
|
||||
afterEach(async () => {
|
||||
// 清理所有连接
|
||||
await (MetonaSqlark as any).disconnectAll?.();
|
||||
});
|
||||
|
||||
it('connect: 创建新实例', async () => {
|
||||
const db = await (MetonaSqlark as any).connect({ name: 'pool-test-1', mode: 'memory' });
|
||||
expect(db).toBeDefined();
|
||||
expect(db.isReady()).toBe(true);
|
||||
expect((MetonaSqlark as any).getActiveConnections()).toContain('pool-test-1');
|
||||
await (MetonaSqlark as any).disconnect('pool-test-1');
|
||||
});
|
||||
|
||||
it('connect: 同名数据库复用实例', async () => {
|
||||
const db1 = await (MetonaSqlark as any).connect({ name: 'pool-test-2', mode: 'memory' });
|
||||
const db2 = await (MetonaSqlark as any).connect({ name: 'pool-test-2' });
|
||||
// 同一个实例
|
||||
expect(db1).toBe(db2);
|
||||
await (MetonaSqlark as any).disconnect('pool-test-2');
|
||||
await (MetonaSqlark as any).disconnect('pool-test-2');
|
||||
});
|
||||
|
||||
it('connect: 不同名数据库不同实例', async () => {
|
||||
const db1 = await (MetonaSqlark as any).connect({ name: 'pool-a', mode: 'memory' });
|
||||
const db2 = await (MetonaSqlark as any).connect({ name: 'pool-b', mode: 'memory' });
|
||||
expect(db1).not.toBe(db2);
|
||||
const active = (MetonaSqlark as any).getActiveConnections();
|
||||
expect(active).toContain('pool-a');
|
||||
expect(active).toContain('pool-b');
|
||||
await (MetonaSqlark as any).disconnectAll();
|
||||
});
|
||||
|
||||
it('disconnect: 引用计数归零自动 close', async () => {
|
||||
const db = await (MetonaSqlark as any).connect({ name: 'pool-close', mode: 'memory' });
|
||||
expect(db.isReady()).toBe(true);
|
||||
|
||||
// 第二次 connect → refCount = 2
|
||||
await (MetonaSqlark as any).connect({ name: 'pool-close' });
|
||||
|
||||
// 第一次 disconnect → refCount = 1
|
||||
await (MetonaSqlark as any).disconnect('pool-close');
|
||||
expect(db.isReady()).toBe(true); // 仍然 alive
|
||||
|
||||
// 第二次 disconnect → refCount = 0 → close
|
||||
await (MetonaSqlark as any).disconnect('pool-close');
|
||||
expect(db.isReady()).toBe(false); // 已关闭
|
||||
});
|
||||
|
||||
it('disconnect: 引用计数 > 1 时不 close', async () => {
|
||||
const db = await (MetonaSqlark as any).connect({ name: 'pool-ref', mode: 'memory' });
|
||||
// 3 次 connect
|
||||
await (MetonaSqlark as any).connect({ name: 'pool-ref' });
|
||||
await (MetonaSqlark as any).connect({ name: 'pool-ref' });
|
||||
// 2 次 disconnect
|
||||
await (MetonaSqlark as any).disconnect('pool-ref');
|
||||
await (MetonaSqlark as any).disconnect('pool-ref');
|
||||
expect(db.isReady()).toBe(true); // 还有 1 个引用
|
||||
// 最后一次
|
||||
await (MetonaSqlark as any).disconnect('pool-ref');
|
||||
expect(db.isReady()).toBe(false);
|
||||
});
|
||||
|
||||
it('disconnectAll: 强制关闭所有', async () => {
|
||||
const db1 = await (MetonaSqlark as any).connect({ name: 'pool-all-1', mode: 'memory' });
|
||||
const db2 = await (MetonaSqlark as any).connect({ name: 'pool-all-2', mode: 'memory' });
|
||||
|
||||
await (MetonaSqlark as any).disconnectAll();
|
||||
expect(db1.isReady()).toBe(false);
|
||||
expect(db2.isReady()).toBe(false);
|
||||
expect((MetonaSqlark as any).getActiveConnections()).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('getActiveConnections: 返回活跃连接列表', async () => {
|
||||
expect((MetonaSqlark as any).getActiveConnections()).toEqual([]);
|
||||
await (MetonaSqlark as any).connect({ name: 'pool-list-1', mode: 'memory' });
|
||||
await (MetonaSqlark as any).connect({ name: 'pool-list-2', mode: 'memory' });
|
||||
const active = (MetonaSqlark as any).getActiveConnections();
|
||||
expect(active).toContain('pool-list-1');
|
||||
expect(active).toContain('pool-list-2');
|
||||
expect(active.length).toBe(2);
|
||||
await (MetonaSqlark as any).disconnectAll();
|
||||
});
|
||||
|
||||
it('connect 复用时数据库操作正常', async () => {
|
||||
const db1 = await (MetonaSqlark as any).connect({ name: 'pool-data', mode: 'memory' });
|
||||
await db1.defineTable('test', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
val: { type: 'number' },
|
||||
});
|
||||
await db1.table('test').insert({ id: 'a', val: 42 });
|
||||
|
||||
// 复用
|
||||
const db2 = await (MetonaSqlark as any).connect({ name: 'pool-data' });
|
||||
const rows = await db2.table('test').select().execute();
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0].val).toBe(42);
|
||||
|
||||
await (MetonaSqlark as any).disconnect('pool-data');
|
||||
await (MetonaSqlark as any).disconnect('pool-data');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user