Files
MetonaSqlark/tests/engine/aria-index-lookup.test.ts
T
thzxx 4c26882caa
CI / test (18.x) (push) Successful in 10m0s
CI / test (20.x) (push) Successful in 10m0s
CI / test (22.x) (push) Successful in 10m0s
CI / test (24.x) (push) Successful in 9m54s
release: v0.2.4 — 701 tests, 32 suites, 零死代码, 零空壳, 全模块接入
2026-07-27 22:18:08 +08:00

106 lines
4.0 KiB
TypeScript

/**
* AriaEngine 二级索引查询测试
* 全部 MemoryBackend,纯异步无定时器,不会卡死
*/
import { AriaEngine } from '../../src/engine/aria/index';
import { createSchema } from '../../src/table/schema';
describe('AriaEngine — 二级索引查询', () => {
let engine: AriaEngine;
beforeEach(async () => {
engine = new AriaEngine({ storageBackend: 'memory' });
await engine.open('idx-test', 1);
await engine.createTable(createSchema('users', {
id: { type: 'string', primaryKey: true },
name: { type: 'string', index: true },
age: { type: 'number', index: true },
email: { type: 'string', unique: true },
}));
await engine.insert('users', [
{ id: '1', name: 'Alice', age: 30, email: 'a@t.com' },
{ id: '2', name: 'Bob', age: 25, email: 'b@t.com' },
{ id: '3', name: 'Charlie', age: 35, email: 'c@t.com' },
{ id: '4', name: 'Alice', age: 30, email: 'a2@t.com' },
]);
});
afterEach(async () => { await engine.close(); });
it('索引 $eq 查询 — name 列', async () => {
const rows = await engine.find('users', { table: 'users', where: { name: 'Alice' } });
expect(rows).toHaveLength(2);
});
it('索引 $eq 查询 — age 列', async () => {
const rows = await engine.find('users', { table: 'users', where: { age: 30 } });
expect(rows).toHaveLength(2);
});
it('索引 $in 查询', async () => {
const rows = await engine.find('users', { table: 'users', where: { age: { $in: [25, 35] } } });
expect(rows).toHaveLength(2);
});
it('索引 $gt 查询', async () => {
const rows = await engine.find('users', { table: 'users', where: { age: { $gt: 28 } } });
expect(rows).toHaveLength(3);
});
it('索引 $lt 查询', async () => {
const rows = await engine.find('users', { table: 'users', where: { age: { $lt: 30 } } });
expect(rows).toHaveLength(1);
});
it('索引 $gte + $lte 范围', async () => {
const rows = await engine.find('users', { table: 'users', where: { age: { $gte: 25, $lte: 30 } } });
expect(rows).toHaveLength(3);
});
it('索引不存在的值返回空', async () => {
const rows = await engine.find('users', { table: 'users', where: { name: 'Nobody' } });
expect(rows).toHaveLength(0);
});
it('唯一索引 $eq 查询', async () => {
const rows = await engine.find('users', { table: 'users', where: { email: 'a@t.com' } });
expect(rows).toHaveLength(1);
expect(rows[0].id).toBe('1');
});
it('PK 等值优先走主索引', async () => {
const rows = await engine.find('users', { table: 'users', where: { id: '2' } });
expect(rows).toHaveLength(1);
expect(rows[0].name).toBe('Bob');
});
it('无索引列回退全表扫描', async () => {
// email 是唯一索引但查询用了 $like(不支持索引)
const rows = await engine.find('users', { table: 'users', where: { email: { $like: 'a%' } } });
expect(rows).toHaveLength(2);
});
it('插入后索引立即可查', async () => {
await engine.insert('users', [{ id: '5', name: 'Diana', age: 28, email: 'd@t.com' }]);
const rows = await engine.find('users', { table: 'users', where: { name: 'Diana' } });
expect(rows).toHaveLength(1);
});
it('删除后索引不再返回', async () => {
await engine.delete('users', { table: 'users', where: { id: '1' } });
const rows = await engine.find('users', { table: 'users', where: { name: 'Alice' } });
expect(rows).toHaveLength(1);
expect(rows[0].id).toBe('4');
});
it('更新后新旧索引均正确', async () => {
await engine.update('users', { table: 'users', where: { id: '1' } }, { name: 'Alicia', age: 31 });
const old = await engine.find('users', { table: 'users', where: { name: 'Alice' } });
expect(old).toHaveLength(1);
const updated = await engine.find('users', { table: 'users', where: { name: 'Alicia' } });
expect(updated).toHaveLength(1);
const byAge = await engine.find('users', { table: 'users', where: { age: 31 } });
expect(byAge).toHaveLength(1);
});
});