From 6246488b5ddef02ce56682139f05eba35659e60f Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Mon, 2 Jun 2025 20:28:00 +0300 Subject: [PATCH] chore(schema-compiler): Add more inheritance tests --- .../unit/__snapshots__/schema.test.ts.snap | 88 +++++++++++++++++++ .../test/unit/fixtures/line_items.yml | 36 ++++++++ .../test/unit/fixtures/orders_ext.js | 7 ++ .../test/unit/fixtures/orders_ext.yml | 5 ++ .../test/unit/schema.test.ts | 34 ++++++- 5 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 packages/cubejs-schema-compiler/test/unit/fixtures/line_items.yml diff --git a/packages/cubejs-schema-compiler/test/unit/__snapshots__/schema.test.ts.snap b/packages/cubejs-schema-compiler/test/unit/__snapshots__/schema.test.ts.snap index d73f1988c6d95..9a09fa58b7fdd 100644 --- a/packages/cubejs-schema-compiler/test/unit/__snapshots__/schema.test.ts.snap +++ b/packages/cubejs-schema-compiler/test/unit/__snapshots__/schema.test.ts.snap @@ -329,6 +329,28 @@ Array [ ] `; +exports[`Schema Testing Inheritance CubeB.js correctly extends cubeA.js (with additions) 13`] = ` +Object { + "order_users": Object { + "relationship": "belongsTo", + "sql": [Function], + }, +} +`; + +exports[`Schema Testing Inheritance CubeB.js correctly extends cubeA.js (with additions) 14`] = ` +Object { + "line_items": Object { + "relationship": "hasMany", + "sql": [Function], + }, + "order_users": Object { + "relationship": "belongsTo", + "sql": [Function], + }, +} +`; + exports[`Schema Testing Inheritance CubeB.js correctly extends cubeA.yml (with additions) 1`] = ` Object { "completed_at": Object { @@ -702,6 +724,28 @@ Array [ ] `; +exports[`Schema Testing Inheritance CubeB.js correctly extends cubeA.yml (with additions) 13`] = ` +Object { + "order_users": Object { + "relationship": "belongsTo", + "sql": [Function], + }, +} +`; + +exports[`Schema Testing Inheritance CubeB.js correctly extends cubeA.yml (with additions) 14`] = ` +Object { + "line_items": Object { + "relationship": "hasMany", + "sql": [Function], + }, + "order_users": Object { + "relationship": "belongsTo", + "sql": [Function], + }, +} +`; + exports[`Schema Testing Inheritance CubeB.yml correctly extends cubeA.js (with additions) 1`] = ` Object { "completed_at": Object { @@ -1003,6 +1047,28 @@ Array [ ] `; +exports[`Schema Testing Inheritance CubeB.yml correctly extends cubeA.js (with additions) 13`] = ` +Object { + "order_users": Object { + "relationship": "belongsTo", + "sql": [Function], + }, +} +`; + +exports[`Schema Testing Inheritance CubeB.yml correctly extends cubeA.js (with additions) 14`] = ` +Object { + "line_items": Object { + "relationship": "hasMany", + "sql": [Function], + }, + "order_users": Object { + "relationship": "belongsTo", + "sql": [Function], + }, +} +`; + exports[`Schema Testing Inheritance CubeB.yml correctly extends cubeA.yml (with additions) 1`] = ` Object { "completed_at": Object { @@ -1348,6 +1414,28 @@ Array [ ] `; +exports[`Schema Testing Inheritance CubeB.yml correctly extends cubeA.yml (with additions) 13`] = ` +Object { + "order_users": Object { + "relationship": "belongsTo", + "sql": [Function], + }, +} +`; + +exports[`Schema Testing Inheritance CubeB.yml correctly extends cubeA.yml (with additions) 14`] = ` +Object { + "line_items": Object { + "relationship": "hasMany", + "sql": [Function], + }, + "order_users": Object { + "relationship": "belongsTo", + "sql": [Function], + }, +} +`; + exports[`Schema Testing Views allows to override \`title\`, \`description\`, \`meta\`, and \`format\` on includes members 1`] = ` Object { "accessPolicy": undefined, diff --git a/packages/cubejs-schema-compiler/test/unit/fixtures/line_items.yml b/packages/cubejs-schema-compiler/test/unit/fixtures/line_items.yml new file mode 100644 index 0000000000000..f40c08cbdc967 --- /dev/null +++ b/packages/cubejs-schema-compiler/test/unit/fixtures/line_items.yml @@ -0,0 +1,36 @@ +cubes: + - name: line_items + sql_table: public.line_items + public: false + + dimensions: + - name: id + sql: id + type: number + primary_key: true + public: true + + - name: product_id + sql: product_id + type: number + + - name: order_id + sql: order_id + type: number + + # - name: quantity + # sql: quantity + # type: number + + - name: price + sql: price + type: number + + measures: + - name: count + type: count + + - name: sum_price + sql: "{price}" + type: sum + public: false diff --git a/packages/cubejs-schema-compiler/test/unit/fixtures/orders_ext.js b/packages/cubejs-schema-compiler/test/unit/fixtures/orders_ext.js index a79b3e2d4e2e1..2af92e7c9a6a3 100644 --- a/packages/cubejs-schema-compiler/test/unit/fixtures/orders_ext.js +++ b/packages/cubejs-schema-compiler/test/unit/fixtures/orders_ext.js @@ -15,6 +15,13 @@ cube('ordersExt', { }, }, + joins: { + line_items: { + relationship: 'one_to_many', + sql: `${CUBE}.id = ${line_items}.order_id`, + } + }, + segments: { anotherStatus: { description: 'Just another one', diff --git a/packages/cubejs-schema-compiler/test/unit/fixtures/orders_ext.yml b/packages/cubejs-schema-compiler/test/unit/fixtures/orders_ext.yml index d3e4121f0a2d8..1cf957ebe4fe6 100644 --- a/packages/cubejs-schema-compiler/test/unit/fixtures/orders_ext.yml +++ b/packages/cubejs-schema-compiler/test/unit/fixtures/orders_ext.yml @@ -12,6 +12,11 @@ cubes: sql: status type: count_distinct + joins: + - name: line_items + sql: '{CUBE.id} = {line_items.order_id}' + relationship: one_to_many + segments: - name: anotherStatus description: Just another one diff --git a/packages/cubejs-schema-compiler/test/unit/schema.test.ts b/packages/cubejs-schema-compiler/test/unit/schema.test.ts index ffccd5d350739..8205fc0aad17e 100644 --- a/packages/cubejs-schema-compiler/test/unit/schema.test.ts +++ b/packages/cubejs-schema-compiler/test/unit/schema.test.ts @@ -3,7 +3,7 @@ import path from 'path'; import { prepareCompiler, prepareJsCompiler, prepareYamlCompiler } from './PrepareCompiler'; import { createCubeSchema, createCubeSchemaWithCustomGranularitiesAndTimeShift, createCubeSchemaWithAccessPolicy } from './utils'; -const CUBE_COMPONENTS = ['dimensions', 'measures', 'segments', 'hierarchies', 'preAggregations', 'accessPolicy']; +const CUBE_COMPONENTS = ['dimensions', 'measures', 'segments', 'hierarchies', 'preAggregations', 'accessPolicy', 'joins']; describe('Schema Testing', () => { const schemaCompile = async () => { @@ -807,6 +807,10 @@ describe('Schema Testing', () => { path.join(process.cwd(), '/test/unit/fixtures/order_users.yml'), 'utf8' ); + const orderLineItems = fs.readFileSync( + path.join(process.cwd(), '/test/unit/fixtures/line_items.yml'), + 'utf8' + ); const ordersExt = fs.readFileSync( path.join(process.cwd(), '/test/unit/fixtures/orders_ext.js'), 'utf8' @@ -825,6 +829,10 @@ describe('Schema Testing', () => { content: orderUsers, fileName: 'order_users.yml', }, + { + content: orderLineItems, + fileName: 'line_items.yml', + }, ]); await compiler.compile(); compiler.throwIfAnyErrors(); @@ -887,6 +895,10 @@ describe('Schema Testing', () => { path.join(process.cwd(), '/test/unit/fixtures/order_users.yml'), 'utf8' ); + const orderLineItems = fs.readFileSync( + path.join(process.cwd(), '/test/unit/fixtures/line_items.yml'), + 'utf8' + ); const ordersExt = fs.readFileSync( path.join(process.cwd(), '/test/unit/fixtures/orders_ext.yml'), 'utf8' @@ -905,6 +917,10 @@ describe('Schema Testing', () => { content: orderUsers, fileName: 'order_users.yml', }, + { + content: orderLineItems, + fileName: 'line_items.yml', + }, ]); await compiler.compile(); compiler.throwIfAnyErrors(); @@ -967,6 +983,10 @@ describe('Schema Testing', () => { path.join(process.cwd(), '/test/unit/fixtures/order_users.yml'), 'utf8' ); + const orderLineItems = fs.readFileSync( + path.join(process.cwd(), '/test/unit/fixtures/line_items.yml'), + 'utf8' + ); const ordersExt = fs.readFileSync( path.join(process.cwd(), '/test/unit/fixtures/orders_ext.yml'), 'utf8' @@ -985,6 +1005,10 @@ describe('Schema Testing', () => { content: orderUsers, fileName: 'order_users.yml', }, + { + content: orderLineItems, + fileName: 'line_items.yml', + }, ]); await compiler.compile(); compiler.throwIfAnyErrors(); @@ -1043,6 +1067,10 @@ describe('Schema Testing', () => { path.join(process.cwd(), '/test/unit/fixtures/order_users.yml'), 'utf8' ); + const orderLineItems = fs.readFileSync( + path.join(process.cwd(), '/test/unit/fixtures/line_items.yml'), + 'utf8' + ); const ordersExt = fs.readFileSync( path.join(process.cwd(), '/test/unit/fixtures/orders_ext.js'), 'utf8' @@ -1061,6 +1089,10 @@ describe('Schema Testing', () => { content: orderUsers, fileName: 'order_users.yml', }, + { + content: orderLineItems, + fileName: 'line_items.yml', + }, ]); await compiler.compile(); compiler.throwIfAnyErrors();