119 lines
3.9 KiB
TypeScript
119 lines
3.9 KiB
TypeScript
import { TestContext } from "../Context";
|
|
import { EntityDict } from "../test-app-domain";
|
|
import { v4 } from 'uuid';
|
|
import assert from 'assert';
|
|
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
|
|
import { describe, it, before, after } from 'mocha';
|
|
import { DbStore } from "../../lib/types/dbStore";
|
|
|
|
export default (storeGetter: () => DbStore<EntityDict, TestContext>) => {
|
|
|
|
// ==================== 布尔运算符测试 ====================
|
|
|
|
it('[5.1]boolean expression $true $false', async () => {
|
|
const store = storeGetter();
|
|
const context = new TestContext(store);
|
|
await context.begin();
|
|
|
|
const id1 = v4();
|
|
const id2 = v4();
|
|
await store.operate('house', {
|
|
id: v4(),
|
|
action: 'create',
|
|
data: [
|
|
{ id: id1, areaId: v4(), ownerId: v4(), district: '杭州', size: 100.0 },
|
|
{ id: id2, areaId: v4(), ownerId: v4(), district: '上海', size: 50.0 },
|
|
]
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// $true: 检查表达式为真
|
|
const r1 = await context.select('house', {
|
|
data: { id: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2] },
|
|
$expr: {
|
|
$true: { $gt: [{ '#attr': 'size' }, 80] }
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 1 && r1[0].id === id1, `$true failed`);
|
|
|
|
// $false: 检查表达式为假
|
|
const r2 = await context.select('house', {
|
|
data: { id: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2] },
|
|
$expr: {
|
|
$false: { $gt: [{ '#attr': 'size' }, 80] }
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r2.length === 1 && r2[0].id === id2, `$false failed`);
|
|
});
|
|
|
|
it('[5.2]logic expression $and $or $not', async () => {
|
|
const store = storeGetter();
|
|
const context = new TestContext(store);
|
|
await context.begin();
|
|
|
|
const id1 = v4();
|
|
const id2 = v4();
|
|
const id3 = v4();
|
|
await store.operate('house', {
|
|
id: v4(),
|
|
action: 'create',
|
|
data: [
|
|
{ id: id1, areaId: v4(), ownerId: v4(), district: '杭州', size: 80.0 },
|
|
{ id: id2, areaId: v4(), ownerId: v4(), district: '上海', size: 120.0 },
|
|
{ id: id3, areaId: v4(), ownerId: v4(), district: '北京', size: 100.0 },
|
|
]
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
const ids = [id1, id2, id3];
|
|
|
|
// $and: size > 70 AND size < 110
|
|
const r1 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: { $in: ids },
|
|
$expr: {
|
|
$and: [
|
|
{ $gt: [{ '#attr': 'size' }, 70] },
|
|
{ $lt: [{ '#attr': 'size' }, 110] }
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 2, `$and failed, expected 2, got ${r1.length}`);
|
|
|
|
// $or: size < 90 OR size > 110
|
|
const r2 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: { $in: ids },
|
|
$expr: {
|
|
$or: [
|
|
{ $lt: [{ '#attr': 'size' }, 90] },
|
|
{ $gt: [{ '#attr': 'size' }, 110] }
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r2.length === 2, `$or failed, expected 2, got ${r2.length}`);
|
|
|
|
// $not: NOT (size = 100)
|
|
const r3 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: { $in: ids },
|
|
$expr: {
|
|
$not: { $eq: [{ '#attr': 'size' }, 100] }
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r3.length === 2, `$not failed, expected 2, got ${r3.length}`);
|
|
});
|
|
|
|
} |