@veltra/sheet 2.1.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/report/binding-badge.js +76 -0
- package/dist/components/report/designer/cell-coords.js +46 -0
- package/dist/components/report/designer/col-widths.js +15 -0
- package/dist/components/report/designer/conditional-rules/helpers.js +139 -0
- package/dist/components/report/designer/drop-highlight-overlay.js +76 -0
- package/dist/components/report/designer/float-panel.js +385 -0
- package/dist/components/report/designer/role.js +17 -0
- package/dist/components/report/designer/rule-preview.js +33 -0
- package/dist/components/report/designer/rule-row.js +269 -0
- package/dist/components/report/designer/rules-dialog.js +135 -0
- package/dist/components/report/designer/topology-overlay.js +122 -0
- package/dist/components/report/designer/topology.js +64 -0
- package/dist/components/report/designer/use-grid-overlay.js +75 -0
- package/dist/components/report/designer-hub.js +177 -0
- package/dist/components/report/field-panel-helpers.js +33 -0
- package/dist/components/report/field-panel.js +132 -0
- package/dist/components/report/filter-bar.js +74 -0
- package/dist/components/report/hub-connection-form.js +145 -0
- package/dist/components/report/hub-dataset-editor.js +394 -0
- package/dist/components/report/report-designer.js +343 -0
- package/dist/components/report/report-viewer.js +111 -0
- package/dist/components/report/style.d.ts +17 -0
- package/dist/components/report/style.js +18 -0
- package/dist/components/report/style2.css +869 -0
- package/dist/components/report/use-report-designer.js +360 -0
- package/dist/components/report/use-report-viewer.js +50 -0
- package/dist/{vue → components/sheet}/formula-bar.js +1 -1
- package/dist/{vue → components/sheet}/formula-suggest-list.js +1 -1
- package/dist/{vue → components/sheet}/import-file.js +1 -1
- package/dist/{vue → components/sheet}/insert-count-menu-item.js +1 -1
- package/dist/{vue → components/sheet}/insert-image.js +1 -1
- package/dist/{vue → components/sheet}/popup-helpers.js +1 -1
- package/dist/{vue → components/sheet}/popups/border-popup.js +1 -1
- package/dist/{vue → components/sheet}/popups/border-preset-glyph.js +1 -1
- package/dist/{vue → components/sheet}/popups/export-popup.js +2 -2
- package/dist/{vue → components/sheet}/popups/fill-color-popup.js +1 -1
- package/dist/{vue → components/sheet}/popups/find-popup.js +1 -1
- package/dist/{vue → components/sheet}/popups/font-color-popup.js +1 -1
- package/dist/{vue → components/sheet}/popups/font-size-popup.js +1 -1
- package/dist/{vue → components/sheet}/popups/import.worker.d.ts +1 -1
- package/dist/{vue → components/sheet}/popups/import.worker.js +1 -1
- package/dist/{vue → components/sheet}/popups/insert-image-popup.js +1 -1
- package/dist/{vue → components/sheet}/sheet-context-menu.js +1 -1
- package/dist/{vue → components/sheet}/sheet-tabs.js +1 -1
- package/dist/{vue → components/sheet}/sheet-toolbar.js +1 -1
- package/dist/{vue → components/sheet}/sheet.js +8 -1
- package/dist/{vue → components/sheet}/use-find-replace.js +1 -1
- package/dist/{vue → components/sheet}/use-formula-ref-select.js +1 -1
- package/dist/{vue → components/sheet}/use-sheet-grid.js +10 -2
- package/dist/{vue → components/sheet}/use-sheet-state.js +3 -3
- package/dist/{vue → components/sheet}/use-sheet-tabs-bar.js +1 -1
- package/dist/{vue → components/sheet}/use-tool-groups.js +2 -2
- package/dist/{vue → components/sheet}/use-tool-popup.js +2 -2
- package/dist/{vue → components/sheet}/use-toolbar-scroll.js +1 -1
- package/dist/index.d.ts +17 -3
- package/dist/index.js +13 -3
- package/dist/report/binding.d.ts +45 -0
- package/dist/report/binding.js +175 -0
- package/dist/report/connector.d.ts +66 -0
- package/dist/report/connector.js +159 -0
- package/dist/report/export-xlsx.d.ts +19 -0
- package/dist/report/export-xlsx.js +95 -0
- package/dist/report/filter-bar.d.ts +10 -0
- package/dist/report/filter-bar.js +31 -0
- package/dist/report/index.d.ts +12 -0
- package/dist/report/params.d.ts +13 -0
- package/dist/report/params.js +61 -0
- package/dist/report/render/aggregate.js +157 -0
- package/dist/report/render/builder.d.ts +12 -0
- package/dist/report/render/builder.js +139 -0
- package/dist/report/render/coordinate.d.ts +25 -0
- package/dist/report/render/coordinate.js +733 -0
- package/dist/report/render/index.d.ts +14 -0
- package/dist/report/render/index.js +21 -0
- package/dist/report/render/style-resolver.js +50 -0
- package/dist/report/render/template-index.d.ts +24 -0
- package/dist/report/render/template-index.js +137 -0
- package/dist/report/rules/evaluate.d.ts +25 -0
- package/dist/report/rules/evaluate.js +82 -0
- package/dist/report/rules/index.d.ts +2 -0
- package/dist/report/template.d.ts +59 -0
- package/dist/report/template.js +80 -0
- package/dist/report/types.d.ts +85 -0
- package/dist/tools/context.d.ts +1 -1
- package/dist/types/index.d.ts +3 -2
- package/dist/types/report-designer.d.ts +36 -0
- package/dist/types/report-viewer.d.ts +38 -0
- package/dist/types/sheet.d.ts +17 -1
- package/package.json +3 -5
- package/src/components/report/__test__/conditional-rules-helpers.test.ts +87 -0
- package/src/components/report/__test__/field-panel-helpers.test.ts +65 -0
- package/src/components/report/__test__/float-panel-position.test.ts +45 -0
- package/src/components/report/__test__/report-designer.test.ts +388 -0
- package/src/components/report/__test__/report-viewer.test.ts +334 -0
- package/src/components/report/__test__/topology.test.ts +143 -0
- package/src/components/report/__test__/use-report-designer.test.ts +565 -0
- package/src/components/report/binding-badge.ts +73 -0
- package/src/components/report/designer/cell-coords.ts +94 -0
- package/src/components/report/designer/col-widths.ts +33 -0
- package/src/components/report/designer/conditional-rules/helpers.ts +186 -0
- package/src/components/report/designer/drop-highlight-overlay.vue +74 -0
- package/src/components/report/designer/float-panel.vue +387 -0
- package/src/components/report/designer/role.ts +32 -0
- package/src/components/report/designer/rule-preview.vue +40 -0
- package/src/components/report/designer/rule-row.vue +252 -0
- package/src/components/report/designer/rules-dialog.vue +126 -0
- package/src/components/report/designer/topology-overlay.vue +127 -0
- package/src/components/report/designer/topology.ts +124 -0
- package/src/components/report/designer/use-grid-overlay.ts +113 -0
- package/src/components/report/designer-hub.vue +198 -0
- package/src/components/report/field-panel-helpers.ts +44 -0
- package/src/components/report/field-panel.vue +136 -0
- package/src/components/report/filter-bar.vue +77 -0
- package/src/components/report/hub-connection-form.vue +124 -0
- package/src/components/report/hub-dataset-editor.vue +369 -0
- package/src/components/report/index.ts +2 -0
- package/src/components/report/report-designer.vue +356 -0
- package/src/components/report/report-viewer.vue +128 -0
- package/src/components/report/style-designer.scss +583 -0
- package/src/components/report/style-hub.scss +292 -0
- package/src/components/report/style-viewer.scss +104 -0
- package/src/components/report/style.scss +3 -0
- package/src/components/report/style.ts +20 -0
- package/src/components/report/use-report-designer.ts +547 -0
- package/src/components/report/use-report-viewer.ts +79 -0
- package/src/{vue → components/sheet}/__test__/formula-bar.test.ts +37 -18
- package/src/{vue → components/sheet}/__test__/insert-image-popup.test.ts +1 -1
- package/src/{vue → components/sheet}/__test__/insert-image.test.ts +1 -1
- package/src/{vue → components/sheet}/__test__/sheet-component.test.ts +40 -3
- package/src/{vue → components/sheet}/__test__/sheet-context-menu.test.ts +1 -1
- package/src/{vue → components/sheet}/formula-bar.vue +1 -1
- package/src/{vue → components/sheet}/insert-image.ts +1 -1
- package/src/{vue → components/sheet}/popup-helpers.ts +1 -1
- package/src/{vue → components/sheet}/popups/border-popup.vue +1 -1
- package/src/{vue → components/sheet}/popups/export-popup.vue +2 -2
- package/src/{vue → components/sheet}/popups/fill-color-popup.vue +1 -1
- package/src/{vue → components/sheet}/popups/find-popup.vue +1 -1
- package/src/{vue → components/sheet}/popups/font-color-popup.vue +1 -1
- package/src/{vue → components/sheet}/popups/font-size-popup.vue +1 -1
- package/src/{vue → components/sheet}/popups/insert-image-popup.vue +1 -1
- package/src/{vue → components/sheet}/sheet-context-menu.ts +2 -2
- package/src/{vue → components/sheet}/sheet-toolbar.vue +2 -2
- package/src/{vue → components/sheet}/sheet.vue +4 -3
- package/src/{vue → components/sheet}/use-find-replace.ts +1 -1
- package/src/{vue → components/sheet}/use-sheet-grid.ts +7 -3
- package/src/{vue → components/sheet}/use-sheet-state.ts +3 -3
- package/src/{vue → components/sheet}/use-tool-groups.ts +2 -2
- package/src/{vue → components/sheet}/use-tool-popup.ts +2 -2
- package/src/index.ts +24 -188
- package/src/report/__test__/binding.test.ts +218 -0
- package/src/report/__test__/connector.test.ts +218 -0
- package/src/report/__test__/export-xlsx.test.ts +118 -0
- package/src/report/__test__/filter-bar.test.ts +58 -0
- package/src/report/__test__/params.test.ts +156 -0
- package/src/report/__test__/render.test.ts +728 -0
- package/src/report/__test__/rules.test.ts +131 -0
- package/src/report/__test__/template.test.ts +355 -0
- package/src/report/binding.ts +243 -0
- package/src/report/connector.ts +205 -0
- package/src/report/export-xlsx.ts +118 -0
- package/src/report/filter-bar.ts +36 -0
- package/src/report/index.ts +13 -0
- package/src/report/params.ts +96 -0
- package/src/report/render/__test__/aggregate.test.ts +16 -0
- package/src/report/render/__test__/coordinate.test.ts +405 -0
- package/src/report/render/aggregate.ts +240 -0
- package/src/report/render/builder.ts +211 -0
- package/src/report/render/coordinate.ts +1239 -0
- package/src/report/render/index.ts +30 -0
- package/src/report/render/style-resolver.ts +68 -0
- package/src/report/render/template-index.ts +200 -0
- package/src/report/rules/evaluate.ts +113 -0
- package/src/report/rules/index.ts +7 -0
- package/src/report/template.ts +141 -0
- package/src/report/types.ts +96 -0
- package/src/tools/builtin.ts +2 -2
- package/src/types/index.ts +2 -6
- package/src/types/report-designer.ts +36 -0
- package/src/types/report-viewer.ts +37 -0
- package/src/types/sheet.ts +22 -1
- /package/dist/{vue → components/sheet}/style.d.ts +0 -0
- /package/dist/{vue → components/sheet}/style.js +0 -0
- /package/dist/{vue → components/sheet}/style2.css +0 -0
- /package/dist/{vue → components/sheet}/use-formula-suggest.js +0 -0
- /package/src/{vue → components/sheet}/__test__/import-file.test.ts +0 -0
- /package/src/{vue → components/sheet}/__test__/insert-count-menu-item.test.ts +0 -0
- /package/src/{vue → components/sheet}/__test__/use-formula-ref-select.test.ts +0 -0
- /package/src/{vue → components/sheet}/__test__/use-formula-suggest.test.ts +0 -0
- /package/src/{vue → components/sheet}/formula-suggest-list.vue +0 -0
- /package/src/{vue → components/sheet}/import-file.ts +0 -0
- /package/src/{vue → components/sheet}/index.ts +0 -0
- /package/src/{vue → components/sheet}/insert-count-menu-item.vue +0 -0
- /package/src/{vue → components/sheet}/parsing.ts +0 -0
- /package/src/{vue → components/sheet}/popups/border-preset-glyph.vue +0 -0
- /package/src/{vue → components/sheet}/popups/import.worker.ts +0 -0
- /package/src/{vue → components/sheet}/sheet-tabs.vue +0 -0
- /package/src/{vue → components/sheet}/style.scss +0 -0
- /package/src/{vue → components/sheet}/style.ts +0 -0
- /package/src/{vue → components/sheet}/use-formula-ref-select.ts +0 -0
- /package/src/{vue → components/sheet}/use-formula-suggest.ts +0 -0
- /package/src/{vue → components/sheet}/use-sheet-tabs-bar.ts +0 -0
- /package/src/{vue → components/sheet}/use-toolbar-scroll.ts +0 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
evaluateCondition,
|
|
5
|
+
evaluateConditionalStyle,
|
|
6
|
+
resolveRuleCompareValue,
|
|
7
|
+
type ConditionalEvalContext
|
|
8
|
+
} from '../rules'
|
|
9
|
+
import type { ConditionalRule } from '../types'
|
|
10
|
+
|
|
11
|
+
function rule(
|
|
12
|
+
operator: ConditionalRule['operator'],
|
|
13
|
+
value: unknown,
|
|
14
|
+
style: ConditionalRule['style'],
|
|
15
|
+
extra?: Partial<ConditionalRule>
|
|
16
|
+
): ConditionalRule {
|
|
17
|
+
return { operator, value, style, ...extra }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function ctx(
|
|
21
|
+
cellValue: unknown,
|
|
22
|
+
record?: Record<string, unknown>,
|
|
23
|
+
bindingField = 'amount'
|
|
24
|
+
): ConditionalEvalContext {
|
|
25
|
+
return { cellValue, bindingField, record }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('report rules', () => {
|
|
29
|
+
describe('evaluateCondition', () => {
|
|
30
|
+
it('数值比较运算符', () => {
|
|
31
|
+
expect(evaluateCondition(150, rule('gt', 100, {}))).toBe(true)
|
|
32
|
+
expect(evaluateCondition(100, rule('gt', 100, {}))).toBe(false)
|
|
33
|
+
expect(evaluateCondition(100, rule('gte', 100, {}))).toBe(true)
|
|
34
|
+
expect(evaluateCondition(99, rule('lt', 100, {}))).toBe(true)
|
|
35
|
+
expect(evaluateCondition(100, rule('lte', 100, {}))).toBe(true)
|
|
36
|
+
expect(evaluateCondition(42, rule('eq', 42, {}))).toBe(true)
|
|
37
|
+
expect(evaluateCondition('42', rule('eq', 42, {}))).toBe(true)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('between 含端点范围比较', () => {
|
|
41
|
+
const between = rule('between', [100, 200], {})
|
|
42
|
+
expect(evaluateCondition(100, between)).toBe(true)
|
|
43
|
+
expect(evaluateCondition(200, between)).toBe(true)
|
|
44
|
+
expect(evaluateCondition(150, between)).toBe(true)
|
|
45
|
+
expect(evaluateCondition(99, between)).toBe(false)
|
|
46
|
+
expect(evaluateCondition(201, between)).toBe(false)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('contains 字符串包含', () => {
|
|
50
|
+
expect(evaluateCondition('华东-上海', rule('contains', '上海', {}))).toBe(true)
|
|
51
|
+
expect(evaluateCondition('华北', rule('contains', '上海', {}))).toBe(false)
|
|
52
|
+
expect(evaluateCondition(null, rule('contains', 'x', {}))).toBe(false)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('非数值比较在 gt 等运算符下返回 false', () => {
|
|
56
|
+
expect(evaluateCondition('abc', rule('gt', 1, {}))).toBe(false)
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
describe('resolveRuleCompareValue', () => {
|
|
61
|
+
it('缺省 field 时取 cellValue', () => {
|
|
62
|
+
const context = ctx(150, { amount: 150, region: '华东' })
|
|
63
|
+
expect(resolveRuleCompareValue(context, rule('gt', 100, {}))).toBe(150)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('指定 field 时取同记录另一字段', () => {
|
|
67
|
+
const context = ctx(150, { amount: 150, region: '华东' })
|
|
68
|
+
expect(resolveRuleCompareValue(context, rule('eq', '华东', {}, { field: 'region' }))).toBe(
|
|
69
|
+
'华东'
|
|
70
|
+
)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
describe('evaluateConditionalStyle', () => {
|
|
75
|
+
const baseStyle = { fill: { color: '#FFFFFF' }, font: { color: '#000000' } }
|
|
76
|
+
|
|
77
|
+
it('未命中规则时原样返回 baseStyle', () => {
|
|
78
|
+
const rules = [rule('gt', 1000, { fill: { color: '#FF0000' } })]
|
|
79
|
+
expect(evaluateConditionalStyle(ctx(50), baseStyle, rules)).toBe(baseStyle)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('命中单条规则时叠加样式增量', () => {
|
|
83
|
+
const rules = [rule('gt', 100, { fill: { color: '#FFCCCC' }, font: { bold: true } })]
|
|
84
|
+
expect(evaluateConditionalStyle(ctx(150), baseStyle, rules)).toEqual({
|
|
85
|
+
fill: { color: '#FFCCCC' },
|
|
86
|
+
font: { color: '#000000', bold: true }
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('跨字段求值命中时叠加样式', () => {
|
|
91
|
+
const rules = [rule('eq', '华东', { fill: { color: '#FFCCCC' } }, { field: 'region' })]
|
|
92
|
+
const context = ctx(100, { amount: 100, region: '华东' })
|
|
93
|
+
expect(evaluateConditionalStyle(context, baseStyle, rules)?.fill?.color).toBe('#FFCCCC')
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('scope: row 仅评估行级规则', () => {
|
|
97
|
+
const rules = [
|
|
98
|
+
rule('gt', 100, { fill: { color: '#FFCCCC' } }, { scope: 'row' }),
|
|
99
|
+
rule('gt', 100, { font: { bold: true } })
|
|
100
|
+
]
|
|
101
|
+
expect(evaluateConditionalStyle(ctx(150), baseStyle, rules, 'row')?.fill?.color).toBe(
|
|
102
|
+
'#FFCCCC'
|
|
103
|
+
)
|
|
104
|
+
expect(evaluateConditionalStyle(ctx(150), baseStyle, rules, 'cell')?.font?.bold).toBe(true)
|
|
105
|
+
expect(evaluateConditionalStyle(ctx(150), baseStyle, rules, 'cell')?.fill?.color).toBe(
|
|
106
|
+
'#FFFFFF'
|
|
107
|
+
)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('多规则按优先级合并,后命中规则覆盖同名字段', () => {
|
|
111
|
+
const rules = [
|
|
112
|
+
rule('gt', 0, { fill: { color: '#FFFFCC' } }),
|
|
113
|
+
rule('gt', 100, { fill: { color: '#FFCCCC' }, font: { bold: true } }),
|
|
114
|
+
rule('gt', 200, { font: { color: '#FF0000' } })
|
|
115
|
+
]
|
|
116
|
+
|
|
117
|
+
const style150 = evaluateConditionalStyle(ctx(150), baseStyle, rules)
|
|
118
|
+
expect(style150?.fill?.color).toBe('#FFCCCC')
|
|
119
|
+
expect(style150?.font).toEqual({ color: '#000000', bold: true })
|
|
120
|
+
|
|
121
|
+
const style250 = evaluateConditionalStyle(ctx(250), baseStyle, rules)
|
|
122
|
+
expect(style250?.fill?.color).toBe('#FFCCCC')
|
|
123
|
+
expect(style250?.font).toEqual({ color: '#FF0000', bold: true })
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('无规则或未传规则时返回 baseStyle', () => {
|
|
127
|
+
expect(evaluateConditionalStyle(ctx(100), baseStyle, undefined)).toBe(baseStyle)
|
|
128
|
+
expect(evaluateConditionalStyle(ctx(100), baseStyle, [])).toBe(baseStyle)
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
})
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { Sheet } from '@veltra/sheet-core'
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { REPORT_META_NAMESPACE } from '../binding'
|
|
5
|
+
import type { DataConnection, DataConnector } from '../connector'
|
|
6
|
+
import {
|
|
7
|
+
assertCompatibleTemplateVersion,
|
|
8
|
+
createReportTemplate,
|
|
9
|
+
fetchTemplateRecords,
|
|
10
|
+
getBoundDatasetIds,
|
|
11
|
+
getTemplateDatasets,
|
|
12
|
+
IncompatibleTemplateVersionError,
|
|
13
|
+
REPORT_TEMPLATE_VERSION,
|
|
14
|
+
resolveParamDefaults,
|
|
15
|
+
resolveTemplateParams,
|
|
16
|
+
type ReportDatasetDef,
|
|
17
|
+
type ReportTemplate
|
|
18
|
+
} from '../template'
|
|
19
|
+
import type { ParamValues, ReportBinding } from '../types'
|
|
20
|
+
|
|
21
|
+
// ---- 内联 fixtures ----
|
|
22
|
+
|
|
23
|
+
const MYSQL: DataConnection = {
|
|
24
|
+
id: 'c-mysql',
|
|
25
|
+
label: '本地 MySQL',
|
|
26
|
+
type: 'mysql',
|
|
27
|
+
host: '127.0.0.1',
|
|
28
|
+
port: 3306,
|
|
29
|
+
database: 'demo',
|
|
30
|
+
username: 'root',
|
|
31
|
+
password: 'secret'
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const PG: DataConnection = {
|
|
35
|
+
...MYSQL,
|
|
36
|
+
id: 'c-pg',
|
|
37
|
+
label: '本地 PG',
|
|
38
|
+
type: 'postgresql',
|
|
39
|
+
port: 5432
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// BETWEEN ${dateRange} 置于 SQL 末尾:后随 AND 时不会推断为 date-range(params.ts 既有推断规则)
|
|
43
|
+
const ORDERS_SQL =
|
|
44
|
+
'SELECT customer, region, amount, orderDate FROM orders ' +
|
|
45
|
+
'WHERE region = ${region} AND amount >= ${minAmount} ' +
|
|
46
|
+
'AND customer LIKE ${keyword} AND orderDate BETWEEN ${dateRange}'
|
|
47
|
+
|
|
48
|
+
const CUSTOMERS_SQL = 'SELECT id, region FROM customers WHERE region = ${region}'
|
|
49
|
+
|
|
50
|
+
const ORDERS_DATASET: ReportDatasetDef = {
|
|
51
|
+
id: 'orders',
|
|
52
|
+
label: '销售明细',
|
|
53
|
+
connection: MYSQL,
|
|
54
|
+
sql: ORDERS_SQL
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const CUSTOMERS_DATASET: ReportDatasetDef = {
|
|
58
|
+
id: 'customers',
|
|
59
|
+
label: '客户',
|
|
60
|
+
connection: PG,
|
|
61
|
+
sql: CUSTOMERS_SQL,
|
|
62
|
+
paramOverrides: {
|
|
63
|
+
region: {
|
|
64
|
+
label: '地区',
|
|
65
|
+
type: 'select',
|
|
66
|
+
defaultValue: '华东',
|
|
67
|
+
options: [
|
|
68
|
+
{ label: '华东', value: '华东' },
|
|
69
|
+
{ label: '华南', value: '华南' }
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const UNUSED_DATASET: ReportDatasetDef = {
|
|
76
|
+
id: 'unused',
|
|
77
|
+
label: '未绑定',
|
|
78
|
+
connection: MYSQL,
|
|
79
|
+
sql: 'SELECT id FROM t WHERE x = ${unusedParam}'
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function binding(dataset: string, field: string): ReportBinding {
|
|
83
|
+
return { dataset, field, aggregate: 'list', expand: 'down', preset: 'detail' }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 构造自包含 Report Template:SheetSnapshot + 内嵌数据集定义 */
|
|
87
|
+
function createTemplate(
|
|
88
|
+
datasets: ReportDatasetDef[],
|
|
89
|
+
bindings: Array<{ row: number; col: number; dataset: string; field: string }>
|
|
90
|
+
): ReportTemplate {
|
|
91
|
+
const sheet = new Sheet()
|
|
92
|
+
for (const item of bindings) {
|
|
93
|
+
sheet.setCellMeta(
|
|
94
|
+
{ row: item.row, col: item.col },
|
|
95
|
+
REPORT_META_NAMESPACE,
|
|
96
|
+
binding(item.dataset, item.field)
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
return createReportTemplate(sheet.snapshot(), datasets)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
describe('ReportTemplate version', () => {
|
|
103
|
+
it('createReportTemplate 写入当前版本', () => {
|
|
104
|
+
const template = createTemplate([ORDERS_DATASET], [])
|
|
105
|
+
expect(template.version).toBe(REPORT_TEMPLATE_VERSION)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('version 缺失时 assertCompatibleTemplateVersion 抛可读错误', () => {
|
|
109
|
+
const sheet = new Sheet()
|
|
110
|
+
const template = sheet.snapshot() as ReportTemplate
|
|
111
|
+
expect(() => assertCompatibleTemplateVersion(template)).toThrow(
|
|
112
|
+
IncompatibleTemplateVersionError
|
|
113
|
+
)
|
|
114
|
+
expect(() => assertCompatibleTemplateVersion(template)).toThrow(/缺少 version/)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('version 高于当前支持时 assertCompatibleTemplateVersion 抛可读错误', () => {
|
|
118
|
+
const template = createTemplate([ORDERS_DATASET], [])
|
|
119
|
+
template.version = REPORT_TEMPLATE_VERSION + 1
|
|
120
|
+
expect(() => assertCompatibleTemplateVersion(template)).toThrow(
|
|
121
|
+
IncompatibleTemplateVersionError
|
|
122
|
+
)
|
|
123
|
+
expect(() => assertCompatibleTemplateVersion(template)).toThrow(/高于当前支持/)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('version 为当前值时通过校验', () => {
|
|
127
|
+
const template = createTemplate([ORDERS_DATASET], [])
|
|
128
|
+
expect(() => assertCompatibleTemplateVersion(template)).not.toThrow()
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
describe('getTemplateDatasets', () => {
|
|
133
|
+
it('返回模板内嵌的数据集定义', () => {
|
|
134
|
+
const template = createTemplate([ORDERS_DATASET], [])
|
|
135
|
+
expect(getTemplateDatasets(template)).toEqual([ORDERS_DATASET])
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('未内嵌数据集(旧模板)回退为空数组', () => {
|
|
139
|
+
const sheet = new Sheet()
|
|
140
|
+
const template = sheet.snapshot() as ReportTemplate
|
|
141
|
+
expect(getTemplateDatasets(template)).toEqual([])
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
describe('getBoundDatasetIds', () => {
|
|
146
|
+
it('按绑定出现顺序返回去重的数据集 id(绑定即真相)', () => {
|
|
147
|
+
const template = createTemplate(
|
|
148
|
+
[],
|
|
149
|
+
[
|
|
150
|
+
{ row: 1, col: 0, dataset: 'orders', field: 'customer' },
|
|
151
|
+
{ row: 1, col: 1, dataset: 'orders', field: 'amount' },
|
|
152
|
+
{ row: 2, col: 0, dataset: 'customers', field: 'region' }
|
|
153
|
+
]
|
|
154
|
+
)
|
|
155
|
+
expect(getBoundDatasetIds(template)).toEqual(['orders', 'customers'])
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
it('无绑定时返回空数组', () => {
|
|
159
|
+
expect(getBoundDatasetIds(createTemplate([], []))).toEqual([])
|
|
160
|
+
})
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
describe('resolveTemplateParams', () => {
|
|
164
|
+
it('从实际绑定的数据集提取参数并集(未绑定数据集的参数不出现)', () => {
|
|
165
|
+
const template = createTemplate(
|
|
166
|
+
[ORDERS_DATASET, CUSTOMERS_DATASET, UNUSED_DATASET],
|
|
167
|
+
[
|
|
168
|
+
{ row: 1, col: 0, dataset: 'orders', field: 'customer' },
|
|
169
|
+
{ row: 2, col: 0, dataset: 'customers', field: 'region' }
|
|
170
|
+
]
|
|
171
|
+
)
|
|
172
|
+
const params = resolveTemplateParams(template)
|
|
173
|
+
expect(params.map((p) => p.id)).toEqual(['region', 'minAmount', 'keyword', 'dateRange'])
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('按 SQL 上下文推断控件类型(= 数值 / LIKE 文本 / BETWEEN 单参 date-range)', () => {
|
|
177
|
+
const template = createTemplate(
|
|
178
|
+
[ORDERS_DATASET],
|
|
179
|
+
[{ row: 1, col: 0, dataset: 'orders', field: 'customer' }]
|
|
180
|
+
)
|
|
181
|
+
const params = resolveTemplateParams(template)
|
|
182
|
+
const byId = new Map(params.map((p) => [p.id, p]))
|
|
183
|
+
expect(byId.get('region')?.type).toBe('number')
|
|
184
|
+
expect(byId.get('minAmount')?.type).toBe('number')
|
|
185
|
+
expect(byId.get('dateRange')?.type).toBe('date-range')
|
|
186
|
+
expect(byId.get('keyword')?.type).toBe('text')
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
it('同名参数合并,先见数据集为准', () => {
|
|
190
|
+
const ordersFirst = createTemplate(
|
|
191
|
+
[ORDERS_DATASET, CUSTOMERS_DATASET],
|
|
192
|
+
[
|
|
193
|
+
{ row: 1, col: 0, dataset: 'orders', field: 'customer' },
|
|
194
|
+
{ row: 2, col: 0, dataset: 'customers', field: 'region' }
|
|
195
|
+
]
|
|
196
|
+
)
|
|
197
|
+
const region = resolveTemplateParams(ordersFirst).find((p) => p.id === 'region')
|
|
198
|
+
// orders 先见:无覆盖元数据,label 回退 id、类型按 orders SQL 推断
|
|
199
|
+
expect(region?.label).toBe('region')
|
|
200
|
+
expect(region?.type).toBe('number')
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
it('应用数据集参数元数据覆盖(label / 类型 / 默认值 / 选项)', () => {
|
|
204
|
+
const template = createTemplate(
|
|
205
|
+
[CUSTOMERS_DATASET],
|
|
206
|
+
[{ row: 1, col: 0, dataset: 'customers', field: 'region' }]
|
|
207
|
+
)
|
|
208
|
+
const [region] = resolveTemplateParams(template)
|
|
209
|
+
expect(region).toEqual({
|
|
210
|
+
id: 'region',
|
|
211
|
+
label: '地区',
|
|
212
|
+
type: 'select',
|
|
213
|
+
defaultValue: '华东',
|
|
214
|
+
options: [
|
|
215
|
+
{ label: '华东', value: '华东' },
|
|
216
|
+
{ label: '华南', value: '华南' }
|
|
217
|
+
]
|
|
218
|
+
})
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
it('绑定引用了模板未定义的数据集时跳过该数据集', () => {
|
|
222
|
+
const template = createTemplate(
|
|
223
|
+
[],
|
|
224
|
+
[
|
|
225
|
+
{ row: 1, col: 0, dataset: 'ghost', field: 'x' },
|
|
226
|
+
{ row: 1, col: 1, dataset: 'orders', field: 'amount' }
|
|
227
|
+
]
|
|
228
|
+
)
|
|
229
|
+
// orders 未内嵌 → 也无参数;整体为空
|
|
230
|
+
expect(resolveTemplateParams(template)).toEqual([])
|
|
231
|
+
|
|
232
|
+
const withOrders = createTemplate(
|
|
233
|
+
[ORDERS_DATASET],
|
|
234
|
+
[
|
|
235
|
+
{ row: 1, col: 0, dataset: 'ghost', field: 'x' },
|
|
236
|
+
{ row: 1, col: 1, dataset: 'orders', field: 'amount' }
|
|
237
|
+
]
|
|
238
|
+
)
|
|
239
|
+
expect(resolveTemplateParams(withOrders).map((p) => p.id)).toEqual([
|
|
240
|
+
'region',
|
|
241
|
+
'minAmount',
|
|
242
|
+
'keyword',
|
|
243
|
+
'dateRange'
|
|
244
|
+
])
|
|
245
|
+
})
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
describe('resolveParamDefaults', () => {
|
|
249
|
+
it('以参数默认值生成初始运行时值', () => {
|
|
250
|
+
const template = createTemplate(
|
|
251
|
+
[ORDERS_DATASET, CUSTOMERS_DATASET],
|
|
252
|
+
[
|
|
253
|
+
{ row: 1, col: 0, dataset: 'customers', field: 'region' },
|
|
254
|
+
{ row: 2, col: 0, dataset: 'orders', field: 'amount' }
|
|
255
|
+
]
|
|
256
|
+
)
|
|
257
|
+
const values = resolveParamDefaults(resolveTemplateParams(template))
|
|
258
|
+
expect(values.region).toBe('华东')
|
|
259
|
+
expect(values.minAmount).toBe(0)
|
|
260
|
+
expect(values.dateRange).toEqual(['', ''])
|
|
261
|
+
expect(values.keyword).toBe('')
|
|
262
|
+
})
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
// ---- fetchTemplateRecords(stub connector:实现 DataConnector 接口的内存测试夹具)----
|
|
266
|
+
|
|
267
|
+
function createStubConnector(handlers: {
|
|
268
|
+
rowsBySql?: Record<string, Record<string, unknown>[]>
|
|
269
|
+
errorBySql?: Record<string, { code: string; message: string }>
|
|
270
|
+
}) {
|
|
271
|
+
const calls: Array<{ connection: DataConnection; sql: string; values: ParamValues }> = []
|
|
272
|
+
const connector: DataConnector = {
|
|
273
|
+
test: () => Promise.resolve({ ok: true, data: undefined }),
|
|
274
|
+
describe: () => Promise.resolve({ ok: true, data: [] }),
|
|
275
|
+
query: (connection, sql, values) => {
|
|
276
|
+
calls.push({ connection, sql, values })
|
|
277
|
+
const error = handlers.errorBySql?.[sql]
|
|
278
|
+
if (error) return Promise.resolve({ ok: false, error })
|
|
279
|
+
return Promise.resolve({
|
|
280
|
+
ok: true,
|
|
281
|
+
data: { fields: [], rows: handlers.rowsBySql?.[sql] ?? [] }
|
|
282
|
+
})
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return { connector, calls }
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
describe('fetchTemplateRecords', () => {
|
|
289
|
+
it('按绑定的数据集逐一经连接器取数并按数据集 id 折叠行记录', async () => {
|
|
290
|
+
const template = createTemplate(
|
|
291
|
+
[ORDERS_DATASET, CUSTOMERS_DATASET, UNUSED_DATASET],
|
|
292
|
+
[
|
|
293
|
+
{ row: 1, col: 0, dataset: 'orders', field: 'customer' },
|
|
294
|
+
{ row: 2, col: 0, dataset: 'customers', field: 'region' }
|
|
295
|
+
]
|
|
296
|
+
)
|
|
297
|
+
const { connector, calls } = createStubConnector({
|
|
298
|
+
rowsBySql: {
|
|
299
|
+
[ORDERS_SQL]: [{ customer: '甲公司', amount: 100 }],
|
|
300
|
+
[CUSTOMERS_SQL]: [{ id: 'C-01', region: '华东' }]
|
|
301
|
+
}
|
|
302
|
+
})
|
|
303
|
+
const values: ParamValues = { region: '华东' }
|
|
304
|
+
const result = await fetchTemplateRecords(connector, template, values)
|
|
305
|
+
|
|
306
|
+
expect(result).toEqual({
|
|
307
|
+
ok: true,
|
|
308
|
+
data: {
|
|
309
|
+
orders: [{ customer: '甲公司', amount: 100 }],
|
|
310
|
+
customers: [{ id: 'C-01', region: '华东' }]
|
|
311
|
+
}
|
|
312
|
+
})
|
|
313
|
+
// 未绑定的数据集不取数;连接对象与参数原样透传
|
|
314
|
+
expect(calls).toHaveLength(2)
|
|
315
|
+
expect(calls[0]).toEqual({ connection: MYSQL, sql: ORDERS_SQL, values })
|
|
316
|
+
expect(calls[1]).toEqual({ connection: PG, sql: CUSTOMERS_SQL, values })
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
it('业务错误(ok:false)原样透传', async () => {
|
|
320
|
+
const template = createTemplate(
|
|
321
|
+
[ORDERS_DATASET],
|
|
322
|
+
[{ row: 1, col: 0, dataset: 'orders', field: 'customer' }]
|
|
323
|
+
)
|
|
324
|
+
const { connector } = createStubConnector({
|
|
325
|
+
errorBySql: { [ORDERS_SQL]: { code: 'SQL_ERROR', message: '语法错误 near WHERE' } }
|
|
326
|
+
})
|
|
327
|
+
const result = await fetchTemplateRecords(connector, template, {})
|
|
328
|
+
expect(result).toEqual({
|
|
329
|
+
ok: false,
|
|
330
|
+
error: { code: 'SQL_ERROR', message: '语法错误 near WHERE' }
|
|
331
|
+
})
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
it('跳过模板未定义的绑定数据集', async () => {
|
|
335
|
+
const template = createTemplate(
|
|
336
|
+
[ORDERS_DATASET],
|
|
337
|
+
[
|
|
338
|
+
{ row: 1, col: 0, dataset: 'ghost', field: 'x' },
|
|
339
|
+
{ row: 1, col: 1, dataset: 'orders', field: 'amount' }
|
|
340
|
+
]
|
|
341
|
+
)
|
|
342
|
+
const { connector, calls } = createStubConnector({ rowsBySql: { [ORDERS_SQL]: [] } })
|
|
343
|
+
const result = await fetchTemplateRecords(connector, template, {})
|
|
344
|
+
expect(result).toEqual({ ok: true, data: { orders: [] } })
|
|
345
|
+
expect(calls).toHaveLength(1)
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
it('无绑定数据集时不取数,返回空记录集', async () => {
|
|
349
|
+
const template = createTemplate([ORDERS_DATASET], [])
|
|
350
|
+
const { connector, calls } = createStubConnector({})
|
|
351
|
+
const result = await fetchTemplateRecords(connector, template, {})
|
|
352
|
+
expect(result).toEqual({ ok: true, data: {} })
|
|
353
|
+
expect(calls).toHaveLength(0)
|
|
354
|
+
})
|
|
355
|
+
})
|