oak-db/test/testcase/projection.ts

91 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('[11.1]expression in projection', async () => {
const store = storeGetter();
const context = new TestContext(store);
await context.begin();
const id1 = v4();
await store.operate('house', {
id: v4(),
action: 'create',
data: {
id: id1,
areaId: v4(),
ownerId: v4(),
district: '杭州',
size: 100.0,
}
}, context, {});
await context.commit();
// 在投影中使用表达式计算 size * 2
const r1 = await context.select('house', {
data: {
id: 1,
size: 1,
$expr: {
$multiply: [{ '#attr': 'size' }, 2]
}
},
filter: { id: id1 }
}, {});
assert(r1.length === 1, `Expression in projection failed`);
assert(r1[0].$expr == 200, `Expected $expr = 200, got ${r1[0].$expr}`);
});
it('[11.2]multiple expressions in projection', async () => {
const store = storeGetter();
const context = new TestContext(store);
await context.begin();
const id1 = v4();
await store.operate('house', {
id: v4(),
action: 'create',
data: {
id: id1,
areaId: v4(),
ownerId: v4(),
district: '杭州',
size: 100.0,
}
}, context, {});
await context.commit();
// 多个表达式在投影中
const r1 = await context.select('house', {
data: {
id: 1,
size: 1,
$expr1: {
$multiply: [{ '#attr': 'size' }, 2]
},
$expr2: {
$add: [{ '#attr': 'size' }, 50]
},
$expr3: {
$gt: [{ '#attr': 'size' }, 80]
}
},
filter: { id: id1 }
}, {});
assert(r1.length === 1, `Multiple expressions failed`);
assert(r1[0].$expr1 == 200, `Expected $expr1 = 200`);
assert(r1[0].$expr2 == 150, `Expected $expr2 = 150`);
assert(r1[0].$expr3 == true || r1[0].$expr3 == 1, `Expected $expr3 = true`);
});
}