20260927215912
This commit is contained in:
1 parent
27e49e2e45
commit
f22e67fc85
294 files changed
+29247
-3576
No files matched your search
@@ -24,6 +24,18 @@ const fields = new Map([
|
||||
['status', { b_field: 'status', b_type: 'select' }],
|
||||
])
|
||||
|
||||
const pad2 = (number) => String(number).padStart(2, '0')
|
||||
|
||||
/**
|
||||
* 本地墙钟串 → UTC 墙钟串。
|
||||
* datetime 条件拼 SQL 前会折算成 UTC(库中存 UTC,见后端 TimeUtils),
|
||||
* 断言用同一折算、不依赖运行环境时区。
|
||||
*/
|
||||
function toUtcWall(localText) {
|
||||
const date = new Date(String(localText).replace(' ', 'T'))
|
||||
return `${date.getUTCFullYear()}-${pad2(date.getUTCMonth() + 1)}-${pad2(date.getUTCDate())} ${pad2(date.getUTCHours())}:${pad2(date.getUTCMinutes())}:${pad2(date.getUTCSeconds())}`
|
||||
}
|
||||
|
||||
describe('查询条件 AST 构造', () => {
|
||||
it('按字段类型给出默认操作符:文本 like、日期 between、其余 eq', () => {
|
||||
expect(defaultOperator({ b_type: 'input' })).toBe('like')
|
||||
@@ -62,13 +74,13 @@ describe('查询条件 AST 构造', () => {
|
||||
{ type: 'condition', field: 'created', operator: 'ge', value: ['2026-01-01', ''] },
|
||||
fields,
|
||||
),
|
||||
).toBe("[created] >= N'2026-01-01 00:00:00'")
|
||||
).toBe(`[created] >= N'${toUtcWall('2026-01-01 00:00:00')}'`)
|
||||
expect(
|
||||
astToSql(
|
||||
{ type: 'condition', field: 'created', operator: 'le', value: ['', '2026-01-31'] },
|
||||
fields,
|
||||
),
|
||||
).toBe("[created] <= N'2026-01-31 23:59:59'")
|
||||
).toBe(`[created] <= N'${toUtcWall('2026-01-31 23:59:59')}'`)
|
||||
})
|
||||
|
||||
it('嵌套条件组按各自逻辑加括号,空组不产生条件', () => {
|
||||
@@ -113,19 +125,29 @@ describe('日期预设区间', () => {
|
||||
it('语义值 { preset } 只在组装 AST 时解析,配置不写死日期', () => {
|
||||
expect(isDatePresetValue({ preset: 'today' })).toBe(true)
|
||||
expect(isDatePresetValue(['2026-09-23'])).toBe(false)
|
||||
expect(resolveDatePresetValue({ preset: 'today' })).toEqual(['2026-09-23', '2026-09-23'])
|
||||
expect(resolveDatePresetValue('2026-09-23')).toBe('2026-09-23')
|
||||
// 不写死基准日:按实现同源解析「今天」,避免测试随日期流逝变成时间炸弹。
|
||||
// preset 由组装 AST 的入口(buildViewAst / buildQuickAst / buildFilterStateAst)解析,
|
||||
// astToSql 只认已解析的区间数组,所以这里走页签条件入口。
|
||||
const [start, end] = resolveDatePresetValue({ preset: 'today' })
|
||||
expect(start).toBe(end)
|
||||
expect(start).toMatch(/^\d{4}-\d{2}-\d{2}$/)
|
||||
expect(
|
||||
astToSql(
|
||||
{
|
||||
type: 'condition',
|
||||
field: 'created',
|
||||
operator: 'between',
|
||||
value: { preset: 'today' },
|
||||
},
|
||||
buildViewAst(
|
||||
{
|
||||
code: 'today',
|
||||
conditions: [
|
||||
{ field: 'created', operator: 'between', value: { preset: 'today' } },
|
||||
],
|
||||
},
|
||||
fields,
|
||||
),
|
||||
fields,
|
||||
),
|
||||
).toBe("[created] BETWEEN N'2026-09-23 00:00:00' AND N'2026-09-23 23:59:59'")
|
||||
).toBe(
|
||||
`[created] BETWEEN N'${toUtcWall(`${start} 00:00:00`)}' AND N'${toUtcWall(`${end} 23:59:59`)}'`,
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -140,8 +162,9 @@ describe('查询入口 → AST(页签 / 关键词 / 快捷筛选 / 更多筛
|
||||
],
|
||||
}
|
||||
const ast = buildViewAst(view, fields)
|
||||
const [start, end] = resolveDatePresetValue({ preset: 'today' })
|
||||
expect(astToSql(ast, fields)).toBe(
|
||||
"([status] = N'失败' AND [created] BETWEEN N'2026-09-23 00:00:00' AND N'2026-09-23 23:59:59')",
|
||||
`([status] = N'失败' AND [created] BETWEEN N'${toUtcWall(`${start} 00:00:00`)}' AND N'${toUtcWall(`${end} 23:59:59`)}')`,
|
||||
)
|
||||
expect(buildViewAst({ code: 'all' }, fields)).toBeNull()
|
||||
})
|
||||
@@ -188,9 +211,10 @@ describe('查询入口 → AST(页签 / 关键词 / 快捷筛选 / 更多筛
|
||||
status: '',
|
||||
created: { preset: 'this_month' },
|
||||
})
|
||||
const [monthStart, monthEnd] = resolveDatePresetValue({ preset: 'this_month' })
|
||||
expect(ast.children).toHaveLength(2)
|
||||
expect(astToSql(ast, fields)).toBe(
|
||||
"([name] LIKE N'%abc%' AND [created] BETWEEN N'2026-09-01 00:00:00' AND N'2026-09-30 23:59:59')",
|
||||
`([name] LIKE N'%abc%' AND [created] BETWEEN N'${toUtcWall(`${monthStart} 00:00:00`)}' AND N'${toUtcWall(`${monthEnd} 23:59:59`)}')`,
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user