423 lines
12 KiB
TypeScript
423 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('[2.1]math expression $add', 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: 80.5,
|
|
},
|
|
{
|
|
id: id2,
|
|
areaId: v4(),
|
|
ownerId: v4(),
|
|
district: '上海',
|
|
size: 100.0,
|
|
},
|
|
]
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 $add: size + 20 > 100
|
|
const r1 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2] },
|
|
$expr: {
|
|
$gt: [
|
|
{
|
|
$add: [{ '#attr': 'size' }, 20]
|
|
},
|
|
100
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 2, `Expected 2 rows, got ${r1.length}`);
|
|
|
|
// 测试多参数 $add: size + 10 + 20 > 120
|
|
const r2 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2] },
|
|
$expr: {
|
|
$gt: [
|
|
{
|
|
$add: [{ '#attr': 'size' }, 10, 20]
|
|
},
|
|
120
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r2.length === 1 && r2[0].id === id2, `Expected id2, got ${r2.map(r => r.id)}`);
|
|
});
|
|
|
|
it('[2.2]math expression $subtract', 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: 80.0,
|
|
},
|
|
{
|
|
id: id2,
|
|
areaId: v4(),
|
|
ownerId: v4(),
|
|
district: '上海',
|
|
size: 120.0,
|
|
},
|
|
]
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 $subtract: size - 50 > 50
|
|
const r1 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2] },
|
|
$expr: {
|
|
$gt: [
|
|
{
|
|
$subtract: [{ '#attr': 'size' }, 50]
|
|
},
|
|
50
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 1 && r1[0].id === id2, `Expected id2, got ${r1.map(r => r.id)}`);
|
|
});
|
|
|
|
it('[2.3]math expression $multiply', 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: 50.0,
|
|
},
|
|
{
|
|
id: id2,
|
|
areaId: v4(),
|
|
ownerId: v4(),
|
|
district: '上海',
|
|
size: 100.0,
|
|
},
|
|
]
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 $multiply: size * 2 >= 200
|
|
const r1 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2] },
|
|
$expr: {
|
|
$gte: [
|
|
{
|
|
$multiply: [{ '#attr': 'size' }, 2]
|
|
},
|
|
200
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 1 && r1[0].id === id2, `Expected id2, got ${r1.map(r => r.id)}`);
|
|
|
|
// 测试多参数 $multiply: size * 2 * 3 > 500
|
|
const r2 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2] },
|
|
$expr: {
|
|
$gt: [
|
|
{
|
|
$multiply: [{ '#attr': 'size' }, 2, 3]
|
|
},
|
|
500
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r2.length === 1 && r2[0].id === id2, `Expected id2, got ${r2.map(r => r.id)}`);
|
|
});
|
|
|
|
it('[2.4]math expression $divide', 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: 80.0,
|
|
},
|
|
{
|
|
id: id2,
|
|
areaId: v4(),
|
|
ownerId: v4(),
|
|
district: '上海',
|
|
size: 200.0,
|
|
},
|
|
]
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 $divide: size / 2 > 50
|
|
const r1 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2] },
|
|
$expr: {
|
|
$gt: [
|
|
{
|
|
$divide: [{ '#attr': 'size' }, 2]
|
|
},
|
|
50
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 1 && r1[0].id === id2, `Expected id2, got ${r1.map(r => r.id)}`);
|
|
});
|
|
|
|
it('[2.5]math expression $abs', 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: 80.0,
|
|
},
|
|
{
|
|
id: id2,
|
|
areaId: v4(),
|
|
ownerId: v4(),
|
|
district: '上海',
|
|
size: 120.0,
|
|
},
|
|
]
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 $abs: abs(size - 100) <= 20
|
|
const r1 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2] },
|
|
$expr: {
|
|
$lte: [
|
|
{
|
|
$abs: {
|
|
$subtract: [{ '#attr': 'size' }, 100]
|
|
}
|
|
},
|
|
20
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 2, `Expected 2 rows, got ${r1.length}`);
|
|
});
|
|
|
|
it('[2.6]math expression $round', 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: 80.567,
|
|
}
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 $round: round(size, 1) = 80.6
|
|
const r1 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: id1,
|
|
$expr: {
|
|
$eq: [
|
|
{
|
|
$round: [{ '#attr': 'size' }, 1]
|
|
},
|
|
80.6
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 1, `Expected 1 row, got ${r1.length}`);
|
|
|
|
// 测试 $round: round(size, 0) = 81
|
|
const r2 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: id1,
|
|
$expr: {
|
|
$eq: [
|
|
{
|
|
$round: [{ '#attr': 'size' }, 0]
|
|
},
|
|
81
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r2.length === 1, `Expected 1 row, got ${r2.length}`);
|
|
});
|
|
|
|
it('[2.7]math expression $ceil and $floor', 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: 80.3,
|
|
}
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 $ceil: ceil(size) = 81
|
|
const r1 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: id1,
|
|
$expr: {
|
|
$eq: [
|
|
{ $ceil: { '#attr': 'size' } },
|
|
81
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 1, `Expected 1 row for ceil, got ${r1.length}`);
|
|
|
|
// 测试 $floor: floor(size) = 80
|
|
const r2 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: id1,
|
|
$expr: {
|
|
$eq: [
|
|
{ $floor: { '#attr': 'size' } },
|
|
80
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r2.length === 1, `Expected 1 row for floor, got ${r2.length}`);
|
|
});
|
|
|
|
it('[2.8]math expression $pow', 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: 10.0,
|
|
}
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 $pow: pow(size, 2) = 100
|
|
const r1 = await context.select('house', {
|
|
data: { id: 1, size: 1 },
|
|
filter: {
|
|
id: id1,
|
|
$expr: {
|
|
$eq: [
|
|
{ $pow: [{ '#attr': 'size' }, 2] },
|
|
100
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 1, `Expected 1 row, got ${r1.length}`);
|
|
});
|
|
|
|
} |