oak-db/test/testcase/aggr.ts

323 lines
12 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('[7.1]aggregation $$count', async () => {
const store = storeGetter();
const context = new TestContext(store);
await context.begin();
const systemId = v4();
await store.operate('system', {
id: v4(),
action: 'create',
data: {
id: systemId,
name: 'test-count',
description: 'test',
folder: '/test',
application$system: [
{ id: v4(), action: 'create', data: { id: v4(), name: 'app1', description: 't1', type: 'web' } },
{ id: v4(), action: 'create', data: { id: v4(), name: 'app2', description: 't2', type: 'web' } },
{ id: v4(), action: 'create', data: { id: v4(), name: 'app3', description: 't3', type: 'wechatMp' } },
]
}
} as EntityDict['system']['CreateSingle'], context, {});
await context.commit();
const result = await store.aggregate('application', {
data: {
'#aggr': { systemId: 1 },
'#count-1': { id: 1 }
},
filter: { systemId }
}, context, {});
assert(result.length === 1, `Expected 1 group`);
assert(result[0]['#count-1'] === 3, `Expected count 3, got ${result[0]['#count-1']}`);
});
it('[1.17]aggregation functions $$sum, $$max, $$min, $$avg', async () => {
const store = storeGetter();
const context = new TestContext(store);
await context.begin();
const areaId = v4();
const ownerId = v4();
// 创建多个房屋记录用于测试聚合函数
await store.operate('house', {
id: v4(),
action: 'create',
data: [
{
id: v4(),
areaId,
ownerId,
district: '杭州',
size: 80.0,
},
{
id: v4(),
areaId,
ownerId,
district: '杭州',
size: 100.0,
},
{
id: v4(),
areaId,
ownerId,
district: '杭州',
size: 120.0,
},
{
id: v4(),
areaId,
ownerId,
district: '上海',
size: 150.0,
},
]
}, context, {});
await context.commit();
// 测试 $$count
const countResult = await store.aggregate('house', {
data: {
'#aggr': {
district: 1,
},
'#count-1': {
id: 1,
}
},
filter: {
areaId,
},
}, context, {});
assert(countResult.length === 2, `Expected 2 groups, got ${countResult.length}`);
const hangzhouCount = countResult.find(r => r['#data']?.district === '杭州');
const shanghaiCount = countResult.find(r => r['#data']?.district === '上海');
assert(hangzhouCount && hangzhouCount['#count-1'] === 3, `Expected 3 houses in 杭州, got ${hangzhouCount?.['#count-1']}`);
assert(shanghaiCount && shanghaiCount['#count-1'] === 1, `Expected 1 house in 上海, got ${shanghaiCount?.['#count-1']}`);
// TODO: 下面的测试用例暂不支持,先注释掉
// 测试 $$sum
// const sumResult = await store.aggregate('house', {
// data: {
// '#aggr': {
// district: 1,
// },
// '#sum-1': {
// $$sum: {
// '#attr': 'size',
// },
// }
// },
// filter: {
// areaId,
// },
// }, context, {});
// const hangzhouSum = sumResult.find(r => r['#data']?.district === '杭州');
// assert(hangzhouSum && hangzhouSum['#sum-1'] === 300, `Expected sum 300 for 杭州, got ${hangzhouSum?.['#sum-1']}`);
// 测试 $$max
// const maxResult = await store.aggregate('house', {
// data: {
// '#aggr': {
// district: 1,
// },
// '#max-1': {
// $$max: {
// '#attr': 'size',
// },
// }
// },
// filter: {
// areaId,
// },
// }, context, {});
// const hangzhouMax = maxResult.find(r => r['#data']?.district === '杭州');
// assert(hangzhouMax && hangzhouMax['#max-1'] === 120, `Expected max 120 for 杭州, got ${hangzhouMax?.['#max-1']}`);
// 测试 $$min
// const minResult = await store.aggregate('house', {
// data: {
// '#aggr': {
// district: 1,
// },
// '#min-1': {
// $$min: {
// '#attr': 'size',
// },
// }
// },
// filter: {
// areaId,
// },
// }, context, {});
// const hangzhouMin = minResult.find(r => r['#data']?.district === '杭州');
// assert(hangzhouMin && hangzhouMin['#min-1'] === 80, `Expected min 80 for 杭州, got ${hangzhouMin?.['#min-1']}`);
// 测试 $$avg
// const avgResult = await store.aggregate('house', {
// data: {
// '#aggr': {
// district: 1,
// },
// '#avg-1': {
// $$avg: {
// '#attr': 'size',
// },
// }
// },
// filter: {
// areaId,
// },
// }, context, {});
// const hangzhouAvg = avgResult.find(r => r['#data']?.district === '杭州');
// assert(hangzhouAvg && hangzhouAvg['#avg-1'] === 100, `Expected avg 100 for 杭州, got ${hangzhouAvg?.['#avg-1']}`);
});
// // TODO: 下面聚合暂不支持
// it('[7.2]aggregation $$sum', async () => {
// const context = new TestContext(store);
// await context.begin();
// const areaId = v4();
// await store.operate('house', {
// id: v4(),
// action: 'create',
// data: [
// { id: v4(), areaId, ownerId: v4(), district: '杭州', size: 100.0 },
// { id: v4(), areaId, ownerId: v4(), district: '杭州', size: 150.0 },
// { id: v4(), areaId, ownerId: v4(), district: '杭州', size: 200.0 },
// ]
// }, context, {});
// await context.commit();
// const result = await store.aggregate('house', {
// data: {
// '#aggr': { areaId: 1 },
// '#sum-1': { $$sum: { '#attr': 'size' } }
// },
// filter: { areaId }
// }, context, {});
// assert(result.length === 1, `Expected 1 group`);
// assert(result[0]['#sum-1'] === 450, `Expected sum 450, got ${result[0]['#sum-1']}`);
// });
// it('[7.3]aggregation $$max $$min', async () => {
// const context = new TestContext(store);
// await context.begin();
// const areaId = v4();
// await store.operate('house', {
// id: v4(),
// action: 'create',
// data: [
// { id: v4(), areaId, ownerId: v4(), district: '杭州', size: 80.0 },
// { id: v4(), areaId, ownerId: v4(), district: '杭州', size: 120.0 },
// { id: v4(), areaId, ownerId: v4(), district: '杭州', size: 200.0 },
// ]
// }, context, {});
// await context.commit();
// const result = await store.aggregate('house', {
// data: {
// '#aggr': { areaId: 1 },
// '#max-1': { $$max: { '#attr': 'size' } },
// '#min-1': { $$min: { '#attr': 'size' } }
// },
// filter: { areaId }
// }, context, {});
// assert(result.length === 1, `Expected 1 group`);
// assert(result[0]['#max-1'] === 200, `Expected max 200, got ${result[0]['#max-1']}`);
// assert(result[0]['#min-1'] === 80, `Expected min 80, got ${result[0]['#min-1']}`);
// });
// it('[7.4]aggregation $$avg', async () => {
// const context = new TestContext(store);
// await context.begin();
// const areaId = v4();
// await store.operate('house', {
// id: v4(),
// action: 'create',
// data: [
// { id: v4(), areaId, ownerId: v4(), district: '杭州', size: 100.0 },
// { id: v4(), areaId, ownerId: v4(), district: '杭州', size: 200.0 },
// { id: v4(), areaId, ownerId: v4(), district: '杭州', size: 300.0 },
// ]
// }, context, {});
// await context.commit();
// const result = await store.aggregate('house', {
// data: {
// '#aggr': { areaId: 1 },
// '#avg-1': { $$avg: { '#attr': 'size' } }
// },
// filter: { areaId }
// }, context, {});
// assert(result.length === 1, `Expected 1 group`);
// assert(result[0]['#avg-1'] === 200, `Expected avg 200, got ${result[0]['#avg-1']}`);
// });
// it('[7.5]aggregation with multiple groups', async () => {
// const context = new TestContext(store);
// await context.begin();
// const areaId = v4();
// await store.operate('house', {
// id: v4(),
// action: 'create',
// data: [
// { id: v4(), areaId, ownerId: v4(), district: '杭州', size: 100.0 },
// { id: v4(), areaId, ownerId: v4(), district: '杭州', size: 150.0 },
// { id: v4(), areaId, ownerId: v4(), district: '上海', size: 200.0 },
// { id: v4(), areaId, ownerId: v4(), district: '上海', size: 300.0 },
// { id: v4(), areaId, ownerId: v4(), district: '北京', size: 250.0 },
// ]
// }, context, {});
// await context.commit();
// const result = await store.aggregate('house', {
// data: {
// '#aggr': { district: 1 },
// '#count-1': { id: 1 },
// '#sum-1': { $$sum: { '#attr': 'size' } },
// '#avg-1': { $$avg: { '#attr': 'size' } }
// },
// filter: { areaId }
// }, context, {});
// assert(result.length === 3, `Expected 3 groups, got ${result.length}`);
// const hz = result.find(r => r['#data']?.district === '杭州');
// const sh = result.find(r => r['#data']?.district === '上海');
// const bj = result.find(r => r['#data']?.district === '北京');
// assert(hz && hz['#count-1'] === 2 && hz['#sum-1'] === 250, `杭州 aggregation failed`);
// assert(sh && sh['#count-1'] === 2 && sh['#sum-1'] === 500, `上海 aggregation failed`);
// assert(bj && bj['#count-1'] === 1 && bj['#sum-1'] === 250, `北京 aggregation failed`);
// });
}