85 lines
2.8 KiB
TypeScript
85 lines
2.8 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('[4.1]comparison expressions $gt $gte $lt $lte $ne', 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: 50.0 },
|
|
{ id: id2, areaId: v4(), ownerId: v4(), district: '上海', size: 100.0 },
|
|
{ id: id3, areaId: v4(), ownerId: v4(), district: '北京', size: 150.0 },
|
|
]
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
const ids = [id1, id2, id3];
|
|
|
|
// $gt: size > 100
|
|
const r1 = await context.select('house', {
|
|
data: { id: 1 },
|
|
filter: {
|
|
id: { $in: ids },
|
|
$expr: { $gt: [{ '#attr': 'size' }, 100] }
|
|
}
|
|
}, {});
|
|
assert(r1.length === 1 && r1[0].id === id3, `$gt failed`);
|
|
|
|
// $gte: size >= 100
|
|
const r2 = await context.select('house', {
|
|
data: { id: 1 },
|
|
filter: {
|
|
id: { $in: ids },
|
|
$expr: { $gte: [{ '#attr': 'size' }, 100] }
|
|
}
|
|
}, {});
|
|
assert(r2.length === 2, `$gte failed, expected 2, got ${r2.length}`);
|
|
|
|
// $lt: size < 100
|
|
const r3 = await context.select('house', {
|
|
data: { id: 1 },
|
|
filter: {
|
|
id: { $in: ids },
|
|
$expr: { $lt: [{ '#attr': 'size' }, 100] }
|
|
}
|
|
}, {});
|
|
assert(r3.length === 1 && r3[0].id === id1, `$lt failed`);
|
|
|
|
// $lte: size <= 100
|
|
const r4 = await context.select('house', {
|
|
data: { id: 1 },
|
|
filter: {
|
|
id: { $in: ids },
|
|
$expr: { $lte: [{ '#attr': 'size' }, 100] }
|
|
}
|
|
}, {});
|
|
assert(r4.length === 2, `$lte failed, expected 2, got ${r4.length}`);
|
|
|
|
// $ne: size != 100
|
|
const r5 = await context.select('house', {
|
|
data: { id: 1 },
|
|
filter: {
|
|
id: { $in: ids },
|
|
$expr: { $ne: [{ '#attr': 'size' }, 100] }
|
|
}
|
|
}, {});
|
|
assert(r5.length === 2, `$ne failed, expected 2, got ${r5.length}`);
|
|
});
|
|
|
|
} |