211 lines
8.3 KiB
TypeScript
211 lines
8.3 KiB
TypeScript
/**
|
|
* AriaEngine 边缘场景扩展测试
|
|
* 全部 MemoryBackend,零卡死
|
|
*/
|
|
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('edge-ext-test', 1);
|
|
});
|
|
|
|
afterEach(async () => { await engine.close(); });
|
|
|
|
describe('复杂查询', () => {
|
|
beforeEach(async () => {
|
|
await engine.createTable(createSchema('users', {
|
|
id: { type: 'string', primaryKey: true },
|
|
name: { type: 'string', required: true },
|
|
age: { type: 'number', default: 0 },
|
|
active: { type: 'boolean', default: true },
|
|
score: { type: 'number', default: 0 },
|
|
}));
|
|
await engine.insert('users', [
|
|
{ id: '1', name: 'Alice', age: 30, active: true, score: 85 },
|
|
{ id: '2', name: 'Bob', age: 25, active: true, score: 92 },
|
|
{ id: '3', name: 'Charlie', age: 35, active: false, score: 78 },
|
|
{ id: '4', name: 'Diana', age: 30, active: true, score: 95 },
|
|
{ id: '5', name: 'Eve', age: 25, active: true, score: 88 },
|
|
]);
|
|
});
|
|
|
|
it('$and 多条件', async () => {
|
|
const rows = await engine.find('users', { table: 'users', where: { $and: [{ age: 30 }, { active: true }] } });
|
|
expect(rows).toHaveLength(2);
|
|
});
|
|
|
|
it('$or 多条件', async () => {
|
|
const rows = await engine.find('users', { table: 'users', where: { $or: [{ name: 'Alice' }, { name: 'Charlie' }] } });
|
|
expect(rows).toHaveLength(2);
|
|
});
|
|
|
|
it('$not 条件', async () => {
|
|
const rows = await engine.find('users', { table: 'users', where: { age: { $not: { $eq: 30 } } } });
|
|
expect(rows).toHaveLength(3);
|
|
});
|
|
|
|
it('$ne 不等于', async () => {
|
|
const rows = await engine.find('users', { table: 'users', where: { age: { $ne: 25 } } });
|
|
expect(rows).toHaveLength(3);
|
|
});
|
|
|
|
it('$nin 不在列表中', async () => {
|
|
const rows = await engine.find('users', { table: 'users', where: { age: { $nin: [25, 35] } } });
|
|
expect(rows).toHaveLength(2);
|
|
});
|
|
|
|
it('$like 模糊匹配', async () => {
|
|
const rows = await engine.find('users', { table: 'users', where: { name: { $like: 'A%' } } });
|
|
expect(rows).toHaveLength(1);
|
|
expect(rows[0].name).toBe('Alice');
|
|
});
|
|
|
|
it('多列 ORDER BY', async () => {
|
|
const rows = await engine.find('users', { table: 'users', orderBy: [{ column: 'age', direction: 'asc' }, { column: 'score', direction: 'desc' }] });
|
|
expect(rows[0].age).toBeLessThanOrEqual(rows[4].age);
|
|
});
|
|
|
|
it('LIMIT + OFFSET 翻页', async () => {
|
|
const page1 = await engine.find('users', { table: 'users', orderBy: [{ column: 'id', direction: 'asc' }], limit: 2, offset: 0 });
|
|
const page2 = await engine.find('users', { table: 'users', orderBy: [{ column: 'id', direction: 'asc' }], limit: 2, offset: 2 });
|
|
expect(page1).toHaveLength(2);
|
|
expect(page2).toHaveLength(2);
|
|
expect(page1[0].id).not.toBe(page2[0].id);
|
|
});
|
|
|
|
it('列投影', async () => {
|
|
const rows = await engine.find('users', { table: 'users', columns: ['id', 'name'], where: { id: '1' } });
|
|
expect(Object.keys(rows[0]).sort()).toEqual(['id', 'name']);
|
|
});
|
|
|
|
it('空结果查询', async () => {
|
|
const rows = await engine.find('users', { table: 'users', where: { age: { $gt: 999 } } });
|
|
expect(rows).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe('边界与错误', () => {
|
|
it('未打开引擎操作抛异常', () => {
|
|
const e = new AriaEngine({ storageBackend: 'memory' });
|
|
return expect(e.createTable(createSchema('t', { id: { type: 'string', primaryKey: true } }))).rejects.toThrow('not opened');
|
|
});
|
|
|
|
it('close 后操作抛异常', async () => {
|
|
await engine.close();
|
|
await expect(engine.find('users', { table: 'users' })).rejects.toThrow('not opened');
|
|
});
|
|
|
|
it('重复 open 幂等', async () => {
|
|
await engine.open('edge-ext-test', 1);
|
|
expect(engine.isOpen()).toBe(true);
|
|
});
|
|
|
|
it('创建已存在的表抛异常', async () => {
|
|
await engine.createTable(createSchema('dup', { id: { type: 'string', primaryKey: true } }));
|
|
await expect(engine.createTable(createSchema('dup', { id: { type: 'string', primaryKey: true } }))).rejects.toThrow('already exists');
|
|
});
|
|
|
|
it('获取不存在的表 schema 返回 null', async () => {
|
|
expect(await engine.getTableSchema('ghost')).toBeNull();
|
|
});
|
|
|
|
it('hasTable 不存在的表返回 false', async () => {
|
|
expect(await engine.hasTable('nope')).toBe(false);
|
|
});
|
|
|
|
it('dropTable 不存在的表抛异常', async () => {
|
|
await expect(engine.dropTable('nope')).rejects.toThrow('does not exist');
|
|
});
|
|
|
|
it('insert 不存在的表抛异常', async () => {
|
|
await expect(engine.insert('ghost', [{ id: '1' }])).rejects.toThrow('does not exist');
|
|
});
|
|
|
|
it('update 不存在的表抛异常', async () => {
|
|
await expect(engine.update('ghost', { table: 'ghost' }, {})).rejects.toThrow('does not exist');
|
|
});
|
|
|
|
it('delete 不存在的表抛异常', async () => {
|
|
await expect(engine.delete('ghost', { table: 'ghost' })).rejects.toThrow('does not exist');
|
|
});
|
|
|
|
it('count 不存在的表抛异常', async () => {
|
|
await expect(engine.count('ghost')).rejects.toThrow('does not exist');
|
|
});
|
|
|
|
it('空表 count 返回 0', async () => {
|
|
await engine.createTable(createSchema('empty', { id: { type: 'string', primaryKey: true } }));
|
|
expect(await engine.count('empty')).toBe(0);
|
|
});
|
|
|
|
it('空表 find 返回空数组', async () => {
|
|
await engine.createTable(createSchema('empty2', { id: { type: 'string', primaryKey: true } }));
|
|
expect(await engine.find('empty2', { table: 'empty2' })).toHaveLength(0);
|
|
});
|
|
|
|
it('clear 空表不报错', async () => {
|
|
await engine.createTable(createSchema('empty3', { id: { type: 'string', primaryKey: true } }));
|
|
await engine.clear('empty3');
|
|
});
|
|
});
|
|
|
|
describe('类型系统', () => {
|
|
beforeEach(async () => {
|
|
await engine.createTable(createSchema('types', {
|
|
id: { type: 'string', primaryKey: true },
|
|
name: { type: 'string', maxLength: 10 },
|
|
age: { type: 'number', min: 0, max: 150 },
|
|
active: { type: 'boolean' },
|
|
created: { type: 'date' },
|
|
meta: { type: 'json' },
|
|
}));
|
|
});
|
|
|
|
it('string maxLength 约束', async () => {
|
|
await expect(engine.insert('types', [{ id: '1', name: 'verylongnamehere' }])).rejects.toThrow('exceeds max length');
|
|
});
|
|
|
|
it('number min 约束', async () => {
|
|
await expect(engine.insert('types', [{ id: '1', name: 'ok', age: -1 }])).rejects.toThrow('below minimum');
|
|
});
|
|
|
|
it('number max 约束', async () => {
|
|
await expect(engine.insert('types', [{ id: '1', name: 'ok', age: 200 }])).rejects.toThrow('above maximum');
|
|
});
|
|
|
|
it('正确值通过所有约束', async () => {
|
|
await engine.insert('types', [{ id: '1', name: 'Alice', age: 30, active: true, created: '2024-01-01', meta: { x: 1 } }]);
|
|
expect(await engine.count('types')).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('大量数据', () => {
|
|
it('200 行批量插入查询', async () => {
|
|
await engine.createTable(createSchema('big', { id: { type: 'string', primaryKey: true }, val: { type: 'number' } }));
|
|
const rows = [];
|
|
for (let i = 0; i < 200; i++) rows.push({ id: `${i}`, val: i });
|
|
await engine.insert('big', rows);
|
|
expect(await engine.count('big')).toBe(200);
|
|
const result = await engine.find('big', { table: 'big', orderBy: [{ column: 'val', direction: 'asc' }], limit: 10 });
|
|
expect(result).toHaveLength(10);
|
|
expect(result[0].val).toBe(0);
|
|
});
|
|
|
|
it('200 行全量更新', async () => {
|
|
await engine.createTable(createSchema('big2', { id: { type: 'string', primaryKey: true }, val: { type: 'number' } }));
|
|
for (let i = 0; i < 200; i++) await engine.insert('big2', [{ id: `${i}`, val: i }]);
|
|
await engine.update('big2', { table: 'big2' }, { val: 999 });
|
|
const rows = await engine.find('big2', { table: 'big2', limit: 5 });
|
|
expect(rows[0].val).toBe(999);
|
|
});
|
|
});
|
|
});
|