Skip to content

Commit 21efbc0

Browse files
committed
mooooore tests :)
1 parent 46fed32 commit 21efbc0

File tree

3 files changed

+237
-10
lines changed

3 files changed

+237
-10
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
cubes:
2+
- name: orders
3+
sql_table: orders
4+
5+
dimensions:
6+
- name: id
7+
sql: id
8+
type: number
9+
primary_key: true
10+
11+
- name: user_id
12+
sql: user_id
13+
type: number
14+
15+
- name: status
16+
sql: status
17+
type: string
18+
19+
- name: created_at
20+
sql: created_at
21+
type: time
22+
23+
- name: completed_at
24+
sql: completed_at
25+
type: time
26+
27+
measures:
28+
- name: count
29+
sql: id
30+
type: count
31+
32+
joins:
33+
- name: order_users
34+
relationship: many_to_one
35+
sql: "${CUBE}.user_id = ${order_users.id}"
36+
37+
segments:
38+
- name: sfUsers
39+
description: SF users segment from createCubeSchema
40+
sql: "${CUBE}.location = 'San Francisco'"
41+
42+
hierarchies:
43+
- name: hello
44+
title: World
45+
levels: [status]
46+
47+
pre_aggregations:
48+
- name: countCreatedAt
49+
type: rollup
50+
measures:
51+
- CUBE.count
52+
time_dimension: created_at
53+
granularity: day
54+
partition_granularity: month
55+
refresh_key:
56+
every: 1 hour
57+
scheduled_refresh: true
58+
59+
accessPolicy:
60+
- role: common
61+
rowLevel:
62+
allowAll: true
63+
- role: admin
64+
conditions:
65+
- if: "{ !security_context.isBlocked }"
66+
rowLevel:
67+
filters:
68+
- member: "{CUBE}.order_users.name"
69+
operator: equals
70+
values: ["completed"]
71+
- or:
72+
- member: "{CUBE}.created_at"
73+
operator: notInDateRange
74+
values:
75+
- 2022-01-01
76+
- "{ security_context.currentDate }"
77+
- member: "created_at"
78+
operator: equals
79+
values:
80+
- "{ securityContext.currentDate }"
81+
memberLevel:
82+
includes:
83+
- status
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
cubes:
2+
- name: orders
3+
sql_table: orders
4+
5+
dimensions:
6+
- name: id
7+
sql: id
8+
type: number
9+
primary_key: true
10+
11+
- name: user_id
12+
sql: user_id
13+
type: number
14+
15+
- name: status
16+
sql: status
17+
type: string
18+
19+
- name: created_at
20+
sql: created_at
21+
type: time
22+
23+
- name: completed_at
24+
sql: completed_at
25+
type: time
26+
27+
measures:
28+
- name: count
29+
sql: id
30+
type: count
31+
32+
joins:
33+
- name: order_users
34+
relationship: many_to_one
35+
sql: "${CUBE}.user_id = ${order_users.id}"
36+
37+
segments:
38+
- name: sfUsers
39+
description: SF users segment from createCubeSchema
40+
sql: "${CUBE}.location = 'San Francisco'"
41+
42+
hierarchies:
43+
- name: hello
44+
title: World
45+
levels: [status]
46+
47+
pre_aggregations:
48+
- name: countCreatedAt
49+
type: rollup
50+
measures:
51+
- CUBE.count
52+
time_dimension: created_at
53+
granularity: day
54+
partition_granularity: month
55+
refresh_key:
56+
every: 1 hour
57+
scheduled_refresh: true
58+
59+
accessPolicy:
60+
- role: common
61+
rowLevel:
62+
allowAll: true
63+
- role: admin
64+
conditions:
65+
- if: "{ !security_context.isBlocked }"
66+
rowLevel:
67+
filters:
68+
- member: "{CUBE}.other.path.created_at"
69+
operator: equals
70+
values: ["completed"]
71+
- or:
72+
- member: "{CUBE}.created_at"
73+
operator: notInDateRange
74+
values:
75+
- 2022-01-01
76+
- "{ security_context.currentDate }"
77+
- member: "created_at"
78+
operator: equals
79+
values:
80+
- "{ securityContext.currentDate }"
81+
memberLevel:
82+
includes:
83+
- status

packages/cubejs-schema-compiler/test/unit/schema.test.ts

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,70 @@ describe('Schema Testing', () => {
392392
}
393393
});
394394

395-
it('valid schema with accessPolicy', async () => {
396-
const { compiler } = prepareJsCompiler([
397-
createCubeSchemaWithAccessPolicy('ProtectedCube'),
398-
]);
399-
await compiler.compile();
400-
compiler.throwIfAnyErrors();
395+
describe('Access Policies', () => {
396+
it('valid schema with accessPolicy', async () => {
397+
const { compiler } = prepareJsCompiler([
398+
createCubeSchemaWithAccessPolicy('ProtectedCube'),
399+
]);
400+
await compiler.compile();
401+
compiler.throwIfAnyErrors();
402+
});
403+
404+
it('throw errors for nonexistent policy members with paths', async () => {
405+
const orders = fs.readFileSync(
406+
path.join(process.cwd(), '/test/unit/fixtures/orders_nonexist_acl.yml'),
407+
'utf8'
408+
);
409+
const orderUsers = fs.readFileSync(
410+
path.join(process.cwd(), '/test/unit/fixtures/order_users.yml'),
411+
'utf8'
412+
);
413+
const { compiler } = prepareCompiler([
414+
{
415+
content: orders,
416+
fileName: 'orders.yml',
417+
},
418+
{
419+
content: orderUsers,
420+
fileName: 'order_users.yml',
421+
},
422+
]);
423+
424+
try {
425+
await compiler.compile();
426+
throw new Error('should throw earlier');
427+
} catch (e: any) {
428+
expect(e.toString()).toMatch(/orders.other cannot be resolved. There's no such member or cube/);
429+
}
430+
});
431+
432+
it('throw errors for incorrect policy members with paths', async () => {
433+
const orders = fs.readFileSync(
434+
path.join(process.cwd(), '/test/unit/fixtures/orders_incorrect_acl.yml'),
435+
'utf8'
436+
);
437+
const orderUsers = fs.readFileSync(
438+
path.join(process.cwd(), '/test/unit/fixtures/order_users.yml'),
439+
'utf8'
440+
);
441+
const { compiler } = prepareCompiler([
442+
{
443+
content: orders,
444+
fileName: 'orders.yml',
445+
},
446+
{
447+
content: orderUsers,
448+
fileName: 'order_users.yml',
449+
},
450+
]);
451+
452+
try {
453+
await compiler.compile();
454+
throw new Error('should throw earlier');
455+
} catch (e: any) {
456+
expect(e.toString()).toMatch(/Paths aren't allowed in the accessPolicy policy but 'order_users.name' provided as a filter member reference for orders/);
457+
}
458+
});
401459
});
402460

403461
describe('Views', () => {
@@ -431,14 +489,15 @@ describe('Schema Testing', () => {
431489

432490
try {
433491
await compiler.compile();
492+
throw new Error('should throw earlier');
434493
} catch (e: any) {
435494
expect(e.toString()).toMatch(/Paths aren't allowed in cube includes but 'nonexistent2\.via\.path' provided as include member/);
436495
expect(e.toString()).toMatch(/Member 'nonexistent1' is included in 'orders_view' but not defined in any cube/);
437496
expect(e.toString()).toMatch(/Member 'nonexistent2\.via\.path' is included in 'orders_view' but not defined in any cube/);
438497
}
439498
});
440499

441-
it('throws errors for incorrect referenced excludes members', async () => {
500+
it('throws errors for incorrect referenced excludes members with paths', async () => {
442501
const orders = fs.readFileSync(
443502
path.join(process.cwd(), '/test/unit/fixtures/orders.js'),
444503
'utf8'
@@ -452,7 +511,7 @@ describe('Schema Testing', () => {
452511
excludes:
453512
- id
454513
- status
455-
- nonexistent3
514+
- nonexistent3.ext
456515
- nonexistent4
457516
`;
458517

@@ -469,9 +528,9 @@ describe('Schema Testing', () => {
469528

470529
try {
471530
await compiler.compile();
531+
throw new Error('should throw earlier');
472532
} catch (e: any) {
473-
expect(e.toString()).toMatch(/Member 'nonexistent3' is included in 'orders_view' but not defined in any cube/);
474-
expect(e.toString()).toMatch(/Member 'nonexistent4' is included in 'orders_view' but not defined in any cube/);
533+
expect(e.toString()).toMatch(/Paths aren't allowed in cube excludes but 'nonexistent3.ext' provided as exclude member/);
475534
}
476535
});
477536

@@ -505,6 +564,7 @@ describe('Schema Testing', () => {
505564

506565
try {
507566
await compiler.compile();
567+
throw new Error('should throw earlier');
508568
} catch (e: any) {
509569
expect(e.toString()).toMatch(/Paths aren't allowed in cube excludes but 'nonexistent5\.via\.path' provided as exclude member/);
510570
}
@@ -546,6 +606,7 @@ describe('Schema Testing', () => {
546606

547607
try {
548608
await compiler.compile();
609+
throw new Error('should throw earlier');
549610
} catch (e: any) {
550611
expect(e.toString()).toMatch(/Included member 'count' conflicts with existing member of 'orders_view'\. Please consider excluding this member or assigning it an alias/);
551612
expect(e.toString()).toMatch(/Included member 'id' conflicts with existing member of 'orders_view'\. Please consider excluding this member or assigning it an alias/);

0 commit comments

Comments
 (0)