-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdataset.test.ts
More file actions
113 lines (101 loc) · 3.65 KB
/
Copy pathdataset.test.ts
File metadata and controls
113 lines (101 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect } from 'vitest';
import { DatasetSchema, defineDataset } from './dataset.zod';
const base = {
name: 'sales',
label: 'Sales',
object: 'opportunity',
include: ['account'],
dimensions: [{ name: 'region', field: 'account.region', type: 'string' as const }],
measures: [
{ name: 'revenue', aggregate: 'sum' as const, field: 'amount' },
{ name: 'deal_count', aggregate: 'count' as const },
],
};
describe('DatasetSchema', () => {
it('accepts a well-formed dataset and applies defaults (certified=false)', () => {
const ds = DatasetSchema.parse(base);
expect(ds.measures[0].certified).toBe(false);
expect(ds.object).toBe('opportunity');
});
it('rejects a non-count measure with no field', () => {
expect(() =>
DatasetSchema.parse({ ...base, measures: [{ name: 'revenue', aggregate: 'sum' }] }),
).toThrowError(/requires `field`/);
});
it('allows count with no field', () => {
expect(() =>
DatasetSchema.parse({ ...base, measures: [{ name: 'total', aggregate: 'count' }] }),
).not.toThrow();
});
it('rejects duplicate measure names', () => {
expect(() =>
DatasetSchema.parse({
...base,
measures: [
{ name: 'revenue', aggregate: 'sum', field: 'amount' },
{ name: 'revenue', aggregate: 'avg', field: 'amount' },
],
}),
).toThrowError(/duplicate measure name/);
});
it('rejects a derived measure referencing an unknown measure', () => {
expect(() =>
DatasetSchema.parse({
...base,
measures: [
{ name: 'revenue', aggregate: 'sum', field: 'amount' },
{ name: 'win_rate', aggregate: 'sum', derived: { op: 'ratio', of: ['won_amount', 'revenue'] } },
],
}),
).toThrowError(/references unknown measure/);
});
it('rejects a derived measure referencing itself', () => {
expect(() =>
DatasetSchema.parse({
...base,
measures: [
{ name: 'revenue', aggregate: 'sum', field: 'amount' },
{ name: 'loop', aggregate: 'sum', derived: { op: 'sum', of: ['loop'] } },
],
}),
).toThrowError(/cannot reference itself/);
});
it('accepts a valid derived measure', () => {
expect(() =>
DatasetSchema.parse({
...base,
measures: [
{ name: 'revenue', aggregate: 'sum', field: 'amount' },
{ name: 'won_amount', aggregate: 'sum', field: 'amount', filter: { stage: 'won' } },
{ name: 'win_rate', aggregate: 'sum', derived: { op: 'ratio', of: ['won_amount', 'revenue'] } },
],
}),
).not.toThrow();
});
it('defineDataset is an identity helper', () => {
const d = defineDataset(base);
expect(d).toBe(base);
});
});
describe('DatasetSchema — derived measure aggregate optionality', () => {
// Regression: a derived measure had to carry a meaningless `aggregate` or
// validation failed, even though `aggregate` is ignored for derived measures.
it('accepts a derived measure with NO aggregate', () => {
expect(() =>
DatasetSchema.parse({
...base,
measures: [
{ name: 'won_amount', aggregate: 'sum', field: 'amount' },
{ name: 'win_rate', derived: { op: 'ratio', of: ['won_amount', 'revenue'] } },
{ name: 'revenue', aggregate: 'sum', field: 'amount' },
],
}),
).not.toThrow();
});
it('still rejects a NON-derived measure with no aggregate', () => {
expect(() =>
DatasetSchema.parse({ ...base, measures: [{ name: 'revenue', field: 'amount' }] }),
).toThrowError(/requires `aggregate`/);
});
});