Files
MetonaSqlark/tests/engine/aria-extra.test.ts
T
thzxx 24b3ca1079
CI / test (20.x) (push) Successful in 10m53s
CI / test (22.x) (push) Successful in 10m47s
CI / test (24.x) (push) Successful in 10m42s
CI / e2e (push) Successful in 10m27s
CI / test (18.x) (push) Successful in 11m1s
release: v0.6.0 — 完全移除 IndexedDB,自研 KVStore 事务存储引擎(多key原子写/快照日志恢复/CRC自愈)+ KVStoreEngine + 旧库迁移工具 + 10万级压力验证 + 崩溃注入e2e
2026-08-10 13:56:04 +08:00

169 lines
9.4 KiB
TypeScript

/**
* AriaEngine 最后补充测试
*/
import { AriaEngine } from '../../src/engine/aria/index';
import { createSchema } from '../../src/table/schema';
import { installOPFSMock } from '../helpers/opfs-mock';
beforeEach(() => { installOPFSMock(new Map()); });
describe('AriaEngine — 补充测试', () => {
let engine: AriaEngine;
beforeEach(async () => { engine = new AriaEngine({ storageBackend: 'memory' }); await engine.open('sup', 1); });
afterEach(async () => { await engine.close(); });
it('a1 PK lookup', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
await engine.insert('t', [{ id: 'x' }]);
expect((await engine.find('t', { table: 't', where: { id: 'x' } }))[0].id).toBe('x');
});
it('a2 multi insert', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, v: { type: 'number' } }));
const pks = await engine.insert('t', [{ id: 'a', v: 1 }, { id: 'b', v: 2 }]);
expect(pks).toEqual(['a', 'b']);
});
it('a3 count after insert', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
expect(await engine.count('t')).toBe(0);
await engine.insert('t', [{ id: '1' }]);
expect(await engine.count('t')).toBe(1);
});
it('a4 count with where', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, v: { type: 'number' } }));
await engine.insert('t', [{ id: '1', v: 10 }, { id: '2', v: 20 }, { id: '3', v: 30 }]);
expect(await engine.count('t', { table: 't', where: { v: { $gt: 15 } } })).toBe(2);
});
it('a5 find all', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
await engine.insert('t', [{ id: '1' }, { id: '2' }, { id: '3' }]);
expect(await engine.find('t', { table: 't' })).toHaveLength(3);
});
it('a6 find with $in', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, v: { type: 'number' } }));
await engine.insert('t', [{ id: '1', v: 10 }, { id: '2', v: 20 }, { id: '3', v: 30 }]);
expect(await engine.find('t', { table: 't', where: { v: { $in: [10, 30] } } })).toHaveLength(2);
});
it('a7 find with $ne', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, v: { type: 'number' } }));
await engine.insert('t', [{ id: '1', v: 10 }, { id: '2', v: 20 }]);
expect(await engine.find('t', { table: 't', where: { v: { $ne: 10 } } })).toHaveLength(1);
});
it('a8 order asc', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, v: { type: 'number' } }));
await engine.insert('t', [{ id: 'c', v: 3 }, { id: 'a', v: 1 }, { id: 'b', v: 2 }]);
const r = await engine.find('t', { table: 't', orderBy: [{ column: 'v', direction: 'asc' }] });
expect(r.map((x: any) => x.v)).toEqual([1, 2, 3]);
});
it('a9 limit only', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
for (let i = 0; i < 10; i++) await engine.insert('t', [{ id: `${i}` }]);
expect(await engine.find('t', { table: 't', limit: 3 })).toHaveLength(3);
});
it('a10 offset only', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
for (let i = 0; i < 5; i++) await engine.insert('t', [{ id: `${i}` }]);
expect(await engine.find('t', { table: 't', offset: 3 })).toHaveLength(2);
});
it('a11 columns projection', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, a: { type: 'number' }, b: { type: 'number' } }));
await engine.insert('t', [{ id: '1', a: 1, b: 2 }]);
expect(Object.keys((await engine.find('t', { table: 't', columns: ['a'] }))[0])).toEqual(['a']);
});
it('a12 boolean true', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, f: { type: 'boolean' } }));
await engine.insert('t', [{ id: '1', f: true }]);
expect((await engine.find('t', { table: 't' }))[0].f).toBe(true);
});
it('a13 boolean false', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, f: { type: 'boolean' } }));
await engine.insert('t', [{ id: '1', f: false }]);
expect((await engine.find('t', { table: 't' }))[0].f).toBe(false);
});
it('a14 date type', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, d: { type: 'date' } }));
await engine.insert('t', [{ id: '1', d: '2024-06-15' }]);
expect((await engine.find('t', { table: 't' }))[0].d).toBe('2024-06-15');
});
it('a15 json array', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, j: { type: 'json' } }));
await engine.insert('t', [{ id: '1', j: [1, 2, 3] }]);
expect((await engine.find('t', { table: 't' }))[0].j).toEqual([1, 2, 3]);
});
it('a16 long string', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, s: { type: 'string' } }));
const long = 'x'.repeat(500);
await engine.insert('t', [{ id: '1', s: long }]);
expect((await engine.find('t', { table: 't' }))[0].s).toBe(long);
});
it('a17 50 rows bulk', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
const data = []; for (let i = 0; i < 50; i++) data.push({ id: `${i}` });
await engine.insert('t', data);
expect(await engine.count('t')).toBe(50);
});
it('a18 getTableSchema null', async () => {
expect(await engine.getTableSchema('nope')).toBeNull();
});
it('a19 close reopen', async () => {
await engine.close();
engine = new AriaEngine({ storageBackend: 'memory' });
await engine.open('sup2', 1);
expect(engine.isOpen()).toBe(true);
});
it('a20 重复 createTable', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
await expect(engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }))).rejects.toThrow();
});
it('a21 find PK not found', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
expect(await engine.find('t', { table: 't', where: { id: 'nope' } })).toHaveLength(0);
});
it('a22 update affect zero', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, v: { type: 'number' } }));
await engine.insert('t', [{ id: '1', v: 1 }]);
expect(await engine.update('t', { table: 't', where: { id: 'x' } }, { v: 9 })).toBe(0);
});
it('a23 delete affect zero', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }));
await engine.insert('t', [{ id: '1' }]);
expect(await engine.delete('t', { table: 't', where: { id: 'x' } })).toBe(0);
});
it('a24 $and with 3 conditions', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, a: { type: 'number' }, b: { type: 'number' }, c: { type: 'number' } }));
await engine.insert('t', [{ id: '1', a: 1, b: 1, c: 1 }, { id: '2', a: 1, b: 1, c: 2 }, { id: '3', a: 1, b: 2, c: 1 }]);
expect(await engine.find('t', { table: 't', where: { $and: [{ a: 1 }, { b: 1 }, { c: 1 }] } })).toHaveLength(1);
});
it('a25 $or with 3 conditions', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, v: { type: 'number' } }));
await engine.insert('t', [{ id: '1', v: 1 }, { id: '2', v: 2 }, { id: '3', v: 3 }, { id: '4', v: 4 }]);
expect(await engine.find('t', { table: 't', where: { $or: [{ v: 1 }, { v: 3 }, { v: 4 }] } })).toHaveLength(3);
});
it('a26 $like with percent', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, n: { type: 'string' } }));
await engine.insert('t', [{ id: '1', n: 'hello' }, { id: '2', n: 'help' }]);
expect(await engine.find('t', { table: 't', where: { n: { $like: 'hel%' } } })).toHaveLength(2);
});
it('a27 col with dot in name', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, 'full.name': { type: 'string' } }));
await engine.insert('t', [{ id: '1', 'full.name': 'Test' }]);
expect(await engine.count('t')).toBe(1);
});
it('a28 table with underscore', async () => {
await engine.createTable(createSchema('my_table', { id: { type: 'string', primaryKey: true } }));
expect(await engine.hasTable('my_table')).toBe(true);
});
it('a29 3-column orderBy', async () => {
await engine.createTable(createSchema('t', { id: { type: 'string', primaryKey: true }, a: { type: 'number' }, b: { type: 'number' }, c: { type: 'number' } }));
await engine.insert('t', [{ id: '1', a: 1, b: 2, c: 3 }, { id: '2', a: 1, b: 1, c: 3 }, { id: '3', a: 2, b: 1, c: 1 }]);
const r = await engine.find('t', { table: 't', orderBy: [{ column: 'a', direction: 'asc' }, { column: 'b', direction: 'asc' }, { column: 'c', direction: 'asc' }] });
expect(r[0].id).toBe('2');
});
it('a30 savepoint create and release', async () => {
await engine.beginTransaction();
await (engine as any).savepoint('sp1');
await (engine as any).releaseSavepoint('sp1');
await engine.rollbackTransaction();
});
});