feat: metona-sqlark v0.1.12 — 前端TypeScript关系型数据库
- 4种存储引擎:Memory / IndexedDB / OPFS / Hybrid - 完整SQL支持:SELECT/INSERT/UPDATE/DELETE/JOIN/GROUP BY/HAVING/DISTINCT - Query Builder链式API + TypeScript泛型支持 - 聚合函数:COUNT/SUM/AVG/MIN/MAX - 事务、插件系统(14 hooks)、发布订阅、数据迁移、导入导出 - React/Vue框架集成 - 264个测试用例,93.46%覆盖率 - 零运行时依赖
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Transaction 测试
|
||||
*/
|
||||
|
||||
import { MemoryEngine } from '../../src/engine/memory';
|
||||
import { TransactionManager, Transaction } from '../../src/transaction/index';
|
||||
import { createSchema } from '../../src/table/schema';
|
||||
|
||||
describe('Transaction', () => {
|
||||
let engine: MemoryEngine;
|
||||
let tm: TransactionManager;
|
||||
|
||||
const userSchema = createSchema('users', {
|
||||
id: { type: 'string', primaryKey: true },
|
||||
name: { type: 'string', required: true },
|
||||
balance: { type: 'number', default: 0 },
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
engine = new MemoryEngine();
|
||||
await engine.open('test-tx', 1);
|
||||
await engine.createTable(userSchema);
|
||||
tm = new TransactionManager(engine);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await engine.close();
|
||||
});
|
||||
|
||||
it('事务中执行插入', async () => {
|
||||
await tm.execute(async (trx) => {
|
||||
await trx.table('users').insert({ id: '1', name: 'Alice', balance: 100 });
|
||||
await trx.table('users').insert({ id: '2', name: 'Bob', balance: 50 });
|
||||
});
|
||||
|
||||
expect(await engine.count('users')).toBe(2);
|
||||
});
|
||||
|
||||
it('事务中执行混合操作', async () => {
|
||||
await engine.insert('users', [{ id: '1', name: 'Alice', balance: 100 }]);
|
||||
|
||||
await tm.execute(async (trx) => {
|
||||
await trx.table('users').update({ balance: 200 }).where({ id: '1' }).execute();
|
||||
await trx.table('users').insert({ id: '2', name: 'Bob', balance: 50 });
|
||||
});
|
||||
|
||||
const rows = await engine.find('users', { table: 'users', where: { id: '1' } });
|
||||
expect(rows[0].balance).toBe(200);
|
||||
expect(await engine.count('users')).toBe(2);
|
||||
});
|
||||
|
||||
it('事务中的 table() 返回相同实例', async () => {
|
||||
await tm.execute(async (trx) => {
|
||||
const t1 = trx.table('users');
|
||||
const t2 = trx.table('users');
|
||||
expect(t1).toBe(t2); // 相同表名返回同一实例
|
||||
});
|
||||
});
|
||||
|
||||
it('Transaction.isCompleted', async () => {
|
||||
let trxRef: Transaction | null = null;
|
||||
await tm.execute(async (trx) => {
|
||||
trxRef = trx;
|
||||
expect(trx.isCompleted()).toBe(false);
|
||||
await trx.table('users').insert({ id: '1', name: 'Alice' });
|
||||
});
|
||||
expect(trxRef!.isCompleted()).toBe(true);
|
||||
});
|
||||
|
||||
it('事务抛出错误', async () => {
|
||||
await expect(tm.execute(async (trx) => {
|
||||
await trx.table('users').insert({ id: '1', name: 'Alice' });
|
||||
throw new Error('intentional error');
|
||||
})).rejects.toThrow('Transaction failed');
|
||||
|
||||
// 注意:MemoryEngine 没有回滚,所以数据已经写入
|
||||
// v0.0.1 的 Transaction 只是错误包装
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user