163 lines
5.4 KiB
TypeScript
163 lines
5.4 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('[3.1]string expression $endsWith', async () => {
|
|
const store = storeGetter();
|
|
const context = new TestContext(store);
|
|
await context.begin();
|
|
|
|
const id1 = v4();
|
|
const id2 = v4();
|
|
await store.operate('user', {
|
|
id: v4(),
|
|
action: 'create',
|
|
data: [
|
|
{
|
|
id: id1,
|
|
name: 'test_user',
|
|
nickname: 'test', idState: 'unverified', userState: 'normal'
|
|
},
|
|
{
|
|
id: id2,
|
|
name: 'admin_role',
|
|
nickname: 'admin', idState: 'unverified', userState: 'normal'
|
|
},
|
|
]
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 $endsWith: name ends with '_user'
|
|
const r1 = await context.select('user', {
|
|
data: { id: 1, name: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2] },
|
|
$expr: {
|
|
$endsWith: [{ '#attr': 'name' }, '_user']
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 1 && r1[0].id === id1, `Expected id1, got ${r1.map(r => r.id)}`);
|
|
});
|
|
|
|
it('[3.2]string expression $includes', async () => {
|
|
const store = storeGetter();
|
|
const context = new TestContext(store);
|
|
await context.begin();
|
|
|
|
const id1 = v4();
|
|
const id2 = v4();
|
|
await store.operate('user', {
|
|
id: v4(),
|
|
action: 'create',
|
|
data: [
|
|
{
|
|
id: id1,
|
|
name: 'test_admin_user',
|
|
nickname: 'test', idState: 'unverified', userState: 'normal'
|
|
},
|
|
{
|
|
id: id2,
|
|
name: 'guest_role',
|
|
nickname: 'guest', idState: 'unverified', userState: 'normal'
|
|
},
|
|
]
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 $includes: name includes 'admin'
|
|
const r1 = await context.select('user', {
|
|
data: { id: 1, name: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2] },
|
|
$expr: {
|
|
$includes: [{ '#attr': 'name' }, 'admin']
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 1 && r1[0].id === id1, `Expected id1, got ${r1.map(r => r.id)}`);
|
|
});
|
|
|
|
it('[3.3]string expression $concat', async () => {
|
|
const store = storeGetter();
|
|
const context = new TestContext(store);
|
|
await context.begin();
|
|
|
|
const id1 = v4();
|
|
await store.operate('user', {
|
|
id: v4(),
|
|
action: 'create',
|
|
data: {
|
|
id: id1,
|
|
name: 'hello',
|
|
nickname: 'world', idState: 'unverified', userState: 'normal'
|
|
}
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 $concat: concat(name, '_', nickname) = 'hello_world'
|
|
const r1 = await context.select('user', {
|
|
data: { id: 1, name: 1, nickname: 1 },
|
|
filter: {
|
|
id: id1,
|
|
$expr: {
|
|
$eq: [
|
|
{ $concat: [{ '#attr': 'name' }, '_', { '#attr': 'nickname' }] },
|
|
'hello_world'
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 1, `Expected 1 row, got ${r1.length}`);
|
|
});
|
|
|
|
|
|
// ==================== 字符串 $startsWith 在表达式中测试 ====================
|
|
|
|
it('[10.2]string $startsWith in expression', async () => {
|
|
const store = storeGetter();
|
|
const context = new TestContext(store);
|
|
await context.begin();
|
|
|
|
const id1 = v4();
|
|
const id2 = v4();
|
|
const id3 = v4();
|
|
|
|
await store.operate('user', {
|
|
id: v4(),
|
|
action: 'create',
|
|
data: [
|
|
{ id: id1, name: 'test_admin', nickname: 'test', idState: 'unverified', userState: 'normal' },
|
|
{ id: id2, name: 'test_user', nickname: 'test', idState: 'unverified', userState: 'normal' },
|
|
{ id: id3, name: 'admin_role', nickname: 'admin1', idState: 'unverified', userState: 'normal' },
|
|
]
|
|
}, context, {});
|
|
await context.commit();
|
|
|
|
// 测试 name 以 nickname 开头
|
|
const r1 = await context.select('user', {
|
|
data: { id: 1, name: 1, nickname: 1 },
|
|
filter: {
|
|
id: { $in: [id1, id2, id3] },
|
|
$expr: {
|
|
$startsWith: [
|
|
{ '#attr': 'name' },
|
|
{ '#attr': 'nickname' }
|
|
]
|
|
}
|
|
}
|
|
}, {});
|
|
assert(r1.length === 2, `$startsWith with two attrs failed, expected 2, got ${r1.length}`);
|
|
const ids = r1.map(r => r.id);
|
|
assert(ids.includes(id1) && ids.includes(id2), `Expected id1 and id2`);
|
|
});
|
|
|
|
} |